commonsrdf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From st...@apache.org
Subject [27/50] incubator-commonsrdf git commit: ensure Graph works on the correct graph names
Date Tue, 04 Oct 2016 01:30:48 GMT
ensure Graph works on the correct graph names


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

Branch: refs/heads/rdf4j
Commit: e3a4fbaa5b871ce83045d50d2929534ed21d8f09
Parents: ca11a42
Author: Stian Soiland-Reyes <stain@apache.org>
Authored: Fri Sep 9 15:24:03 2016 +0100
Committer: Stian Soiland-Reyes <stain@apache.org>
Committed: Fri Sep 9 15:24:03 2016 +0100

----------------------------------------------------------------------
 .../commons/rdf/jsonldjava/JsonLdDataset.java   | 13 ++++++
 .../commons/rdf/jsonldjava/JsonLdGraph.java     | 25 ++++++++++-
 .../commons/rdf/jsonldjava/JsonLdGraphLike.java | 44 ++++++++------------
 .../rdf/jsonldjava/JsonLdRDFTermFactory.java    |  3 +-
 .../rdf/jsonldjava/JsonLdUnionGraph.java        | 44 ++++++++++++++++++--
 5 files changed, 96 insertions(+), 33 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/e3a4fbaa/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/JsonLdDataset.java
----------------------------------------------------------------------
diff --git a/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/JsonLdDataset.java
b/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/JsonLdDataset.java
index 0e9422f..569180e 100644
--- a/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/JsonLdDataset.java
+++ b/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/JsonLdDataset.java
@@ -20,6 +20,7 @@ package org.apache.commons.rdf.jsonldjava;
 import java.util.List;
 import java.util.Optional;
 import java.util.function.Predicate;
+import java.util.stream.Collectors;
 import java.util.stream.Stream;
 
 import org.apache.commons.rdf.api.BlankNodeOrIRI;
@@ -82,6 +83,12 @@ public class JsonLdDataset extends JsonLdGraphLike<org.apache.commons.rdf.api.Qu
 		super.remove(graphName, subject, predicate, object);
 	}
 
+
+	@Override
+	public void remove(Quad q) {
+		remove(q.getGraphName(), q.getSubject(), q.getPredicate(), q.getObject());
+	}
+	
 	@Override
 	public Stream<? extends Quad> stream(Optional<BlankNodeOrIRI> graphName, BlankNodeOrIRI
subject, IRI predicate,
 			RDFTerm object) {		
@@ -92,8 +99,14 @@ public class JsonLdDataset extends JsonLdGraphLike<org.apache.commons.rdf.api.Qu
 	}
 
 	@Override
+	public long size() {		
+		return rdfDataSet.graphNames().parallelStream().map(rdfDataSet::getQuads).collect(Collectors.summingLong(List::size));
+	}
+
+	@Override
 	Quad asTripleOrQuad(com.github.jsonldjava.core.RDFDataset.Quad jsonldQuad) {
 		return factory.createQuad(jsonldQuad);
 	}
 
+
 }

http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/e3a4fbaa/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/JsonLdGraph.java
----------------------------------------------------------------------
diff --git a/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/JsonLdGraph.java
b/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/JsonLdGraph.java
index b8dc869..5e13a4e 100644
--- a/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/JsonLdGraph.java
+++ b/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/JsonLdGraph.java
@@ -18,6 +18,7 @@
 package org.apache.commons.rdf.jsonldjava;
 
 import java.util.List;
+import java.util.Objects;
 import java.util.Optional;
 import java.util.stream.Stream;
 
@@ -25,6 +26,7 @@ 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.RDFTerm;
+import org.apache.commons.rdf.api.Triple;
 
 import com.github.jsonldjava.core.RDFDataset;
 
@@ -39,7 +41,7 @@ public class JsonLdGraph extends JsonLdGraphLike<org.apache.commons.rdf.api.Trip
 	
 	JsonLdGraph(RDFDataset rdfDataSet, Optional<BlankNodeOrIRI> graphName, String bnodePrefix)
{
 		super(rdfDataSet, bnodePrefix);
-		this.graphName = graphName;
+		this.graphName = Objects.requireNonNull(graphName);
 	}
 	
 	JsonLdGraph(String bnodePrefix) {
@@ -47,8 +49,18 @@ public class JsonLdGraph extends JsonLdGraphLike<org.apache.commons.rdf.api.Trip
 		this.graphName = Optional.empty();
 	}
 
+	@Override
+	public void clear() {
+		filteredGraphs(graphName).forEach(l -> l.clear());
+	}
 
 	@Override
+	public void add(Triple t) {
+		// Ensure it's added in the correct graph
+		super.add(graphName.orElse(null), t.getSubject(), t.getPredicate(), t.getObject());
+	}
+	
+	@Override
 	public void add(BlankNodeOrIRI subject, IRI predicate, RDFTerm object) {
 		super.add(graphName.orElse(null), subject, predicate, object);
 	}
