You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@clerezza.apache.org by mi...@apache.org on 2010/04/27 17:38:20 UTC

svn commit: r938526 - in /incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.rdf.utils/src: main/java/org/apache/clerezza/rdf/utils/RdfList.java test/java/org/apache/clerezza/rdf/utils/RdfListTest.java

Author: mir
Date: Tue Apr 27 15:38:20 2010
New Revision: 938526

URL: http://svn.apache.org/viewvc?rev=938526&view=rev
Log:
CLEREZZA-196: added static helper methods to the RdfList

Modified:
    incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.rdf.utils/src/main/java/org/apache/clerezza/rdf/utils/RdfList.java
    incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.rdf.utils/src/test/java/org/apache/clerezza/rdf/utils/RdfListTest.java

Modified: incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.rdf.utils/src/main/java/org/apache/clerezza/rdf/utils/RdfList.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/RdfList.java?rev=938526&r1=938525&r2=938526&view=diff
==============================================================================
--- incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.rdf.utils/src/main/java/org/apache/clerezza/rdf/utils/RdfList.java (original)
+++ incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.rdf.utils/src/main/java/org/apache/clerezza/rdf/utils/RdfList.java Tue Apr 27 15:38:20 2010
@@ -20,7 +20,10 @@ package org.apache.clerezza.rdf.utils;
 
 import java.util.AbstractList;
 import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.Iterator;
 import java.util.List;
+import java.util.Set;
 import org.apache.clerezza.rdf.core.BNode;
 import org.apache.clerezza.rdf.core.NonLiteral;
 import org.apache.clerezza.rdf.core.Resource;
@@ -42,7 +45,7 @@ import org.apache.clerezza.rdf.ontologie
  * for read operations (such as for immutable <code>TripleCollection</code>s) is
  * not problematic.
  *
- * @author rbn
+ * @author rbn, mir
  */
 public class RdfList extends AbstractList<Resource> {
 
@@ -79,6 +82,20 @@ public class RdfList extends AbstractLis
 
 	}
 
