commonsrdf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From st...@apache.org
Subject [03/50] incubator-commonsrdf git commit: Added JenaAny/JenaVariable and fromJenaGeneralized(Triple)
Date Fri, 07 Oct 2016 14:22:16 GMT
Added JenaAny/JenaVariable and fromJenaGeneralized(Triple)


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

Branch: refs/heads/master
Commit: c38aae23447e1eaafff80c3036b643e8bda65a17
Parents: 35c3261
Author: Stian Soiland-Reyes <stain@apache.org>
Authored: Fri Jul 8 12:40:18 2016 +0100
Committer: Stian Soiland-Reyes <stain@apache.org>
Committed: Fri Jul 8 12:40:18 2016 +0100

----------------------------------------------------------------------
 .../org/apache/commons/rdf/jena/JenaAny.java    |  5 +
 .../apache/commons/rdf/jena/JenaVariable.java   |  6 ++
 .../commons/rdf/jena/RDFTermFactoryJena.java    | 98 +++++++++++++++++++-
 .../apache/commons/rdf/jena/impl/AnyImpl.java   | 31 +++++++
 .../commons/rdf/jena/impl/BlankNodeImpl.java    |  3 +
 .../apache/commons/rdf/jena/impl/IRIImpl.java   |  4 +
 .../commons/rdf/jena/impl/JenaFactory.java      | 40 +++++---
 .../commons/rdf/jena/impl/LiteralImpl.java      |  3 +
 .../commons/rdf/jena/impl/VariableImpl.java     | 33 +++++++
 9 files changed, 207 insertions(+), 16 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/c38aae23/jena/src/main/java/org/apache/commons/rdf/jena/JenaAny.java