@@ -59,9 +71,20 @@ public class JsonLdGraph extends JsonLdGraphLike<org.apache.commons.rdf.api.Trip
 	}
 	
 	@Override
+	public boolean contains(Triple t) {
+		return contains(graphName, t.getSubject(), t.getPredicate(), t.getObject());
+	}
+	
+	@Override
 	public void remove(BlankNodeOrIRI subject, IRI predicate, RDFTerm object) {
 		super.remove(graphName, subject, predicate, object);
 	}
+	
+	@Override
+	public void remove(Triple t) {
+		// Only remove from the particular graph
+		remove(graphName, t.getSubject(), t.getPredicate(), t.getObject());		
+	}
 
 	@Override
 	public long size() {

http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/e3a4fbaa/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/JsonLdGraphLike.java
----------------------------------------------------------------------
diff --git a/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/JsonLdGraphLike.java
b/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/JsonLdGraphLike.java
index 2047a6e..ad2cc23 100644
--- a/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/JsonLdGraphLike.java
+++ b/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/JsonLdGraphLike.java
@@ -19,10 +19,10 @@ package org.apache.commons.rdf.jsonldjava;
 
 import java.nio.charset.StandardCharsets;
 import java.util.List;
+import java.util.Objects;
 import java.util.Optional;
 import java.util.UUID;
 import java.util.function.Predicate;
-import java.util.stream.Collectors;
 import java.util.stream.Stream;
 
 import org.apache.commons.rdf.api.BlankNode;
@@ -73,9 +73,9 @@ abstract class JsonLdGraphLike<T extends TripleLike<BlankNodeOrIRI,
IRI, RDFTerm
 		this(rdfDataSet, "urn:uuid:" + SALT + "#" +  "g"+ System.identityHashCode(rdfDataSet));
 	}
 
-	JsonLdGraphLike(RDFDataset rdfDataset, String bnodePrefix) {
-		rdfDataSet = rdfDataset;
-		this.bnodePrefix = bnodePrefix;
+	JsonLdGraphLike(RDFDataset rdfDataSet, String bnodePrefix) {
+		this.rdfDataSet = Objects.requireNonNull(rdfDataSet);
+		this.bnodePrefix = Objects.requireNonNull(bnodePrefix);
 		this.factory = new JsonLdRDFTermFactory(bnodePrefix);
 	}
 	
@@ -86,7 +86,6 @@ abstract class JsonLdGraphLike<T extends TripleLike<BlankNodeOrIRI,
IRI, RDFTerm
 	@Override
 	public void add(T tripleOrQuad) {
 		String g = graphNameAsJsonLdString(tripleOrQuad);
-
 		String s = asJsonLdString(tripleOrQuad.getSubject());
 		String p = asJsonLdString(tripleOrQuad.getPredicate());
 		
@@ -100,11 +99,6 @@ abstract class JsonLdGraphLike<T extends TripleLike<BlankNodeOrIRI,
IRI, RDFTerm
 			rdfDataSet.addQuad(s,p,literal.getLexicalForm(), datatype, language, g);
 		}
 	}
-
-	@Override
-	public void clear() {
-		rdfDataSet.clear();
-	}
 	
 	public void close() {
 		// Drop the memory reference, but don't clear it
@@ -112,6 +106,16 @@ abstract class JsonLdGraphLike<T extends TripleLike<BlankNodeOrIRI,
IRI, RDFTerm
 	}	
 
 	@Override
+	public void clear() {		
+		filteredGraphs(null).forEach(s -> s.clear());
+		//   In theory we could use
+		//rdfDataSet.clear();
+		//   but then we would need to also do
+		//rdfDataSet.put("@default",  new ArrayList());
+		//   .. both of which seems to be touching too much on JsonLd-Java's internal structure
+	}
+
+	@Override
 	public boolean contains(T tripleOrQuad) {		
 		return stream().anyMatch(Predicate.isEqual(tripleOrQuad));
 	}
@@ -119,21 +123,7 @@ abstract class JsonLdGraphLike<T extends TripleLike<BlankNodeOrIRI,
IRI, RDFTerm
 	public RDFDataset getRdfDataSet() {
 		return rdfDataSet;
 	}
-	
-	@Override
-	public void remove(T t) {
-		if (t instanceof org.apache.commons.rdf.api.Quad) {
-			org.apache.commons.rdf.api.Quad q = (org.apache.commons.rdf.api.Quad) t;
-			remove(q.getGraphName(), q.getSubject(), q.getPredicate(), q.getObject());
-		} else {
-			remove(Optional.empty(), t.getSubject(), t.getPredicate(), t.getObject());
-		}
-	}
-	
-	@Override
-	public long size() {		
-		return rdfDataSet.graphNames().parallelStream().map(rdfDataSet::getQuads).collect(Collectors.summingLong(List::size));
-	}
+
 	
 	@Override
 	public Stream<? extends T> stream() {
@@ -209,11 +199,11 @@ abstract class JsonLdGraphLike<T extends TripleLike<BlankNodeOrIRI,
IRI, RDFTerm
 				.map(rdfDataSet::getQuads);
 	}
 
