You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commonsrdf.apache.org by st...@apache.org on 2016/06/14 15:45:03 UTC

[3/8] incubator-commonsrdf git commit: Split out AbstractRepositoryGraphLike

Split out AbstractRepositoryGraphLike

.. to be used by RepositoryDataset


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

Branch: refs/heads/rdf4j
Commit: e477b937ecd9b20970ff720e2711ee844b9e9e94
Parents: 3af404e
Author: Stian Soiland-Reyes <st...@apache.org>
Authored: Fri Jun 3 15:26:26 2016 +0100
Committer: Stian Soiland-Reyes <st...@apache.org>
Committed: Fri Jun 3 15:27:13 2016 +0100

----------------------------------------------------------------------
 .../apache/commons/rdf/rdf4j/RDF4JDataset.java  |  30 +++++
 .../apache/commons/rdf/rdf4j/RDF4JGraph.java    |  33 +----
 .../commons/rdf/rdf4j/RDF4JGraphLike.java       |  63 ++++++++++
 .../rdf4j/impl/AbstractRepositoryGraphLike.java | 125 +++++++++++++++++++
 .../rdf/rdf4j/impl/RepositoryGraphImpl.java     |  94 ++------------
 5 files changed, 230 insertions(+), 115 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/e477b937/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/RDF4JDataset.java
----------------------------------------------------------------------
diff --git a/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/RDF4JDataset.java b/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/RDF4JDataset.java
new file mode 100644
index 0000000..b0ca08d
--- /dev/null
+++ b/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/RDF4JDataset.java
@@ -0,0 +1,30 @@
+/**
+ * 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.rdf4j;
+
+import org.apache.commons.rdf.api.Dataset;
+import org.apache.commons.rdf.api.Quad;
+
+
+/**
+ * Marker interface for RDF4J implementations of Dataset.
+ * 
+ */
+public interface RDF4JDataset extends Dataset, RDF4JGraphLike<Quad> {
+	
+}

http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/e477b937/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 abc4a98..d7cb50e 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,43 +17,14 @@
  */
 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;
+import org.apache.commons.rdf.api.Triple;
 
 
 /**
  * Marker interface for RDF4J implementations of Graph.
  * 
  */
