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: COMMONSRDF-35 rdf4j add createDataset() using MemoryStore
Date Fri, 07 Oct 2016 15:01:20 GMT
COMMONSRDF-35 rdf4j add createDataset() using MemoryStore

also: don't initialize/shutdown unless explicitly asked to do so


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

Branch: refs/heads/master
Commit: ed77aad82ea1cb4c9d5f694753fb9e04dd4a0b95
Parents: 06711f9
Author: Stian Soiland-Reyes <stain@apache.org>
Authored: Sat Oct 1 20:28:13 2016 +0100
Committer: Stian Soiland-Reyes <stain@apache.org>
Committed: Sat Oct 1 20:28:34 2016 +0100

----------------------------------------------------------------------
 rdf4j/pom.xml                                   |  9 +-
 .../commons/rdf/rdf4j/RDF4JTermFactory.java     | 88 +++++++++++---------
 .../rdf4j/impl/AbstractRepositoryGraphLike.java | 10 +--
 .../rdf/rdf4j/impl/RepositoryDatasetImpl.java   | 26 +++---
 .../rdf/rdf4j/impl/RepositoryGraphImpl.java     |  8 +-
 5 files changed, 74 insertions(+), 67 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/ed77aad8/rdf4j/pom.xml
----------------------------------------------------------------------
diff --git a/rdf4j/pom.xml b/rdf4j/pom.xml
index db51613..ce21b98 100644
--- a/rdf4j/pom.xml
+++ b/rdf4j/pom.xml
@@ -121,20 +121,19 @@
 
 
 		<!-- There are multiple RDF4J sail/repository implementations, we use the 
-			below for testing. -->
+			below from RDF4JTermFactory. -->
 		<dependency>
 			<groupId>org.eclipse.rdf4j</groupId>
 			<artifactId>rdf4j-sail-memory</artifactId>
-			<scope>test</scope>
 		</dependency>
 		<dependency>
 			<groupId>org.eclipse.rdf4j</groupId>
-			<artifactId>rdf4j-sail-nativerdf</artifactId>
-			<scope>test</scope>
+			<artifactId>rdf4j-repository-sail</artifactId>
 		</dependency>
+		<!--  and nativerdf for testing -->
 		<dependency>
 			<groupId>org.eclipse.rdf4j</groupId>
-			<artifactId>rdf4j-repository-sail</artifactId>
+			<artifactId>rdf4j-sail-nativerdf</artifactId>
 			<scope>test</scope>
 		</dependency>
 

http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/ed77aad8/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 32044f1..04598d4 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
@@ -19,7 +19,7 @@ package org.apache.commons.rdf.rdf4j;
 
 import java.util.UUID;
 
-// To avoid confusion, avoid importing 
+// To avoid confusion, avoid importing
 // classes that are in both
 // commons.rdf and openrdf.model (e.g. IRI)
 import org.apache.commons.rdf.api.BlankNode;
