commonsrdf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From st...@apache.org
Subject [1/9] incubator-commonsrdf git commit: sesame Triple/Graph support
Date Tue, 03 May 2016 22:15:50 GMT
Repository: incubator-commonsrdf
Updated Branches:
  refs/heads/rdf4j dadfaeca2 -> b58505747


sesame Triple/Graph support


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

Branch: refs/heads/rdf4j
Commit: 36db3bcc78a1cabbc5ee50b1b519aa133eb93194
Parents: dadfaec
Author: Stian Soiland-Reyes <stain@apache.org>
Authored: Tue May 3 19:49:22 2016 +0100
Committer: Stian Soiland-Reyes <stain@apache.org>
Committed: Tue May 3 19:49:22 2016 +0100

----------------------------------------------------------------------
 .../commons/rdf/rdf4j/Rdf4JRDFTermFactory.java  | 110 ++++++++++++-------
 1 file changed, 71 insertions(+), 39 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/36db3bcc/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/Rdf4JRDFTermFactory.java
----------------------------------------------------------------------
diff --git a/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/Rdf4JRDFTermFactory.java b/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/Rdf4JRDFTermFactory.java
index 2973406..8b0b178 100644
--- a/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/Rdf4JRDFTermFactory.java
+++ b/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/Rdf4JRDFTermFactory.java
@@ -22,6 +22,10 @@ import java.util.Optional;
 import java.util.UUID;
 import java.util.stream.Stream;
 
+
+// To avoid confusion, avoid importing 
+// classes that are in both
+// commons.rdf and openrdf.model (e.g. IRI)
 import org.apache.commons.rdf.api.BlankNode;
 import org.apache.commons.rdf.api.BlankNodeOrIRI;
 import org.apache.commons.rdf.api.RDFTerm;
@@ -29,8 +33,8 @@ import org.apache.commons.rdf.api.RDFTermFactory;
 import org.apache.commons.rdf.api.Triple;
 import org.apache.commons.rdf.simple.Types;
 import org.openrdf.model.BNode;
-import org.openrdf.model.IRI;
 import org.openrdf.model.Model;
+import org.openrdf.model.Resource;
 import org.openrdf.model.Statement;
 import org.openrdf.model.Value;
 import org.openrdf.model.ValueFactory;
