commonsrdf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From st...@apache.org
Subject [2/8] incubator-commonsrdf git commit: asStatement(TripleLike)
Date Tue, 14 Jun 2016 15:45:02 GMT
asStatement(TripleLike)


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

Branch: refs/heads/rdf4j
Commit: 3af404e39faa069e25a3deccda763f01b950fb6e
Parents: 5ebb9d8
Author: Stian Soiland-Reyes <stain@apache.org>
Authored: Fri Jun 3 15:17:24 2016 +0100
Committer: Stian Soiland-Reyes <stain@apache.org>
Committed: Fri Jun 3 15:17:24 2016 +0100

----------------------------------------------------------------------
 .../commons/rdf/rdf4j/RDF4JTermFactory.java     | 200 +++++++++----------
 1 file changed, 95 insertions(+), 105 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/3af404e3/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/RDF4JTermFactory.java
----------------------------------------------------------------------
diff --git a/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/RDF4JTermFactory.java b/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/RDF4JTermFactory.java
index 1b59c6f..9aa0fda 100644
--- a/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/RDF4JTermFactory.java
+++ b/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/RDF4JTermFactory.java
@@ -29,10 +29,11 @@ import org.apache.commons.rdf.api.Quad;
 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.rdf4j.impl.BlankNodeImpl;
-import org.apache.commons.rdf.rdf4j.impl.ModelGraphImpl;
 import org.apache.commons.rdf.rdf4j.impl.IRIImpl;
 import org.apache.commons.rdf.rdf4j.impl.LiteralImpl;
+import org.apache.commons.rdf.rdf4j.impl.ModelGraphImpl;
 import org.apache.commons.rdf.rdf4j.impl.QuadImpl;
 import org.apache.commons.rdf.rdf4j.impl.RepositoryGraphImpl;
 import org.apache.commons.rdf.rdf4j.impl.TripleImpl;
