commonsrdf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From st...@apache.org
Subject [05/50] incubator-commonsrdf git commit: Added JenaDataset
Date Fri, 07 Oct 2016 14:22:59 GMT
Added JenaDataset


Project: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/commit/3a09004c
Tree: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/tree/3a09004c
Diff: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/diff/3a09004c

Branch: refs/heads/rdf4j
Commit: 3a09004ca1757490e7273d78ab00f901a3d0a613
Parents: a3cb3b1
Author: Stian Soiland-Reyes <stain@apache.org>
Authored: Fri Jul 8 16:39:25 2016 +0100
Committer: Stian Soiland-Reyes <stain@apache.org>
Committed: Fri Jul 8 16:39:25 2016 +0100

----------------------------------------------------------------------
 .../apache/commons/rdf/jena/JenaDataset.java    |  30 +++
 .../commons/rdf/jena/JenaRDFTermFactory.java    |  35 +++-
 .../commons/rdf/jena/impl/DatasetImpl.java      | 187 +++++++++++++++++++
 .../commons/rdf/jena/impl/JenaFactory.java      |  16 +-
 4 files changed, 263 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/3a09004c/jena/src/main/java/org/apache/commons/rdf/jena/JenaDataset.java
----------------------------------------------------------------------
diff --git a/jena/src/main/java/org/apache/commons/rdf/jena/JenaDataset.java b/jena/src/main/java/org/apache/commons/rdf/jena/JenaDataset.java
new file mode 100644
index 0000000..98fb840
--- /dev/null
+++ b/jena/src/main/java/org/apache/commons/rdf/jena/JenaDataset.java
@@ -0,0 +1,30 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.rdf.jena;
+
+import org.apache.commons.rdf.api.Graph;
+import org.apache.jena.sparql.core.DatasetGraph;
+
+/** Access the Jena graph backing this object */
+public interface JenaDataset extends org.apache.commons.rdf.api.Dataset {
+	public DatasetGraph asJenaDatasetGraph();
+	
+	public Graph getUnionGraph();
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/3a09004c/jena/src/main/java/org/apache/commons/rdf/jena/JenaRDFTermFactory.java
----------------------------------------------------------------------
diff --git a/jena/src/main/java/org/apache/commons/rdf/jena/JenaRDFTermFactory.java b/jena/src/main/java/org/apache/commons/rdf/jena/JenaRDFTermFactory.java
index 12b152e..78dc5f7 100644
--- a/jena/src/main/java/org/apache/commons/rdf/jena/JenaRDFTermFactory.java
+++ b/jena/src/main/java/org/apache/commons/rdf/jena/JenaRDFTermFactory.java
@@ -24,6 +24,7 @@ import java.util.function.Consumer;
 
 import org.apache.commons.rdf.api.BlankNode;
 import org.apache.commons.rdf.api.BlankNodeOrIRI;
+import org.apache.commons.rdf.api.Dataset;
 import org.apache.commons.rdf.api.Graph;
 import org.apache.commons.rdf.api.IRI;
 import org.apache.commons.rdf.api.Literal;
@@ -74,6 +75,11 @@ public final class JenaRDFTermFactory implements RDFTermFactory {
 	public JenaBlankNode createBlankNode(String name) {
 		return JenaFactory.createBlankNode(name, salt);
 	}
+	
+	@Override
+	public Dataset createDataset() throws UnsupportedOperationException {
+		return JenaFactory.createDataset(salt);
+	}
 
 	@Override
 	public JenaGraph createGraph() {
@@ -578,19 +584,40 @@ public final class JenaRDFTermFactory implements RDFTermFactory {
 	}
 
 	/**
-	 * Convert a CommonsRDF Triple to a Jena Triple. If the Triple was from Jena
-	 * originally, return that original object else create a copy using Jena
-	 * objects.
+	 * Convert a CommonsRDF {@link Triple} to a Jena
+	 * {@link org.apache.jena.graph.Triple}.
+	 * <p>
+	 * If the triple was from Jena originally, return that original object, else
+	 * create a copy using Jena objects.
 	 */
 	public static org.apache.jena.graph.Triple toJena(Triple triple) {
 		if (triple instanceof JenaTriple)
 			return ((JenaTriple) triple).asJenaTriple();
-		return new org.apache.jena.graph.Triple(
+		return org.apache.jena.graph.Triple.create(
 				toJena(triple.getSubject()), 
 				toJena(triple.getPredicate()),
 				toJena(triple.getObject()));
 	}
 
+
+	/**
+	 * Convert a CommonsRDF {@link Quad} to a Jena
+	 * {@link org.apache.jena.sparql.core.Quad}.
+	 * <p>
+	 * If the quad was from Jena originally, return that original object,
+	 * otherwise create a copy using Jena objects.
+	 */
+	public static org.apache.jena.sparql.core.Quad toJena(Quad quad) {
+		if (quad instanceof JenaQuad) {
+			return ((JenaQuad) quad).asJenaQuad();
+		}
+		return org.apache.jena.sparql.core.Quad.create(
+				toJena(quad.getGraphName().orElse(null)),
+				toJena(quad.getSubject()), 
+				toJena(quad.getPredicate()), 
+				toJena(quad.getObject()));
+	}
+
 	// Some simple validations - full IRI parsing is not cheap.
 	private static void validateIRI(String iri) {
 		if (iri.contains(" "))

http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/3a09004c/jena/src/main/java/org/apache/commons/rdf/jena/impl/DatasetImpl.java
----------------------------------------------------------------------
diff --git a/jena/src/main/java/org/apache/commons/rdf/jena/impl/DatasetImpl.java b/jena/src/main/java/org/apache/commons/rdf/jena/impl/DatasetImpl.java
new file mode 100644
index 0000000..4094efe
--- /dev/null
+++ b/jena/src/main/java/org/apache/commons/rdf/jena/impl/DatasetImpl.java
@@ -0,0 +1,187 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.rdf.jena.impl;
+
+import static org.apache.jena.graph.Node.ANY;
+
+import java.io.StringWriter;
+import java.util.Optional;
+import java.util.UUID;
+import java.util.stream.Stream;
+
+import org.apache.commons.rdf.api.BlankNodeOrIRI;
+import org.apache.commons.rdf.api.Graph;
+import org.apache.commons.rdf.api.IRI;
+import org.apache.commons.rdf.api.Quad;
+import org.apache.commons.rdf.api.RDFTerm;
+import org.apache.commons.rdf.jena.JenaDataset;
+import org.apache.commons.rdf.jena.JenaRDFTermFactory;
+import org.apache.jena.atlas.iterator.Iter;
+import org.apache.jena.graph.Node;
+import org.apache.jena.riot.Lang;
+import org.apache.jena.riot.RDFDataMgr;
+import org.apache.jena.sparql.core.DatasetGraph;
+import org.apache.jena.sparql.core.GraphView;
+
+public class DatasetImpl implements JenaDataset {
+
+	private DatasetGraph graph;
+	private UUID salt;
+
+	/* package */ DatasetImpl(DatasetGraph graph, UUID salt) {
+		this.graph = graph;
+		this.salt = salt;
+	}
+
+	@Override
+	public void add(BlankNodeOrIRI graphName, BlankNodeOrIRI subject, IRI predicate, RDFTerm
object) {
+		graph.add(				
+				org.apache.jena.sparql.core.Quad.create(
+				JenaRDFTermFactory.toJena(graphName),
+				JenaRDFTermFactory.toJena(subject),				
+				JenaRDFTermFactory.toJena(predicate), 
+				JenaRDFTermFactory.toJena(object)));
+	}
+
+	@Override
+	public void add(Quad quad) {
+		graph.add(JenaRDFTermFactory.toJena(quad));
+	}
+	
+	@Override
+	public DatasetGraph asJenaDatasetGraph() {		
+		return graph;
+	}
+
+	@Override
+	public void clear() {
+		graph.clear();
+	}
+
+	@Override
+	public void close() {
+		graph.close();
+	}
+
+
+	@Override
+	public boolean contains(Optional<BlankNodeOrIRI> graphName, BlankNodeOrIRI subject,
IRI predicate, RDFTerm object) {		
+		return graph.contains(
+				toJenaPattern(graphName),
+				toJenaPattern(subject), 
+				toJenaPattern(predicate),
+				toJenaPattern(object));
+	}
+
+	private Node toJenaPattern(Optional<? extends RDFTerm> graphName) {
+		// In theory we could have done:
+		//   JenaRDFTermFactory.toJena(graphName.orElse(JenaFactory::createAnyVariable))
+		// but because of generics casting rules that doesn't work :(						
+
+		if (graphName == null) {
+			return ANY;
+		}
+		// null: default graph
+		return JenaRDFTermFactory.toJena(graphName.orElse(null));
+	}
+
+	private Node toJenaPattern(RDFTerm term) {
+		if (term == null)
+			return ANY;
+		return JenaRDFTermFactory.toJena(term);
+	}
+
+	@Override
+	public boolean contains(Quad quad) {
+		return graph.contains(JenaRDFTermFactory.toJena(quad));
+	}
+
+	@Override
+	public void remove(Optional<BlankNodeOrIRI> graphName, BlankNodeOrIRI subject, IRI
predicate, RDFTerm object) {
+		graph.delete(org.apache.jena.sparql.core.Quad.create(
+				toJenaPattern(graphName),
+				toJenaPattern(subject),
+				toJenaPattern(predicate), 
+				toJenaPattern(object)));
+	}
+
+	@Override
+	public void remove(Quad quad) {
+		graph.delete(JenaRDFTermFactory.toJena(quad));
+	}
+
+	@Override
+	public long size() {
+		return graph.size();
+	}
+
+	@Override
+	public Stream<? extends Quad> stream() {
+		JenaRDFTermFactory factory = new JenaRDFTermFactory(salt);
+		return Iter.asStream(graph.find(ANY, ANY, ANY, ANY), true)
+				.map(factory::fromJena);
+	}
+
+	@Override
+	public Stream<? extends Quad> stream(Optional<BlankNodeOrIRI> g, BlankNodeOrIRI
s, IRI p, RDFTerm o) {
+		JenaRDFTermFactory factory = new JenaRDFTermFactory(salt);
+		return Iter.asStream(graph.find(toJenaPattern(g), toJenaPattern(s), toJenaPattern(p), toJenaPattern(o)),
true)
+				.map(factory::fromJena);
+	}
+
+	@Override
+	public String toString() {
+		StringWriter sw = new StringWriter();
+		RDFDataMgr.write(sw, graph, Lang.NT);
+		return sw.toString();
+	}
+
+	@Override
+	public Graph getGraph() {
+		GraphView gv = GraphView.createDefaultGraph(graph);
+		return new GraphImpl(gv, salt);
+	}
+
+	@Override
+	public Graph getUnionGraph() {
+		GraphView gv = GraphView.createUnionGraph(graph);
+		return new GraphImpl(gv, salt);
+	}
+	
+	@Override
+	public Optional<Graph> getGraph(BlankNodeOrIRI graphName) {
+		GraphView gv = GraphView.createNamedGraph(graph, JenaRDFTermFactory.toJena(graphName));
+		return Optional.of(new GraphImpl(gv, salt));
+	}	
+
+	@Override
+	public Stream<BlankNodeOrIRI> getGraphNames() {
+		JenaRDFTermFactory factory = new JenaRDFTermFactory(salt);
+		return Iter.asStream(graph.listGraphNodes()).map(node -> 
+			(BlankNodeOrIRI) factory.fromJena(node));		
+	}
+
+	@Override
+	public Iterable<Quad> iterate() {
+		return Iter.asStream(graph.find(), false)
+				.map(q -> (Quad) JenaRDFTermFactory.fromJena(q, salt))
+				::iterator;
+	}
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/3a09004c/jena/src/main/java/org/apache/commons/rdf/jena/impl/JenaFactory.java
----------------------------------------------------------------------
diff --git a/jena/src/main/java/org/apache/commons/rdf/jena/impl/JenaFactory.java b/jena/src/main/java/org/apache/commons/rdf/jena/impl/JenaFactory.java
index b685832..da24277 100644
--- a/jena/src/main/java/org/apache/commons/rdf/jena/impl/JenaFactory.java
+++ b/jena/src/main/java/org/apache/commons/rdf/jena/impl/JenaFactory.java
@@ -23,6 +23,7 @@ import java.util.UUID;
 
 import org.apache.commons.rdf.api.BlankNode;
 import org.apache.commons.rdf.api.BlankNodeOrIRI;
+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.api.RDFTerm;
@@ -40,6 +41,8 @@ import org.apache.commons.rdf.jena.JenaTripleLike;
 import org.apache.commons.rdf.jena.JenaVariable;
 import org.apache.jena.graph.Node;
 import org.apache.jena.graph.NodeFactory;
+import org.apache.jena.sparql.core.DatasetGraph;
+import org.apache.jena.sparql.core.DatasetGraphFactory;
 import org.apache.jena.sparql.graph.GraphFactory;
 
 public class JenaFactory {
@@ -55,6 +58,16 @@ public class JenaFactory {
 		return new BlankNodeImpl(NodeFactory.createBlankNode(), salt);
 	}
 
+	public static Dataset createDataset() {
+		return createDataset(UUID.randomUUID());
+	}
+	
+	public static Dataset createDataset(UUID salt) {
+		DatasetGraph dg = DatasetGraphFactory.createGeneral();
+		// Or which createMethod() -- a bit confusing with lots of choice..
+		return new DatasetImpl(dg, salt);
+	}
+
 	public static JenaGraph createGraph() {
 		return createGraph(UUID.randomUUID());
 	}
@@ -91,7 +104,7 @@ public class JenaFactory {
 		return new VariableImpl(NodeFactory.createVariable(name));
 	}
 	
-	public static JenaAny createVariable() {
+	public static JenaAny createAnyVariable() {
 		return AnyImpl.Singleton.instance;
 	}
 
@@ -152,4 +165,5 @@ public class JenaFactory {
 	public static Quad fromJena(org.apache.jena.sparql.core.Quad quad, UUID salt) {
 		return new QuadImpl(quad, salt);
 	}
+
 }


Mime
View raw message