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/10/03 17:33:46 UTC

[1/2] incubator-commonsrdf git commit: About closing streams

Repository: incubator-commonsrdf
Updated Branches:
  refs/heads/rdf4j ffa822e7b -> 69814ee5a


About closing streams


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

Branch: refs/heads/rdf4j
Commit: f0bdb07eb9e0932480f1151899f8f99fcb073a48
Parents: ffa822e
Author: Stian Soiland-Reyes <st...@apache.org>
Authored: Mon Oct 3 18:32:46 2016 +0100
Committer: Stian Soiland-Reyes <st...@apache.org>
Committed: Mon Oct 3 18:32:46 2016 +0100

----------------------------------------------------------------------
 .../apache/commons/rdf/rdf4j/RDF4JDataset.java  | 59 ++++++++++++++++++++
 .../apache/commons/rdf/rdf4j/RDF4JGraph.java    | 56 +++++++++++++++----
 2 files changed, 105 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/f0bdb07e/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
index b0ca08d..98a90cf 100644
--- a/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/RDF4JDataset.java
+++ b/rdf4j/src/main/java/org/apache/commons/rdf/rdf4j/RDF4JDataset.java
@@ -17,8 +17,15 @@
  */
 package org.apache.commons.rdf.rdf4j;
 
+import java.util.Optional;
+import java.util.stream.Stream;
+
+import org.apache.commons.rdf.api.BlankNodeOrIRI;
 import org.apache.commons.rdf.api.Dataset;
+import org.apache.commons.rdf.api.IRI;
 import org.apache.commons.rdf.api.Quad;
+import org.apache.commons.rdf.api.RDFTerm;
+import org.eclipse.rdf4j.repository.RepositoryConnection;
 
 
 /**
@@ -27,4 +34,56 @@ import org.apache.commons.rdf.api.Quad;
  */
 public interface RDF4JDataset extends Dataset, RDF4JGraphLike<Quad> {
 	
+	/**
+	 * {@inheritDoc}
+	 * <p>
+	 * Note that the stream must be closed with {@link Stream#close()} to ensure
+	 * the underlying {@link RepositoryConnection} is closed.
+	 * <p>
+	 * This can generally achieved using a try-with-resources block, e.g.:
+	 * <pre>
+	 * int subjects;
+	 * try (Stream&lt;RDF4JQuad&gt; s : graph.stream()) {
+	 *   subjects = s.map(RDF4JQuad::getSubject).distinct().count()
+	 * }
+	 * </pre>
+	 */
+	@Override
+	Stream<RDF4JQuad> stream();
+	
+	/**
+	 * {@inheritDoc}
+	 * <p>
+	 * Note that the stream must be closed with {@link Stream#close()} to ensure
+	 * the underlying {@link RepositoryConnection} is closed.
+	 * <p>
+	 * This can generally achieved using a try-with-resources block, e.g.:
+	 * <pre>
+	 * int subjects;
+	 * try (Stream&lt;RDF4JQuad&gt; s : graph.stream()) {
+	 *   subjects = s.map(RDF4JQuad::getSubject).distinct().count()
+	 * }
+	 * </pre>
+	 */	
+	@Override
+	Stream<RDF4JQuad> stream(Optional<BlankNodeOrIRI> graphName, BlankNodeOrIRI subject, IRI predicate,
+			RDFTerm object);
+
+	/**
+	 * {@inheritDoc}
+	 * <p>
+	 * Note that the stream must be closed with {@link Stream#close()} to ensure
+	 * the underlying {@link RepositoryConnection} is closed.
+	 * <p>
+	 * This can generally achieved using a try-with-resources block, e.g.:
+	 * <pre>
+	 * int graphs;
+	 * try (Stream&lt;BlankNodeOrIRI&gt; s : graph.stream()) {
+	 *   graphs = s.count()
+	 * }
+	 * </pre>
+	 */
+	@Override
+	Stream<BlankNodeOrIRI> getGraphNames();
+	
 }

http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/f0bdb07e/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 907300a..e0e1e4a 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,16 +17,20 @@
  */
 package org.apache.commons.rdf.rdf4j;
 
-import java.util.Optional;
 import java.util.Set;
+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.impl.ModelGraphImpl;
 import org.apache.commons.rdf.rdf4j.impl.RepositoryGraphImpl;
 import org.eclipse.rdf4j.model.Model;
 import org.eclipse.rdf4j.model.Resource;
 import org.eclipse.rdf4j.repository.Repository;
