You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@clerezza.apache.org by fl...@apache.org on 2014/09/23 20:06:55 UTC

[1/2] git commit: add partial graph context functionnality

Repository: clerezza
Updated Branches:
  refs/heads/CLEREZZA-950 [created] 418222808


add partial graph context functionnality


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

Branch: refs/heads/CLEREZZA-950
Commit: a0842b683533db2b273576da618405344c8d5ac4
Parents: 3898f17
Author: Florent André <fl...@apache.org>
Authored: Tue Sep 23 20:04:07 2014 +0200
Committer: Florent André <fl...@apache.org>
Committed: Tue Sep 23 20:04:07 2014 +0200

----------------------------------------------------------------------
 .../apache/clerezza/rdf/utils/GraphNode.java    | 64 +++++++++++++-------
 .../clerezza/rdf/utils/TestGraphNode.java       | 29 +++++++++
 2 files changed, 71 insertions(+), 22 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/clerezza/blob/a0842b68/rdf.utils/src/main/java/org/apache/clerezza/rdf/utils/GraphNode.java
----------------------------------------------------------------------
diff --git a/rdf.utils/src/main/java/org/apache/clerezza/rdf/utils/GraphNode.java b/rdf.utils/src/main/java/org/apache/clerezza/rdf/utils/GraphNode.java
index 487d7d2..5fde21a 100644
--- a/rdf.utils/src/main/java/org/apache/clerezza/rdf/utils/GraphNode.java
+++ b/rdf.utils/src/main/java/org/apache/clerezza/rdf/utils/GraphNode.java
@@ -96,22 +96,39 @@ public class GraphNode {
      * @return the context of the node represented by the instance
      */
     public Graph getNodeContext() {
+        return getNodeContext(true, true);
+    }
+    
+    /**
+     * This node context can selectively contains the triples containing where the node
+     * is subject or object and recursively the context of the b-nodes in any
+     * of these statements.
+     *
+     * The triples in the Graph returned by this method contain the same bnode
+     * instances as in the original graph.
+     *
+     * @param forward context with only this node as subject
+     * @param backward context with only this node as object
+     * 
+     * @return the context of the node represented by the instance
+     */
+    public Graph getNodeContext(boolean forward, boolean backward) {
         Lock l = readLock();
         l.lock();
         try {
             final HashSet<Resource> dontExpand = new HashSet<Resource>();
             dontExpand.add(resource);
             if (resource instanceof UriRef) {
-                return getContextOf((UriRef) resource, dontExpand).getGraph();
+                return getContextOf((UriRef) resource, dontExpand, forward, backward).getGraph();
             }
-            return getContextOf(resource, dontExpand).getGraph();
+            return getContextOf(resource, dontExpand, forward, backward).getGraph();
         } finally {
             l.unlock();
         }
 
     }
 
-    private MGraph getContextOf(UriRef node, final Set<Resource> dontExpand) {
+    private MGraph getContextOf(UriRef node, final Set<Resource> dontExpand, boolean forward, boolean backward) {
         final String uriPrefix = node.getUnicodeString()+'#';
         return getContextOf(node, dontExpand, new Acceptor() {
 
@@ -125,7 +142,7 @@ public class GraphNode {
                 }
                 return false;
             }
-        });
+        }, forward, backward);
     }
 
     /**
@@ -136,7 +153,7 @@ public class GraphNode {
      * is a BNode it should be contained (potentially faster)
      * @return the context of a node
      */
-    private MGraph getContextOf(Resource node, final Set<Resource> dontExpand) {
+    private MGraph getContextOf(Resource node, final Set<Resource> dontExpand, boolean forward, boolean backward) {
         return getContextOf(node, dontExpand, new Acceptor() {
 
             @Override
@@ -146,15 +163,15 @@ public class GraphNode {
                 }
                 return false;
             }
-        });
+        }, forward, backward);
     }
 
     private interface Acceptor {
         boolean expand(Resource resource);
     }
