commonsrdf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From st...@apache.org
Subject [3/4] incubator-commonsrdf git commit: COMMONSRDF-12 add also graph.iterate(s, p, o)
Date Mon, 13 Apr 2015 09:02:10 GMT
COMMONSRDF-12 add also graph.iterate(s,p,o)

.. and its test


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

Branch: refs/heads/master
Commit: aeaef1cda9619187b6704ad17bcb753d1a81404a
Parents: 73dba32
Author: Stian Soiland-Reyes <stain@apache.org>
Authored: Mon Apr 13 09:55:41 2015 +0100
Committer: Stian Soiland-Reyes <stain@apache.org>
Committed: Mon Apr 13 10:00:57 2015 +0100

----------------------------------------------------------------------
 .../java/org/apache/commons/rdf/api/Graph.java  | 49 ++++++++++++++++++--
 .../commons/rdf/api/AbstractGraphTest.java      | 25 ++++++++--
 2 files changed, 67 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/aeaef1cd/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 3b0bbd6..693e7e0 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
@@ -152,9 +152,9 @@ public interface Graph extends AutoCloseable {
     /**
      * Get an Iterable for iterating over all triples in the graph.
      * <p>
-     * This method is meant to be used with a Java for-each loop, e.g.: 
+     * This method is meant to be used with a Java for-each loop, e.g.:
      * <code>
-     *  for (Triple t : GraphUtil.iterate(graph)) {
+     *  for (Triple t : graph.iterate()) {
      *      System.out.println(t);
      *  }
      * </code>
@@ -180,8 +180,51 @@ public interface Graph extends AutoCloseable {
      *             active.
      */
     @SuppressWarnings("unchecked")
-    default Iterable<Triple> iterate() 
+    default Iterable<Triple> iterate()
             throws ConcurrentModificationException, IllegalStateException {
         return ((Stream<Triple>)getTriples())::iterator;
     }
+
+    /**
+     * Get an Iterable for iterating over the triples in the graph that
+     * match the pattern.
+     * <p>
+     * This method is meant to be used with a Java for-each loop, e.g.:
+     * <code>
+     *  IRI alice = factory.createIRI("http://example.com/alice");
+     *  IRI knows = factory.createIRI("http://xmlns.com/foaf/0.1/");
+     *  for (Triple t : graph.iterate(alice, knows, null)) {
+     *      System.out.println(t.getObject());
+     *  }
+     * </code>
+     * The behaviour of the iterator is not specified if {@link #add(Triple)},
+     * {@link #remove(Triple)} or {@link #clear()}, are called on the
+     * {@link Graph} before it terminates. It is undefined if the returned
+     * {@link Iterator} supports the {@link Iterator#remove()} method.
+     * <p>
+     * Implementations may throw {@link ConcurrentModificationException} from
+     * Iterator methods if they detect a concurrency conflict while the Iterator
+     * is active.
+     * <p>
+     * The {@link Iterable#iterator()} must only be called once, that is the
+     * Iterable must only be iterated over once. A {@link IllegalStateException}
+     * may be thrown on attempt to reuse the Iterable.
+     *
+     * @param subject   The triple subject (null is a wildcard)
+     * @param predicate The triple predicate (null is a wildcard)
+     * @param object    The triple object (null is a wildcard)
+     * @return A {@link Iterable} that returns {@link Iterator} over the
+     *         matching triples in the graph
+     * @throws IllegalStateException
+     *             if the {@link Iterable} has been reused
+     * @throws ConcurrentModificationException
+     *             if a concurrency conflict occurs while the Iterator is
+     *             active.
+     */
+    @SuppressWarnings("unchecked")
+    default Iterable<Triple> iterate(
+            BlankNodeOrIRI subject, IRI predicate, RDFTerm object)
+        throws ConcurrentModificationException, IllegalStateException {
+        return ((Stream<Triple>) getTriples(subject, predicate, object))::iterator;
+    }
 }

http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/aeaef1cd/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 3405ab8..627f41a 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
@@ -17,7 +17,9 @@
  */
 package org.apache.commons.rdf.api;
 
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
 
 import java.util.ArrayList;
 import java.util.Iterator;
@@ -138,7 +140,7 @@ public abstract class AbstractGraphTest {
     }
 
     @Test
-    public void iterable() throws Exception {
+    public void iterate() throws Exception {
 
         Assume.assumeTrue(graph.size() > 0);
 
@@ -146,7 +148,7 @@ public abstract class AbstractGraphTest {
         for (Triple t : graph.iterate()) {
             triples.add(t);
         }
-        assertEquals(graph.size(), (long)triples.size());
+        assertEquals(graph.size(), triples.size());
         if (bobNameTriple != null) {
             assertTrue(triples.contains(bobNameTriple));
         }
@@ -166,7 +168,22 @@ public abstract class AbstractGraphTest {
         }
         assertEquals(graph.size(), count);
     }
-    
+
+    @Test
+    public void iterateFilter() throws Exception {
+        List<RDFTerm> friends = new ArrayList<>();
+        IRI alice = factory.createIRI("http://example.com/alice");
+        IRI knows = factory.createIRI("http://xmlns.com/foaf/0.1/knows");
+        for (Triple t : graph.iterate(alice, knows, null)) {
+            friends.add(t.getObject());
+        }
+        assertEquals(1, friends.size());
+        assertEquals(bob, friends.get(0));
+
+        // .. can we iterate over zero hits?
+        assertFalse(graph.iterate(bob, knows, alice).iterator().hasNext());
+    }
+
     @Test
     public void contains() throws Exception {
         assertFalse(graph.contains(bob, knows, alice)); // or so he claims..


Mime
View raw message