+import org.eclipse.rdf4j.repository.RepositoryConnection;
 
 
 /**
@@ -48,24 +52,56 @@ public interface RDF4JGraph extends Graph, RDF4JGraphLike<Triple> {
 	 * contexts, while retrieval (e.g. {@link #contains(Triple)}) will succeed
 	 * if the triple is in at least one of the specified contexts.
 	 * <p>
-	 * The context mask array may contain the {@link RDF4JBlankNodeOrIRI}
-	 * <code>null</code>, indicating the default context (the <em>default
-	 * graph</em> in RDF datasets).
+	 * The context mask array may contain <code>null</code>, indicating the
+	 * default context (the <em>default graph</em> in RDF datasets).
 	 * <p>
 	 * If the context mask is {@link Set#isEmpty()}, then this is a <em>union
 	 * graph</em> which triples reflect statements in any contexts. Triples
-	 * added to the graph will be added in the default context, e.g.
-	 * equivalent to <code>new Resource[1]{null}</code>) in RDF4J.
+	 * added to the graph will be added in the default context, e.g. equivalent
+	 * to <code>new Resource[1]{null}</code>) in RDF4J.
 	 * <p>
 	 * Note that the context mask itself cannot be <code>null</code>.
 	 * <p>
-	 * The returned set is an immutable copy, to specify a different mask, use 
+	 * The returned set is an immutable copy; to specify a different mask, use
 	 * {@link RDF4JTermFactory#asRDFTermGraph(Repository, Set)}.
 	 * 
-	 * @return The context mask as an array of {@link Resource}s, or
-	 *         {@link Optional#empty()} indicating the union graph (any
-	 *         context).
+	 * @return The context mask as an set of {@link BlankNodeOrIRI}s, which may
+	 *         contain the value <code>null</code>.
 	 */
 	public Set<RDF4JBlankNodeOrIRI<Resource>> getContextMask();
 	
+	/**
+	 * {@inheritDoc}
+	 * <p>
+	 * Note that the stream must be closed with {@link Stream#close()} to ensure
+	 * the underlying {@link RepositoryConnection} is closed.
+	 * <p>
+	 * This can generally achieved using a try-with-resources block, e.g.:
+	 * <pre>
+	 * int subjects;
+	 * try (Stream&lt;RDF4JTriple&gt; s : graph.stream()) {
+	 *   subjects = s.map(RDF4JTriple::getSubject).distinct().count()
+	 * }
+	 * </pre>
+	 */
+	@Override
+	Stream<RDF4JTriple> stream();
+
+	/**
+	 * {@inheritDoc}
+	 * <p>
+	 * Note that the stream must be closed with {@link Stream#close()} to ensure
+	 * the underlying {@link RepositoryConnection} is closed.
+	 * <p>
+	 * This can generally achieved using a try-with-resources block, e.g.:
+	 * <pre>
+	 * int subjects;
+	 * try (Stream&lt;RDF4JTriple&gt; s : graph.stream()) {
+	 *   subjects = s.map(RDF4JTriple::getSubject).distinct().count()
+	 * }
+	 * </pre>
+	 */	
+	@Override
+	Stream<RDF4JTriple> stream(BlankNodeOrIRI subject, IRI predicate, RDFTerm object);
+	
 }


[2/2] incubator-commonsrdf git commit: added asRDFTermGraph(Repository, Set<>

Posted by st...@apache.org.
added asRDFTermGraph(Repository, Set<>

.. and remove the array arguments as it becomes tricky with
null.


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

Branch: refs/heads/rdf4j
Commit: 69814ee5ae3f2393aa4fa14ea192ce3e06068f0d
Parents: f0bdb07
Author: Stian Soiland-Reyes <st...@apache.org>
Authored: Mon Oct 3 18:32:57 2016 +0100
Committer: Stian Soiland-Reyes <st...@apache.org>
Committed: Mon Oct 3 18:32:57 2016 +0100

----------------------------------------------------------------------
 .../commons/rdf/rdf4j/RDF4JTermFactory.java     | 25 ++++++++++----------
 1 file changed, 13 insertions(+), 12 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/69814ee5/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 a844a1a..54edaa7 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
@@ -17,9 +17,9 @@
  */
 package org.apache.commons.rdf.rdf4j;
 
-import java.util.Arrays;
+import java.util.Objects;
+import java.util.Set;
 import java.util.UUID;
-import java.util.stream.Collectors;
 
 // To avoid confusion, avoid importing
 // classes that are in both
@@ -325,19 +325,20 @@ public class RDF4JTermFactory implements RDFTermFactory {
 	 * @param repository
 	 *            RDF4J {@link Repository} to connect to.
 	 * @param contexts
+	 *            A {@link Set} of {@link BlankNodeOrIRI} specifying the
+	 *            graph names to use as a context. The set may include the value
+	 *            <code>null</code> to indicate the default graph. The empty set
+	 *            indicates any context, e.g. the <em>union graph</em>.
 	 * 
 	 * @return A {@link Graph} backed by the RDF4J repository.
 	 */
-	public RDF4JGraph asRDFTermGraph(Repository repository, BlankNodeOrIRI... contexts) {
-		if (contexts.length == 0) {
-			throw new IllegalArgumentException("At least one context must be specified. Use asRDFTermGraphUnion for union graph.");
-		}
-		Resource[] resources = new Resource[contexts.length];
-		for (int i=0; i<contexts.length; i++) {
-			resources[i] = (Resource) asValue(contexts[i]);
-		}
-		return new RepositoryGraphImpl(repository, false, true, resources);
-	}	
+	public RDF4JGraph asRDFTermGraph(Repository repository, Set<? extends BlankNodeOrIRI> contexts) {	
+		/** NOTE: asValue() deliberately CAN handle <code>null</code> */
+		Resource[] resources = contexts.stream()
+				.map(g -> (Resource) asValue(g)).toArray(Resource[]::new);
+		return new RepositoryGraphImpl(Objects.requireNonNull(repository), 
+				false, true, resources);		
+	}
 	
 	/**
 	 * Adapt an RDF4J {@link Repository} as a Commons RDF {@link Graph}.