----------------------------------------------------------------------
diff --git a/jena/src/main/java/org/apache/commons/rdf/jena/JenaAny.java b/jena/src/main/java/org/apache/commons/rdf/jena/JenaAny.java
new file mode 100644
index 0000000..90599a4
--- /dev/null
+++ b/jena/src/main/java/org/apache/commons/rdf/jena/JenaAny.java
@@ -0,0 +1,5 @@
+package org.apache.commons.rdf.jena;
+
+public interface JenaAny extends JenaRDFTerm {
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/c38aae23/jena/src/main/java/org/apache/commons/rdf/jena/JenaVariable.java
----------------------------------------------------------------------
diff --git a/jena/src/main/java/org/apache/commons/rdf/jena/JenaVariable.java b/jena/src/main/java/org/apache/commons/rdf/jena/JenaVariable.java
new file mode 100644
index 0000000..55791d4
--- /dev/null
+++ b/jena/src/main/java/org/apache/commons/rdf/jena/JenaVariable.java
@@ -0,0 +1,6 @@
+package org.apache.commons.rdf.jena;
+
+public interface JenaVariable extends JenaRDFTerm {
+	public String getVariableName();
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/c38aae23/jena/src/main/java/org/apache/commons/rdf/jena/RDFTermFactoryJena.java
----------------------------------------------------------------------
diff --git a/jena/src/main/java/org/apache/commons/rdf/jena/RDFTermFactoryJena.java b/jena/src/main/java/org/apache/commons/rdf/jena/RDFTermFactoryJena.java
index 2ffabfb..760d911 100644
--- a/jena/src/main/java/org/apache/commons/rdf/jena/RDFTermFactoryJena.java
+++ b/jena/src/main/java/org/apache/commons/rdf/jena/RDFTermFactoryJena.java
@@ -32,6 +32,7 @@ import org.apache.commons.rdf.api.RDFSyntax;
 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.jena.impl.JenaFactory;
 import org.apache.jena.datatypes.RDFDatatype;
 import org.apache.jena.datatypes.xsd.XSDDatatype;
@@ -104,6 +105,34 @@ public final class RDFTermFactoryJena implements RDFTermFactory {
 	public JenaTriple createTriple(BlankNodeOrIRI subject, IRI predicate, RDFTerm object) {
 		return JenaFactory.createTriple(subject, predicate, object);
 	}
+	
+	/**
+	 * Adapt a generalized Jena Triple to a CommonsRDF {@link TripleLike} statement.
+	 * <p>
+	 * The generalized triple supports any {@link RDFTerm} as its {@link TripleLike#getSubject()}
+	 * {@link TripleLike#getPredicate()} or {@link TripleLike#getObject()}. 
+	 * <p>
+	 * If the Jena triple contains any {@link Node#isBlank()}, then any corresponding
+	 * {@link BlankNode} will use a {@link UUID} salt from this
+	 * {@link RDFTermFactoryJena} instance in combination with
+	 * {@link Node#getBlankNodeId()} for the purpose of its
+	 * {@link BlankNode#uniqueReference()}.
+	 *
+	 * @see #fromJena(org.apache.jena.graph.Triple, UUID)
+	 * @see #fromJena(RDFTermFactory, org.apache.jena.graph.Triple)
+	 * 
+	 * @param subject The subject of the statement
+	 *            
+	 * @return Adapted {@link TripleLike}. Note that the generalized triple does
+	 *         <strong>not</strong> implement {@link Triple#equals(Object)} or
+	 *         {@link Triple#hashCode()}.
+	 * @throws ConversionException
+	 *             if any of the triple's nodes are not concrete
+	 */
+	public JenaTripleLike<RDFTerm, RDFTerm, RDFTerm> createGeneralizedTriple(
+			RDFTerm subject, RDFTerm predicate, RDFTerm object) {
+		return JenaFactory.createGeneralizedTriple(subject, predicate, object);
+	}
 
 	/**
 	 * Adapt an existing Jena Node to CommonsRDF {@link RDFTerm}.
@@ -219,6 +248,65 @@ public final class RDFTermFactoryJena implements RDFTermFactory {
 		return JenaFactory.fromJena(triple, salt);
 	}
 
+
+	/**
+	 * Adapt a generalized Jena Triple to a CommonsRDF {@link TripleLike}.
+	 * <p>
+	 * The generalized triple supports any {@link RDFTerm} as its {@link TripleLike#getSubject()}
+	 * {@link TripleLike#getPredicate()} or {@link TripleLike#getObject()}. 
+	 * <p>
+	 * If the Jena triple contains any {@link Node#isBlank()}, then any corresponding
+	 * {@link BlankNode} will use the provided {@link UUID} salt 
+	 * in combination with
+	 * {@link Node#getBlankNodeId()} for the purpose of its
+	 * {@link BlankNode#uniqueReference()}.
+	 *
+	 * @see #fromJena(org.apache.jena.graph.Triple, UUID)
+	 * @see #fromJena(RDFTermFactory, org.apache.jena.graph.Triple)
+	 * 
+	 * @param triple
+	 *            Jena triple
+	 * @param salt
+	 *            UUID salt for the purpose of
+	 *            {@link BlankNode#uniqueReference()}
+	 * @return Adapted {@link TripleLike}. Note that the generalized triple does
+	 *         <strong>not</strong> implement {@link Triple#equals(Object)} or
+	 *         {@link Triple#hashCode()}.
+	 * @throws ConversionException
+	 *             if any of the triple's nodes are not concrete
+	 */
+	public JenaTripleLike<RDFTerm, RDFTerm, RDFTerm> fromJenaGeneralized(org.apache.jena.graph.Triple
triple, UUID salt) throws ConversionException {
+		return JenaFactory.fromJenaGeneralized(triple, salt);
+	}
+	
+	/**
+	 * Adapt a generalized Jena Triple to a CommonsRDF {@link TripleLike}.
+	 * <p>
+	 * The generalized triple supports any {@link RDFTerm} as its {@link TripleLike#getSubject()}
+	 * {@link TripleLike#getPredicate()} or {@link TripleLike#getObject()}. 
+	 * <p>
+	 * If the Jena triple contains any {@link Node#isBlank()}, then any corresponding
+	 * {@link BlankNode} will use a {@link UUID} salt from this
+	 * {@link RDFTermFactoryJena} instance in combination with
+	 * {@link Node#getBlankNodeId()} for the purpose of its
+	 * {@link BlankNode#uniqueReference()}.
+	 *
+	 * @see #fromJena(org.apache.jena.graph.Triple, UUID)
+	 * @see #fromJena(RDFTermFactory, org.apache.jena.graph.Triple)
+	 * 
+	 * @param triple
+	 *            Jena triple
+	 * @return Adapted {@link TripleLike}. Note that the generalized triple does
+	 *         <strong>not</strong> implement {@link Triple#equals(Object)} or
+	 *         {@link Triple#hashCode()}.
+	 * @throws ConversionException
+	 *             if any of the triple's nodes are not concrete
+	 */
+	public JenaTripleLike<RDFTerm, RDFTerm, RDFTerm> fromJenaGeneralized(org.apache.jena.graph.Triple
triple) throws ConversionException {
+		return JenaFactory.fromJenaGeneralized(triple, salt);
+	}
+	
+	
 	/**
 	 * Adapt an existing Jena Triple to CommonsRDF {@link Triple}.
 	 * <p>
@@ -270,9 +358,9 @@ public final class RDFTermFactoryJena implements RDFTermFactory {
 		BlankNodeOrIRI subject;
 		IRI predicate;
 		try {
-			subject = (BlankNodeOrIRI) (fromJena(factory, triple.getSubject()));
-			predicate = (IRI) (fromJena(factory, triple.getPredicate()));
-		} (catch ClassCastException ex) {
+			subject = (BlankNodeOrIRI) fromJena(factory, triple.getSubject());
+			predicate = (IRI) fromJena(factory, triple.getPredicate());
+		} catch (ClassCastException ex) {
 			throw new ConversionException("Can't convert generalized triple: " + triple, ex);
 		}
 		RDFTerm object = fromJena(factory, triple.getObject());
@@ -400,11 +488,11 @@ public final class RDFTermFactoryJena implements RDFTermFactory {
 		if (term == null) {
 			return null;
 		}
-		if (term instanceof JenaNode)
+		if (term instanceof JenaRDFTerm)
 			// TODO: What if it's a BlankNodeImpl with
 			// a different salt? Do we need to rewrite the
 			// jena blanknode identifier?
-			return ((JenaNode) term).asJenaNode();
+			return ((JenaRDFTerm) term).asJenaNode();
 
 		if (term instanceof IRI)
 			return NodeFactory.createURI(((IRI) term).getIRIString());

http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/c38aae23/jena/src/main/java/org/apache/commons/rdf/jena/impl/AnyImpl.java
----------------------------------------------------------------------
diff --git a/jena/src/main/java/org/apache/commons/rdf/jena/impl/AnyImpl.java b/jena/src/main/java/org/apache/commons/rdf/jena/impl/AnyImpl.java
new file mode 100644
index 0000000..956851b
--- /dev/null
+++ b/jena/src/main/java/org/apache/commons/rdf/jena/impl/AnyImpl.java
@@ -0,0 +1,31 @@
+package org.apache.commons.rdf.jena.impl;
+
+import org.apache.commons.rdf.jena.JenaAny;
+import org.apache.commons.rdf.jena.JenaRDFTerm;
+import org.apache.jena.graph.Node;
+
+public class AnyImpl implements JenaRDFTerm, JenaAny {
+
+	static class Singleton {
+		static AnyImpl instance = new AnyImpl();
+	}
+	
+	/**
+	 * Private constructor
+	 * 
+	 * @see {@link Singleton#instance}
+	 */
+	private AnyImpl() {
+	}
+	
+	@Override
+	public String ntriplesString() {
+		return "[]";
+	}
+
+	@Override
+	public Node asJenaNode() {
+		return Node.ANY;
+	}
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/c38aae23/jena/src/main/java/org/apache/commons/rdf/jena/impl/BlankNodeImpl.java
----------------------------------------------------------------------
diff --git a/jena/src/main/java/org/apache/commons/rdf/jena/impl/BlankNodeImpl.java b/jena/src/main/java/org/apache/commons/rdf/jena/impl/BlankNodeImpl.java
index 928da61..2acb635 100644
--- a/jena/src/main/java/org/apache/commons/rdf/jena/impl/BlankNodeImpl.java
+++ b/jena/src/main/java/org/apache/commons/rdf/jena/impl/BlankNodeImpl.java
@@ -30,6 +30,9 @@ public class BlankNodeImpl extends AbstractRDFTerm implements JenaBlankNode
{
 
 	/* package */ BlankNodeImpl(Node node, UUID salt) {
 		super(node);
+		if (! node.isBlank()) {
+			throw new IllegalArgumentException("Node is not a blank node: " + node);
+		}				
 		this.salt = salt;
 	}
 

http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/c38aae23/jena/src/main/java/org/apache/commons/rdf/jena/impl/IRIImpl.java
----------------------------------------------------------------------
diff --git a/jena/src/main/java/org/apache/commons/rdf/jena/impl/IRIImpl.java b/jena/src/main/java/org/apache/commons/rdf/jena/impl/IRIImpl.java
index 9ad20d0..e85c80f 100644
--- a/jena/src/main/java/org/apache/commons/rdf/jena/impl/IRIImpl.java
+++ b/jena/src/main/java/org/apache/commons/rdf/jena/impl/IRIImpl.java
@@ -27,6 +27,10 @@ public class IRIImpl extends AbstractRDFTerm implements JenaIRI {
 
 	/* package */ IRIImpl(Node node) {
 		super(node);
+		if (! node.isURI()) {
+			throw new IllegalArgumentException("Node is not a blank node: " + node);
+		}				
+		
 	}
 
 	/* package */ IRIImpl(String iriStr) {

http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/c38aae23/jena/src/main/java/org/apache/commons/rdf/jena/impl/JenaFactory.java
----------------------------------------------------------------------
diff --git a/jena/src/main/java/org/apache/commons/rdf/jena/impl/JenaFactory.java b/jena/src/main/java/org/apache/commons/rdf/jena/impl/JenaFactory.java
index 797557c..4a6d44b 100644
--- a/jena/src/main/java/org/apache/commons/rdf/jena/impl/JenaFactory.java
+++ b/jena/src/main/java/org/apache/commons/rdf/jena/impl/JenaFactory.java
@@ -26,6 +26,7 @@ import org.apache.commons.rdf.api.IRI;
 import org.apache.commons.rdf.api.Quad;
 import org.apache.commons.rdf.api.RDFTerm;
 import org.apache.commons.rdf.jena.ConversionException;
+import org.apache.commons.rdf.jena.JenaAny;
 import org.apache.commons.rdf.jena.JenaBlankNode;
 import org.apache.commons.rdf.jena.JenaGraph;
 import org.apache.commons.rdf.jena.JenaIRI;
@@ -33,6 +34,7 @@ import org.apache.commons.rdf.jena.JenaLiteral;
 import org.apache.commons.rdf.jena.JenaRDFTerm;
 import org.apache.commons.rdf.jena.JenaTriple;
 import org.apache.commons.rdf.jena.JenaTripleLike;
+import org.apache.commons.rdf.jena.JenaVariable;
 import org.apache.jena.graph.Node;
 import org.apache.jena.graph.NodeFactory;
 import org.apache.jena.sparql.graph.GraphFactory;
@@ -58,7 +60,6 @@ public class JenaFactory {
 		return new GraphImpl(GraphFactory.createDefaultGraph(), salt);
 	}
 
-	// basic components to commonsrdf backed by Jena.
 	public static JenaIRI createIRI(String iriStr) {
 		return new IRIImpl(iriStr);
 	}
@@ -78,28 +79,45 @@ public class JenaFactory {
 	public static JenaTriple createTriple(BlankNodeOrIRI subject, IRI predicate, RDFTerm object)
{
 		return new TripleImpl(subject, predicate, object);
 	}
+	
+	public static JenaVariable createVariable(String name) {
+		return new VariableImpl(NodeFactory.createVariable(name));
+	}
+	
+	public static JenaAny createVariable() {
+		return AnyImpl.Singleton.instance;
+	}
 
 	public static JenaTripleLike<RDFTerm, RDFTerm, RDFTerm> createGeneralizedTriple(RDFTerm
subject, RDFTerm predicate, RDFTerm object) {
 		return new GeneralizedTripleImpl(subject, predicate, object);
 	}
 	
 	public static JenaRDFTerm fromJena(Node node, UUID salt) throws ConversionException {
-		if (node.isURI())
+		if (! node.isConcrete()) {
+			throw new ConversionException("Node is not a concrete RDF Term: " + node); 
+		}
+		return fromJenaGeneralized(node, salt);		
+	}
+
+	public static JenaRDFTerm fromJenaGeneralized(Node node, UUID salt) {
+		if (node.isURI()) {
 			return new IRIImpl(node);
+		}
 		if (node.isLiteral()) {
 			return new LiteralImpl(node);
-//			String lang = node.getLiteralLanguage();
-//			if (lang != null && lang.isEmpty())
-//				return createLiteralLang(node.getLiteralLexicalForm(), lang);
-//			if (node.getLiteralDatatype().equals(XSDDatatype.XSDstring))
-//				return createLiteral(node.getLiteralLexicalForm());
-//			return createLiteralDT(node.getLiteralLexicalForm(), node.getLiteralDatatype().getURI());
 		}
-		if (node.isBlank())
+		if (node.isBlank()) {
 			return new BlankNodeImpl(node, salt);
-		throw new ConversionException("Node is not a concrete RDF Term: " + node);
+		}
+		if (node.equals(Node.ANY)) {
+			return AnyImpl.Singleton.instance;
+		}
+		if (node.isVariable()) {
+			return new VariableImpl(node);
+		}
+		throw new IllegalArgumentException("Unrecognized node type: " + node);
 	}
-
+	
 	public static JenaGraph fromJena(org.apache.jena.graph.Graph graph) {
 		return new GraphImpl(graph, UUID.randomUUID());
 	}

http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/c38aae23/jena/src/main/java/org/apache/commons/rdf/jena/impl/LiteralImpl.java
----------------------------------------------------------------------
diff --git a/jena/src/main/java/org/apache/commons/rdf/jena/impl/LiteralImpl.java b/jena/src/main/java/org/apache/commons/rdf/jena/impl/LiteralImpl.java
index 0530d08..2750198 100644
--- a/jena/src/main/java/org/apache/commons/rdf/jena/impl/LiteralImpl.java
+++ b/jena/src/main/java/org/apache/commons/rdf/jena/impl/LiteralImpl.java
@@ -30,6 +30,9 @@ public class LiteralImpl extends AbstractRDFTerm implements JenaLiteral
{
 
 	/* package */ LiteralImpl(Node node) {
 		super(node);
+		if (! node.isLiteral()) {
+			throw new IllegalArgumentException("Node is not a literal: " + node);
+		}		
 	}
 
 	@Override

http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/c38aae23/jena/src/main/java/org/apache/commons/rdf/jena/impl/VariableImpl.java
----------------------------------------------------------------------
diff --git a/jena/src/main/java/org/apache/commons/rdf/jena/impl/VariableImpl.java b/jena/src/main/java/org/apache/commons/rdf/jena/impl/VariableImpl.java
new file mode 100644
index 0000000..13f62e9
--- /dev/null
+++ b/jena/src/main/java/org/apache/commons/rdf/jena/impl/VariableImpl.java
@@ -0,0 +1,33 @@
+package org.apache.commons.rdf.jena.impl;
+
+import org.apache.commons.rdf.jena.JenaRDFTerm;
+import org.apache.commons.rdf.jena.JenaVariable;
+import org.apache.jena.graph.Node;
+
+public class VariableImpl implements JenaRDFTerm, JenaVariable {
+
+	private Node node;
+
+	VariableImpl(Node node) {	
+		if (! node.isVariable()) {
+			throw new IllegalArgumentException("Node is not a variable: " + node);
+		}
+		this.node = node;
+	}
+
+	@Override
+	public String ntriplesString() {
+		return "?" + getVariableName();
+	}
+
+	@Override
+	public String getVariableName() {
+		return node.getName();
+	}
+
+	@Override
+	public Node asJenaNode() {
+		return node;
+	}
+
+}


Mime
View raw message