commonsrdf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From st...@apache.org
Subject [7/7] incubator-commonsrdf git commit: Added RepositoryGraphImpl
Date Fri, 03 Jun 2016 13:28:41 GMT
Added RepositoryGraphImpl


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

Branch: refs/heads/rdf4j
Commit: 99bd16f6576ffed1efbbe3a734f77e7d82ac1157
Parents: 712eec6
Author: Stian Soiland-Reyes <stain@apache.org>
Authored: Fri Jun 3 11:54:39 2016 +0100
Committer: Stian Soiland-Reyes <stain@apache.org>
Committed: Fri Jun 3 11:54:39 2016 +0100

----------------------------------------------------------------------
 .../apache/commons/rdf/rdf4j/RDF4JGraph.java    |  24 ++-
 .../commons/rdf/rdf4j/RDF4JTermFactory.java     | 132 +++++++++-------
 .../commons/rdf/rdf4j/impl/BlankNodeImpl.java   |  32 ++--
 .../commons/rdf/rdf4j/impl/GraphImpl.java       |  44 +++---
 .../apache/commons/rdf/rdf4j/impl/QuadImpl.java |  34 ++---
 .../rdf/rdf4j/impl/RepositoryGraphImpl.java     | 153 +++++++++++++++++++
 .../commons/rdf/rdf4j/impl/TripleImpl.java      |   4 +-
 7 files changed, 310 insertions(+), 113 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/99bd16f6/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/RDF4JGraph.java
----------------------------------------------------------------------
diff --git a/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/RDF4JGraph.java b/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/RDF4JGraph.java
index 2fcd2ea..abc4a98 100644
--- a/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/RDF4JGraph.java
+++ b/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/RDF4JGraph.java
@@ -17,8 +17,11 @@
  */
 package org.apache.commons.rdf.rdf4j;
 
+import java.util.Optional;
+
 import org.apache.commons.rdf.api.Graph;
 import org.eclipse.rdf4j.model.Model;
+import org.eclipse.rdf4j.repository.Repository;
 
 
 /**
@@ -28,12 +31,29 @@ import org.eclipse.rdf4j.model.Model;
 public interface RDF4JGraph extends Graph {
 	
 	/**
-	 * Return the corresponding RDF4J {@link Model}.
+	 * Return the corresponding RDF4J {@link Model}, if present.
+	 * <p>
+	 * The return value is {@link Optional#isPresent()} if this graph is
+	 * backed by a Model.
 	 * <p>
 	 * Changes to the Model are reflected in this Graph, and
 	 * vice versa.
 	 * 
 	 * @return The corresponding RDF4J Model.
 	 */
-	public Model asModel();
+	public Optional<Model> asModel();
+	
+	/**
+	 * Return the corresponding RDF4J {@link Repository}, if present.
+	 * <p>
+	 * The return value is {@link Optional#isPresent()} if this graph is
+	 * backed by a Repository.
+	 * <p>
+	 * Changes to the Repository are reflected in this Graph, and
+	 * vice versa.
+	 * 
+	 * @return The corresponding RDF4J Repository.
+	 */
+	public Optional<Repository> asRepository();
+
 }

http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/99bd16f6/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 3df7de9..125a46c 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
@@ -34,6 +34,7 @@ import org.apache.commons.rdf.rdf4j.impl.GraphImpl;
 import org.apache.commons.rdf.rdf4j.impl.IRIImpl;
 import org.apache.commons.rdf.rdf4j.impl.LiteralImpl;
 import org.apache.commons.rdf.rdf4j.impl.QuadImpl;
+import org.apache.commons.rdf.rdf4j.impl.RepositoryGraphImpl;
 import org.apache.commons.rdf.rdf4j.impl.TripleImpl;
 import org.eclipse.rdf4j.model.BNode;
 import org.eclipse.rdf4j.model.Model;
@@ -42,6 +43,7 @@ import org.eclipse.rdf4j.model.Value;
 import org.eclipse.rdf4j.model.ValueFactory;
 import org.eclipse.rdf4j.model.impl.LinkedHashModel;
 import org.eclipse.rdf4j.model.impl.SimpleValueFactory;