@@ -48,6 +48,9 @@ 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;
+import org.eclipse.rdf4j.repository.sail.SailRepository;
+import org.eclipse.rdf4j.sail.Sail;
+import org.eclipse.rdf4j.sail.memory.MemoryStore;
 
 /**
  * RDF4J implementation of RDFTermFactory
@@ -96,7 +99,7 @@ import org.eclipse.rdf4j.repository.Repository;
  * is to use the static methods {@link #asRDFTerm(Value, UUID)},
  * {@link #asQuad(Statement, UUID)} or {@link #asTriple(Statement, UUID)} with
  * a provided {@link UUID} salt.
- * 
+ *
  */
 public class RDF4JTermFactory implements RDFTermFactory {
 
@@ -110,7 +113,7 @@ public class RDF4JTermFactory implements RDFTermFactory {
 	 * {@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
 	 *            The RDF4J {@link Value} to convert.
 	 * @param salt
@@ -141,7 +144,7 @@ public class RDF4JTermFactory implements RDFTermFactory {
 
 	/**
 	 * Adapt a RDF4J {@link Statement} as a Commons RDF {@link Triple}.
-	 * 
+	 *
 	 * @param statement
 	 *            The statement to convert
 	 * @param salt
@@ -171,18 +174,18 @@ public class RDF4JTermFactory implements RDFTermFactory {
 	/**
 	 * Adapt a RDF4J {@link Statement} as a Commons RDF {@link Quad}.
 	 * <p>
-	 * For the purpose of {@link BlankNode} equivalence, this 
-	 * method will use an internal salt UUID that is unique per instance of 
-	 * {@link RDF4JTermFactory}. 
+	 * For the purpose of {@link BlankNode} equivalence, this
+	 * method will use an internal salt UUID that is unique per instance of
+	 * {@link RDF4JTermFactory}.
 	 * <p>
 	 * <strong>NOTE:</strong> If combining RDF4J {@link Statement}s
-	 * multiple repositories or models, then their {@link BNode}s 
-	 * may have the same {@link BNode#getID()}, which with this method 
+	 * multiple repositories or models, then their {@link BNode}s
+	 * may have the same {@link BNode#getID()}, which with this method
 	 * would become equivalent according to {@link BlankNode#equals(Object)} and
-	 * {@link BlankNode#uniqueReference()}, 
+	 * {@link BlankNode#uniqueReference()},
 	 * unless a separate {@link RDF4JTermFactory}
-	 * instance is used per RDF4J repository/model.  
-	 * 
+	 * instance is used per RDF4J repository/model.
+	 *
 	 * @see #asQuad(Statement, UUID)
 	 * @param statement
 	 *            The statement to convert
@@ -210,9 +213,9 @@ public class RDF4JTermFactory implements RDFTermFactory {
 		return new QuadImpl(statement, salt);
 	}
 
-	
+
 	/**
-	 * 
+	 *
 	 * Adapt a RDF4J {@link Value} as a Commons RDF {@link RDFTerm}.
 	 * <p>
 	 * The value will be of the same kind as the term, e.g. a
@@ -223,23 +226,23 @@ public class RDF4JTermFactory implements RDFTermFactory {
 	 * {@link org.eclipse.rdf4j.model.Literal}. is converted to a
 	 * {@link org.apache.commons.rdf.api.Literal}
 	 * <p>
-	 * For the purpose of {@link BlankNode} equivalence, this 
-	 * method will use an internal salt UUID that is unique per instance of 
-	 * {@link RDF4JTermFactory}. 
+	 * For the purpose of {@link BlankNode} equivalence, this
+	 * method will use an internal salt UUID that is unique per instance of
+	 * {@link RDF4JTermFactory}.
 	 * <p>
 	 * <strong>NOTE:</strong> If combining RDF4J values from
-	 * multiple repositories or models, then their {@link BNode}s 
-	 * may have the same {@link BNode#getID()}, which with this method 
+	 * multiple repositories or models, then their {@link BNode}s
+	 * may have the same {@link BNode#getID()}, which with this method
 	 * would become equivalent according to {@link BlankNode#equals(Object)} and
-	 * {@link BlankNode#uniqueReference()}, 
+	 * {@link BlankNode#uniqueReference()},
 	 * unless a separate {@link RDF4JTermFactory}
-	 * instance is used per RDF4J repository/model.  
-	 * 
+	 * instance is used per RDF4J repository/model.
+	 *
 	 * @param value The RDF4J {@link Value} to convert.
 	 * @param <T>
 	 *            The subclass of {@link Value}, e.g. {@link BNode}
 	 * @return A {@link RDFTerm} that corresponds to the RDF4J value
-	 * @throws IllegalArgumentException if the value is not a BNode, Literal or IRI 
+	 * @throws IllegalArgumentException if the value is not a BNode, Literal or IRI
 	 */
 	public <T extends Value> RDF4JTerm<T> asRDFTerm(T value) {
 		return asRDFTerm(value, salt);
@@ -249,20 +252,20 @@ public class RDF4JTermFactory implements RDFTermFactory {
 	 * Adapt an RDF4J {@link Repository} as a Commons RDF {@link Dataset}.
 	 * <p>
 	 * Changes to the dataset are reflected in the repository, and vice versa.
-	 * 
+	 *
 	 * @param repository
 	 *            RDF4J {@link Repository} to connect to.
 	 * @return A {@link Dataset} backed by the RDF4J repository.
 	 */
 	public RDF4JDataset asRDFTermDataset(Repository repository) {
-		return new RepositoryDatasetImpl(repository);
+		return new RepositoryDatasetImpl(repository, false, false);
 	}
 
 	/**
 	 * Adapt an RDF4J {@link Repository} as a Commons RDF {@link Dataset}.
 	 * <p>
 	 * Changes to the dataset are reflected in the repository, and vice versa.
-	 * 
+	 *
 	 * @param repository
 	 *            RDF4J {@link Repository} to connect to.
 	 * @param includeInferred
@@ -270,14 +273,14 @@ public class RDF4JTermFactory implements RDFTermFactory {
 	 * @return A {@link Dataset} backed by the RDF4J repository.
 	 */
 	public RDF4JDataset asRDFTermDataset(Repository repository, boolean includeInferred) {
-		return new RepositoryDatasetImpl(repository, includeInferred);
+		return new RepositoryDatasetImpl(repository, false, includeInferred);
 	}
-	
+
 	/**
 	 * Adapt an RDF4J {@link Model} as a Commons RDF {@link Graph}.
 	 * <p>
 	 * Changes to the graph are reflected in the model, and vice versa.
-	 * 
+	 *
 	 * @param model
 	 *            RDF4J {@link Model} to adapt.
 	 * @return Adapted {@link Graph}.
@@ -292,7 +295,7 @@ public class RDF4JTermFactory implements RDFTermFactory {
 	 * The graph will include triples in any contexts (e.g. the union graph).
 	 * <p>
 	 * Changes to the graph are reflected in the repository, and vice versa.
-	 * 
+	 *
 	 * @param repository
 	 *            RDF4J {@link Repository} to connect to.
 	 * @return A {@link Graph} backed by the RDF4J repository.
@@ -307,7 +310,7 @@ public class RDF4JTermFactory implements RDFTermFactory {
 	 * The graph will include triples in any contexts (e.g. the union graph).
 	 * <p>
 	 * Changes to the graph are reflected in the repository, and vice versa.
-	 * 
+	 *
 	 * @param repository
 	 *            RDF4J {@link Repository} to connect to.
 	 * @param includeInferred
@@ -329,10 +332,10 @@ public class RDF4JTermFactory implements RDFTermFactory {
 	 * If the <code>tripleLike</code> argument is an {@link RDF4JTriple} or
 	 * a {@link RDF4JQuad}, then its {@link RDF4JTripleLike#asStatement()} is
 	 * returned as-is. Note that this means that a {@link RDF4JTriple} would
-	 * preserve its {@link Statement#getContext()}, and that any 
+	 * preserve its {@link Statement#getContext()}, and that any
 	 * {@link BlankNode}s would be deemed equivalent in RDF4J
 	 * if they have the same {@link BNode#getID()}.
-	 * 
+	 *
 	 * @param tripleLike
 	 *            A {@link Triple} or {@link Quad} to adapt
 	 * @return A corresponding {@link Statement}
@@ -371,7 +374,7 @@ public class RDF4JTermFactory implements RDFTermFactory {
 	 * according to {@link BlankNode#equals(Object)} and
 	 * {@link BlankNode#uniqueReference()}, unless a separate
 	 * {@link RDF4JTermFactory} instance is used per RDF4J repository/model.
-	 * 
+	 *
 	 * @param statement
 	 *            The RDF4J {@link Statement} to adapt.
 	 * @return A {@link RDF4JTriple} that is equivalent to the statement
@@ -398,7 +401,7 @@ public class RDF4JTermFactory implements RDFTermFactory {
 	 * {@link RDF4JTerm#asValue()} is returned without any conversion. Note that
 	 * 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
 	 * @return Adapted RDF4J {@link Value}
@@ -450,6 +453,13 @@ public class RDF4JTermFactory implements RDFTermFactory {
 	}
 
 	@Override
+	public Dataset createDataset() {
+		Sail sail = new MemoryStore();
+		Repository repository = new SailRepository(sail);
+		return asRDFTermDataset(repository);
+	}
+
+	@Override
 	public RDF4JGraph createGraph() throws UnsupportedOperationException {
 		return asRDFTermGraph(new LinkedHashModel());
 	}
@@ -486,7 +496,7 @@ public class RDF4JTermFactory implements RDFTermFactory {
 			throws IllegalArgumentException, UnsupportedOperationException {
 		final Statement statement = getValueFactory().createStatement(
 				(org.eclipse.rdf4j.model.Resource) asValue(subject),
-				(org.eclipse.rdf4j.model.IRI) asValue(predicate), 
+				(org.eclipse.rdf4j.model.IRI) asValue(predicate),
 				asValue(object));
 		return asTriple(statement);
 	}
@@ -496,12 +506,12 @@ public class RDF4JTermFactory implements RDFTermFactory {
 			throws IllegalArgumentException, UnsupportedOperationException {
 		final Statement statement = getValueFactory().createStatement(
 				(org.eclipse.rdf4j.model.Resource) asValue(subject),
-				(org.eclipse.rdf4j.model.IRI) asValue(predicate), 
-				asValue(object), 
+				(org.eclipse.rdf4j.model.IRI) asValue(predicate),
+				asValue(object),
 				(org.eclipse.rdf4j.model.Resource)asValue(graphName));
 		return asQuad(statement);
 	}
-	
+
 	public ValueFactory getValueFactory() {
 		return valueFactory;
 	}

http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/ed77aad8/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/impl/AbstractRepositoryGraphLike.java
----------------------------------------------------------------------
diff --git a/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/impl/AbstractRepositoryGraphLike.java
b/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/impl/AbstractRepositoryGraphLike.java
index bd47436..04c77fd 100644
--- a/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/impl/AbstractRepositoryGraphLike.java
+++ b/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/impl/AbstractRepositoryGraphLike.java
@@ -35,26 +35,26 @@ public abstract class AbstractRepositoryGraphLike<T extends TripleLike<BlankNode
 
 	protected Repository repository;
 	protected boolean includeInferred;
-	protected boolean shouldWeShutdown = false;
+	protected boolean handleInitAndShutdown = false;
 	protected RDF4JTermFactory rdf4jTermFactory;
 
 	public AbstractRepositoryGraphLike(Repository repository) {
-		this(repository, false);
+		this(repository, true, false);
 	}
 
-	public AbstractRepositoryGraphLike(Repository repository, boolean includeInferred) {
+	public AbstractRepositoryGraphLike(Repository repository, boolean handleInitAndShutdown,
boolean includeInferred) {
 		this.repository = repository;
 		this.includeInferred = includeInferred;
 		if (!repository.isInitialized()) {
 			repository.initialize();
-			shouldWeShutdown = true;
+			handleInitAndShutdown = true;
 		}
 		rdf4jTermFactory = new RDF4JTermFactory(repository.getValueFactory());
 	}
 
 	@Override
 	public void close() throws Exception {
-		if (shouldWeShutdown) {
+		if (handleInitAndShutdown) {
 			repository.shutDown();
 		}
 		// else: repository was initialized outside, so we should not shut it

http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/ed77aad8/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/impl/RepositoryDatasetImpl.java
----------------------------------------------------------------------
diff --git a/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/impl/RepositoryDatasetImpl.java
b/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/impl/RepositoryDatasetImpl.java
index fa989a1..04d685c 100644
--- a/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/impl/RepositoryDatasetImpl.java
+++ b/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/impl/RepositoryDatasetImpl.java
@@ -38,12 +38,12 @@ import org.eclipse.rdf4j.repository.RepositoryResult;
 
 public class RepositoryDatasetImpl extends AbstractRepositoryGraphLike<Quad> implements
RDF4JDataset, Dataset {
 
-	public RepositoryDatasetImpl(Repository repository, boolean includeInferred) {
-		super(repository, includeInferred);
+	public RepositoryDatasetImpl(Repository repository, boolean handleInitAndShutdown, boolean
includeInferred) {
+		super(repository, handleInitAndShutdown, includeInferred);
 	}
 
 	public RepositoryDatasetImpl(Repository repository) {
-		super(repository);
+		this(repository, false, false);
 	}
 
 
@@ -84,17 +84,16 @@ public class RepositoryDatasetImpl extends AbstractRepositoryGraphLike<Quad>
imp
 
 	@Override
 	public long size() {
-		if (includeInferred) { 
+		if (includeInferred) {
 			// We'll need to count them all
 			return stream().count();
-		} 
+		}
 		// else: Ask directly
 		try (RepositoryConnection conn = getRepositoryConnection()) {
 			return conn.size();
 		}
 	}
 
-	
 	@Override
 	public void add(BlankNodeOrIRI graphName, BlankNodeOrIRI subject, IRI predicate, RDFTerm
object) {
 		Resource context = (Resource) rdf4jTermFactory.asValue(graphName);
@@ -113,7 +112,6 @@ public class RepositoryDatasetImpl extends AbstractRepositoryGraphLike<Quad>
imp
 		org.eclipse.rdf4j.model.IRI pred = (org.eclipse.rdf4j.model.IRI) rdf4jTermFactory.asValue(predicate);
 		Value obj = rdf4jTermFactory.asValue(object);
 		Resource[] contexts = asContexts(graphName);
-		
 		try (RepositoryConnection conn = getRepositoryConnection()) {
 			return conn.hasStatement(subj, pred, obj, includeInferred, contexts);
 		}
@@ -124,7 +122,7 @@ public class RepositoryDatasetImpl extends AbstractRepositoryGraphLike<Quad>
imp
 		if (graphName == null) {
 			// no contexts == any contexts
 			 contexts = new Resource[0];
-		} else {	
+		} else {
 			BlankNodeOrIRI g = graphName.orElse(null);
 			Resource context = (Resource) rdf4jTermFactory.asValue(g);
 			contexts = new Resource[] { context };
@@ -149,14 +147,14 @@ public class RepositoryDatasetImpl extends AbstractRepositoryGraphLike<Quad>
imp
 	public Stream<RDF4JQuad> stream() {
 		return stream(null, null, null, null);
 	}
-	
+
 	@Override
 	public Stream<RDF4JQuad> stream(Optional<BlankNodeOrIRI> graphName, 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);
 		Resource[] contexts = asContexts(graphName);
-		
+
 		RepositoryConnection conn = getRepositoryConnection();
 		// NOTE: connection will be closed outside by the Iterations.stream()
 		RepositoryResult<Statement> statements = conn.getStatements(subj, pred, obj, includeInferred,
contexts);
@@ -171,14 +169,14 @@ public class RepositoryDatasetImpl extends AbstractRepositoryGraphLike<Quad>
imp
 	@Override
 	public Graph getGraph() {
 		// default context only
-		return new RepositoryGraphImpl(repository, includeInferred, (Resource)null);		
+		return new RepositoryGraphImpl(repository, false, includeInferred, (Resource)null);
 	}
 
 	@Override
 	public Optional<Graph> getGraph(BlankNodeOrIRI graphName) {
 		// NOTE: May be null to indicate default context
-		Resource context = (Resource) rdf4jTermFactory.asValue(graphName);		
-		return Optional.of(new RepositoryGraphImpl(repository, includeInferred, context));		
+		Resource context = (Resource) rdf4jTermFactory.asValue(graphName);
+		return Optional.of(new RepositoryGraphImpl(repository, false, includeInferred, context));
 	}
 
 	@Override
@@ -188,5 +186,5 @@ public class RepositoryDatasetImpl extends AbstractRepositoryGraphLike<Quad>
imp
 		// NOTE: connection will be closed outside by the Iterations.stream()
 		return Iterations.stream(contexts).map(g -> (BlankNodeOrIRI) rdf4jTermFactory.asRDFTerm(g));
 	}
-	
+
 }

http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/ed77aad8/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
index 401f221..e62fdc8 100644
--- 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
@@ -39,8 +39,8 @@ public class RepositoryGraphImpl extends AbstractRepositoryGraphLike<Triple>
imp
 
 	private final Resource[] contextFilter;
 
-	public RepositoryGraphImpl(Repository repository, boolean includeInferred, boolean unionGraph)
{
-		super(repository, includeInferred);
+	public RepositoryGraphImpl(Repository repository, boolean handleInitAndShutdown, boolean
includeInferred, boolean unionGraph) {
+		super(repository, handleInitAndShutdown, includeInferred);
 		if (unionGraph) {
 			// no context filter aka any context
 			this.contextFilter = new Resource[] { };
@@ -50,8 +50,8 @@ public class RepositoryGraphImpl extends AbstractRepositoryGraphLike<Triple>
imp
 		}
 	}
 
-	public RepositoryGraphImpl(Repository repository, boolean includeInferred, Resource... contextFilter)
{
-		super(repository, includeInferred);
+	public RepositoryGraphImpl(Repository repository, boolean handleInitAndShutdown, boolean
includeInferred, Resource... contextFilter) {
+		super(repository, handleInitAndShutdown, includeInferred);
 		this.contextFilter = contextFilter;
 	}
 


Mime
View raw message