@@ -48,51 +49,47 @@ import org.eclipse.rdf4j.repository.Repository;
 /**
  * RDF4J implementation of RDFTermFactory
  * <p>
- * The {@link #RDF4JTermFactory()} constructor
- * uses a {@link SimpleValueFactory} to create corresponding
- * RDF4J {@link Value} instances. Alternatively, 
- * this factory can be constructed with a {@link ValueFactory} using
- * {@link #RDF4JTermFactory(ValueFactory)}.  
+ * The {@link #RDF4JTermFactory()} constructor uses a {@link SimpleValueFactory}
+ * to create corresponding RDF4J {@link Value} instances. Alternatively, this
+ * factory can be constructed with a {@link ValueFactory} using
+ * {@link #RDF4JTermFactory(ValueFactory)}.
  * <p>
- * {@link #asRDFTerm(Value)} can be used to convert any RDF4J {@link Value}
- * to an RDFTerm. Note that adapted {@link BNode}s are considered equal if they
- * are converted with the same {@link RDF4JTermFactory} instance and have the same
- * {@link BNode#getID()}. 
+ * {@link #asRDFTerm(Value)} can be used to convert any RDF4J {@link Value} to
+ * an RDFTerm. Note that adapted {@link BNode}s are considered equal if they are
+ * converted with the same {@link RDF4JTermFactory} instance and have the same
+ * {@link BNode#getID()}.
  * <p>
- * {@link #createGraph()} creates a new Graph backed by {@link LinkedHashModel}. To 
- * use other models, see {@link #asRDFTermGraph(Model)}.
+ * {@link #createGraph()} creates a new Graph backed by {@link LinkedHashModel}.
+ * To use other models, see {@link #asRDFTermGraph(Model)}.
  * <p>
- * {@link #asTriple(Statement)} can be used to convert a RDF4J {@link Statement} to
- * a Commons RDF {@link Triple}.
+ * {@link #asTriple(Statement)} can be used to convert a RDF4J {@link Statement}
+ * to a Commons RDF {@link Triple}.
  * <p>
- * {@link #asStatement(Triple)} can be used to convert any Commons RDF {@link Triple} 
- * to a RDF4J {@link Statement}.
- * <p
- * {@link #asValue(RDFTerm)} can be used to convert any Commons RDF {@link RDFTerm} to 
- * a corresponding RDF4J {@link Value}.
+ * {@link #asStatement(Triple)} can be used to convert any Commons RDF
+ * {@link Triple} to a RDF4J {@link Statement}. <p {@link #asValue(RDFTerm)} can
+ * be used to convert any Commons RDF {@link RDFTerm} to a corresponding RDF4J
+ * {@link Value}.
  * 
  */
 public class RDF4JTermFactory implements RDFTermFactory {
-		
+
 	/**
 	 * 
-	 * Adapt a RDF4J {@link Value} as a Commons RDF 
-	 * {@link RDFTerm}.
+	 * Adapt a RDF4J {@link Value} as a Commons RDF {@link RDFTerm}.
 	 * <p>
 	 * <p>
 	 * The value will be of the same kind as the term, e.g. a
 	 * {@link org.eclipse.rdf4j.model.BNode} is converted to a
-	 * {@link org.apache.commons.rdf.api.BlankNode}, 
-	 * a {@link org.eclipse.rdf4j.model.IRI}
-	 * is converted to a {@link org.apache.commons.rdf.api.IRI}
-	 * and a {@link org.eclipse.rdf4j.model.Literal}.
-	 * is converted to a 
-	 * {@link org.apache.commons.rdf.api.Literal} 
+	 * {@link org.apache.commons.rdf.api.BlankNode}, a
+	 * {@link org.eclipse.rdf4j.model.IRI} is converted to a
+	 * {@link org.apache.commons.rdf.api.IRI} and a
+	 * {@link org.eclipse.rdf4j.model.Literal}. is converted to a
+	 * {@link org.apache.commons.rdf.api.Literal}
 	 * 
 	 * @param value
 	 * @return
 	 */
-	public static RDF4JTerm<?> asRDFTerm(final org.eclipse.rdf4j.model.Value value, UUID
salt) {		
+	public static RDF4JTerm<?> asRDFTerm(final org.eclipse.rdf4j.model.Value value, UUID
salt) {
 		if (value instanceof BNode) {
 			return new BlankNodeImpl((BNode) value, salt);
 		}
@@ -102,12 +99,11 @@ public class RDF4JTermFactory implements RDFTermFactory {
 		if (value instanceof org.eclipse.rdf4j.model.IRI) {
 			return new IRIImpl((org.eclipse.rdf4j.model.IRI) value);
 		}
-		throw new IllegalArgumentException("Value is not a BNode, Literal or IRI: " + value.getClass());
	
+		throw new IllegalArgumentException("Value is not a BNode, Literal or IRI: " + value.getClass());
 	}
 
 	/**
-	 * Adapt a RDF4J {@link Statement} as a Commons RDF 
-	 * {@link Triple}.
+	 * Adapt a RDF4J {@link Statement} as a Commons RDF {@link Triple}.
 	 * 
 	 * @param statement
 	 * @return
@@ -115,22 +111,21 @@ public class RDF4JTermFactory implements RDFTermFactory {
 	public static RDF4JTriple asTriple(final Statement statement, UUID salt) {
 		return new TripleImpl(statement, salt);
 	}
-	
+
 	private UUID salt = UUID.randomUUID();
-	
+
 	private ValueFactory valueFactory;
-	
+
 	public RDF4JTermFactory() {
 		this.valueFactory = SimpleValueFactory.getInstance();
 	}
 
-	public RDF4JTermFactory(ValueFactory valueFactory) { 
+	public RDF4JTermFactory(ValueFactory valueFactory) {
 		this.valueFactory = valueFactory;
-	}	
-	
+	}
+
 	/**
-	 * Adapt a RDF4J {@link Statement} as a Commons RDF 
-	 * {@link Quad}.
+	 * Adapt a RDF4J {@link Statement} as a Commons RDF {@link Quad}.
 	 * 
 	 * @param statement
 	 * @return A {@link RDF4JQuad} that is equivalent to the statement
@@ -138,37 +133,34 @@ public class RDF4JTermFactory implements RDFTermFactory {
 	public RDF4JQuad asQuad(final Statement statement) {
 		return new QuadImpl(statement, salt);
 	}
-	
+
 	/**
 	 * 
-	 * Adapt a RDF4J {@link Value} as a Commons RDF 
-	 * {@link RDFTerm}.
+	 * Adapt a RDF4J {@link Value} as a Commons RDF {@link RDFTerm}.
 	 * <p>
 	 * <p>
 	 * The value will be of the same kind as the term, e.g. a
 	 * {@link org.eclipse.rdf4j.model.BNode} is converted to a
-	 * {@link org.apache.commons.rdf.api.BlankNode}, 
-	 * a {@link org.eclipse.rdf4j.model.IRI}
-	 * is converted to a {@link org.apache.commons.rdf.api.IRI}
-	 * and a {@link org.eclipse.rdf4j.model.Literal}.
-	 * is converted to a 
-	 * {@link org.apache.commons.rdf.api.Literal} 
+	 * {@link org.apache.commons.rdf.api.BlankNode}, a
+	 * {@link org.eclipse.rdf4j.model.IRI} is converted to a
+	 * {@link org.apache.commons.rdf.api.IRI} and a
+	 * {@link org.eclipse.rdf4j.model.Literal}. is converted to a
+	 * {@link org.apache.commons.rdf.api.Literal}
 	 * 
 	 * @param value
 	 * @return
 	 */
-	public RDF4JTerm<?> asRDFTerm(final org.eclipse.rdf4j.model.Value value) {		
-		return asRDFTerm(value, salt);	
-	}	
+	public RDF4JTerm<?> asRDFTerm(final org.eclipse.rdf4j.model.Value value) {
+		return asRDFTerm(value, salt);
+	}
 
 	/**
-	 * Adapt an RDF4J {@link Model} as a 
-	 * Commons RDF {@link Graph}.
+	 * Adapt an RDF4J {@link Model} as a Commons RDF {@link Graph}.
 	 * <p>
-	 * Changes to the graph are reflected in the model, and
-	 * vice versa. 
+	 * Changes to the graph are reflected in the model, and vice versa.
 	 * 
-	 * @param model RDF4J {@link Model} to adapt.
+	 * @param model
+	 *            RDF4J {@link Model} to adapt.
 	 * @return Adapted {@link Graph}.
 	 */
 	public RDF4JGraph asRDFTermGraph(Model model) {
@@ -176,43 +168,40 @@ public class RDF4JTermFactory implements RDFTermFactory {
 	}
 
 	/**
-	 * Adapt an RDF4J {@link Repository} as a 
-	 * Commons RDF {@link Graph}.
+	 * Adapt an RDF4J {@link Repository} as a Commons RDF {@link Graph}.
 	 * <p>
-	 * Changes to the graph are reflected in the repository, and
-	 * vice versa. 
+	 * Changes to the graph are reflected in the repository, and vice versa.
 	 * 
-	 * @param model RDF4J {@link Repository} to connect to.
+	 * @param model
+	 *            RDF4J {@link Repository} to connect to.
 	 * @return Adapted {@link Graph}.
 	 */
 	public RDF4JGraph asRDFTermGraph(Repository repository) {
 		return new RepositoryGraphImpl(repository);
-	}	
-	
-	public Statement asStatement(Quad quad) {
-		return valueFactory.createStatement(
-				(org.eclipse.rdf4j.model.Resource) asValue(quad.getSubject()), 
-				(org.eclipse.rdf4j.model.IRI) asValue(quad.getPredicate()), 
-				asValue(quad.getObject()), 				
-				(org.eclipse.rdf4j.model.Resource) asValue(quad.getGraphName().orElse(null))
-				);
 	}
 
-	public Statement asStatement(Triple triple) {
-		if (triple instanceof RDF4JTripleLike) { 
-			// This covers both RDF4JQuad and RDF4JTriple
-			RDF4JTripleLike rdf4jTriple = (RDF4JTriple) triple;
+	public Statement asStatement(TripleLike<BlankNodeOrIRI, org.apache.commons.rdf.api.IRI,
RDFTerm> tripleLike) {
+		if (tripleLike instanceof RDF4JTripleLike) {
+			// Return original statement - this covers both RDF4JQuad and RDF4JTriple
+			RDF4JTripleLike rdf4jTriple = (RDF4JTripleLike) tripleLike;
 			return rdf4jTriple.asStatement();
 		}
-		return valueFactory.createStatement(
-				(org.eclipse.rdf4j.model.Resource) asValue(triple.getSubject()), 
-				(org.eclipse.rdf4j.model.IRI) asValue(triple.getPredicate()), 
-				asValue(triple.getObject()));
+
+		org.eclipse.rdf4j.model.Resource subject = (org.eclipse.rdf4j.model.Resource) asValue(tripleLike.getSubject());
+		org.eclipse.rdf4j.model.IRI predicate = (org.eclipse.rdf4j.model.IRI) asValue(tripleLike.getPredicate());
+		Value object = asValue(tripleLike.getObject());
+		
+		org.eclipse.rdf4j.model.Resource context = null;
+		if (tripleLike instanceof Quad) {
+			Quad quad = (Quad) tripleLike;
+			context = (org.eclipse.rdf4j.model.Resource) asValue(quad.getGraphName().orElse(null));
+		}
+		
+		return valueFactory.createStatement(subject, predicate, object, context);
 	}
-	
+
 	/**
-	 * Adapt a RDF4J {@link Statement} as a Commons RDF 
-	 * {@link Triple}.
+	 * Adapt a RDF4J {@link Statement} as a Commons RDF {@link Triple}.
 	 * 
 	 * @param statement
 	 * @return A {@link RDF4JTriple} that is equivalent to the statement
@@ -220,14 +209,15 @@ public class RDF4JTermFactory implements RDFTermFactory {
 	public RDF4JTriple asTriple(final Statement statement) {
 		return new TripleImpl(statement, salt);
 	}
-	
+
 	/**
 	 * Adapt a Commons RDF {@link RDFTerm} as a RDF4J {@link Value}.
 	 * <p>
 	 * The value will be of the same kind as the term, e.g. a
 	 * {@link org.apache.commons.rdf.api.BlankNode} is converted to a
-	 * {@link org.eclipse.rdf4j.model.BNode}, a {@link org.apache.commons.rdf.api.IRI}
-	 * is converted to a {@link org.eclipse.rdf4j.model.IRI} and a
+	 * {@link org.eclipse.rdf4j.model.BNode}, a
+	 * {@link org.apache.commons.rdf.api.IRI} is converted to a
+	 * {@link org.eclipse.rdf4j.model.IRI} and a
 	 * {@link org.apache.commons.rdf.api.Literal} is converted to a
 	 * {@link org.eclipse.rdf4j.model.Literal}.
 	 * <p>
@@ -236,20 +226,21 @@ public class RDF4JTermFactory implements RDFTermFactory {
 	 * <p>
 	 * If the provided term is an instance of {@link RDF4JTerm}, then the
 	 * {@link RDF4JTerm#asValue()} is returned without any conversion. Note that
-	 * this could mean that a {@link Value} from a different kind of 
+	 * this could mean that a {@link Value} from a different kind of
 	 * {@link ValueFactory} could be returned.
 	 * 
-	 * @param term RDFTerm to adapt to RDF4J Value 
+	 * @param term
+	 *            RDFTerm to adapt to RDF4J Value
 	 * @return Adapted RDF4J {@link Value}
 	 */
-	public Value asValue(RDFTerm term) {		
-		if (term == null) { 
+	public Value asValue(RDFTerm term) {
+		if (term == null) {
 			return null;
 		}
 		if (term instanceof RDF4JTerm) {
 			// One of our own - avoid converting again.
 			// (This is crucial to avoid double-escaping in BlankNode)
-			return ((RDF4JTerm<?>)term).asValue();
+			return ((RDF4JTerm<?>) term).asValue();
 		}
 		if (term instanceof org.apache.commons.rdf.api.IRI) {
 			org.apache.commons.rdf.api.IRI iri = (org.apache.commons.rdf.api.IRI) term;
@@ -262,13 +253,14 @@ public class RDF4JTermFactory implements RDFTermFactory {
 				String lang = literal.getLanguageTag().get();
 				return valueFactory.createLiteral(label, lang);
 			}
-			org.eclipse.rdf4j.model.IRI dataType = (org.eclipse.rdf4j.model.IRI ) asValue(literal.getDatatype());
+			org.eclipse.rdf4j.model.IRI dataType = (org.eclipse.rdf4j.model.IRI) asValue(literal.getDatatype());
 			return valueFactory.createLiteral(label, dataType);
 		}
 		if (term instanceof BlankNode) {
-			// This is where it gets tricky to support round trips!			
+			// This is where it gets tricky to support round trips!
 			BlankNode blankNode = (BlankNode) term;
-			// FIXME: The uniqueReference might not be a valid BlankNode identifier..
+			// FIXME: The uniqueReference might not be a valid BlankNode
+			// identifier..
 			// does it have to be in RDF4J?
 			return valueFactory.createBNode(blankNode.uniqueReference());
 		}
@@ -278,29 +270,30 @@ public class RDF4JTermFactory implements RDFTermFactory {
 	@Override
 	public RDF4JBlankNode createBlankNode() throws UnsupportedOperationException {
 		BNode bnode = valueFactory.createBNode();
-		return (RDF4JBlankNode)asRDFTerm(bnode);
+		return (RDF4JBlankNode) asRDFTerm(bnode);
 	}
 
 	@Override
 	public RDF4JBlankNode createBlankNode(String name) throws UnsupportedOperationException
{
 		BNode bnode = valueFactory.createBNode(name);
-		return (RDF4JBlankNode)asRDFTerm(bnode);
+		return (RDF4JBlankNode) asRDFTerm(bnode);
 	}
 
 	@Override
 	public RDF4JGraph createGraph() throws UnsupportedOperationException {
 		return asRDFTermGraph(new LinkedHashModel());
 	}
-	
+
 	@Override
 	public RDF4JIRI createIRI(String iri) throws IllegalArgumentException, UnsupportedOperationException
{
 		return (RDF4JIRI) asRDFTerm(valueFactory.createIRI(iri));
 	}
 
 	@Override
-	public RDF4JLiteral createLiteral(String lexicalForm) throws IllegalArgumentException, UnsupportedOperationException
{
+	public RDF4JLiteral createLiteral(String lexicalForm)
+			throws IllegalArgumentException, UnsupportedOperationException {
 		org.eclipse.rdf4j.model.Literal lit = valueFactory.createLiteral(lexicalForm);
-		return (RDF4JLiteral)asRDFTerm(lit);
+		return (RDF4JLiteral) asRDFTerm(lit);
 	}
 
 	@Override
@@ -308,25 +301,22 @@ public class RDF4JTermFactory implements RDFTermFactory {
 			throws IllegalArgumentException, UnsupportedOperationException {
 		org.eclipse.rdf4j.model.IRI iri = valueFactory.createIRI(dataType.getIRIString());
 		org.eclipse.rdf4j.model.Literal lit = valueFactory.createLiteral(lexicalForm, iri);
-		return (org.apache.commons.rdf.api.Literal)asRDFTerm(lit);
+		return (org.apache.commons.rdf.api.Literal) asRDFTerm(lit);
 	}
 
 	@Override
 	public org.apache.commons.rdf.api.Literal createLiteral(String lexicalForm, String languageTag)
 			throws IllegalArgumentException, UnsupportedOperationException {
 		org.eclipse.rdf4j.model.Literal lit = valueFactory.createLiteral(lexicalForm, languageTag);
-		return (org.apache.commons.rdf.api.Literal)asRDFTerm(lit);
+		return (org.apache.commons.rdf.api.Literal) asRDFTerm(lit);
 	}
 
 	@Override
 	public RDF4JTriple createTriple(BlankNodeOrIRI subject, org.apache.commons.rdf.api.IRI predicate,
RDFTerm object)
 			throws IllegalArgumentException, UnsupportedOperationException {
-		final Statement statement = valueFactory.createStatement(
-				(org.eclipse.rdf4j.model.Resource) asValue(subject), 
-				(org.eclipse.rdf4j.model.IRI) asValue(predicate), 
-				asValue(object));
+		final Statement statement = valueFactory.createStatement((org.eclipse.rdf4j.model.Resource)
asValue(subject),
+				(org.eclipse.rdf4j.model.IRI) asValue(predicate), asValue(object));
 		return asTriple(statement);
 	}
 
-	
 }


Mime
View raw message