@@ -41,9 +45,17 @@ import org.openrdf.rio.turtle.TurtleUtil;
 public class Rdf4JRDFTermFactory implements RDFTermFactory {
 	private static final String QUOTE = "\"";
 
-	ValueFactory valueFactory = SimpleValueFactory.getInstance();
+	private ValueFactory valueFactory;
+	
+	private String salt = "urn:uuid:" + UUID.randomUUID() + "#";
+	
+	public Rdf4JRDFTermFactory() {
+		this.valueFactory = SimpleValueFactory.getInstance();
+	}
 	
-	String salt = "urn:uuid:" + UUID.randomUUID() + "#";
+	public Rdf4JRDFTermFactory(ValueFactory valueFactory) { 
+		this.valueFactory = valueFactory;
+	}	
 	
 	@Override
 	public BlankNode createBlankNode() throws UnsupportedOperationException {
@@ -88,6 +100,13 @@ public class Rdf4JRDFTermFactory implements RDFTermFactory {
 		return asRDFTermGraph(new LinkedHashModel());
 	}
 	
+	private Statement asStatement(Triple triple) {
+		return valueFactory.createStatement(
+				(org.openrdf.model.Resource) asValue(triple.getSubject()), 
+				(org.openrdf.model.IRI) asValue(triple.getPredicate()), 
+				asValue(triple.getObject()));
+	}
+	
 	@Override
 	public Triple createTriple(BlankNodeOrIRI subject, org.apache.commons.rdf.api.IRI predicate,
RDFTerm object)
 			throws IllegalArgumentException, UnsupportedOperationException {
@@ -95,23 +114,13 @@ public class Rdf4JRDFTermFactory implements RDFTermFactory {
 				(org.openrdf.model.Resource) asValue(subject), 
 				(org.openrdf.model.IRI) asValue(predicate), 
 				asValue(object));
-		return new Triple() {			
-			@Override
-			public BlankNodeOrIRI getSubject() {
-				return (BlankNodeOrIRI) asRDFTerm(statement.getSubject());
-			}
-			@Override
-			public org.apache.commons.rdf.api.IRI getPredicate() {
-				return (org.apache.commons.rdf.api.IRI) asRDFTerm(statement.getPredicate());
-			}
-			@Override
-			public RDFTerm getObject() {
-				return asRDFTerm(statement.getObject());
-			}
-		};
+		return asTriple(statement);
 	}	
 	
 	private Value asValue(RDFTerm object) {
+		if (object == null) { 
+			return null;
+		}
 		if (object instanceof org.apache.commons.rdf.api.IRI) {
 			org.apache.commons.rdf.api.IRI iri = (org.apache.commons.rdf.api.IRI) object;
 			return valueFactory.createIRI(iri.getIRIString());
@@ -146,62 +155,85 @@ public class Rdf4JRDFTermFactory implements RDFTermFactory {
 					return size;
 				} else {
 					// Collection.size() can't help us, we'll have to count
-					return model.stream().count();
+					return model.parallelStream().count();
 				}				
 			}
 			
 			@Override
-			public void remove(BlankNodeOrIRI subject, IRI predicate, RDFTerm object) {
-				// TODO Auto-generated method stub
+			public void remove(BlankNodeOrIRI subject, org.apache.commons.rdf.api.IRI predicate, RDFTerm
object) {
+				model.remove(
+						(Resource)asValue(subject), 
+						(org.openrdf.model.IRI)asValue(predicate), 
+						asValue(object));
 				
 			}
 			
 			@Override
-			public void remove(Triple triple) {
-				// TODO Auto-generated method stub
-				
+			public void remove(Triple triple) { 
+				model.remove(asStatement(triple));				
 			}
 			
 			@Override
 			public Stream<? extends Triple> getTriples(BlankNodeOrIRI subject, org.apache.commons.rdf.api.IRI
predicate, RDFTerm object) {
-				// TODO Auto-generated method stub
-				return null;
+				return model.filter(
+						(Resource)asValue(subject), 
+						(org.openrdf.model.IRI)asValue(predicate), 
+						asValue(object)).parallelStream()
+					.map(Rdf4JRDFTermFactory.this::asTriple);
 			}
 			
 			@Override
-			public Stream<? extends Triple> getTriples() {
-				// TODO Auto-generated method stub
-				return null;
+			public Stream<Triple> getTriples() {
+				return model.parallelStream().map(Rdf4JRDFTermFactory.this::asTriple);
 			}
 			
 			@Override
 			public boolean contains(BlankNodeOrIRI subject, org.apache.commons.rdf.api.IRI predicate,
RDFTerm object) {
-				// TODO Auto-generated method stub
-				return false;
+				return model.contains(
+						(Resource)asValue(subject), 
+						(org.openrdf.model.IRI)asValue(predicate), 
+						asValue(object));
 			}
 			
 			@Override
 			public boolean contains(Triple triple) {
-				// TODO Auto-generated method stub
-				return false;
+				return model.contains(asStatement(triple));
 			}
 			
+
 			@Override
 			public void clear() {
-				// TODO Auto-generated method stub
-				
+				model.clear();
 			}
 			
 			@Override
-			public void add(BlankNodeOrIRI subject, IRI predicate, RDFTerm object) {
-				// TODO Auto-generated method stub
-				
+			public void add(BlankNodeOrIRI subject, org.apache.commons.rdf.api.IRI predicate, RDFTerm
object) {
+				model.add(
+						(Resource)asValue(subject), 
+						(org.openrdf.model.IRI)asValue(predicate), 
+						asValue(object));				
 			}
 			
 			@Override
 			public void add(Triple triple) {
-				// TODO Auto-generated method stub
-				
+				model.add(asStatement(triple));
+			}
+		};
+	}
+
+	protected Triple asTriple(final Statement statement) {
+		return new Triple() {			
+			@Override
+			public BlankNodeOrIRI getSubject() {
+				return (BlankNodeOrIRI) asRDFTerm(statement.getSubject());
+			}
+			@Override
+			public org.apache.commons.rdf.api.IRI getPredicate() {
+				return (org.apache.commons.rdf.api.IRI) asRDFTerm(statement.getPredicate());
+			}
+			@Override
+			public RDFTerm getObject() {
+				return asRDFTerm(statement.getObject());
 			}
 		};
 	}


Mime
View raw message