commonsrdf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From st...@apache.org
Subject [16/19] incubator-commonsrdf git commit: Added DatasetGraphView
Date Mon, 05 Sep 2016 15:54:00 GMT
Added DatasetGraphView


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

Branch: refs/heads/master
Commit: 580484c9d7d02d34b64e5276744c1f644dbbd2f2
Parents: 14e505e
Author: Stian Soiland-Reyes <stain@apache.org>
Authored: Tue Jun 14 16:44:24 2016 +0100
Committer: Stian Soiland-Reyes <stain@apache.org>
Committed: Tue Jun 14 16:44:24 2016 +0100

----------------------------------------------------------------------
 .../commons/rdf/simple/DatasetGraphView.java    | 138 +++++++++++++++++++
 .../apache/commons/rdf/simple/DatasetImpl.java  |   8 +-
 2 files changed, 139 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/580484c9/simple/src/main/java/org/apache/commons/rdf/simple/DatasetGraphView.java
----------------------------------------------------------------------
diff --git a/simple/src/main/java/org/apache/commons/rdf/simple/DatasetGraphView.java b/simple/src/main/java/org/apache/commons/rdf/simple/DatasetGraphView.java
new file mode 100644
index 0000000..a347c3e
--- /dev/null
+++ b/simple/src/main/java/org/apache/commons/rdf/simple/DatasetGraphView.java
@@ -0,0 +1,138 @@
+/**
+ * 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.simple;
+
+import java.util.Optional;
+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.Quad;
+import org.apache.commons.rdf.api.RDFTerm;
+import org.apache.commons.rdf.api.Triple;
+
+/**
+ * A {@link Graph} view on a {@link Dataset}.
+ * <p>
+ * This view is backed by a {@link Dataset}, and can be constructed in two ways:
+ * 
+ * <dl>
+ * <dt>{@link #DatasetGraphView(Dataset)}</dt>
+ * <dd>Expose a <em>union graph</em> view of the Dataset, where all the
+ * {@link Quad}s of the Dataset is represented as a {@link Triple}. Adding
+ * triples will add them to the <em>default graph</em>, while removing triples
+ * will remove from all graphs.</dd>
+* 
+ * <dt>{@link #DatasetGraphView(Dataset, IRI)}</dt>
+ * <dd>Expose a particular graph of the Dataset, either named by an {@link IRI}, a
+ * {@link BlankNode}, or  <code>null</code> for the <em>default graph</em>.</dd>
+ * </dl>
+ * <p>
+ * Changes in the Graph are reflected directly in the Dataset and vice versa.  
+ * This class is thread-safe is the underlying Dataset is thread-safe.
+ */
+public class DatasetGraphView implements Graph {
+
+	private final boolean unionGraph;
+	private final BlankNodeOrIRI namedGraph;
+	private final Dataset dataset;
+
+	public DatasetGraphView(Dataset dataset) {
+		this.dataset = dataset;
+		this.namedGraph = null;
+		this.unionGraph = true;
+	}
+
+	public DatasetGraphView(Dataset dataset, BlankNodeOrIRI namedGraph) {
+		this.dataset = dataset;
+		this.namedGraph = namedGraph;
+		this.unionGraph = false;
+	}
+
+	@Override
+	public void close() throws Exception {
+		dataset.close();
+
+	}
+
+	@Override
+	public void add(Triple triple) {
+		dataset.add(namedGraph, triple.getSubject(), triple.getPredicate(), triple.getObject());
+	}
+
+	@Override
+	public void add(BlankNodeOrIRI subject, IRI predicate, RDFTerm object) {
+		dataset.add(namedGraph, subject, predicate, object);
+	}
+
+	@Override
+	public boolean contains(Triple triple) {
+		return dataset.contains(unionOrNamedGraph(), triple.getSubject(), triple.getPredicate(),
triple.getObject());
+	}
+
+	private Optional<BlankNodeOrIRI> unionOrNamedGraph() {
+		if (unionGraph) {
+			return null;
+		}
+		return Optional.ofNullable(namedGraph);
+	}
+
+	@Override
+	public boolean contains(BlankNodeOrIRI subject, IRI predicate, RDFTerm object) {
+		return dataset.contains(unionOrNamedGraph(), subject, predicate, object);
+	}
+
+	@Override
+	public void remove(Triple triple) {
+		dataset.remove(unionOrNamedGraph(), triple.getSubject(), triple.getPredicate(), triple.getObject());
+	}
+
+	@Override
+	public void remove(BlankNodeOrIRI subject, IRI predicate, RDFTerm object) {
+		dataset.remove(unionOrNamedGraph(), subject, predicate, object);
+	}
+
+	@Override
+	public void clear() {
+		dataset.remove(unionOrNamedGraph(), null, null, null);
+	}
+
+	@Override
+	public long size() {
+		return stream().count();
+	}
+
+	@Override
+	public Stream<? extends Triple> stream() {
+		return stream(null, null, null);
+	}
+
+	@Override
+	public Stream<? extends Triple> stream(BlankNodeOrIRI subject, IRI predicate, RDFTerm
object) {
+		Stream<Triple> stream = dataset.stream(unionOrNamedGraph(), subject, predicate, object).map(Quad::asTriple);
+		if (unionGraph) {
+			// remove duplicates
+			return stream.distinct();
+		}
+		return stream;
+	}
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/580484c9/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 9de358f..c372012 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
@@ -204,13 +204,7 @@ final class DatasetImpl implements Dataset {
 
 	@Override
 	public Optional<Graph> getGraph(BlankNodeOrIRI graphName) {
-		// NOTE: Always returns a new copy
-		Graph g = new GraphImpl(factory);
-		stream(Optional.ofNullable(graphName), null, null, null)
-			.map(Quad::asTriple)
-			.sequential()
-			.forEach(g::add);
-		return Optional.of(g);
+		return Optional.of(new DatasetGraphView(this, graphName));
 	}
 
 	@Override


Mime
View raw message