[ 
https://issues.apache.org/jira/browse/JENA-2051?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17286440#comment-17286440
 ] 

Andy Seaborne commented on JENA-2051:
-------------------------------------

Hi there -

The union model is the union of all named graphs and does not include the 
default graph.

Is that the issue for you? 

One way is to union the default graph with the named graph union.


> Union Model Returns Empty Model Even When Default Model Contains Statements
> ---------------------------------------------------------------------------
>
>                 Key: JENA-2051
>                 URL: https://issues.apache.org/jira/browse/JENA-2051
>             Project: Apache Jena
>          Issue Type: Bug
>            Reporter: BR
>            Priority: Trivial
>
> I'm using Jena 3.15.0.
> When I try to use Dataset#getUnionModel no statements are returned in the 
> scenario below. 
> The work around to provide your own union method seems to be reasonable. This 
> is also shown in the test case below.
> {code}
>       private String unionOfJsonLdGraphs(List<String> jsonLdGraphs) {
>               return jsonLdGraphs.stream()
>                               .map(graph -> from(graph))
>                               
> .flatMap(ContentProfileGraphExtractorImpl::modelsFromDataSet)
>                               // Why did this not work?
>                               //.map(Dataset::getUnionModel)
>                               .reduce(this::union)
>                               
> .map(ContentProfileGraphExtractorImpl::modelToString)
>                               .orElse("{}");
>       }
>       
>       private static Stream<Model> modelsFromDataSet(Dataset dataset) {
>               Stream<Model> namedModels = 
> stream(spliteratorUnknownSize(dataset.listNames(), ORDERED), 
> false).map(graphName->dataset.getNamedModel(graphName));
>               Stream<Model> defaultModel = of(dataset.getDefaultModel());
>               return concat(defaultModel, namedModels);
>       }
>       
>       private Model union(Model modelA, Model modelB) {
>               if(null!=modelA && null!=modelB) {
>                       return modelA.union(modelB);
>               } else if(null!=modelA) {
>                       return modelA;
>               } else if(null!=modelB) {
>                       return modelB;
>               } else {
>                       throw new RuntimeException();
>               }
>       }
>       /**
>        * Creates a String containing the JSON-LD of the Model.
>        * 
>        * @see 
> https://github.com/apache/jena/blob/master/jena-arq/src-examples/arq/examples/riot/Ex_WriteJsonLD.java
>        * @see https://json-ld.org/spec/latest/json-ld-framing/
>        * @see https://w3c.github.io/json-ld-framing/
>        */
>       private static String modelToString(Model model) {
>               Map<String, Object> frame = new HashMap<>();
>               Map<String, Object> context = new HashMap<>();
>               context.put("@vocab", 
> "https://data.elsevier.com/lifescience/entellect/patent/";);
>               frame.put("@context", context);
>               
>               // Construct Frame
>               Context serializationContext = new Context();
>               serializationContext.put(JSONLD_FRAME, frame);
>               ByteArrayOutputStream outputStream = new 
> ByteArrayOutputStream();
>               
> RDFWriter.create().format(JSONLD_FRAME_PRETTY).source(model).context(serializationContext)
>                               .output(outputStream);
>               return new String(outputStream.toByteArray(), UTF_8);
>       }
> {code}



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to