commonsrdf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From st...@apache.org
Subject [1/2] incubator-commonsrdf git commit: expose includeInferred
Date Tue, 14 Jun 2016 17:02:28 GMT
Repository: incubator-commonsrdf
Updated Branches:
  refs/heads/rdf4j 81f195f31 -> ea6b992b0


expose includeInferred


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

Branch: refs/heads/rdf4j
Commit: ccb5c5fd46ec268fb6400f35c85ee5265b387618
Parents: 81f195f
Author: Stian Soiland-Reyes <stain@apache.org>
Authored: Tue Jun 14 17:57:54 2016 +0100
Committer: Stian Soiland-Reyes <stain@apache.org>
Committed: Tue Jun 14 17:57:54 2016 +0100

----------------------------------------------------------------------
 .../commons/rdf/rdf4j/RDF4JTermFactory.java     | 34 +++++++++++++++++---
 .../rdf/rdf4j/impl/RepositoryDatasetImpl.java   | 17 +++++-----
 .../rdf/rdf4j/impl/RepositoryGraphImpl.java     | 15 ++++++---
 3 files changed, 47 insertions(+), 19 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/ccb5c5fd/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 9aa0fda..9326807 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
@@ -170,19 +170,43 @@ public class RDF4JTermFactory implements RDFTermFactory {
 	/**
 	 * Adapt an RDF4J {@link Repository} as a Commons RDF {@link Graph}.
 	 * <p>
+	 * 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 model
 	 *            RDF4J {@link Repository} to connect to.
-	 * @return Adapted {@link Graph}.
+	 * @return A {@link Graph} backed by the RDF4J repository.
 	 */
 	public RDF4JGraph asRDFTermGraph(Repository repository) {
-		return new RepositoryGraphImpl(repository);
+		return new RepositoryGraphImpl(repository, false, true);
+	}
+
+	/**
+	 * Adapt an RDF4J {@link Repository} as a Commons RDF {@link Graph}.
+	 * <p>
+	 * 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
+	 *            If true, any inferred triples are included in the graph
+	 * @param unionGraph
+	 *            If true, triples from any context is included in the graph,
+	 *            otherwise only triples in the default context
+	 *            <code>null</code>.
+	 * @return A {@link Graph} backed by the RDF4J repository.
+	 */
+	public RDF4JGraph asRDFTermGraph(Repository repository, boolean includeInferred, boolean
unionGraph) {
+		return new RepositoryGraphImpl(repository, includeInferred, unionGraph);
 	}
 
 	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
+			// Return original statement - this covers both RDF4JQuad and
+			// RDF4JTriple
 			RDF4JTripleLike rdf4jTriple = (RDF4JTripleLike) tripleLike;
 			return rdf4jTriple.asStatement();
 		}
@@ -190,13 +214,13 @@ public class RDF4JTermFactory implements RDFTermFactory {
 		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);
 	}
 

http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/ccb5c5fd/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 0fb8a78..fa989a1 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
@@ -17,7 +17,6 @@
  */
 package org.apache.commons.rdf.rdf4j.impl;
 
-import java.util.ConcurrentModificationException;
 import java.util.Optional;
 import java.util.stream.Stream;
 
@@ -85,6 +84,11 @@ public class RepositoryDatasetImpl extends AbstractRepositoryGraphLike<Quad>
imp
 
 	@Override
 	public long size() {
+		if (includeInferred) { 
+			// We'll need to count them all
+			return stream().count();
+		} 
+		// else: Ask directly
 		try (RepositoryConnection conn = getRepositoryConnection()) {
 			return conn.size();
 		}
@@ -165,19 +169,14 @@ public class RepositoryDatasetImpl extends AbstractRepositoryGraphLike<Quad>
imp
 	}
 
 	@Override
-	public Iterable<Quad> iterate() throws ConcurrentModificationException, IllegalStateException
{
-		// TODO Auto-generated method stub
-		return null;
-	}
-
-	@Override
 	public Graph getGraph() {
-		// TODO:  Specify default context only? (the below will give the union graph)
-		return new RepositoryGraphImpl(repository, includeInferred);		
+		// default context only
+		return new RepositoryGraphImpl(repository, 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));		
 	}

http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/ccb5c5fd/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 2950579..7c15d1d 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
@@ -36,11 +36,16 @@ import org.eclipse.rdf4j.repository.RepositoryResult;
 
 public class RepositoryGraphImpl extends AbstractRepositoryGraphLike<Triple> implements
Graph, RDF4JGraph {
 
-	private Resource[] contextFilter;
+	private final Resource[] contextFilter;
 
-	public RepositoryGraphImpl(Repository repository) {
-		// All contexts (supplying null would mean default graph only)
-		this(repository, false);
+	public RepositoryGraphImpl(Repository repository, boolean includeInferred, boolean unionGraph)
{
+		super(repository, includeInferred);
+		if (unionGraph) {
+			this.contextFilter = null;
+		} else {
+			// default context: null
+			this.contextFilter = new Resource[] { null };
+		}
 	}
 
 	public RepositoryGraphImpl(Repository repository, boolean includeInferred, Resource... contextFilter)
{
@@ -140,7 +145,7 @@ public class RepositoryGraphImpl extends AbstractRepositoryGraphLike<Triple>
imp
 		Value obj = rdf4jTermFactory.asValue(object);
 		RepositoryConnection conn = getRepositoryConnection();
 		// FIXME: Is it OK that we don't close the connection?
-		RepositoryResult<Statement> statements = conn.getStatements(subj, pred, obj, contextFilter);
+		RepositoryResult<Statement> statements = conn.getStatements(subj, pred, obj, includeInferred,
contextFilter);
 		return Iterations.stream(statements).map(this::asTripleLike);
 	}
 	


Mime
View raw message