Skip to content
Permalink
Browse files
Adding convenient Dataset and DatasetGraph conversions
  • Loading branch information
ajs6f committed Jan 29, 2018
1 parent e893f14 commit 0c9ae7e83f9253d5b9efc4b7ad8261c09e4f8e74
Showing 2 changed files with 87 additions and 2 deletions.
@@ -39,12 +39,14 @@
import org.apache.jena.datatypes.xsd.XSDDatatype;
import org.apache.jena.graph.Node;
import org.apache.jena.graph.NodeFactory;
import org.apache.jena.query.DatasetFactory;
import org.apache.jena.riot.Lang;
import org.apache.jena.riot.RDFDataMgr;
import org.apache.jena.riot.RDFLanguages;
import org.apache.jena.riot.system.StreamRDF;
import org.apache.jena.riot.system.StreamRDFBase;
import org.apache.jena.sparql.core.DatasetGraph;
import org.apache.jena.sparql.core.DatasetGraphFactory;
import org.apache.jena.sparql.graph.GraphFactory;

/**
@@ -664,6 +666,39 @@ public void quad(final org.apache.jena.sparql.core.Quad quad) {
};
}

/**
* Convert a CommonsRDF Dataset to a Jena Dataset. If the Dataset was from Jena
* originally, return that original object wrapped else create a copy using Jena
* objects.
*
* @param dataset
* Commons RDF {@link Dataset} to convert
* @return Converted Jena {@link org.apache.jena.query.Dataset}
*/
public org.apache.jena.query.Dataset asJenaDataset(final Dataset dataset) {
return DatasetFactory.wrap(asJenaDatasetGraph(dataset));
}

/**
* Convert a CommonsRDF Dataset to a Jena DatasetGraph. If the Dataset was from Jena
* originally, return that original object's underlying DatasetGraph else create a
* copy using Jena objects.
*
* @param dataset
* Commons RDF {@link Dataset} to convert
* @return Converted Jena {@link org.apache.jena.sparql.core.DatasetGraph}
*/
public DatasetGraph asJenaDatasetGraph(final Dataset dataset) {
final DatasetGraph dsg;
if (dataset instanceof JenaDataset)
dsg = ((JenaDataset) dataset).asJenaDatasetGraph();
else {
dsg = DatasetGraphFactory.createGeneral();
dataset.stream().map(this::asJenaQuad).forEach(dsg::add);
}
return dsg;
}

/**
* Convert a CommonsRDF Graph to a Jena Graph. If the Graph was from Jena
* originally, return that original object else create a copy using Jena
@@ -17,14 +17,64 @@
*/
package org.apache.commons.rdf.jena;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;

import org.apache.commons.rdf.api.AbstractRDFTest;
import org.apache.commons.rdf.api.RDF;
import org.apache.commons.rdf.api.BlankNode;
import org.apache.commons.rdf.api.Dataset;
import org.apache.commons.rdf.api.IRI;
import org.apache.commons.rdf.api.Quad;
import org.apache.commons.rdf.simple.SimpleRDF;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.Property;
import org.apache.jena.rdf.model.RDFNode;
import org.apache.jena.rdf.model.Resource;
import org.apache.jena.rdf.model.Statement;
import org.apache.jena.sparql.core.DatasetGraph;
import org.apache.jena.sparql.core.DatasetGraphFactory;
import org.junit.Test;

public class JenaRDFTest extends AbstractRDFTest {

@Override
public RDF createFactory() {
public JenaRDF createFactory() {
return new JenaRDF();
}

@Test
public void roundTripDatasetGraphShouldMaintainIdentity() {
DatasetGraph dsg = DatasetGraphFactory.create();
JenaDataset dataset = createFactory().asDataset(dsg);
DatasetGraph roundTrippedDSG = createFactory().asJenaDatasetGraph(dataset);
assertSame("Should have gotten the same DatasetGraph object from a round trip!", dsg, roundTrippedDSG);
}

@Test
public void testSimpleDatasetConversion() {
SimpleRDF factory = new SimpleRDF();
Dataset ds = factory.createDataset();
final BlankNode subject = factory.createBlankNode("b1");
final IRI predicate = factory.createIRI("http://example.com/pred");
final IRI object = factory.createIRI("http://example.com/obj");
final IRI graph = factory.createIRI("http://example.com/graph");
final Quad quad = factory.createQuad(graph, subject, predicate, object);
ds.add(quad);
JenaRDF jenaFactory = createFactory();
org.apache.jena.query.Dataset jenaDS = jenaFactory.asJenaDataset(ds);
assertEquals("Should have found one named graph!", 1, jenaDS.asDatasetGraph().size());
Model namedModel = jenaDS.getNamedModel(graph.getIRIString());
assertEquals("Should have found one triple in named graph!", 1, namedModel.size());
Statement statement = namedModel.listStatements().next();
Resource jenaSubject = statement.getSubject();
Property jenaPredicate = statement.getPredicate();
RDFNode jenaObject = statement.getObject();
assertTrue(jenaSubject.isAnon());
assertTrue(jenaObject.isResource());
assertEquals(subject.ntriplesString(), "_:" + jenaSubject.getId().getLabelString());
assertEquals(predicate.getIRIString(), jenaPredicate.getURI());
assertEquals(object.getIRIString(), jenaObject.asResource().getURI());
}

}

0 comments on commit 0c9ae7e

Please sign in to comment.