commonsrdf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From st...@apache.org
Subject [23/50] incubator-commonsrdf git commit: Graph uses factory
Date Mon, 03 Oct 2016 16:40:19 GMT
Graph uses factory


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

Branch: refs/heads/master
Commit: dde5abac27983ace5ebbdc1b37c35d65a0df3dc0
Parents: 80ad999
Author: Stian Soiland-Reyes <stain@apache.org>
Authored: Thu Sep 8 17:10:31 2016 +0100
Committer: Stian Soiland-Reyes <stain@apache.org>
Committed: Thu Sep 8 17:10:31 2016 +0100

----------------------------------------------------------------------
 .../commons/rdf/jsonldjava/JsonLdGraph.java     | 79 ++++++--------------
 .../rdf/jsonldjava/JsonLdRDFTermFactory.java    |  7 +-
 2 files changed, 30 insertions(+), 56 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/dde5abac/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 c31ad71..30c13a8 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
@@ -49,21 +49,22 @@ public class JsonLdGraph implements Graph {
 	 */
 	private RDFDataset rdfDataSet;
 
-	public RDFDataset getRdfDataSet() {
-		return rdfDataSet;
-	}
-
-	private JsonLdRDFTermFactory rdfTermFactory;
-
 	/**
 	 * If true, include all Quad statements as Triples. If false, 
 	 * only Quads in the default graph (<code>null</code>) are
 	 * included.
 	 */
-	private boolean unionGraph = false;
+	private final boolean unionGraph;
+
+	/**
+	 * Prefix to use in blank node identifiers
+	 */
+	private final String bnodePrefix;
 
+	private JsonLdRDFTermFactory factory;
+	
 	public JsonLdGraph() {
-		this(new RDFDataset(), false);
+		this(new RDFDataset(), false);		
 	}
 	
 	public JsonLdGraph(RDFDataset rdfDataset) {
@@ -73,8 +74,16 @@ public class JsonLdGraph implements Graph {
 	public JsonLdGraph(RDFDataset rdfDataset, boolean unionGraph) {
 		this.rdfDataSet = rdfDataset;	
 		this.unionGraph = unionGraph;
+		this.bnodePrefix = "urn:uuid:" + SALT + "#" +  "g"+ System.identityHashCode(rdfDataSet);
+		this.factory = new JsonLdRDFTermFactory(bnodePrefix);
 	}
 
+	
+	public RDFDataset getRdfDataSet() {
+		return rdfDataSet;
+	}
+
+
 	@Override
 	public void add(BlankNodeOrIRI subject, IRI predicate, RDFTerm object) {
 		String subjectStr;
@@ -121,7 +130,7 @@ public class JsonLdGraph implements Graph {
 	@Override
 	public void close() {
 		// Drop the memory reference, but don't clear it
-		rdfDataSet = null;		
+		rdfDataSet = null;			
 	}
 	
 	@Override
@@ -134,22 +143,12 @@ public class JsonLdGraph implements Graph {
 		return stream().anyMatch(Predicate.isEqual(triple));
 	}
 
-	public RDFTermFactory getContext() {
-		// Note: This does not need to be synchronized, it's OK 
-		// if you get a few accidental copies as the
-		// same bnodePrefix() is passed to each
-		if (rdfTermFactory == null) {
-			rdfTermFactory = new JsonLdRDFTermFactory(bnodePrefix());
-		}
-		return rdfTermFactory;
-	}
-
 	@Override
 	public Stream<? extends Triple> stream() {
 		if (! unionGraph) {
-			return rdfDataSet.getQuads("@default").parallelStream().map(this::asTriple);
+			return rdfDataSet.getQuads("@default").parallelStream().map(factory::asTriple);
 		}
-		return rdfDataSet.graphNames().parallelStream().map(rdfDataSet::getQuads).flatMap(List<Quad>::parallelStream).map(this::asTriple);
+		return rdfDataSet.graphNames().parallelStream().map(rdfDataSet::getQuads).flatMap(List<Quad>::parallelStream).map(factory::asTriple);
 	}
 
 	@Override
@@ -193,41 +192,11 @@ public class JsonLdGraph implements Graph {
 			return rdfDataSet.graphNames().parallelStream().map(rdfDataSet::getQuads).collect(Collectors.summingLong(List::size));
 		}
 	}
-
-	private Node asJsonLdNode(RDFTerm term) {
-		if (term instanceof IRI) {
-			return new RDFDataset.IRI( ((IRI)term).getIRIString() );
-		}
-		if (term instanceof BlankNode) {
-			
-			String uniqueReference = ((BlankNode)term).uniqueReference();
-			if (uniqueReference.startsWith(bnodePrefix())) {
-				// one of our own
-				// TODO: Retrieve the original BlankNode
-				return new RDFDataset.BlankNode(term.ntriplesString());
-			} 
-			return new RDFDataset.BlankNode( "_:" + uniqueReference );
-		}
-		if (term instanceof Literal) {
-			Literal literal = (Literal) term;
-			return new RDFDataset.Literal(literal.getLexicalForm(), literal.getDatatype().getIRIString(),

-					literal.getLanguageTag().orElse(null));
-		}
-		throw new IllegalArgumentException("RDFTerm not instanceof IRI, BlankNode or Literal: "
+ term);
-	}
-
-	private Triple asTriple(final RDFDataset.Quad quad) {
-		return new JsonLdTriple(quad, bnodePrefix());
-	}
-
-	public String bnodePrefix() {
-		return "urn:uuid:" + SALT + "#" +  "g"+ System.identityHashCode(rdfDataSet);
-	}
-
+	
 	private Predicate<? super Quad> quadFilter(BlankNodeOrIRI subject, IRI predicate,
RDFTerm object) {
-		Optional<Node> subjectNode = Optional.ofNullable(subject).map(this::asJsonLdNode);
-		Optional<Node> predicateNode = Optional.ofNullable(predicate).map(this::asJsonLdNode);
-		Optional<Node> objectNode = Optional.ofNullable(object).map(this::asJsonLdNode);
+		Optional<Node> subjectNode = Optional.ofNullable(subject).map(factory::asJsonLdNode);
+		Optional<Node> predicateNode = Optional.ofNullable(predicate).map(factory::asJsonLdNode);
+		Optional<Node> objectNode = Optional.ofNullable(object).map(factory::asJsonLdNode);
 		
 		return q -> {
 		    if (subjectNode.isPresent() && subjectNode.get().compareTo(q.getSubject())
!= 0) {

http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/dde5abac/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 5ac360a..12ab13d 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
@@ -27,6 +27,7 @@ import org.apache.commons.rdf.api.Literal;
 import org.apache.commons.rdf.api.QuadLike;
 import org.apache.commons.rdf.api.RDFTerm;
 import org.apache.commons.rdf.api.RDFTermFactory;
+import org.apache.commons.rdf.api.Triple;
 import org.apache.commons.rdf.api.TripleLike;
 import org.apache.commons.rdf.simple.Types;
 
@@ -74,6 +75,10 @@ final class JsonLdRDFTermFactory implements RDFTermFactory {
 		return new JsonLdTriple.JsonLdTripleImpl(asJsonLdQuad(subject, predicate, object), bnodePrefix);
 	}
 	
+	public Triple asTriple(final RDFDataset.Quad quad) {
+		return new JsonLdTriple.JsonLdTripleImpl(quad, bnodePrefix);
+	}
+
 	@Override
 	public JsonLdQuad createQuad(BlankNodeOrIRI graphName, BlankNodeOrIRI subject, IRI predicate,
RDFTerm object)
 			throws IllegalArgumentException, UnsupportedOperationException {
@@ -93,7 +98,7 @@ final class JsonLdRDFTermFactory implements RDFTermFactory {
 	}
 
 
-	private Node asJsonLdNode(RDFTerm term) {
+	public Node asJsonLdNode(RDFTerm term) {
 		if (term instanceof JsonLdTerm) {
 			// Return original Node
 			return ((JsonLdTerm)term).asNode();


Mime
View raw message