-    private MGraph getContextOf(Resource node, final Set<Resource> dontExpand, Acceptor acceptor) {
+    private MGraph getContextOf(Resource node, final Set<Resource> dontExpand, Acceptor acceptor, boolean forward, boolean backward) {
         MGraph result = new SimpleMGraph();
-        if (node instanceof NonLiteral) {
+        if (node instanceof NonLiteral && forward) {
             Iterator<Triple> forwardProperties = graph.filter((NonLiteral) node, null, null);
             while (forwardProperties.hasNext()) {
                 Triple triple = forwardProperties.next();
@@ -162,20 +179,23 @@ public class GraphNode {
                 Resource object = triple.getObject();
                 if (acceptor.expand(object) && !dontExpand.contains(object)) {
                     dontExpand.add(object);
-                    result.addAll(getContextOf(object, dontExpand, acceptor));
+                    result.addAll(getContextOf(object, dontExpand, acceptor, forward, backward));
                 }
             }
         }
-        Iterator<Triple> backwardProperties = graph.filter(null, null, node);
-        while (backwardProperties.hasNext()) {
-            Triple triple = backwardProperties.next();
-            result.add(triple);
-            NonLiteral subject = triple.getSubject();
-            if (acceptor.expand(subject) && !dontExpand.contains(subject)) {
-                dontExpand.add(subject);
-                result.addAll(getContextOf(subject, dontExpand, acceptor));
+        if(backward){
+        	Iterator<Triple> backwardProperties = graph.filter(null, null, node);
+            while (backwardProperties.hasNext()) {
+                Triple triple = backwardProperties.next();
+                result.add(triple);
+                NonLiteral subject = triple.getSubject();
+                if (acceptor.expand(subject) && !dontExpand.contains(subject)) {
+                    dontExpand.add(subject);
+                    result.addAll(getContextOf(subject, dontExpand, acceptor, forward, backward));
+                }
             }
         }
+        
         return result;
     }
 
@@ -477,7 +497,7 @@ public class GraphNode {
 
 
     /**
-     * Coverts the value into a typed literals and sets it as object of the
+     * Converts the value into a typed literals and sets it as object of the
      * specified property
      *
      * @param property the predicate of the triple to be created
@@ -556,22 +576,22 @@ public class GraphNode {
     }
 
     /**
-     * Replaces the graph node resouce with the specified <code>NonLiteral</code>.
+     * Replaces the graph node resource with the specified <code>NonLiteral</code>.
      * The resource is only replaced where it is either subject or object.
      * @param replacement
-     * @return a GraphNode representing the replecement node
+     * @return a GraphNode representing the replacement node
      */
     public GraphNode replaceWith(NonLiteral replacement) {
         return replaceWith(replacement, false);
     }
 
     /**
-     * Replaces the graph node resouce with the specified <code>NonLiteral</code>.
+     * Replaces the graph node resource with the specified <code>NonLiteral</code>.
      * Over the boolean <code>checkPredicate</code> it can be specified if the
      * resource should also be replaced where it is used as predicate.
      * @param replacement
      * @param checkPredicates
-     * @return a GraphNode representing the replecement node
+     * @return a GraphNode representing the replacement node
      */
     public GraphNode replaceWith(NonLiteral replacement, boolean checkPredicates) {
         MGraph newTriples = new SimpleMGraph();

http://git-wip-us.apache.org/repos/asf/clerezza/blob/a0842b68/rdf.utils/src/test/java/org/apache/clerezza/rdf/utils/TestGraphNode.java
----------------------------------------------------------------------
diff --git a/rdf.utils/src/test/java/org/apache/clerezza/rdf/utils/TestGraphNode.java b/rdf.utils/src/test/java/org/apache/clerezza/rdf/utils/TestGraphNode.java
index c8f8cf9..51267cb 100644
--- a/rdf.utils/src/test/java/org/apache/clerezza/rdf/utils/TestGraphNode.java
+++ b/rdf.utils/src/test/java/org/apache/clerezza/rdf/utils/TestGraphNode.java
@@ -24,10 +24,14 @@ import java.util.Iterator;
 import java.util.Set;
 
 import junit.framework.Assert;
+import static org.junit.Assert.*;
+
 import org.junit.Test;
 import org.apache.clerezza.rdf.core.BNode;
+import org.apache.clerezza.rdf.core.Graph;
 import org.apache.clerezza.rdf.core.Literal;
 import org.apache.clerezza.rdf.core.MGraph;
+import org.apache.clerezza.rdf.core.NonLiteral;
 import org.apache.clerezza.rdf.core.Resource;
 import org.apache.clerezza.rdf.core.Triple;
 import org.apache.clerezza.rdf.core.UriRef;
@@ -71,6 +75,31 @@ public class TestGraphNode {
         n.addProperty(property1, bNode2);
         Assert.assertEquals(1, g.size());
     }
+    
+    @Test
+    public void getNodeContextTest(){
+    	MGraph g = new SimpleMGraph();
+    	NonLiteral subject1 = new UriRef("http://example.org/subject1");
+    	NonLiteral subject2 = new UriRef("http://example.org/subject2");
+        UriRef property1 = new UriRef("http://example.org/property1");
+        //UriRef property2 = new UriRef("http://example.org/property2");
+        Triple forwardTriple = new TripleImpl(subject1, property1, new PlainLiteralImpl("literal"));
+        Triple backwardTriple = new TripleImpl(subject2, property1, subject1);
+        g.add(forwardTriple);
+        g.add(backwardTriple);
+        
+        GraphNode n = new GraphNode(subject1, g);
+        
+        assertEquals(2, n.getNodeContext().size());
+        
+        Graph fwd = n.getNodeContext(true,false);
+        assertEquals(1, fwd.size());
+        assertEquals(true, fwd.contains(forwardTriple));
+        
+        Graph bwd = n.getNodeContext(false,true);
+        assertEquals(1,bwd.size());
+        assertEquals(true, bwd.contains(backwardTriple));
+    }
 
     @Test
     public void testGetSubjectAndObjectNodes() {


[2/2] git commit: remove deprecated use of Junit's assert

Posted by fl...@apache.org.
remove deprecated use of Junit's assert


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

Branch: refs/heads/CLEREZZA-950
Commit: 4182228087a74f577ba1a3d983e0827f91a94925
Parents: a0842b6
Author: Florent André <fl...@apache.org>
Authored: Tue Sep 23 20:05:29 2014 +0200
Committer: Florent André <fl...@apache.org>
Committed: Tue Sep 23 20:05:29 2014 +0200

----------------------------------------------------------------------
 .../clerezza/rdf/utils/TestGraphNode.java       | 65 ++++++++++----------
 1 file changed, 32 insertions(+), 33 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/clerezza/blob/41822280/rdf.utils/src/test/java/org/apache/clerezza/rdf/utils/TestGraphNode.java
----------------------------------------------------------------------
diff --git a/rdf.utils/src/test/java/org/apache/clerezza/rdf/utils/TestGraphNode.java b/rdf.utils/src/test/java/org/apache/clerezza/rdf/utils/TestGraphNode.java
index 51267cb..0bf0e1e 100644
--- a/rdf.utils/src/test/java/org/apache/clerezza/rdf/utils/TestGraphNode.java
+++ b/rdf.utils/src/test/java/org/apache/clerezza/rdf/utils/TestGraphNode.java
@@ -23,7 +23,6 @@ import java.util.HashSet;
 import java.util.Iterator;
 import java.util.Set;
 
-import junit.framework.Assert;
 import static org.junit.Assert.*;
 
 import org.junit.Test;
@@ -59,10 +58,10 @@ public class TestGraphNode {
         g.add(new TripleImpl(property1, property1, bNode2));
         g.add(new TripleImpl(property1, property1, new PlainLiteralImpl("bla bla")));
         GraphNode n = new GraphNode(bNode1, g);
-        Assert.assertEquals(4, n.getNodeContext().size());
+        assertEquals(4, n.getNodeContext().size());
         n.deleteNodeContext();
-        Assert.assertEquals(1, g.size());
-        Assert.assertFalse(n.getObjects(property2).hasNext());
+        assertEquals(1, g.size());
+        assertFalse(n.getObjects(property2).hasNext());
     }
 
     @Test
@@ -73,7 +72,7 @@ public class TestGraphNode {
         UriRef property1 = new UriRef("http://example.org/property1");
         GraphNode n = new GraphNode(bNode1, g);
         n.addProperty(property1, bNode2);
-        Assert.assertEquals(1, g.size());
+        assertEquals(1, g.size());
     }
     
     @Test
@@ -114,7 +113,7 @@ public class TestGraphNode {
                 Iterator<GraphNode> objectNodes = node.getObjectNodes(property);
                 while (objectNodes.hasNext()) {
                     GraphNode graphNode = objectNodes.next();
-                    Assert.assertTrue(objects.contains(graphNode.getNode()));
+                    assertTrue(objects.contains(graphNode.getNode()));
                 }
             }
         }
@@ -129,7 +128,7 @@ public class TestGraphNode {
                 Iterator<GraphNode> subjectNodes = node.getSubjectNodes(property);
                 while (subjectNodes.hasNext()) {
                     GraphNode graphNode = subjectNodes.next();
-                    Assert.assertTrue(subjects.contains(graphNode.getNode()));
+                    assertTrue(subjects.contains(graphNode.getNode()));
                 }
             }
         }
@@ -159,11 +158,11 @@ public class TestGraphNode {
         while(properties.hasNext()){
             i++;
             UriRef prop = properties.next();
-            Assert.assertTrue(props.contains(prop));
+            assertTrue(props.contains(prop));
             props.remove(prop);
         }
-        Assert.assertEquals(i, 4);
-        Assert.assertEquals(props.size(), 0);
+        assertEquals(i, 4);
+        assertEquals(props.size(), 0);
 
     }
 
@@ -183,7 +182,7 @@ public class TestGraphNode {
         g.add(new TripleImpl(bNode2, property1, new PlainLiteralImpl("bla bla")));
         GraphNode n = new GraphNode(bNode1, g);
         n.deleteProperties(property1);
-        Assert.assertEquals(3, g.size());
+        assertEquals(3, g.size());
     }
 
     @Test
@@ -202,7 +201,7 @@ public class TestGraphNode {
         g.add(new TripleImpl(bNode2, property1, new PlainLiteralImpl("bla bla")));
         GraphNode n = new GraphNode(bNode1, g);
         n.deleteProperty(property1, new PlainLiteralImpl("literal"));
-        Assert.assertEquals(4, g.size());
+        assertEquals(4, g.size());
     }
 
     @Test
@@ -231,21 +230,21 @@ public class TestGraphNode {
                 new SimpleMGraph(initialGraph.iterator()));
 
         node.replaceWith(newUriRef, true);
-        Assert.assertEquals(5, node.getGraph().size());
+        assertEquals(5, node.getGraph().size());
         Triple expectedTriple1 = new TripleImpl(bNode1, newUriRef, literal1);
         Triple expectedTriple2 = new TripleImpl(bNode1, property2, newUriRef);
         Triple expectedTriple3 = new TripleImpl(newUriRef, newUriRef, bNode2);
         Triple expectedTriple4 = new TripleImpl(newUriRef, newUriRef, literal2);
 
-        Assert.assertTrue(node.getGraph().contains(expectedTriple1));
-        Assert.assertTrue(node.getGraph().contains(expectedTriple2));
-        Assert.assertTrue(node.getGraph().contains(expectedTriple3));
-        Assert.assertTrue(node.getGraph().contains(expectedTriple4));
+        assertTrue(node.getGraph().contains(expectedTriple1));
+        assertTrue(node.getGraph().contains(expectedTriple2));
+        assertTrue(node.getGraph().contains(expectedTriple3));
+        assertTrue(node.getGraph().contains(expectedTriple4));
 
-        Assert.assertFalse(node.getGraph().contains(triple1));
-        Assert.assertFalse(node.getGraph().contains(triple2));
-        Assert.assertFalse(node.getGraph().contains(triple4));
-        Assert.assertFalse(node.getGraph().contains(triple5));
+        assertFalse(node.getGraph().contains(triple1));
+        assertFalse(node.getGraph().contains(triple2));
+        assertFalse(node.getGraph().contains(triple4));
+        assertFalse(node.getGraph().contains(triple5));
 
         node = new GraphNode(property1, new SimpleMGraph(initialGraph.iterator()));
         node.replaceWith(newBnode);
@@ -253,25 +252,25 @@ public class TestGraphNode {
         Triple expectedTriple6 = new TripleImpl(newBnode, property1, bNode2);
         Triple expectedTriple7 = new TripleImpl(newBnode, property1, literal2);
 
-        Assert.assertTrue(node.getGraph().contains(triple1));
-        Assert.assertTrue(node.getGraph().contains(expectedTriple5));
-        Assert.assertTrue(node.getGraph().contains(expectedTriple6));
-        Assert.assertTrue(node.getGraph().contains(expectedTriple7));
+        assertTrue(node.getGraph().contains(triple1));
+        assertTrue(node.getGraph().contains(expectedTriple5));
+        assertTrue(node.getGraph().contains(expectedTriple6));
+        assertTrue(node.getGraph().contains(expectedTriple7));
 
         node = new GraphNode(literal1, new SimpleMGraph(initialGraph.iterator()));
         node.replaceWith(newBnode);
         Triple expectedTriple8 = new TripleImpl(bNode1, property1, newBnode);
-        Assert.assertTrue(node.getGraph().contains(expectedTriple8));
+        assertTrue(node.getGraph().contains(expectedTriple8));
 
         node = new GraphNode(property1, new SimpleMGraph(initialGraph.iterator()));
         node.replaceWith(newUriRef);
         Triple expectedTriple9 = new TripleImpl(bNode1, property2, newUriRef);
         Triple expectedTriple10 = new TripleImpl(newUriRef, property1, bNode2);
         Triple expectedTriple11 = new TripleImpl(newUriRef, property1, literal2);
-        Assert.assertTrue(node.getGraph().contains(triple1));
-        Assert.assertTrue(node.getGraph().contains(expectedTriple9));
-        Assert.assertTrue(node.getGraph().contains(expectedTriple10));
-        Assert.assertTrue(node.getGraph().contains(expectedTriple11));
+        assertTrue(node.getGraph().contains(triple1));
+        assertTrue(node.getGraph().contains(expectedTriple9));
+        assertTrue(node.getGraph().contains(expectedTriple10));
+        assertTrue(node.getGraph().contains(expectedTriple11));
     }
 
     @Test
@@ -282,10 +281,10 @@ public class TestGraphNode {
         UriRef property1 = new UriRef("http://example.org/property1");
         GraphNode n = new GraphNode(bNode1, g);
         n.addProperty(property1, bNode2);
-        Assert.assertTrue(n.equals(new GraphNode(bNode1, g)));
-        Assert.assertFalse(n.equals(new GraphNode(bNode2, g)));
+        assertTrue(n.equals(new GraphNode(bNode1, g)));
+        assertFalse(n.equals(new GraphNode(bNode2, g)));
         GraphNode n2 = null;
-        Assert.assertFalse(n.equals(n2));
+        assertFalse(n.equals(n2));
     }
 
     private Set<Resource> createSet(Iterator<? extends Resource> resources) {