commonsrdf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From st...@apache.org
Subject [41/50] incubator-commonsrdf git commit: Added GraphLike and renamed getTriples() -> stream()
Date Mon, 20 Jun 2016 08:58:35 GMT
Added GraphLike and renamed getTriples() -> stream()


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

Branch: refs/heads/parser-with-quads
Commit: b25af1363bac61ad0c088f92479ff6f44b821f82
Parents: bc639bb
Author: Stian Soiland-Reyes <stain@apache.org>
Authored: Thu Jun 2 12:10:13 2016 +0100
Committer: Stian Soiland-Reyes <stain@apache.org>
Committed: Thu Jun 2 12:10:13 2016 +0100

----------------------------------------------------------------------
 .../org/apache/commons/rdf/api/Dataset.java     | 12 +--
 .../java/org/apache/commons/rdf/api/Graph.java  | 10 +-
 .../org/apache/commons/rdf/api/GraphLike.java   | 97 ++++++++++++++++++++
 .../commons/rdf/api/AbstractGraphTest.java      | 20 ++--
 .../apache/commons/rdf/simple/DatasetImpl.java  | 17 ++--
 .../apache/commons/rdf/simple/GraphImpl.java    | 12 +--
 .../commons/rdf/simple/TestWritingGraph.java    |  8 +-
 7 files changed, 137 insertions(+), 39 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/b25af136/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 706366c..c809bc9 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
@@ -28,7 +28,7 @@ import java.util.stream.Stream;
  * <a href="http://www.w3.org/TR/rdf11-concepts/" >RDF-1.1 Concepts and Abstract
  * Syntax</a>, a W3C Recommendation published on 25 February 2014.
  */
