Author: mir
Date: Tue Mar 23 15:29:34 2010
New Revision: 926621
URL: http://svn.apache.org/viewvc?rev=926621&view=rev
Log:
CLEREZZA-171: Added getObjectNodes() and getSubjectNodes()-method to GraphNode.
Modified:
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.rdf.utils/pom.xml
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.rdf.utils/src/main/java/org/apache/clerezza/rdf/utils/GraphNode.java
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.rdf.utils/src/test/java/org/apache/clerezza/rdf/utils/TestGraphNode.java
Modified:
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.rdf.utils/pom.xml
URL:
http://svn.apache.org/viewvc/incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.rdf.utils/pom.xml?rev=926621&r1=926620&r2=926621&view=diff
==============================================================================
---
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.rdf.utils/pom.xml
(original)
+++
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.rdf.utils/pom.xml
Tue Mar 23 15:29:34 2010
@@ -29,5 +29,9 @@
<groupId>org.apache.clerezza</groupId>
<artifactId>org.apache.clerezza.utils</artifactId>
</dependency>
+ <dependency>
+ <groupId>org.apache.clerezza</groupId>
+
<artifactId>org.apache.clerezza.rdf.core.test</artifactId>
+ </dependency>
</dependencies>
</project>
\ No newline at end of file
Modified:
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.rdf.utils/src/main/java/org/apache/clerezza/rdf/utils/GraphNode.java
URL:
http://svn.apache.org/viewvc/incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.rdf.utils/src/main/java/org/apache/clerezza/rdf/utils/GraphNode.java?rev=926621&r1=926620&r2=926621&view=diff
==============================================================================
---
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.rdf.utils/src/main/java/org/apache/clerezza/rdf/utils/GraphNode.java
(original)
+++
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.rdf.utils/src/main/java/org/apache/clerezza/rdf/utils/GraphNode.java
Tue Mar 23 15:29:34 2010
@@ -59,7 +59,7 @@ public class GraphNode {
/**
* Gets the graph the node represented by this instance is in
- *
+ *
* @return the graph of this GraphNode
*/
public TripleCollection getGraph() {
@@ -68,7 +68,7 @@ public class GraphNode {
/**
* Gets the unwrapped node
- *
+ *
* @return the node represented by this GraphNode
*/
public Resource getNode() {
@@ -86,14 +86,14 @@ public class GraphNode {
}
/**
- * The context of a node are the triples containing a node
+ * The context of a node are the triples containing a node
* as 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.
- *
- * @return the context of the node represented by the instance
+ *
+ * @return the context of the node represented by the instance
*/
public Graph getNodeContext() {
final HashSet<BNode> dontExpand = new HashSet<BNode>();
@@ -310,7 +310,7 @@ public class GraphNode {
/**
* Count the number of triples in the underlying triple-collection
* with this node as object and a specified property as predicate.
- *
+ *
* @param property the property to be examined
* @return the number of triples in the underlying triple-collection
* which meet the specified condition
@@ -351,12 +351,12 @@ public class GraphNode {
return getTypeSelectedObjects(property, UriRef.class);
}
-
+
/**
* Get all available properties as an {...@link Iterator}<{...@link
UriRef}>.
* You can use <code>getObjects(UriRef property)</code> to get the
values of
* each property
- *
+ *
* @return an iterator over properties of this node
*/
public Iterator<UriRef> getProperties() {
@@ -388,7 +388,7 @@ public class GraphNode {
* Get all inverse properties as an {...@link Iterator}<{...@link
UriRef}>.
* You can use <code>getSubject(UriRef property)</code> to get the
values of
* each inverse property
- *
+ *
* @return an iterator over properties pointing to this node
*/
public Iterator<UriRef> getInverseProperties() {
@@ -397,7 +397,7 @@ public class GraphNode {
}
/**
- *
+ *
* @param triples
* @returnan {...@link Iterator}<{...@link UriRef}> containing the
predicates from
* an {...@link Iterator}<{...@link Triple}>
@@ -538,6 +538,68 @@ public class GraphNode {
}
/**
+ * Returns a iterator containing all objects of the triples where this
+ * graph node is the subject and has the specified property. The objects
+ * are returned as <code>GraphNode</code>s.
+ *
+ * @param property
+ * @return
+ */
+ public Iterator<GraphNode> getObjectNodes(UriRef property) {
+ final Iterator<Resource> objects = this.getObjects(property);
+ return new Iterator<GraphNode>() {
+
+ @Override
+ public boolean hasNext() {
+ return objects.hasNext();
+ }
+
+ @Override
+ public GraphNode next() {
+ Resource object = objects.next();
+ return new GraphNode(object, graph);
+
+ }
+
+ @Override
+ public void remove() {
+ objects.remove();
+ }
+ };
+ }
+
+ /**
+ * Returns a iterator containing all subjects of the triples where this
+ * graph node is the object and has the specified property. The subjects
+ * are returned as <code>GraphNode</code>s.
+ *
+ * @param property
+ * @return
+ */
+ public Iterator<GraphNode> getSubjectNodes(UriRef property) {
+ final Iterator<NonLiteral> subjects =
this.getSubjects(property);
+ return new Iterator<GraphNode>() {
+
+ @Override
+ public boolean hasNext() {
+ return subjects.hasNext();
+ }
+
+ @Override
+ public GraphNode next() {
+ Resource object = subjects.next();
+ return new GraphNode(object, graph);
+
+ }
+
+ @Override
+ public void remove() {
+ subjects.remove();
+ }
+ };
+ }
+
+ /**
*
* @param obj
* @return true if obj is an instance of the same class represening the
same
Modified:
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.rdf.utils/src/test/java/org/apache/clerezza/rdf/utils/TestGraphNode.java
URL:
http://svn.apache.org/viewvc/incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.rdf.utils/src/test/java/org/apache/clerezza/rdf/utils/TestGraphNode.java?rev=926621&r1=926620&r2=926621&view=diff
==============================================================================
---
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.rdf.utils/src/test/java/org/apache/clerezza/rdf/utils/TestGraphNode.java
(original)
+++
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.rdf.utils/src/test/java/org/apache/clerezza/rdf/utils/TestGraphNode.java
Tue Mar 23 15:29:34 2010
@@ -19,25 +19,29 @@
package org.apache.clerezza.rdf.utils;
import java.util.ArrayList;
+import java.util.HashSet;
import java.util.Iterator;
+import java.util.Set;
import junit.framework.Assert;
import org.junit.Test;
import org.apache.clerezza.rdf.core.BNode;
import org.apache.clerezza.rdf.core.Literal;
import org.apache.clerezza.rdf.core.MGraph;
+import org.apache.clerezza.rdf.core.Resource;
import org.apache.clerezza.rdf.core.Triple;
import org.apache.clerezza.rdf.core.UriRef;
import org.apache.clerezza.rdf.core.impl.PlainLiteralImpl;
import org.apache.clerezza.rdf.core.impl.SimpleMGraph;
import org.apache.clerezza.rdf.core.impl.TripleImpl;
+import org.apache.clerezza.rdf.core.test.RandomGraph;
/**
*
* @author reto, mir
*/
public class TestGraphNode {
-
+
@Test
public void nodeContext() {
MGraph g = new SimpleMGraph();
@@ -56,7 +60,7 @@ public class TestGraphNode {
Assert.assertEquals(1, g.size());
Assert.assertFalse(n.getObjects(property2).hasNext());
}
-
+
@Test
public void addNode() {
MGraph g = new SimpleMGraph();
@@ -67,7 +71,44 @@ public class TestGraphNode {
n.addProperty(property1, bNode2);
Assert.assertEquals(1, g.size());
}
-
+
+ @Test
+ public void testGetSubjectAndObjectNodes() {
+ RandomGraph graph = new RandomGraph(5, 20, new SimpleMGraph());
+ for (int i = 0; i < 200; i++) {
+ graph.evolve();
+ }
+ for (int j = 0; j < 200; j++) {
+ Triple randomTriple = graph.getRandomTriple();
+ GraphNode node = new
GraphNode(randomTriple.getSubject(), graph);
+ Iterator<UriRef> properties = node.getProperties();
+ while (properties.hasNext()) {
+ UriRef property = properties.next();
+ Set<Resource> objects =
createSet(node.getObjects(property));
+ Iterator<GraphNode> objectNodes =
node.getObjectNodes(property);
+ while (objectNodes.hasNext()) {
+ GraphNode graphNode =
objectNodes.next();
+
Assert.assertTrue(objects.contains(graphNode.getNode()));
+ }
+ }
+ }
+
+ for (int j = 0; j < 200; j++) {
+ Triple randomTriple = graph.getRandomTriple();
+ GraphNode node = new
GraphNode(randomTriple.getObject(), graph);
+ Iterator<UriRef> properties = node.getProperties();
+ while (properties.hasNext()) {
+ UriRef property = properties.next();
+ Set<Resource> subjects =
createSet(node.getSubjects(property));
+ Iterator<GraphNode> subjectNodes =
node.getSubjectNodes(property);
+ while (subjectNodes.hasNext()) {
+ GraphNode graphNode =
subjectNodes.next();
+
Assert.assertTrue(subjects.contains(graphNode.getNode()));
+ }
+ }
+ }
+ }
+
@Test
public void getAvailableProperties(){
MGraph g = new SimpleMGraph();
@@ -97,9 +138,9 @@ public class TestGraphNode {
}
Assert.assertEquals(i, 4);
Assert.assertEquals(props.size(), 0);
-
+
}
-
+
@Test
public void deleteAll() {
MGraph g = new SimpleMGraph();
@@ -118,7 +159,7 @@ public class TestGraphNode {
n.deleteProperties(property1);
Assert.assertEquals(3, g.size());
}
-
+
@Test
public void deleteSingleProperty() {
MGraph g = new SimpleMGraph();
@@ -137,7 +178,7 @@ public class TestGraphNode {
n.deleteProperty(property1, new PlainLiteralImpl("literal"));
Assert.assertEquals(4, g.size());
}
-
+
@Test
public void replaceWith() {
MGraph initialGraph = new SimpleMGraph();
@@ -149,7 +190,7 @@ public class TestGraphNode {
UriRef newUriRef = new UriRef("http://example.org/newName");
Literal literal1 = new PlainLiteralImpl("literal");
Literal literal2 = new PlainLiteralImpl("bla bla");
-
+
Triple triple1 = new TripleImpl(bNode1, property1, literal1);
Triple triple2 = new TripleImpl(bNode1, property2, property1);
Triple triple3 = new TripleImpl(bNode2, property2, bNode1);
@@ -162,7 +203,7 @@ public class TestGraphNode {
initialGraph.add(triple5);
GraphNode node = new GraphNode(property1,
new SimpleMGraph(initialGraph.iterator()));
-
+
node.replaceWith(newUriRef, true);
Assert.assertEquals(5, node.getGraph().size());
Triple expectedTriple1 = new TripleImpl(bNode1, newUriRef,
literal1);
@@ -185,7 +226,7 @@ public class TestGraphNode {
Triple expectedTriple5 = new TripleImpl(bNode1, property2,
newBnode);
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));
@@ -207,4 +248,13 @@ public class TestGraphNode {
Assert.assertTrue(node.getGraph().contains(expectedTriple11));
}
+ private Set<Resource> createSet(Iterator<? extends Resource> resources)
{
+ Set<Resource> set = new HashSet<Resource>();
+ while (resources.hasNext()) {
+ Resource resource = resources.next();
+ set.add(resource);
+ }
+ return set;
+ }
+
}