commonsrdf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From st...@apache.org
Subject [17/50] incubator-commonsrdf git commit: Support for Quad
Date Fri, 07 Oct 2016 14:22:04 GMT
Support for Quad

and somewhat generalized Quad


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

Branch: refs/heads/jena
Commit: a1186976eee3d1a46028b9e80eea275ad56e2fea
Parents: e534598
Author: Stian Soiland-Reyes <stain@apache.org>
Authored: Thu Sep 8 15:53:50 2016 +0100
Committer: Stian Soiland-Reyes <stain@apache.org>
Committed: Thu Sep 8 15:53:50 2016 +0100

----------------------------------------------------------------------
 .../commons/rdf/jsonldjava/JsonLdQuad.java      | 81 ++++++++++++++++++++
 .../rdf/jsonldjava/JsonLdRDFTermFactory.java    | 38 ++++++---
 2 files changed, 108 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/a1186976/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/JsonLdQuad.java
----------------------------------------------------------------------
diff --git a/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/JsonLdQuad.java b/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/JsonLdQuad.java
new file mode 100644
index 0000000..e8dbfbd
--- /dev/null
+++ b/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/JsonLdQuad.java
@@ -0,0 +1,81 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.rdf.jsonldjava;
+
+import java.util.Objects;
+import java.util.Optional;
+
+import org.apache.commons.rdf.api.BlankNodeOrIRI;
+import org.apache.commons.rdf.api.IRI;
+import org.apache.commons.rdf.api.Quad;
+import org.apache.commons.rdf.api.RDFTerm;
+
+final class JsonLdQuad implements org.apache.commons.rdf.api.Quad {
+	private final com.github.jsonldjava.core.RDFDataset.Quad quad;
+	private String blankNodePrefix;
+
+	private static JsonLdRDFTermFactory rdfTermFactory = new JsonLdRDFTermFactory();
+	
+	JsonLdQuad(com.github.jsonldjava.core.RDFDataset.Quad quad, String blankNodePrefix) {
+		this.quad = quad;
+		this.blankNodePrefix = blankNodePrefix;			
+	}
+
+	@Override
+	public BlankNodeOrIRI getSubject() {
+		return (BlankNodeOrIRI) rdfTermFactory.asTerm(quad.getSubject(), blankNodePrefix);
+	}
+
+	@Override
+	public IRI getPredicate() {
+		return (IRI) rdfTermFactory.asTerm(quad.getPredicate(), blankNodePrefix);
+	}
+
+	@Override
+	public RDFTerm getObject() {
+		return rdfTermFactory.asTerm(quad.getObject(), blankNodePrefix);
+	}
+
+	@Override
+	public Optional<BlankNodeOrIRI> getGraphName() {
+		BlankNodeOrIRI g = (BlankNodeOrIRI) rdfTermFactory.asTerm(quad.getGraph(), 
+				blankNodePrefix);
+		return Optional.ofNullable(g);
+	}
+	
+	@Override
+	public boolean equals(Object obj) {
+		if (obj == this) {
+			return true;
+		}
+		if (! (obj instanceof Quad)) {
+			return false;
+		}
+		Quad other = (Quad) obj;
+		return getGraphName().equals(other.getGraphName()) &&
+				getSubject().equals(other.getSubject()) && 
+				getPredicate().equals(other.getPredicate()) && 
+				getObject().equals(other.getObject());
+	}
+
+	@Override
+	public int hashCode() {
+		return Objects.hash(getGraphName(), getSubject(), getPredicate(), getObject());
+	}
+
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/a1186976/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 e07cbae..f57eb86 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
@@ -24,9 +24,12 @@ 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.Quad;
+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;
 
 import com.github.jsonldjava.core.RDFDataset;
@@ -74,6 +77,12 @@ final class JsonLdRDFTermFactory implements RDFTermFactory {
 	}
 	
 	@Override
+	public Quad createQuad(BlankNodeOrIRI graphName, BlankNodeOrIRI subject, IRI predicate,
RDFTerm object)
+			throws IllegalArgumentException, UnsupportedOperationException {
+		return new JsonLdQuad(asJsonLdQuad(graphName, subject, predicate, object), bnodePrefix);
+	}
+	
+	@Override
 	public Literal createLiteral(String literal) {		
 		return new JsonLdLiteral(new RDFDataset.Literal(literal, null, null));
 	}
@@ -113,22 +122,29 @@ final class JsonLdRDFTermFactory implements RDFTermFactory {
 	}
 	
 
-	public RDFDataset.Quad asJsonLdQuad(BlankNodeOrIRI subject, IRI predicate, RDFTerm object)
{
+	public RDFDataset.Quad asJsonLdQuad(RDFTerm subject, RDFTerm predicate, RDFTerm object)
{
+		return asJsonLdQuad(null, subject, predicate, object);
+	}
+
+	public RDFDataset.Quad asJsonLdQuad(RDFTerm graphName, RDFTerm subject, RDFTerm predicate,
RDFTerm object) {
 		String graph = null;
-		// TODO: Add Quad to commons-rdf
-//		if (triple instanceof Quad) {
-//			String graph = triple.getGraph().getIRIString();
-//		}
 		return new RDFDataset.Quad(asJsonLdNode(subject), asJsonLdNode(predicate), asJsonLdNode(object),
graph);
 	}
-
-
 	
-	public RDFDataset.Quad asJsonLdQuad(Triple triple) {
-		return asJsonLdQuad(triple.getSubject(), triple.getPredicate(), triple.getObject());
+	@SuppressWarnings("rawtypes")
+	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());
 	}
-
-	RDFTerm asTerm(final Node node, String blankNodePrefix) {		
+	
+	RDFTerm asTerm(final Node node, String blankNodePrefix) {	
+		if (node == null) {
+			return null; // e.g. default graph
+		}
 		if (node.isIRI()) {
 			return new JsonLdIRI(node);
 		} else if (node.isBlankNode()) {


Mime
View raw message