-public interface Dataset extends AutoCloseable {
+public interface Dataset extends AutoCloseable, GraphLike<Quad, BlankNodeOrIRI, IRI, RDFTerm>
{
 
 	/**
 	 * Add a quad to the dataset, possibly mapping any of the components of the
@@ -144,7 +144,7 @@ public interface Dataset extends AutoCloseable {
 	 * 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
+	 * {@link Quad#getGraphName()} of all the {@link #stream()} of this
 	 * dataset (excluding the default graph).
 	 * <p>
 	 * The returned {@link Stream} SHOULD NOT contain duplicate graph names.
@@ -217,7 +217,7 @@ public interface Dataset extends AutoCloseable {
 	 *
 	 * @return A {@link Stream} over all of the quads in the dataset
 	 */
-	Stream<? extends Quad> getQuads();
+	Stream<? extends Quad> stream();
 
 	/**
 	 * Get all quads contained by the dataset matched with the pattern.
@@ -244,7 +244,7 @@ public interface Dataset extends AutoCloseable {
 	 *            The quad object (<code>null</code> is a wildcard)
 	 * @return A {@link Stream} over the matched quads.
 	 */
-	Stream<? extends Quad> getQuads(Optional<BlankNodeOrIRI> graphName, BlankNodeOrIRI
subject, IRI predicate,
+	Stream<? extends Quad> stream(Optional<BlankNodeOrIRI> graphName, BlankNodeOrIRI
subject, IRI predicate,
 			RDFTerm object);
 
 	/**
@@ -281,7 +281,7 @@ public interface Dataset extends AutoCloseable {
 	 */
 	@SuppressWarnings("unchecked")
 	default Iterable<Quad> iterate() throws ConcurrentModificationException, IllegalStateException
{
-		return ((Stream<Quad>) getQuads())::iterator;
+		return ((Stream<Quad>) stream())::iterator;
 	}
 
 	/**
@@ -333,6 +333,6 @@ public interface Dataset extends AutoCloseable {
 	@SuppressWarnings("unchecked")
 	default Iterable<Quad> iterate(Optional<BlankNodeOrIRI> graphName, BlankNodeOrIRI
subject, IRI predicate,
 			RDFTerm object) throws ConcurrentModificationException, IllegalStateException {
-		return ((Stream<Quad>) getQuads(graphName, subject, predicate, object))::iterator;
+		return ((Stream<Quad>) stream(graphName, subject, predicate, object))::iterator;
 	}
 }

http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/b25af136/api/src/main/java/org/apache/commons/rdf/api/Graph.java
----------------------------------------------------------------------
diff --git a/api/src/main/java/org/apache/commons/rdf/api/Graph.java b/api/src/main/java/org/apache/commons/rdf/api/Graph.java
index c421226..42ced98 100644
--- a/api/src/main/java/org/apache/commons/rdf/api/Graph.java
+++ b/api/src/main/java/org/apache/commons/rdf/api/Graph.java
@@ -27,7 +27,7 @@ import java.util.stream.Stream;
  * href="http://www.w3.org/TR/rdf11-concepts/" >RDF-1.1 Concepts and Abstract
  * Syntax</a>, a W3C Recommendation published on 25 February 2014.
  */
-public interface Graph extends AutoCloseable {
+public interface Graph extends AutoCloseable,GraphLike<Triple, BlankNodeOrIRI, IRI, RDFTerm>
{
 
     /**
      * Add a triple to the graph, possibly mapping any of the components of the
@@ -128,7 +128,7 @@ public interface Graph extends AutoCloseable {
      *
      * @return A {@link Stream} over all of the triples in the graph
      */
-    Stream<? extends Triple> getTriples();
+    Stream<? extends Triple> stream();
 
     /**
      * Get all triples contained by the graph matched with the pattern.
@@ -148,7 +148,7 @@ public interface Graph extends AutoCloseable {
      * @param object    The triple object (null is a wildcard)
      * @return A {@link Stream} over the matched triples.
      */
-    Stream<? extends Triple> getTriples(BlankNodeOrIRI subject, IRI predicate,
+    Stream<? extends Triple> stream(BlankNodeOrIRI subject, IRI predicate,
                                         RDFTerm object);
 
     /**
@@ -184,7 +184,7 @@ public interface Graph extends AutoCloseable {
     @SuppressWarnings("unchecked")
     default Iterable<Triple> iterate()
             throws ConcurrentModificationException, IllegalStateException {
-        return ((Stream<Triple>)getTriples())::iterator;
+        return ((Stream<Triple>)stream())::iterator;
     }
 
     /**
@@ -231,6 +231,6 @@ public interface Graph extends AutoCloseable {
     default Iterable<Triple> iterate(
             BlankNodeOrIRI subject, IRI predicate, RDFTerm object)
         throws ConcurrentModificationException, IllegalStateException {
-        return ((Stream<Triple>) getTriples(subject, predicate, object))::iterator;
+        return ((Stream<Triple>) stream(subject, predicate, object))::iterator;
     }
 }

http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/b25af136/api/src/main/java/org/apache/commons/rdf/api/GraphLike.java
----------------------------------------------------------------------
diff --git a/api/src/main/java/org/apache/commons/rdf/api/GraphLike.java b/api/src/main/java/org/apache/commons/rdf/api/GraphLike.java
new file mode 100644
index 0000000..57b55cb
--- /dev/null
+++ b/api/src/main/java/org/apache/commons/rdf/api/GraphLike.java
@@ -0,0 +1,97 @@
+/**
+ * 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.api;
+
+import java.util.ConcurrentModificationException;
+import java.util.stream.Stream;
+
+/**
+ * A "graph-like" interface that contains {@link TripleLike} statements.
+ * <p>
+ * Extended by {@link Graph} (for {@link Triple}) and {@link Dataset} (for
+ * {@link Quad}).
+ * <p>
+ * Unlike {@link Graph} and {@link Dataset}, this interface can support
+ * with generalised {@link TripleLike} or {@link QuadLike} statements, and
+ * does not include semantics like {@link #size()} or the requirement of
+ * mapping {@link RDFTerm} instances from different implementations.
+ * 
+ * @see Graph
+ * @see Dataset
+ * @see TripleLike
+ */
+public interface GraphLike<T extends TripleLike<S, P, O>, S extends RDFTerm, P extends
RDFTerm, O extends RDFTerm> {
+
+	/**
+	 * Add a statement.
+	 * 
+	 * @param statement
+	 *            The TripleLike statement to add
+	 */
+	void add(T statement);
+
+	/**
+	 * Check if statement is contained.
+	 * 
+	 * @param statement
+	 *            The {@link TripleLike} statement to chec
+	 * @return True if the statement is contained
+	 */
+	boolean contains(T statement);
+
+	/**
+	 * Add a statement.
+	 * 
+	 * @param statement
+	 *            The TripleLike statement to add
+	 */
+	void remove(T statement);
+
+	/**
+	 * Remove all statements.
+	 */
+	void clear();
+
+	/**
+	 * Number of statements.
+	 * 
+	 * @return
+	 */
+	long size();
+
+	/**
+	 * Return a Stream of contained statements.
+	 * 
+	 * @return A {@link Stream} of {@link TripleLike} statements.
+	 */
+	Stream<? extends T> stream();
+
+	/**
+	 * Iterate over contained statements.
+	 *  
+	 * @return An {@link Iterable} of {@link TripleLike} statements.
+	 * 
+     * @throws IllegalStateException
+     *             if the {@link Iterable} has been reused
+     * @throws ConcurrentModificationException
+     *             if a concurrency conflict occurs while the Iterator is
+     *             active.
+	 */
+	Iterable<T> iterate() throws ConcurrentModificationException, IllegalStateException;
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/b25af136/api/src/test/java/org/apache/commons/rdf/api/AbstractGraphTest.java
----------------------------------------------------------------------
diff --git a/api/src/test/java/org/apache/commons/rdf/api/AbstractGraphTest.java b/api/src/test/java/org/apache/commons/rdf/api/AbstractGraphTest.java
index 9029cc4..15773df 100644
--- a/api/src/test/java/org/apache/commons/rdf/api/AbstractGraphTest.java
+++ b/api/src/test/java/org/apache/commons/rdf/api/AbstractGraphTest.java
@@ -177,7 +177,7 @@ public abstract class AbstractGraphTest {
 
         assertTrue(graph.contains(alice, knows, bob));
 
-        Optional<? extends Triple> first = graph.getTriples().skip(4)
+        Optional<? extends Triple> first = graph.stream().skip(4)
                 .findFirst();
         Assume.assumeTrue(first.isPresent());
         Triple existingTriple = first.get();
@@ -218,7 +218,7 @@ public abstract class AbstractGraphTest {
         graph.remove(alice, knows, bob);
         assertEquals(shrunkSize, graph.size());
 
-        Optional<? extends Triple> anyTriple = graph.getTriples().findAny();
+        Optional<? extends Triple> anyTriple = graph.stream().findAny();
         Assume.assumeTrue(anyTriple.isPresent());
 
         Triple otherTriple = anyTriple.get();
@@ -242,9 +242,9 @@ public abstract class AbstractGraphTest {
     @Test
     public void getTriples() throws Exception {
 
-        long tripleCount = graph.getTriples().count();
+        long tripleCount = graph.stream().count();
         assertTrue(tripleCount > 0);
-        assertTrue(graph.getTriples().allMatch(t -> graph.contains(t)));
+        assertTrue(graph.stream().allMatch(t -> graph.contains(t)));
         // Check exact count
         Assume.assumeNotNull(bnode1, bnode2, aliceName, bobName, secretClubName,
                 companyName, bobNameTriple);
@@ -254,15 +254,15 @@ public abstract class AbstractGraphTest {
     @Test
     public void getTriplesQuery() throws Exception {
 
-        long aliceCount = graph.getTriples(alice, null, null).count();
+        long aliceCount = graph.stream(alice, null, null).count();
         assertTrue(aliceCount > 0);
         Assume.assumeNotNull(aliceName);
         assertEquals(3, aliceCount);
 
         Assume.assumeNotNull(bnode1, bnode2, bobName, companyName, secretClubName);
-        assertEquals(4, graph.getTriples(null, name, null).count());
+        assertEquals(4, graph.stream(null, name, null).count());
         Assume.assumeNotNull(bnode1);
-        assertEquals(3, graph.getTriples(null, member, null).count());
+        assertEquals(3, graph.stream(null, member, null).count());
     }
 
     /**
@@ -287,14 +287,14 @@ public abstract class AbstractGraphTest {
         // Find a secret organizations
         assertEquals(
                 "\"The Secret Club\"",
-                graph.getTriples(null, knows, null)
+                graph.stream(null, knows, null)
                         // Find One-way "knows"
                         .filter(t -> !graph.contains(
                                 (BlankNodeOrIRI) t.getObject(), knows,
                                 t.getSubject()))
                         .map(knowsTriple -> graph
                                 // and those they know, what are they member of?
-                                .getTriples(
+                                .stream(
                                         (BlankNodeOrIRI) knowsTriple
                                                 .getObject(), member, null)
                                         // keep those which first-guy is a member of
@@ -305,7 +305,7 @@ public abstract class AbstractGraphTest {
                                 .get().getObject())
                                 // then look up the name of that org
                         .map(org -> graph
-                                .getTriples((BlankNodeOrIRI) org, name, null)
+                                .stream((BlankNodeOrIRI) org, name, null)
                                 .findFirst().get().getObject().ntriplesString())
                         .findFirst().get());
 

http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/b25af136/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 af9b01d..9de358f 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
@@ -120,7 +120,7 @@ final class DatasetImpl implements Dataset {
 
 	@Override
 	public boolean contains(Optional<BlankNodeOrIRI> graphName, BlankNodeOrIRI subject,
IRI predicate, RDFTerm object) {
-		return getQuads(graphName, subject, predicate, object).findAny().isPresent();
+		return stream(graphName, subject, predicate, object).findAny().isPresent();
 	}
     
     @Override
@@ -129,12 +129,12 @@ final class DatasetImpl implements Dataset {
     }
     
 	@Override
-	public Stream<Quad> getQuads() {
+	public Stream<Quad> stream() {
         return quads.parallelStream().unordered();
 	}
 
 	@Override
-	public Stream<Quad> getQuads(Optional<BlankNodeOrIRI> graphName, BlankNodeOrIRI
subject, IRI predicate,
+	public Stream<Quad> stream(Optional<BlankNodeOrIRI> graphName, BlankNodeOrIRI
subject, IRI predicate,
 			RDFTerm object) {
 		final Optional<BlankNodeOrIRI> newGraphName = graphName.map(g -> (BlankNodeOrIRI)internallyMap(g));
         final BlankNodeOrIRI newSubject = (BlankNodeOrIRI) internallyMap(subject);
@@ -160,12 +160,12 @@ final class DatasetImpl implements Dataset {
 	}    
 
     private Stream<Quad> getQuads(final Predicate<Quad> filter) {
-        return getQuads().filter(filter);
+        return stream().filter(filter);
     }
 
 	@Override
 	public void remove(Optional<BlankNodeOrIRI> graphName, BlankNodeOrIRI subject, IRI
predicate, RDFTerm object) {
-        Stream<Quad> toRemove = getQuads(graphName, subject, predicate, object);
+        Stream<Quad> toRemove = stream(graphName, subject, predicate, object);
         for (Quad t : toRemove.collect(Collectors.toList())) {
             // Avoid ConcurrentModificationException in ArrayList
             remove(t);
@@ -184,7 +184,7 @@ final class DatasetImpl implements Dataset {
 
     @Override
     public String toString() {
-        String s = getQuads().limit(TO_STRING_MAX).map(Object::toString)
+        String s = stream().limit(TO_STRING_MAX).map(Object::toString)
                 .collect(Collectors.joining("\n"));
         if (size() > TO_STRING_MAX) {
             return s + "\n# ... +" + (size() - TO_STRING_MAX) + " more";
@@ -206,7 +206,7 @@ final class DatasetImpl implements Dataset {
 	public Optional<Graph> getGraph(BlankNodeOrIRI graphName) {
 		// NOTE: Always returns a new copy
 		Graph g = new GraphImpl(factory);
-		getQuads(Optional.ofNullable(graphName), null, null, null)
+		stream(Optional.ofNullable(graphName), null, null, null)
 			.map(Quad::asTriple)
 			.sequential()
 			.forEach(g::add);
@@ -216,11 +216,12 @@ final class DatasetImpl implements Dataset {
 	@Override
 	public Stream<BlankNodeOrIRI> getGraphNames() {
 		// Not very efficient..
-		return getQuads()
+		return stream()
 				.map(Quad::getGraphName)
 				.filter(Optional::isPresent).map(Optional::get)
 				.distinct();
 	}
 
 
+
 }

http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/b25af136/simple/src/main/java/org/apache/commons/rdf/simple/GraphImpl.java
----------------------------------------------------------------------
diff --git a/simple/src/main/java/org/apache/commons/rdf/simple/GraphImpl.java b/simple/src/main/java/org/apache/commons/rdf/simple/GraphImpl.java
index 024da21..1f19d57 100644
--- a/simple/src/main/java/org/apache/commons/rdf/simple/GraphImpl.java
+++ b/simple/src/main/java/org/apache/commons/rdf/simple/GraphImpl.java
@@ -111,7 +111,7 @@ final class GraphImpl implements Graph {
     @Override
     public boolean contains(BlankNodeOrIRI subject, IRI predicate,
                             RDFTerm object) {
-        return getTriples(subject, predicate, object).findFirst().isPresent();
+        return stream(subject, predicate, object).findFirst().isPresent();
     }
 
     @Override
@@ -120,12 +120,12 @@ final class GraphImpl implements Graph {
     }
 
     @Override
-    public Stream<Triple> getTriples() {
+    public Stream<Triple> stream() {
         return triples.parallelStream().unordered();
     }
 
     @Override
-    public Stream<Triple> getTriples(final BlankNodeOrIRI subject,
+    public Stream<Triple> stream(final BlankNodeOrIRI subject,
                                      final IRI predicate, final RDFTerm object) {
         final BlankNodeOrIRI newSubject = (BlankNodeOrIRI) internallyMap(subject);
         final IRI newPredicate = (IRI) internallyMap(predicate);
@@ -148,12 +148,12 @@ final class GraphImpl implements Graph {
     }
 
     private Stream<Triple> getTriples(final Predicate<Triple> filter) {
-        return getTriples().filter(filter);
+        return stream().filter(filter);
     }
 
     @Override
     public void remove(BlankNodeOrIRI subject, IRI predicate, RDFTerm object) {
-        Stream<Triple> toRemove = getTriples(subject, predicate, object);
+        Stream<Triple> toRemove = stream(subject, predicate, object);
         for (Triple t : toRemove.collect(Collectors.toList())) {
             // Avoid ConcurrentModificationException in ArrayList
             remove(t);
@@ -172,7 +172,7 @@ final class GraphImpl implements Graph {
 
     @Override
     public String toString() {
-        String s = getTriples().limit(TO_STRING_MAX).map(Object::toString)
+        String s = stream().limit(TO_STRING_MAX).map(Object::toString)
                 .collect(Collectors.joining("\n"));
         if (size() > TO_STRING_MAX) {
             return s + "\n# ... +" + (size() - TO_STRING_MAX) + " more";

http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/b25af136/simple/src/test/java/org/apache/commons/rdf/simple/TestWritingGraph.java
----------------------------------------------------------------------
diff --git a/simple/src/test/java/org/apache/commons/rdf/simple/TestWritingGraph.java b/simple/src/test/java/org/apache/commons/rdf/simple/TestWritingGraph.java
index 9862770..6cd8f52 100644
--- a/simple/src/test/java/org/apache/commons/rdf/simple/TestWritingGraph.java
+++ b/simple/src/test/java/org/apache/commons/rdf/simple/TestWritingGraph.java
@@ -102,7 +102,7 @@ public class TestWritingGraph {
     public void countQuery() {
         IRI subject = factory.createIRI("subj");
         IRI predicate = factory.createIRI("pred");
-        long count = graph.getTriples(subject, predicate, null).unordered()
+        long count = graph.stream(subject, predicate, null).unordered()
                 .parallel().count();
         //System.out.println("Counted - " + count);
         assertEquals(count, TRIPLES);
@@ -123,7 +123,7 @@ public class TestWritingGraph {
             graphFile.toFile().deleteOnExit();
         }
 
-        Stream<CharSequence> stream = graph.getTriples().map(TestWritingGraph::tripleAsString);
+        Stream<CharSequence> stream = graph.stream().map(TestWritingGraph::tripleAsString);
         Files.write(graphFile, stream::iterator, StandardCharsets.UTF_8);
     }
 
@@ -139,7 +139,7 @@ public class TestWritingGraph {
         IRI subject = factory.createIRI("subj");
         IRI predicate = factory.createIRI("pred");
         Stream<CharSequence> stream = graph
-                .getTriples(subject, predicate, null).map(TestWritingGraph::tripleAsString);
+                .stream(subject, predicate, null).map(TestWritingGraph::tripleAsString);
         Files.write(graphFile, stream::iterator, StandardCharsets.UTF_8);
 
     }
@@ -156,7 +156,7 @@ public class TestWritingGraph {
         IRI subject = factory.createIRI("nonexistent");
         IRI predicate = factory.createIRI("pred");
         Stream<CharSequence> stream = graph
-                .getTriples(subject, predicate, null).map(Object::toString);
+                .stream(subject, predicate, null).map(Object::toString);
         Files.write(graphFile, stream::iterator, StandardCharsets.UTF_8);
 
     }


Mime
View raw message