+import org.eclipse.rdf4j.repository.Repository;
 
 /**
  * RDF4J implementation of RDFTermFactory
@@ -72,18 +74,6 @@ import org.eclipse.rdf4j.model.impl.SimpleValueFactory;
  */
 public class RDF4JTermFactory implements RDFTermFactory {
 		
-	private UUID salt = UUID.randomUUID();
-
-	private ValueFactory valueFactory;
-	
-	public RDF4JTermFactory() {
-		this.valueFactory = SimpleValueFactory.getInstance();
-	}
-	
-	public RDF4JTermFactory(ValueFactory valueFactory) { 
-		this.valueFactory = valueFactory;
-	}
-	
 	/**
 	 * 
 	 * Adapt a RDF4J {@link Value} as a Commons RDF 
@@ -102,11 +92,54 @@ public class RDF4JTermFactory implements RDFTermFactory {
 	 * @param value
 	 * @return
 	 */
-	public RDF4JTerm<?> asRDFTerm(final org.eclipse.rdf4j.model.Value value) {		
-		return asRDFTerm(value, salt);	
+	public static RDF4JTerm<?> asRDFTerm(final org.eclipse.rdf4j.model.Value value, UUID
salt) {		
+		if (value instanceof BNode) {
+			return new BlankNodeImpl((BNode) value, salt);
+		}
+		if (value instanceof org.eclipse.rdf4j.model.Literal) {
+			return new LiteralImpl((org.eclipse.rdf4j.model.Literal) value);
+		}
+		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());
	
 	}
 
 	/**
+	 * Adapt a RDF4J {@link Statement} as a Commons RDF 
+	 * {@link Triple}.
+	 * 
+	 * @param statement
+	 * @return
+	 */
+	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) { 
+		this.valueFactory = valueFactory;
+	}	
+	
+	/**
+	 * Adapt a RDF4J {@link Statement} as a Commons RDF 
+	 * {@link Quad}.
+	 * 
+	 * @param statement
+	 * @return A {@link RDF4JQuad} that is equivalent to the statement
+	 */
+	public RDF4JQuad asQuad(final Statement statement) {
+		return new QuadImpl(statement, salt);
+	}
+	
+	/**
 	 * 
 	 * Adapt a RDF4J {@link Value} as a Commons RDF 
 	 * {@link RDFTerm}.
@@ -124,19 +157,10 @@ public class RDF4JTermFactory implements RDFTermFactory {
 	 * @param value
 	 * @return
 	 */
-	public static RDF4JTerm<?> asRDFTerm(final org.eclipse.rdf4j.model.Value value, UUID
salt) {		
-		if (value instanceof BNode) {
-			return new BlankNodeImpl((BNode) value, salt);
-		}
-		if (value instanceof org.eclipse.rdf4j.model.Literal) {
-			return new LiteralImpl((org.eclipse.rdf4j.model.Literal) value);
-		}
-		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());
	