-	String graphNameAsJsonLdString(T tripleOrQuad) {
+	String graphNameAsJsonLdString(T tripleOrQuad) {		
 		if (tripleOrQuad instanceof org.apache.commons.rdf.api.Quad) {
 			org.apache.commons.rdf.api.Quad quad = (org.apache.commons.rdf.api.Quad)tripleOrQuad;
 			return quad.getGraphName().map(this::asJsonLdString).orElse("@default");			
-		}
+		}		
 		return "@default";
 	}
 	

http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/e3a4fbaa/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/JsonLdRDFTermFactory.java
----------------------------------------------------------------------
diff --git a/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/JsonLdRDFTermFactory.java
b/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/JsonLdRDFTermFactory.java
index 15bc466..1dff9fb 100644
--- a/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/JsonLdRDFTermFactory.java
+++ b/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/JsonLdRDFTermFactory.java
@@ -18,6 +18,7 @@
 package org.apache.commons.rdf.jsonldjava;
 
 import java.nio.charset.StandardCharsets;
+import java.util.Objects;
 import java.util.UUID;
 
 import org.apache.commons.rdf.api.BlankNode;
@@ -49,7 +50,7 @@ public final class JsonLdRDFTermFactory implements RDFTermFactory {
 	}
 	
 	JsonLdRDFTermFactory(String bnodePrefix) {
-		this.bnodePrefix = bnodePrefix;
+		this.bnodePrefix = Objects.requireNonNull(bnodePrefix);
 	}
 	
 	public Node asJsonLdNode(RDFTerm term) {

http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/e3a4fbaa/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/JsonLdUnionGraph.java
----------------------------------------------------------------------
diff --git a/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/JsonLdUnionGraph.java
b/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/JsonLdUnionGraph.java
index ce164c6..6b32f8c 100644
--- a/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/JsonLdUnionGraph.java
+++ b/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/JsonLdUnionGraph.java
@@ -24,9 +24,28 @@ 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.RDFTerm;
+import org.apache.commons.rdf.api.Triple;
 
 import com.github.jsonldjava.core.RDFDataset;
 
+/**
+ * A <strong>union graph</em> representation of a JsonLd {@link RDFDataset}.
+ * <p>
+ * A union graph contains all the triples of the dataset, irregardless of their
+ * graph names.
+ * <p>
+ * {@link #add(Triple)} and {@link #add(BlankNodeOrIRI, IRI, RDFTerm)} 
+ * will add the triple to the default graph
+ * (e.g. <code>@default</code> in JSON-LD), while
+ * the remaining methods (including
+ * {@link #remove(Triple)} or {@link #remove(BlankNodeOrIRI, IRI, RDFTerm)}) 
+ * relate to triples from <strong>all</strong> graphs.
+ * <p>
+ * <strong>Note:</strong>
+ * Some operations like {@link #stream()} and {@link #size()} are
+ * inefficient as they skip any duplicate triples from multiple
+ * graphs.
+ */
 public class JsonLdUnionGraph extends JsonLdGraphLike<org.apache.commons.rdf.api.Triple>
implements Graph {
 
 	JsonLdUnionGraph(String bnodePrefix) {
@@ -58,12 +77,28 @@ public class JsonLdUnionGraph extends JsonLdGraphLike<org.apache.commons.rdf.api
 	}
 
 	@Override
+	public void remove(Triple t) {
+		// Remove from ALL graphs, not just default graph
+		super.remove(null, t.getSubject(), t.getPredicate(), t.getObject());
+	}
+	
+	@Override
 	public Stream<JsonLdTriple> stream(BlankNodeOrIRI subject, IRI predicate,
-			RDFTerm object) {		
+			RDFTerm object) {				
 		return filteredGraphs(null)
 				.flatMap(List::stream)
 				.filter(quadFilter(subject, predicate, object))
-				.map(factory::createTriple);
+				.map(factory::createTriple)
+				// Make sure we don't have duplicate triples
+				// NOTE: This can be quite inefficient
+				.distinct();
+	}
+	
+	@Override
+	public Stream<? extends Triple> stream() {		
+		// NOTE: inefficient as we have to remove duplicate triples 
+		// in different graphs :-(
+		return super.stream().distinct();
 	}
 
 	@Override
@@ -73,8 +108,9 @@ public class JsonLdUnionGraph extends JsonLdGraphLike<org.apache.commons.rdf.api
 	
 	@Override
 	public long size() {
-		// NOTE: Inefficient as we'll have remove duplicate triples :-( 
-		return stream().distinct().count();
+		// Note: Our specialized stream() already removes duplicates using .distinct()
+		return stream().count();
 	}
+
 }
 


Mime
View raw message