-public interface RDF4JGraph extends Graph {
-	
-	/**
-	 * 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 Optional<Model> asModel();
+public interface RDF4JGraph extends Graph, RDF4JGraphLike<Triple> {
 	
-	/**
-	 * 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/e477b937/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/RDF4JGraphLike.java
----------------------------------------------------------------------
diff --git a/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/RDF4JGraphLike.java b/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/RDF4JGraphLike.java
new file mode 100644
index 0000000..1750bd8
--- /dev/null
+++ b/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/RDF4JGraphLike.java
@@ -0,0 +1,63 @@
+/**
+ * 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.rdf4j;
+
+import java.util.Optional;
+
+import org.apache.commons.rdf.api.BlankNodeOrIRI;
+import org.apache.commons.rdf.api.GraphLike;
+import org.apache.commons.rdf.api.IRI;
+import org.apache.commons.rdf.api.RDFTerm;
+import org.apache.commons.rdf.api.TripleLike;
+import org.eclipse.rdf4j.model.Model;
+import org.eclipse.rdf4j.repository.Repository;
+
+/**
+ * Marker interface for RDF4J implementations of GraphLike.
+ * 
+ * @see RDF4JGraph
+ * 
+ */
+public interface RDF4JGraphLike<T extends TripleLike<BlankNodeOrIRI, IRI, RDFTerm>>
+		extends GraphLike<T, BlankNodeOrIRI, IRI, RDFTerm>, AutoCloseable {
+
+	/**
+	 * Return the corresponding RDF4J {@link Model}, if present.
+	 * <p>
+	 * The return value is {@link Optional#isPresent()} if this is backed by a
+	 * Model.
+	 * <p>
+	 * Changes to the Model are reflected in both directions.
+	 * 
+	 * @return The corresponding RDF4J Model.
+	 */
+	public Optional<Model> asModel();
+
+	/**
+	 * Return the corresponding RDF4J {@link Repository}, if present.
+	 * <p>
+	 * The return value is {@link Optional#isPresent()} if this is backed by a
+	 * Repository.
+	 * <p>
+	 * Changes to the Repository are reflected in both directions.
+	 * 
+	 * @return The corresponding RDF4J Repository.
+	 */
+	public Optional<Repository> asRepository();
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/e477b937/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
new file mode 100644
index 0000000..1283e2e
--- /dev/null
+++ b/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/impl/AbstractRepositoryGraphLike.java
@@ -0,0 +1,125 @@
+/**
+ * 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.rdf4j.impl;
+
+import java.util.Optional;
+
+import org.apache.commons.rdf.api.BlankNodeOrIRI;
+import org.apache.commons.rdf.api.IRI;
+import org.apache.commons.rdf.api.RDFTerm;
+//import org.apache.commons.rdf.api.tripleLike;
+import org.apache.commons.rdf.api.TripleLike;
+import org.apache.commons.rdf.rdf4j.RDF4JGraphLike;
+import org.apache.commons.rdf.rdf4j.RDF4JTermFactory;
+import org.eclipse.rdf4j.model.Model;
+import org.eclipse.rdf4j.model.Statement;
+import org.eclipse.rdf4j.repository.Repository;
+import org.eclipse.rdf4j.repository.RepositoryConnection;
+
+public abstract class AbstractRepositoryGraphLike<T extends TripleLike<BlankNodeOrIRI, IRI, RDFTerm>>
+		implements RDF4JGraphLike<T> {
+
+	protected Repository repository;
+	protected boolean includeInferred;
+	protected boolean shouldWeShutdown = false;
+	protected RDF4JTermFactory rdf4jTermFactory;
+
+	public AbstractRepositoryGraphLike(Repository repository) {
+		this(repository, false);
+	}
+
+	public AbstractRepositoryGraphLike(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(T tripleLike) {
+		Statement statement = rdf4jTermFactory.asStatement(tripleLike);
+		try (RepositoryConnection conn = getRepositoryConnection()) {
+			conn.add(statement);
+			conn.commit();
+		}
+	}
+
+
+	@Override
+	public boolean contains(T tripleLike) {
+		Statement statement = rdf4jTermFactory.asStatement(tripleLike);
+		try (RepositoryConnection conn = getRepositoryConnection()) {
+			return conn.hasStatement(statement, includeInferred);
+		}
+	}
+
+	@Override
+	public void remove(T tripleLike) {
+		Statement statement = rdf4jTermFactory.asStatement(tripleLike);
+		try (RepositoryConnection conn = getRepositoryConnection()) {
+			conn.remove(statement);
+			conn.commit();
+		}
+	}
+
+	@Override
+	public void clear() {
+		try (RepositoryConnection conn = getRepositoryConnection()) {
+			conn.clear();
+			conn.commit();
+		}
+	}
+
+	@Override
+	public long size() {
+		try (RepositoryConnection conn = getRepositoryConnection()) {
+			// FIXME: The below might contain duplicate statements across
+			// multiple contexts
+			return conn.size();
+		}
+
+	}
+
+	protected abstract T asTripleLike(Statement s);
+
+	protected 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/e477b937/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 2e608a1..1fe1498 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
@@ -17,7 +17,6 @@
  */
 package org.apache.commons.rdf.rdf4j.impl;
 
-import java.util.Optional;
 import java.util.stream.Stream;
 
 import org.apache.commons.rdf.api.BlankNodeOrIRI;
@@ -26,10 +25,8 @@ 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;
@@ -37,43 +34,14 @@ 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 class RepositoryGraphImpl extends AbstractRepositoryGraphLike<Triple> implements Graph, RDF4JGraph {
 
 	public RepositoryGraphImpl(Repository repository) {
-		this(repository, false);
+		super(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);
-			conn.commit();
-		}
+		super(repository, includeInferred);
 	}
 
 	@Override
@@ -86,14 +54,8 @@ public class RepositoryGraphImpl implements Graph, RDF4JGraph {
 			conn.commit();
 		}
 	}
-
-	@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) {
@@ -106,15 +68,6 @@ public class RepositoryGraphImpl implements Graph, RDF4JGraph {
 	}
 
 	@Override
-	public void remove(Triple triple) {
-		Statement statement = rdf4jTermFactory.asStatement(triple);
-		try (RepositoryConnection conn = getRepositoryConnection()) {
-			conn.remove(statement);
-			conn.commit();
-		}
-	}
-
-	@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);
@@ -126,28 +79,10 @@ public class RepositoryGraphImpl implements Graph, RDF4JGraph {
 	}
 
 	@Override
-	public void clear() {
-		try (RepositoryConnection conn = getRepositoryConnection()) {
-			conn.clear();
-			conn.commit();
-		}
-	}
-
-	@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);
@@ -156,21 +91,12 @@ public class RepositoryGraphImpl implements Graph, RDF4JGraph {
 		RepositoryConnection conn = getRepositoryConnection();
 		// FIXME: Is it OK that we don't close the connection?
 		RepositoryResult<Statement> statements = conn.getStatements(subj, pred, obj);
-		return Iterations.stream(statements).map(rdf4jTermFactory::asTriple);
-
+		return Iterations.stream(statements).map(this::asTripleLike);
 	}
-
-	private RepositoryConnection getRepositoryConnection() {
-		return repository.getConnection();
-	}
-
-	public Optional<Repository> asRepository() {
-		return Optional.of(repository);
-	}
-
+	
 	@Override
-	public Optional<Model> asModel() {
-		return Optional.empty();
+	protected RDF4JTriple asTripleLike(Statement statement) {
+		return rdf4jTermFactory.asTriple(statement);
 	}
 
 }