+	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}.
@@ -150,7 +174,30 @@ public class RDF4JTermFactory implements RDFTermFactory {
 	public RDF4JGraph asRDFTermGraph(Model model) {
 		return new GraphImpl(model);
 	}
+
+	/**
+	 * Adapt an RDF4J {@link Repository} as a 
+	 * Commons RDF {@link Graph}.
+	 * <p>
+	 * Changes to the graph are reflected in the repository, and
+	 * vice versa. 
+	 * 
+	 * @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
@@ -161,16 +208,7 @@ public class RDF4JTermFactory implements RDFTermFactory {
 				(org.eclipse.rdf4j.model.Resource) asValue(triple.getSubject()), 
 				(org.eclipse.rdf4j.model.IRI) asValue(triple.getPredicate()), 
 				asValue(triple.getObject()));
-	}	
-
-	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))
-				);
-	}	
+	}
 	
 	/**
 	 * Adapt a RDF4J {@link Statement} as a Commons RDF 
@@ -182,28 +220,6 @@ public class RDF4JTermFactory implements RDFTermFactory {
 	public RDF4JTriple asTriple(final Statement statement) {
 		return new TripleImpl(statement, salt);
 	}
-
-	/**
-	 * Adapt a RDF4J {@link Statement} as a Commons RDF 
-	 * {@link Quad}.
-	 * 
-	 * @param statement
-	 * @return A {@link RDF4JQuad} that is equivalent to the statement
-	 */
-	public RDF4JQuad asQuad(final Statement statement) {
-		return new QuadImpl(statement, salt);
-	}
-	
-	/**
-	 * Adapt a RDF4J {@link Statement} as a Commons RDF 
-	 * {@link Triple}.
-	 * 
-	 * @param statement
-	 * @return
-	 */
-	public static RDF4JTriple asTriple(final Statement statement, UUID salt) {
-		return new TripleImpl(statement, salt);
-	}
 	
 	/**
 	 * Adapt a Commons RDF {@link RDFTerm} as a RDF4J {@link Value}.

http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/99bd16f6/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/impl/BlankNodeImpl.java
----------------------------------------------------------------------
diff --git a/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/impl/BlankNodeImpl.java b/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/impl/BlankNodeImpl.java
index 6921f45..625088a 100644
--- a/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/impl/BlankNodeImpl.java
+++ b/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/impl/BlankNodeImpl.java
@@ -29,8 +29,8 @@ public final class BlankNodeImpl extends AbstractRDFTerm<BNode>
 	implements RDF4JBlankNode {
 	
 	private transient int hashCode = 0;
-	private long saltUUIDmost;
 	private long saltUUIDleast;
+	private long saltUUIDmost;
 	
 	public BlankNodeImpl(BNode bNode, UUID salt) {
 		super(bNode);			
@@ -60,21 +60,6 @@ public final class BlankNodeImpl extends AbstractRDFTerm<BNode>
 		return hashCode = uniqueReference().hashCode();
 	}
 
-	@Override
-	public String ntriplesString() {
-		if (isValidBlankNodeLabel(value.getID())) { 
-			return "_:" + value.getID();
-		} else {
-			return "_:" + UUID.nameUUIDFromBytes(value.getID().getBytes(StandardCharsets.UTF_8));
-		}
-	}
-
-	@Override
-	public String uniqueReference() {
-		UUID uuid = new UUID(saltUUIDmost, saltUUIDleast);
-		return "urn:uuid:" + uuid + "#" + value.getID();
-	}
-
 	private boolean isValidBlankNodeLabel(String id) {
 		// FIXME: Replace with a regular expression?			
 		if (id.isEmpty()) { 
@@ -90,4 +75,19 @@ public final class BlankNodeImpl extends AbstractRDFTerm<BNode>
 		}
 		return true;
 	}
+
+	@Override
+	public String ntriplesString() {
+		if (isValidBlankNodeLabel(value.getID())) { 
+			return "_:" + value.getID();
+		} else {
+			return "_:" + UUID.nameUUIDFromBytes(value.getID().getBytes(StandardCharsets.UTF_8));
+		}
+	}
+
+	@Override
+	public String uniqueReference() {
+		UUID uuid = new UUID(saltUUIDmost, saltUUIDleast);
+		return "urn:uuid:" + uuid + "#" + value.getID();
+	}
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/99bd16f6/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/impl/GraphImpl.java
----------------------------------------------------------------------
diff --git a/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/impl/GraphImpl.java b/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/impl/GraphImpl.java
index 966ac32..bf3aad2 100644
--- a/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/impl/GraphImpl.java
+++ b/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/impl/GraphImpl.java
@@ -17,6 +17,7 @@
  */
 package org.apache.commons.rdf.rdf4j.impl;
 
+import java.util.Optional;
 import java.util.stream.Stream;
 
 import org.apache.commons.rdf.api.BlankNodeOrIRI;
@@ -27,6 +28,7 @@ import org.apache.commons.rdf.rdf4j.RDF4JTermFactory;
 import org.apache.commons.rdf.rdf4j.RDF4JTriple;
 import org.eclipse.rdf4j.model.Model;
 import org.eclipse.rdf4j.model.Resource;
+import org.eclipse.rdf4j.repository.Repository;
 
 public final class GraphImpl implements RDF4JGraph {
 	
@@ -56,11 +58,16 @@ public final class GraphImpl implements RDF4JGraph {
 		model.add(rdf4jTermFactory.asStatement(triple));
 	}
 
-	public Model asModel() { 
-		return model;
+	public Optional<Model> asModel() { 
+		return Optional.of(model);
 	}
 
 	@Override
+	public Optional<Repository> asRepository() {
+		return Optional.empty();
+	}
+	
+	@Override
 	public void clear() {
 		model.clear();
 	}
@@ -79,26 +86,11 @@ public final class GraphImpl implements RDF4JGraph {
 	}
 
 	@Override
-	public Stream<RDF4JTriple> stream() {
-		return model.parallelStream().map(rdf4jTermFactory::asTriple);
-	}
-
-	@Override
-	public Stream<RDF4JTriple> stream(BlankNodeOrIRI subject, org.apache.commons.rdf.api.IRI
predicate, RDFTerm object) {
-		return model.filter(
-				(Resource)rdf4jTermFactory.asValue(subject), 
-				(org.eclipse.rdf4j.model.IRI)rdf4jTermFactory.asValue(predicate), 
-				rdf4jTermFactory.asValue(object)).parallelStream()
-			.map(rdf4jTermFactory::asTriple);
-	}
-
-	@Override
 	public void remove(BlankNodeOrIRI subject, org.apache.commons.rdf.api.IRI predicate, RDFTerm
object) {
 		model.remove(
 				(Resource)rdf4jTermFactory.asValue(subject), 
 				(org.eclipse.rdf4j.model.IRI)rdf4jTermFactory.asValue(predicate), 
-				rdf4jTermFactory.asValue(object));
-		
+				rdf4jTermFactory.asValue(object));		
 	}
 
 	@Override
@@ -112,8 +104,24 @@ public final class GraphImpl implements RDF4JGraph {
 		if (size < Integer.MAX_VALUE) {
 			return size;
 		} else {
+			// TODO: Check if this can really happen with RDF4J models
 			// Collection.size() can't help us, we'll have to count
 			return model.parallelStream().count();
 		}				
 	}
+
+	@Override
+	public Stream<RDF4JTriple> stream() {
+		return model.parallelStream().map(rdf4jTermFactory::asTriple);
+	}
+
+	@Override
+	public Stream<RDF4JTriple> stream(BlankNodeOrIRI subject, org.apache.commons.rdf.api.IRI
predicate, RDFTerm object) {
+		return model.filter(
+				(Resource)rdf4jTermFactory.asValue(subject), 
+				(org.eclipse.rdf4j.model.IRI)rdf4jTermFactory.asValue(predicate), 
+				rdf4jTermFactory.asValue(object)).parallelStream()
+			.map(rdf4jTermFactory::asTriple);
+	}
+	
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/99bd16f6/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/impl/QuadImpl.java
----------------------------------------------------------------------
diff --git a/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/impl/QuadImpl.java b/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/impl/QuadImpl.java
index 0e11eb2..1d887a0 100644
--- a/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/impl/QuadImpl.java
+++ b/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/impl/QuadImpl.java
@@ -13,9 +13,9 @@ import org.apache.commons.rdf.rdf4j.RDF4JTermFactory;
 import org.eclipse.rdf4j.model.Statement;
 
 public final class QuadImpl implements Quad, RDF4JQuad {
-		private final Statement statement;	
+		private transient int hashCode = 0;	
 		private UUID salt;
-		private transient int hashCode = 0;
+		private final Statement statement;
 		
 		public QuadImpl(Statement statement, UUID salt) {
 			this.statement = statement;
@@ -27,6 +27,11 @@ public final class QuadImpl implements Quad, RDF4JQuad {
 		}
 		
 		@Override
+		public Triple asTriple() {
+			return new TripleImpl(statement, salt);
+		}
+	
+		@Override
 		public boolean equals(Object obj) {
 			if (obj instanceof Triple) {
 				Triple triple = (Triple) obj;
@@ -38,6 +43,15 @@ public final class QuadImpl implements Quad, RDF4JQuad {
 		}
 	
 		@Override
+		public Optional<BlankNodeOrIRI> getGraphName() {
+			if (statement.getContext() == null) { 
+				return Optional.empty();
+			}
+			BlankNodeOrIRI g = (BlankNodeOrIRI) RDF4JTermFactory.asRDFTerm(statement.getContext(),
salt);
+			return Optional.of(g);
+		}
+		
+		@Override
 		public RDFTerm getObject() {
 			return RDF4JTermFactory.asRDFTerm(statement.getObject(), salt);
 		}
@@ -51,7 +65,7 @@ public final class QuadImpl implements Quad, RDF4JQuad {
 		public BlankNodeOrIRI getSubject() {
 			return (BlankNodeOrIRI) RDF4JTermFactory.asRDFTerm(statement.getSubject(), salt);
 		}
-	
+
 		@Override
 		public int hashCode() {
 			if (hashCode  != 0) {
@@ -64,18 +78,4 @@ public final class QuadImpl implements Quad, RDF4JQuad {
 		public String toString() {
 			return statement.toString();
 		}
-
-		@Override
-		public Optional<BlankNodeOrIRI> getGraphName() {
-			if (statement.getContext() == null) { 
-				return Optional.empty();
-			}
-			BlankNodeOrIRI g = (BlankNodeOrIRI) RDF4JTermFactory.asRDFTerm(statement.getContext(),
salt);
-			return Optional.of(g);
-		}
-		
-		@Override
-		public Triple asTriple() {
-			return new TripleImpl(statement, salt);
-		}
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/99bd16f6/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/impl/RepositoryGraphImpl.java
----------------------------------------------------------------------
diff --git a/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/impl/RepositoryGraphImpl.java
b/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/impl/RepositoryGraphImpl.java
new file mode 100644
index 0000000..50ee2ee
--- /dev/null
+++ b/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/impl/RepositoryGraphImpl.java
@@ -0,0 +1,153 @@
+package org.apache.commons.rdf.rdf4j.impl;
+
+import java.util.Optional;
+import java.util.stream.Stream;
+
+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.RDFTerm;
+import org.apache.commons.rdf.api.Triple;
+import org.apache.commons.rdf.rdf4j.RDF4JGraph;
+import org.apache.commons.rdf.rdf4j.RDF4JTermFactory;
+import org.apache.commons.rdf.rdf4j.RDF4JTriple;
+import org.eclipse.rdf4j.common.iteration.Iterations;
+import org.eclipse.rdf4j.model.Model;
+import org.eclipse.rdf4j.model.Resource;
+import org.eclipse.rdf4j.model.Statement;
+import org.eclipse.rdf4j.model.Value;
+import org.eclipse.rdf4j.repository.Repository;
+import org.eclipse.rdf4j.repository.RepositoryConnection;
+import org.eclipse.rdf4j.repository.RepositoryResult;
+
+public class RepositoryGraphImpl implements Graph, RDF4JGraph {
+
+	private Repository repository;
+	private boolean includeInferred;
+	boolean shouldWeShutdown = false;
+	private RDF4JTermFactory rdf4jTermFactory;
+
+	public RepositoryGraphImpl(Repository repository) {
+		this(repository, false);
+	}
+
+	public RepositoryGraphImpl(Repository repository, boolean includeInferred) {
+		this.repository = repository;
+		this.includeInferred = includeInferred;
+		if (! repository.isInitialized()) {
+			repository.initialize();
+			shouldWeShutdown = true;
+		}
+		rdf4jTermFactory = new RDF4JTermFactory(repository.getValueFactory());		
+	}
+
+	
+	@Override
+	public void close() throws Exception {
+		if (shouldWeShutdown) {		
+			repository.shutDown();
+		}
+		// else: repository was initialized outside, so we should not shut it down
+	}
+
+	@Override
+	public void add(Triple triple) {
+		Statement statement = rdf4jTermFactory.asStatement(triple);
+		try (RepositoryConnection conn = getRepositoryConnection()) {			
+			conn.add(statement);
+		}
+	}
+
+	@Override
+	public void add(BlankNodeOrIRI subject, IRI predicate, RDFTerm object) {
+		Resource subj = (Resource) rdf4jTermFactory.asValue(subject);
+		org.eclipse.rdf4j.model.IRI pred = (org.eclipse.rdf4j.model.IRI) rdf4jTermFactory.asValue(predicate);
+		Value obj = rdf4jTermFactory.asValue(object);
+		try (RepositoryConnection conn = getRepositoryConnection()) {
+			conn.add(subj, pred, obj);
+		}
+	}
+
+	@Override
+	public boolean contains(Triple triple) {
+		Statement statement = rdf4jTermFactory.asStatement(triple);
+		try (RepositoryConnection conn = getRepositoryConnection()) {			
+			return conn.hasStatement(statement, includeInferred );
+		}
+	}
+
+	@Override
+	public boolean contains(BlankNodeOrIRI subject, IRI predicate, RDFTerm object) {
+		Resource subj = (Resource) rdf4jTermFactory.asValue(subject);
+		org.eclipse.rdf4j.model.IRI pred = (org.eclipse.rdf4j.model.IRI) rdf4jTermFactory.asValue(predicate);
+		Value obj = rdf4jTermFactory.asValue(object);
+		try (RepositoryConnection conn = getRepositoryConnection()) {
+			return conn.hasStatement(subj, pred, obj, includeInferred);
+		}
+	}
+
+	@Override
+	public void remove(Triple triple) {
+		Statement statement = rdf4jTermFactory.asStatement(triple);
+		try (RepositoryConnection conn = getRepositoryConnection()) {			
+			conn.remove(statement);
+		}
+	}
+
+	@Override
+	public void remove(BlankNodeOrIRI subject, IRI predicate, RDFTerm object) {
+		Resource subj = (Resource) rdf4jTermFactory.asValue(subject);
+		org.eclipse.rdf4j.model.IRI pred = (org.eclipse.rdf4j.model.IRI) rdf4jTermFactory.asValue(predicate);
+		Value obj = rdf4jTermFactory.asValue(object);
+		try (RepositoryConnection conn = getRepositoryConnection()) {
+			conn.remove(subj, pred, obj);
+		}
+	}
+
+	@Override
+	public void clear() {
+		try (RepositoryConnection conn = getRepositoryConnection()) {
+			conn.clear();
+		}
+	}
+
+	@Override
+	public long size() {
+		try (RepositoryConnection conn = getRepositoryConnection()) {
+			// FIXME: The below might contain duplicate statements across multiple contexts
+			return conn.size();
+		}
+		
+	}
+
+	@Override
+	public Stream<RDF4JTriple> stream() {
+		return stream(null, null, null);
+	}
+
+	@Override
+	public Stream<RDF4JTriple> stream(BlankNodeOrIRI subject, IRI predicate, RDFTerm object)
{
+		Resource subj = (Resource) rdf4jTermFactory.asValue(subject);
+		org.eclipse.rdf4j.model.IRI pred = (org.eclipse.rdf4j.model.IRI) rdf4jTermFactory.asValue(predicate);
+		Value obj = rdf4jTermFactory.asValue(object);
+		try (RepositoryConnection conn = getRepositoryConnection()) {
+			// FIXME: Will the above close too early? (e.g. when returning iterator)
+			RepositoryResult<Statement> statements = conn.getStatements(subj, pred, obj);
+			return Iterations.stream(statements).map(rdf4jTermFactory::asTriple);
+		}		
+	}
+
+	private RepositoryConnection getRepositoryConnection() {
+		return repository.getConnection();
+	}
+
+	public Optional<Repository> asRepository() {
+		return Optional.of(repository);
+	}
+
+	@Override
+	public Optional<Model> asModel() {
+		return Optional.empty();
+	}	
+	
+}

http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/99bd16f6/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/impl/TripleImpl.java
----------------------------------------------------------------------
diff --git a/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/impl/TripleImpl.java b/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/impl/TripleImpl.java
index 74564fb..c494006 100644
--- a/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/impl/TripleImpl.java
+++ b/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/impl/TripleImpl.java
@@ -11,8 +11,8 @@ import org.apache.commons.rdf.rdf4j.RDF4JTriple;
 import org.eclipse.rdf4j.model.Statement;
 
 public final class TripleImpl implements Triple, RDF4JTriple {
-		private final Statement statement;	
-		private UUID salt;
+		private UUID salt;	
+		private final Statement statement;
 		
 		public TripleImpl(Statement statement, UUID salt) {
 			this.statement = statement;


Mime
View raw message