commonsrdf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From st...@apache.org
Subject [02/50] incubator-commonsrdf git commit: Move some converters to JsonLdRDFTermFactory
Date Mon, 03 Oct 2016 16:39:58 GMT
Move some converters to JsonLdRDFTermFactory


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

Branch: refs/heads/master
Commit: e98a9635f7f95c55eef48f98cf6ae7050c8235f3
Parents: 4da1bae
Author: Stian Soiland-Reyes <stain@apache.org>
Authored: Mon Apr 4 16:32:31 2016 +0100
Committer: Stian Soiland-Reyes <stain@apache.org>
Committed: Mon Apr 4 16:32:31 2016 +0100

----------------------------------------------------------------------
 .../commons/rdf/jsonldjava/JsonLdGraph.java     |  69 +++++++------
 .../rdf/jsonldjava/JsonLdRDFTermFactory.java    | 102 +++++++++++++++++++
 .../commons/rdf/jsonldjava/JsonLdTriple.java    |   8 +-
 3 files changed, 141 insertions(+), 38 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/e98a9635/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 730191c..14990d3 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
@@ -32,7 +32,6 @@ import org.apache.commons.rdf.api.Literal;
 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.simple.SimpleRDFTermFactory;
 
 import com.github.jsonldjava.core.RDFDataset;
 import com.github.jsonldjava.core.RDFDataset.Node;
@@ -40,9 +39,40 @@ import com.github.jsonldjava.core.RDFDataset.Quad;
 
 public class JsonLdGraph implements Graph {
 	
-	private static UUID SALT = UUID.randomUUID(); 
+	/** 
+	 * Used by {@link #bnodePrefix()} to get a unique UUID per JVM run
+	 */
+	private static UUID SALT = UUID.randomUUID();
 	
-	private RDFDataset rdfDataSet = new RDFDataset();
+	private JsonLdRDFTermFactory rdfTermFactory;
+
+	public JsonLdGraph() {
+		this(new RDFDataset(), false);
+	}
+
+	public JsonLdGraph(RDFDataset rdfDataset) {
+		this(rdfDataset, false);
+	}
+
+	public JsonLdGraph(RDFDataset rdfDataset, boolean unionGraph) {
+		this.rdfDataSet = rdfDataset;	
+		this.unionGraph = unionGraph;
+	}
+	
+	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;
+	}
+
+	/**
+	 * The underlying JSON-LD {@link RDFDataset}.
+	 */
+	private RDFDataset rdfDataSet;
 
 	/**
 	 * If true, include all Quad statements as Triples. If false, 
@@ -53,6 +83,7 @@ public class JsonLdGraph implements Graph {
 
 	@Override
 	public void close() throws Exception {
+		// Drop the memory reference, but don't clear it
 		rdfDataSet = null;		
 	}
 
@@ -63,8 +94,6 @@ public class JsonLdGraph implements Graph {
 		
 		add(triple.getSubject(), triple.getPredicate(), triple.getObject());
 	}
-	private static RDFTermFactory rdfTermFactory = new SimpleRDFTermFactory();
-
 	private Triple asTriple(final RDFDataset.Quad quad) {
 		return new JsonLdTriple(quad, bnodePrefix());
 	}
@@ -72,36 +101,6 @@ public class JsonLdGraph implements Graph {
 	private String bnodePrefix() {
 		return "urn:uuid:" + SALT + "#" +  "g"+ System.identityHashCode(rdfDataSet);
 	}
-	
-	static RDFTerm asTerm(final Node node, String blankNodePrefix) {		
-		if (node.isIRI()) {
-			return new JsonLdIRI(node);
-		} else if (node.isBlankNode()) {
-			return new JsonLdBlankNode(node, blankNodePrefix);
-		} else if (node.isLiteral()) {
-			// TODO: Our own JsonLdLiteral
-			if (node.getLanguage() != null) {
-				return rdfTermFactory.createLiteral(node.getValue(), node.getLanguage());
-			} else {
-				return rdfTermFactory.createLiteral(node.getValue(), rdfTermFactory.createIRI(node.getDatatype()));
-			}
-		} else {
-			throw new IllegalArgumentException("Node is neither IRI, BlankNode nor Literal: " + node);
-		}
-	}
-	
-	private RDFDataset.Quad asJsonLdQuad(Triple triple) {
-		Node subject = asJsonLdNode(triple.getSubject());
-		Node predicate = asJsonLdNode(triple.getPredicate());
-		Node object = asJsonLdNode(triple.getObject());
-		String graph = null;
-		// TODO: Add Quad to commons-rdf
-//		if (triple instanceof Quad) {
-//			String graph = triple.getGraph().getIRIString();
-//		}
-		return new RDFDataset.Quad(subject, predicate, object, graph);
-		
-	}
 
 	private Node asJsonLdNode(RDFTerm term) {
 		if (term instanceof IRI) {

http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/e98a9635/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 e2ee429..99927ad 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
@@ -17,12 +17,114 @@
  */
 package org.apache.commons.rdf.jsonldjava;
 