+	/**
+	 * Get a list for the specified resource node. If the resource node does not
+	 * have rdf:first and rdf:rest properties an empty list is created by
+	 * specifying that the resource is owl:sameAs rdf:nil.
+	 *
+	 * If the list is modified using the created instance
+	 * <code>listResource</code> will always be the first list.
+	 *
+	 * @param listNode
+	 */
+	public RdfList(GraphNode listNode) {
+		this((NonLiteral)listNode.getNode(), listNode.getGraph());
+	}
+
 	private void expandTill(int pos) {
 		if (totallyExpanded) {
 			return;
@@ -204,4 +221,95 @@ public class RdfList extends AbstractLis
 	private Resource getFirstEntry(NonLiteral listResource) {
 		return tc.filter(listResource, RDF.first, null).next().getObject();
 	}
+
+	public NonLiteral getListResource() {
+		return firstList;
+	}
+
+	@Override
+	public boolean equals(Object obj) {
+		if (obj == null) {
+			return false;
+		}
+		if (getClass() != obj.getClass()) {
+			return false;
+		}
+		final RdfList other = (RdfList) obj;
+
+		if (!other.firstList.equals(this.firstList)) {
+			return false;
+		}
+
+		if (!other.tc.equals(this.tc)) {
+			return false;
+		}
+
+		return true;
+	}
+
+	@Override
+	public int hashCode() {
+		return 17 * this.firstList.hashCode() + this.tc.hashCode();
+	}
+
+	/**
+	 * Returns the rdf lists of which the specified <code>GraphNode</code> is
+	 * an element of. Sublists of other lists are not returned.
+	 *
+	 * @param element
+	 * @return
+	 */
+	public static Set<RdfList> findContainingLists(GraphNode element) {
+		Set<GraphNode> listNodes = findContainingListNodes(element);
+		if (listNodes.isEmpty()) {
+			return null;
+		}
+
+		Set<RdfList> rdfLists = new HashSet<RdfList>();
+		for (Iterator<GraphNode> it = listNodes.iterator(); it.hasNext();) {
+			GraphNode listNode = it.next();
+			rdfLists.add(new RdfList(listNode));
+		}
+		return rdfLists;
+	}
+
+	/**
+	 * Returns a set of <code>GraphNode</code>S which are the first list nodes (meaning
+	 * they are not the beginning of a sublist) of the list containing the specified
+	 * <code>GraphNode</code> as an element.
+	 *
+	 * @param element
+	 * @return
+	 */
+	public static Set<GraphNode> findContainingListNodes(GraphNode element) {
+		Iterator<GraphNode> partOfaListNodesIter = element.getSubjectNodes(RDF.first);
+		if (!partOfaListNodesIter.hasNext()) {
+			return null;
+		}
+		Set<GraphNode> listNodes = new HashSet<GraphNode>();
+
+		while (partOfaListNodesIter.hasNext()) {
+			listNodes.addAll(findAllListNodes(partOfaListNodesIter.next()));
+		}
+		return listNodes;
+	}
+	
+	private static Set<GraphNode> findAllListNodes(GraphNode listPart) {
+		Iterator<GraphNode> invRestNodesIter;
+		Set<GraphNode> listNodes = new HashSet<GraphNode>();
+		do {
+			invRestNodesIter = listPart.getSubjectNodes(RDF.rest);
+			if (invRestNodesIter.hasNext()) {
+				listPart = invRestNodesIter.next();
+				while (invRestNodesIter.hasNext()) {
+					GraphNode graphNode = invRestNodesIter.next();
+					listNodes.addAll(findAllListNodes(graphNode));
+				}
+			} else {
+				listNodes.add(listPart);
+				break;
+			}
+		} while (true);
+		return listNodes;
+	}
 }

Modified: incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.rdf.utils/src/test/java/org/apache/clerezza/rdf/utils/RdfListTest.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/RdfListTest.java?rev=938526&r1=938525&r2=938526&view=diff
==============================================================================
--- incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.rdf.utils/src/test/java/org/apache/clerezza/rdf/utils/RdfListTest.java (original)
+++ incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.rdf.utils/src/test/java/org/apache/clerezza/rdf/utils/RdfListTest.java Tue Apr 27 15:38:20 2010
@@ -19,6 +19,7 @@
 package org.apache.clerezza.rdf.utils;
 
 import java.util.List;
+import java.util.Set;
 
 import junit.framework.Assert;
 
@@ -134,4 +135,49 @@ public class RdfListTest {
 		Assert.assertEquals(2, tc.size());
 
 	}
+
+	@Test
+	public void findContainingListNodesAndfindContainingListsTest() {
+		TripleCollection tc = new SimpleMGraph();
+		GraphNode listA = new GraphNode(new UriRef("http:///listA"), tc);
+		GraphNode listB = new GraphNode(new UriRef("http:///listB"), tc);
+		BNode element1 = new BNode();
+		BNode element2 = new BNode();
+		BNode element3 = new BNode();
+		BNode element4 = new BNode();
+		BNode element5 = new BNode();
+
+		RdfList rdfListA = new RdfList(listA);
+		rdfListA.add(element1);
+		rdfListA.add(element2);
+		rdfListA.add(element3);
+		rdfListA.add(element4);
+
+		RdfList rdfListB = new RdfList(listB);
+		rdfListB.add(element2);
+		rdfListB.add(element4);
+		rdfListB.add(element5);
+
+		Set<GraphNode> containingListNodes = RdfList.findContainingListNodes(
+				new GraphNode(element3, tc));
+		Assert.assertEquals(1, containingListNodes.size());
+		Assert.assertTrue(containingListNodes.contains(listA));
+
+		Set<RdfList> containingLists = RdfList.findContainingLists(
+				new GraphNode(element3, tc));
+		Assert.assertEquals(1, containingLists.size());
+		Assert.assertTrue(containingLists.contains(rdfListA));
+
+		containingListNodes = RdfList.findContainingListNodes(
+				new GraphNode(element4, tc));
+		Assert.assertEquals(2, containingListNodes.size());
+		Assert.assertTrue(containingListNodes.contains(listA));
+		Assert.assertTrue(containingListNodes.contains(listB));
+
+		containingLists = RdfList.findContainingLists(
+				new GraphNode(element4, tc));
+		Assert.assertEquals(2, containingLists.size());
+		Assert.assertTrue(containingLists.contains(rdfListA));
+		Assert.assertTrue(containingLists.contains(rdfListB));
+	}
 }
\ No newline at end of file