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/04/13 03:12:19 UTC
[2/2] incubator-commonsrdf git commit: add getGraph(),
getGraph(graphName), getGraphNames()
add getGraph(), getGraph(graphName), getGraphNames()
Project: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/commit/839a6425
Tree: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/tree/839a6425
Diff: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/diff/839a6425
Branch: refs/heads/quad
Commit: 839a6425fdb4985ef1594af87c6e12dcef8ec519
Parents: bfa15b8
Author: Stian Soiland-Reyes <st...@apache.org>
Authored: Wed Apr 13 02:11:36 2016 +0100
Committer: Stian Soiland-Reyes <st...@apache.org>
Committed: Wed Apr 13 02:11:36 2016 +0100
----------------------------------------------------------------------
.../org/apache/commons/rdf/api/Dataset.java | 65 ++++++++++++++++++++
.../apache/commons/rdf/simple/DatasetImpl.java | 30 +++++++++
2 files changed, 95 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/839a6425/api/src/main/java/org/apache/commons/rdf/api/Dataset.java
----------------------------------------------------------------------
diff --git a/api/src/main/java/org/apache/commons/rdf/api/Dataset.java b/api/src/main/java/org/apache/commons/rdf/api/Dataset.java
index 3d24022..706366c 100644
--- a/api/src/main/java/org/apache/commons/rdf/api/Dataset.java
+++ b/api/src/main/java/org/apache/commons/rdf/api/Dataset.java
@@ -99,6 +99,71 @@ public interface Dataset extends AutoCloseable {
}
/**
+ * Get the default graph of this dataset.
+ * <p>
+ * The {@link Triple}s of the default graph are equivalent to the
+ * {@link Quad}s in this Dataset which has the {@link Quad#getGraphName()}
+ * set to {@link Optional#empty()}.
+ * <p>
+ * It is unspecified if modifications to the returned Graph are reflected in
+ * this Dataset.
+ * <p>
+ * The returned graph MAY be empty.
+ *
+ * @see #getGraph(BlankNodeOrIRI)
+ * @return The default graph of this Dataset
+ */
+ Graph getGraph();
+
+ /**
+ * Get a named graph in this dataset.
+ * <p>
+ * The {@link Triple}s of the named graph are equivalent to the
+ * the Quads of this Dataset which has the
+ * {@link Quad#getGraphName()} equal to the provided <code>graphName</code>, or
+ * equal to {@link Optional#empty()} if the provided <code>graphName</code> is
+ * <code>null</code>.
+ * <p>
+ * It is unspecified if modifications to the returned Graph are reflected in
+ * this Dataset.
+ * <p>
+ * It is unspecified if requesting an unknown or empty graph will return
+ * {@link Optional#empty()} or create a new empty {@link Graph}.
+ *
+ * @see #getGraph()
+ * @see #getGraphNames()
+ * @param graphName
+ * The name of the graph, or <code>null</code> for the default
+ * graph.
+ * @return The named Graph, or {@link Optional#empty()} if the dataset do
+ * not contain the named graph.
+ */
+ Optional<Graph> getGraph(BlankNodeOrIRI graphName);
+
+ /**
+ * Get the graph names in this Dataset.
+ * <p>
+ * The set of returned graph names is equivalent to the set of unique
+ * {@link Quad#getGraphName()} of all the {@link #getQuads()} of this
+ * dataset (excluding the default graph).
+ * <p>
+ * The returned {@link Stream} SHOULD NOT contain duplicate graph names.
+ * <p>
+ * The graph names can be used with {@link #getGraph(BlankNodeOrIRI)} to
+ * retrieve the corresponding {@link Graph}, however callers should be aware
+ * of any concurrent modifications to the Dataset may cause such calls to
+ * return {@link Optional#empty()}.
+ * <p>
+ * Note that a Dataset always contains a <strong>default graph</strong>
+ * which is not named, and thus is not represented in the returned Stream.
+ * The default graph is accessible via {@link #getGraph()} or by using
+ * {@link Optional#empty()} in the Quad access methods).
+ *
+ * @return A {@link Stream} of the graph names of this Dataset.
+ */
+ Stream<BlankNodeOrIRI> getGraphNames();
+
+ /**
* Remove a concrete quad from the dataset.
*
* @param quad
http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/839a6425/simple/src/main/java/org/apache/commons/rdf/simple/DatasetImpl.java
----------------------------------------------------------------------
diff --git a/simple/src/main/java/org/apache/commons/rdf/simple/DatasetImpl.java b/simple/src/main/java/org/apache/commons/rdf/simple/DatasetImpl.java
index 63d4025..af9b01d 100644
--- a/simple/src/main/java/org/apache/commons/rdf/simple/DatasetImpl.java
+++ b/simple/src/main/java/org/apache/commons/rdf/simple/DatasetImpl.java
@@ -28,6 +28,7 @@ import java.util.stream.Stream;
import org.apache.commons.rdf.api.BlankNode;
import org.apache.commons.rdf.api.BlankNodeOrIRI;
import org.apache.commons.rdf.api.Dataset;
+import org.apache.commons.rdf.api.Graph;
import org.apache.commons.rdf.api.IRI;
import org.apache.commons.rdf.api.Literal;
import org.apache.commons.rdf.api.Quad;
@@ -192,5 +193,34 @@ final class DatasetImpl implements Dataset {
}
}
+ @Override
+ public void close() {
+ }
+
+ @Override
+ public Graph getGraph() {
+ return getGraph(null).get();
+ }
+
+ @Override
+ public Optional<Graph> getGraph(BlankNodeOrIRI graphName) {
+ // NOTE: Always returns a new copy
+ Graph g = new GraphImpl(factory);
+ getQuads(Optional.ofNullable(graphName), null, null, null)
+ .map(Quad::asTriple)
+ .sequential()
+ .forEach(g::add);
+ return Optional.of(g);
+ }
+
+ @Override
+ public Stream<BlankNodeOrIRI> getGraphNames() {
+ // Not very efficient..
+ return getQuads()
+ .map(Quad::getGraphName)
+ .filter(Optional::isPresent).map(Optional::get)
+ .distinct();
+ }
+
}