+import java.util.UUID;
+
+import org.apache.commons.rdf.api.BlankNode;
+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.Literal;
+import org.apache.commons.rdf.api.RDFTerm;
+import org.apache.commons.rdf.api.Triple;
 import org.apache.commons.rdf.simple.SimpleRDFTermFactory;
 
+import com.github.jsonldjava.core.RDFDataset;
+import com.github.jsonldjava.core.RDFDataset.Node;
+
 final class JsonLdRDFTermFactory extends SimpleRDFTermFactory {
+	
+	public JsonLdRDFTermFactory() {
+		// An "outside Graph" bnodePrefix
+		this("urn:uuid:" + UUID.randomUUID() + "#b");
+	}
+	
+	JsonLdRDFTermFactory(String bnodePrefix) {
+		this.bnodePrefix = bnodePrefix;
+	}
+	
+	String bnodePrefix;
+	
 	@Override
 	public Graph createGraph() throws UnsupportedOperationException {
 		return new JsonLdGraph();
 	}
+	
+	@Override
+	public IRI createIRI(String iri) {
+		return new JsonLdIRI(new RDFDataset.IRI(iri));
+	}
+	
+	@Override
+	public BlankNode createBlankNode() {
+		String id = "_:" + UUID.randomUUID().toString();
+		return new JsonLdBlankNode(new RDFDataset.BlankNode(id), bnodePrefix);
+	}
+	
+	@Override
+	public BlankNode createBlankNode(String name) {
+		String id = "_:" + name;
+		// TODO: Check if name is valid JSON-LD BlankNode identifier
+		return new JsonLdBlankNode(new RDFDataset.BlankNode(id), bnodePrefix);
+	}
+	
+	@Override
+	public Triple createTriple(BlankNodeOrIRI subject, IRI predicate, RDFTerm object) {
+		return super.createTriple(subject, predicate, object);
+	}
+
+
+	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);
+	}
+	
+
+	public RDFDataset.Quad asJsonLdQuad(Triple triple) {
+		Node subject = asJsonLdNode(triple.getSubject());
+		Node predicate = asJsonLdNode(triple.getPredicate());
+		Node object = asJsonLdNode(triple.getObject());
+		String graph = null;
+		// TODO: Add Quad to commons-rdf
+//		if (triple instanceof Quad) {
+//			String graph = triple.getGraph().getIRIString();
+//		}
+		return new RDFDataset.Quad(subject, predicate, object, graph);
+		
+	}
+	
+	RDFTerm asTerm(final Node node, String blankNodePrefix) {		
+		if (node.isIRI()) {
+			return new JsonLdIRI(node);
+		} else if (node.isBlankNode()) {
+			return new JsonLdBlankNode(node, blankNodePrefix);
+		} else if (node.isLiteral()) {
+			// TODO: Our own JsonLdLiteral
+			if (node.getLanguage() != null) {
+				return createLiteral(node.getValue(), node.getLanguage());
+			} else {
+				return createLiteral(node.getValue(), createIRI(node.getDatatype()));
+			}
+		} else {
+			throw new IllegalArgumentException("Node is neither IRI, BlankNode nor Literal: " + node);
+		}
+	}	
+	
+	
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/e98a9635/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/JsonLdTriple.java
----------------------------------------------------------------------
diff --git a/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/JsonLdTriple.java
b/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/JsonLdTriple.java
index 1a2dd91..49e95e3 100644
--- a/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/JsonLdTriple.java
+++ b/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/JsonLdTriple.java
@@ -30,6 +30,8 @@ final class JsonLdTriple implements Triple {
 	private final Quad quad;
 	private String blankNodePrefix;
 
+	private static JsonLdRDFTermFactory rdfTermFactory = new JsonLdRDFTermFactory();
+	
 	JsonLdTriple(Quad quad, String blankNodePrefix) {
 		this.quad = quad;
 		this.blankNodePrefix = blankNodePrefix;			
@@ -37,17 +39,17 @@ final class JsonLdTriple implements Triple {
 
 	@Override
 	public BlankNodeOrIRI getSubject() {
-		return (BlankNodeOrIRI) JsonLdGraph.asTerm(quad.getSubject(), blankNodePrefix);
+		return (BlankNodeOrIRI) rdfTermFactory.asTerm(quad.getSubject(), blankNodePrefix);
 	}
 
 	@Override
 	public IRI getPredicate() {
-		return (IRI) JsonLdGraph.asTerm(quad.getPredicate(), blankNodePrefix);
+		return (IRI) rdfTermFactory.asTerm(quad.getPredicate(), blankNodePrefix);
 	}
 
 	@Override
 	public RDFTerm getObject() {
-		return JsonLdGraph.asTerm(quad.getObject(), blankNodePrefix);
+		return rdfTermFactory.asTerm(quad.getObject(), blankNodePrefix);
 	}
 
 	@Override


Mime
View raw message