commonsrdf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From st...@apache.org
Subject [32/50] incubator-commonsrdf git commit: Remove generalized triple/quad support in factory
Date Mon, 03 Oct 2016 16:40:28 GMT
Remove generalized triple/quad support in 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/02db403b
Tree: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/tree/02db403b
Diff: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/diff/02db403b

Branch: refs/heads/master
Commit: 02db403b7d1fe638cf9881df5bfc07d0d72f4275
Parents: fd8dcef
Author: Stian Soiland-Reyes <stain@apache.org>
Authored: Fri Sep 9 16:14:25 2016 +0100
Committer: Stian Soiland-Reyes <stain@apache.org>
Committed: Fri Sep 9 16:14:25 2016 +0100

----------------------------------------------------------------------
 .../commons/rdf/jsonldjava/JsonLdLiteral.java   |   2 +-
 .../rdf/jsonldjava/JsonLdRDFTermFactory.java    | 193 ++++++++++++++-----
 .../commons/rdf/jsonldjava/JsonLdTerm.java      |   4 +-
 3 files changed, 150 insertions(+), 49 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/02db403b/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/JsonLdLiteral.java
----------------------------------------------------------------------
diff --git a/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/JsonLdLiteral.java
b/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/JsonLdLiteral.java
index befc410..8eba6e7 100644
--- a/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/JsonLdLiteral.java
+++ b/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/JsonLdLiteral.java
@@ -85,7 +85,7 @@ public interface JsonLdLiteral extends JsonLdTerm, Literal {
 		public boolean equals(Object obj) {
 			if (obj instanceof JsonLdLiteral) {
 				JsonLdLiteral other = (JsonLdLiteral) obj;
-				return asNode().compareTo(other.asNode()) == 0;
+				return asJsonLdNode().compareTo(other.asJsonLdNode()) == 0;
 			}
 			if (obj instanceof Literal) {
 				Literal other = (Literal) obj;

http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/02db403b/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 6d490b8..d436434 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,10 +27,9 @@ 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;
-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.TripleLike;
+import org.apache.commons.rdf.api.Triple;
 import org.apache.commons.rdf.jsonldjava.JsonLdBlankNode.JsonLdBlankNodeImpl;
 import org.apache.commons.rdf.jsonldjava.JsonLdLiteral.JsonLdLiteralImpl;
 import org.apache.commons.rdf.jsonldjava.JsonLdQuad.JsonLdQuadImpl;
@@ -41,75 +40,104 @@ import com.github.jsonldjava.core.RDFDataset;
 import com.github.jsonldjava.core.RDFDataset.Node;
 
 public final class JsonLdRDFTermFactory implements RDFTermFactory {
-	
+
 	private final String bnodePrefix;
-	
+
 	public JsonLdRDFTermFactory() {
 		// An "outside Graph" bnodePrefix
 		this("urn:uuid:" + UUID.randomUUID() + "#b");
 	}
-		
+
 	JsonLdRDFTermFactory(String bnodePrefix) {
 		this.bnodePrefix = Objects.requireNonNull(bnodePrefix);
 	}
-	
+
 	public Node asJsonLdNode(RDFTerm term) {
 		if (term instanceof JsonLdTerm) {
 			// Return original Node
-			return ((JsonLdTerm)term).asNode();
+			return ((JsonLdTerm) term).asJsonLdNode();
 		}
 		if (term instanceof IRI) {
-			return new RDFDataset.IRI( ((IRI)term).getIRIString() );
+			return new RDFDataset.IRI(((IRI) term).getIRIString());
 		}
-		if (term instanceof BlankNode) {			
-			String ref = ((BlankNode)term).uniqueReference();
+		if (term instanceof BlankNode) {
+			String ref = ((BlankNode) term).uniqueReference();
 			if (ref.startsWith(bnodePrefix)) {
 				// one of our own (but no longer a JsonLdBlankNode),
 				// we can recover the label after our unique prefix
 				return new RDFDataset.BlankNode(ref.replace(bnodePrefix, ""));
-			} 
-			// The "foreign" unique reference might not be a valid bnode string, 
+			}
+			// The "foreign" unique reference might not be a valid bnode string,
 			// we'll convert to a UUID
 			UUID uuid = UUID.nameUUIDFromBytes(ref.getBytes(StandardCharsets.UTF_8));
-			return new RDFDataset.BlankNode( "_:" + uuid );
+			return new RDFDataset.BlankNode("_:" + uuid);
 		}
 		if (term instanceof Literal) {
 			Literal literal = (Literal) term;
-			return new RDFDataset.Literal(literal.getLexicalForm(), literal.getDatatype().getIRIString(),

+			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(RDFTerm subject, RDFTerm predicate, RDFTerm object)
{
-		return asJsonLdQuad(null, subject, predicate, object);
+
+	RDFDataset.Quad createJsonLdQuad(BlankNodeOrIRI graphName, BlankNodeOrIRI subject, IRI predicate,
RDFTerm object) {
+		return new RDFDataset.Quad(asJsonLdNode(subject), asJsonLdNode(predicate), asJsonLdNode(object),
asJsonLdString(graphName));
 	}
-	
-	public RDFDataset.Quad asJsonLdQuad(RDFTerm graphName, RDFTerm subject, RDFTerm predicate,
RDFTerm object) {
-		String graph = null;
-		return new RDFDataset.Quad(asJsonLdNode(subject), asJsonLdNode(predicate), asJsonLdNode(object),
graph);
+
+	String asJsonLdString(BlankNodeOrIRI blankNodeOrIRI) {
+		if (blankNodeOrIRI == null) {
+			return null;
+		}
+		if (blankNodeOrIRI instanceof IRI) {
+			return ((IRI)blankNodeOrIRI).getIRIString();
+		} else if (blankNodeOrIRI instanceof BlankNode) {
+			BlankNode blankNode = (BlankNode) blankNodeOrIRI;
+			String ref = blankNode.uniqueReference();
+			if (ref.startsWith(bnodePrefix)) { 
+				// One of ours (but possibly not a JsonLdBlankNode) -  
+				// we can use the suffix directly
+				return ref.replace(bnodePrefix, "_:");
+			} else {
+				// Map to unique bnode identifier, e.g. _:0dbd92ee-ab1a-45e7-bba2-7ade54f87ec5
+				UUID uuid = UUID.nameUUIDFromBytes(ref.getBytes(StandardCharsets.UTF_8));
+				return "_:"+ uuid;
+			}
+		} else {
+			throw new IllegalArgumentException("Expected a BlankNode or IRI, not: " + blankNodeOrIRI);
+		}
+	}	
+
+	/**
+	 * Adapt a Commons RDF {@link Triple} as a JsonLd {@link RDFDataset.Quad}.
+	 * 
+	 * @param triple Commons RDF {@link Triple} to adapt
+	 * @return Adapted JsonLd {@link RDFDataset.Quad}
+	 */
+	public RDFDataset.Quad asJsonLdQuad(Triple triple) {
+		return createJsonLdQuad(null, triple.getSubject(), triple.getPredicate(), triple.getObject());
 	}
 	
-	@SuppressWarnings({ "rawtypes", "unchecked" })
-	public RDFDataset.Quad asJsonLdQuad(TripleLike tripleOrQuad) {
-		RDFTerm g = null;	
-		if (tripleOrQuad instanceof QuadLike) {
-			QuadLike quadLike = (QuadLike) tripleOrQuad;
-			g = (RDFTerm) quadLike.getGraphName().orElse(null);
-		}		
-		return asJsonLdQuad(g, tripleOrQuad.getSubject(), tripleOrQuad.getPredicate(), tripleOrQuad.getObject());
+	/**
+	 * Adapt a Commons RDF {@link org.apache.commons.rdf.api.Quad} as a JsonLd {@link RDFDataset.Quad}.
+	 * 
+	 * @param quad Commons RDF {@link org.apache.commons.rdf.api.Quad}  to adapt
+	 * @return Adapted JsonLd {@link RDFDataset.Quad}
+	 */
+	public RDFDataset.Quad asJsonLdQuad(org.apache.commons.rdf.api.Quad quad) {
+		BlankNodeOrIRI g = quad.getGraphName().orElse(null);		
+		return createJsonLdQuad(g, quad.getSubject(), quad.getPredicate(), quad.getObject());
 	}
-	
+
 	@Override
 	public JsonLdBlankNode createBlankNode() {
 		String id = "_:" + UUID.randomUUID().toString();
 		return new JsonLdBlankNodeImpl(new RDFDataset.BlankNode(id), bnodePrefix);
 	}
-	
+
 	@Override
 	public JsonLdBlankNode createBlankNode(String name) {
 		String id = "_:" + name;
-		// TODO: Check if name is valid JSON-LD BlankNode identifier		
+		// TODO: Check if name is valid JSON-LD BlankNode identifier
 		return new JsonLdBlankNodeImpl(new RDFDataset.BlankNode(id), bnodePrefix);
 	}
 
@@ -117,29 +145,72 @@ public final class JsonLdRDFTermFactory implements RDFTermFactory {
 	public Dataset createDataset() {
 		return new JsonLdDataset(bnodePrefix);
 	}
-	
+
+	/**
+	 * Adapt a JsonLd {@link RDFDataset} as a Commons RDF {@link Dataset}.
+	 * <p>
+	 * Changes to the Commons RDF {@link Dataset} are reflected in the JsonLd
+	 * {@link RDFDataset} and vice versa.
+	 * 
+	 * @see #asGraph(RDFDataset)
+	 * @param rdfDataSet JsonLd {@link RDFDataset} to adapt
+	 * @return Adapted {@link Dataset}
+	 */
 	public Dataset asDataset(RDFDataset rdfDataSet) {
 		return new JsonLdDataset(rdfDataSet);
 	}
 
+	/**
+	 * Adapt a JsonLd {@link RDFDataset} as a Commons RDF {@link Graph}.
+	 * <p>
+	 * Only triples in the <em>default graph</em> are included. To retrieve
+	 * any other graph, {@link #asDataset(RDFDataset)} together with 
+	 * {@link Dataset#getGraph(BlankNodeOrIRI)}.  
+	 * <p>
+	 * Changes to the Commons RDF {@link Graph} are reflected in the JsonLd
+	 * {@link RDFDataset} and vice versa.
+	 * 
+	 * @see #asDataset(RDFDataset)
+	 * @see #asUnionGraph(RDFDataset) 
+	 * @param rdfDataSet JsonLd {@link RDFDataset} to adapt
+	 * @return Adapted {@link Graph} covering the <em>default graph</em>
+	 */	
 	public Graph asGraph(RDFDataset rdfDataSet) {
 		return new JsonLdGraph(rdfDataSet);
 	}
 
+	/**
+	 * Adapt a JsonLd {@link RDFDataset} as a Commons RDF {@link Graph}.
+	 * <p>
+	 * The graph can be seen as a <em>union graph</em> as it will contains all the
+	 * triples across all the graphs of the underlying {@link RDFDataset}.
+	 * <p>
+	 * Note that some triple operations on a union graph 
+	 * can be inefficient as they need to remove any duplicate
+	 * triples across the graphs.   
+	 * <p>
+	 * Changes to the Commons RDF {@link Graph} are reflected in the JsonLd
+	 * {@link RDFDataset} and vice versa. Triples removed from the graph are
+	 * removed from <strong>all</strong> graphs, while triples added
+	 * are added to the <em>default graph</em>.
+	 * 
+	 * @param rdfDataSet JsonLd {@link RDFDataset} to adapt
+	 * @return Adapted {@link Dataset}
+	 */	
 	public Graph asUnionGraph(RDFDataset rdfDataSet) {
 		return new JsonLdUnionGraph(rdfDataSet);
 	}
-	
+
 	@Override
 	public Graph createGraph() {
 		return new JsonLdGraph(bnodePrefix);
 	}
-	
+
 	@Override
 	public JsonLdIRI createIRI(String iri) {
 		return new JsonLdIRI.JsonLdIRIImpl(iri);
 	}
-	
+
 	@Override
 	public JsonLdLiteral createLiteral(String literal) {
 		return new JsonLdLiteralImpl(new RDFDataset.Literal(literal, null, null));
@@ -158,28 +229,59 @@ public final class JsonLdRDFTermFactory implements RDFTermFactory {
 	@Override
 	public JsonLdQuad createQuad(BlankNodeOrIRI graphName, BlankNodeOrIRI subject, IRI predicate,
RDFTerm object)
 			throws IllegalArgumentException, UnsupportedOperationException {
-		return new JsonLdQuadImpl(asJsonLdQuad(graphName, subject, predicate, object), bnodePrefix);
+		return new JsonLdQuadImpl(createJsonLdQuad(graphName, subject, predicate, object), bnodePrefix);
 	}
-	
 
+	/**
+	 * Adapt a JsonLd {@link RDFDataset.Quad} as a Commons RDF
+	 * {@link org.apache.commons.rdf.api.Quad}.
+	 * <p>
+	 * The underlying JsonLd quad can be retrieved with
+	 * {@link JsonLdQuad#asJsonLdQuad()}.
+	 * 
+	 * @param quad
+	 *            A JsonLd {@link RDFDataset.Quad} to adapt
+	 * @return Adapted {@link JsonLdQuad}
+	 */
 	public JsonLdQuad asQuad(final RDFDataset.Quad quad) {
 		return new JsonLdQuadImpl(quad, bnodePrefix);
 	}
 
 	@Override
 	public JsonLdTriple createTriple(BlankNodeOrIRI subject, IRI predicate, RDFTerm object)
{
-		return new JsonLdTripleImpl(asJsonLdQuad(subject, predicate, object), bnodePrefix);
+		return new JsonLdTripleImpl(createJsonLdQuad(null, subject, predicate, object), bnodePrefix);
 	}
-	
+
+	/**
+	 * Adapt a JsonLd {@link RDFDataset.Quad} as a Commons RDF
+	 * {@link org.apache.commons.rdf.api.Triple}.
+	 * <p>
+	 * The underlying JsonLd quad can be retrieved with
+	 * {@link JsonLdTriple#asJsonLdQuad()}.
+	 * 
+	 * @param quad
+	 *            A JsonLd {@link RDFDataset.Quad} to adapt
+	 * @return Adapted {@link JsonLdTriple}
+	 */
 	public JsonLdTriple asTriple(final RDFDataset.Quad quad) {
 		return new JsonLdTripleImpl(quad, bnodePrefix);
 	}
-	
+
+	/**
+	 * Adapt a JsonLd {@link RDFDataset.Node} as a Commons RDF {@link RDFTerm}.
+	 * <p>
+	 * The underlying node can be retrieved with
+	 * {@link JsonLdTerm#asJsonLdNode()}.
+	 * 
+	 * @param node
+	 *            A JsonLd {@link Node} to adapt
+	 * @return Adapted {@link JsonLdTerm}
+	 */
 	public JsonLdTerm asRDFTerm(final Node node) {
 		return asRDFTerm(node, bnodePrefix);
 	}
-	
-	JsonLdTerm asRDFTerm(final Node node, String blankNodePrefix) {	
+
+	JsonLdTerm asRDFTerm(final Node node, String blankNodePrefix) {
 		if (node == null) {
 			return null; // e.g. default graph
 		}
@@ -197,7 +299,6 @@ public final class JsonLdRDFTermFactory implements RDFTermFactory {
 		} 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/02db403b/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/JsonLdTerm.java
----------------------------------------------------------------------
diff --git a/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/JsonLdTerm.java b/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/JsonLdTerm.java
index 80a7b56..2f250b9 100644
--- a/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/JsonLdTerm.java
+++ b/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/JsonLdTerm.java
@@ -28,14 +28,14 @@ public interface JsonLdTerm extends RDFTerm {
 	 *  
 	 * @return JsonLd {@link Node}
 	 */
-	Node asNode();
+	Node asJsonLdNode();
 	
 	abstract class JsonLdTermImpl implements JsonLdTerm {
 		final Node node;
 		JsonLdTermImpl(Node node) {
 			this.node = node;
 		}
-		public Node asNode() {
+		public Node asJsonLdNode() {
 			return node;
 		}
 	}


Mime
View raw message