You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@jena.apache.org by "BR (Jira)" <ji...@apache.org> on 2021/02/18 12:00:00 UTC

[jira] [Created] (JENA-2051) Union Model Returns Empty Model Even When Default Model Contains Statements

BR created JENA-2051:
------------------------

             Summary: 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


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)