Author: bblfish
Date: Thu Jun 23 15:31:14 2011
New Revision: 1138934

URL: http://svn.apache.org/viewvc?rev=1138934&view=rev
Log:
CLEREZZA-510 added a lot more test cases and support for one-to-many mappings 
on collections and one-to-one mappings on collections (lists only)

Modified:
    
incubator/clerezza/trunk/parent/rdf.scala.utils/src/main/scala/org/apache/clerezza/rdf/scala/utils/EasyGraph.scala
    
incubator/clerezza/trunk/parent/rdf.scala.utils/src/test/scala/org/apache/clerezza/rdf/scala/utils/EasyGraphTest.scala

Modified: 
incubator/clerezza/trunk/parent/rdf.scala.utils/src/main/scala/org/apache/clerezza/rdf/scala/utils/EasyGraph.scala
URL: 
http://svn.apache.org/viewvc/incubator/clerezza/trunk/parent/rdf.scala.utils/src/main/scala/org/apache/clerezza/rdf/scala/utils/EasyGraph.scala?rev=1138934&r1=1138933&r2=1138934&view=diff
==============================================================================
--- 
incubator/clerezza/trunk/parent/rdf.scala.utils/src/main/scala/org/apache/clerezza/rdf/scala/utils/EasyGraph.scala
 (original)
+++ 
incubator/clerezza/trunk/parent/rdf.scala.utils/src/main/scala/org/apache/clerezza/rdf/scala/utils/EasyGraph.scala
 Thu Jun 23 15:31:14 2011
@@ -19,17 +19,16 @@
 
 package org.apache.clerezza.rdf.scala.utils
 
-import org.apache.clerezza.rdf.core._
-import impl._
 import java.math.BigInteger
 import java.lang.Boolean
 import java.net.{URL, URI}
-import org.apache.clerezza.rdf.core._
 import org.apache.clerezza.rdf.utils.UnionMGraph
 import org.apache.clerezza.rdf.utils.GraphNode
 import scala.collection.mutable.HashMap
 import org.apache.clerezza.rdf.ontologies.{XSD, RDF}
 import java.util.{HashSet, Date}
+import org.apache.clerezza.rdf.core._
+import impl._
 
 object EasyGraph {
 
@@ -93,9 +92,11 @@ class EzLiteral(lexicalForm: String) ext
                }
        }
 
-       override def hashCode() = XSD.string.hashCode() +lexicalForm.hashCode()
+       override def hashCode() = XSD.string.hashCode() + lexicalForm.hashCode()
 
        def getDataType = XSD.string
+
+       override def toString() = lexicalForm
 }
 
 
@@ -129,7 +130,11 @@ class EasyGraph(val graph: HashSet[Tripl
        def bnode(name: String): EasyGraphNode = {
                namedBnodes.get(name) match {
                        case Some(ezGraphNode) => ezGraphNode
-                       case None => { val ezgn = bnode; namedBnodes.put(name, 
ezgn); ezgn }
+                       case None => {
+                               val ezgn = bnode;
+                               namedBnodes.put(name, ezgn);
+                               ezgn
+                       }
                }
        }
 
@@ -258,28 +263,24 @@ class EasyGraphNode(val ref: NonLiteral,
                //
                def -->(obj: Resource): EasyGraphNode = add(obj)
 
-               /* add a relation to each object in the argument list */
-               def -->(objs: Resource*): EasyGraphNode = {
-                       for (o <- objs) add(o)
-                       EasyGraphNode.this
-               }
-
-               def -->[T <: Resource](objs: Iterable[T]): EasyGraphNode = {
-                       for (o <- objs) add(o)
-                       EasyGraphNode.this
-               }
+               def -->(lit: String): EasyGraphNode = add(new EzLiteral(lit))
 
-               def -->(lit: String): EasyGraphNode = add(new 
PlainLiteralImpl(lit))
+               /**
+                * Adds a relation to a real linked list.
+                * If you want one relation to each entry use -->> or ⟶*
+                */
+               def -->(list: List[Resource]) = addList(list)
 
                def -->(sub: EasyGraphNode): EasyGraphNode = {
                        EasyGraphNode.this + sub
                        add(sub.ref)
                }
 
-               def -->>(uri: String) = add(new UriRef(uri))
-
-               def -->>(uris: Seq[String]) = {
-                       for (u <- uris) add(new UriRef(u))
+               /**
+                * Add one relation for each member of the iterable collection
+                */
+               def -->>[T <: Resource](uris: Iterable[T]): EasyGraphNode = {
+                       for (u <- uris) addTriple(u)
                        EasyGraphNode.this
                }
 
@@ -291,16 +292,53 @@ class EasyGraphNode(val ref: NonLiteral,
 
                def ⟶(obj: String) = -->(obj)
 
-               def ⟶(obj: Resource) = -->(obj)
+               def ⟶(obj: Resource): EasyGraphNode = -->(obj)
 
-               def ⟶*[T <: Resource](objs: Iterable[T]) = -->(objs)
+               def ⟶(list: List[Resource]): EasyGraphNode = addList(list)
+
+               /**
+                * Add one relation for each member of the iterable collection
+                */
+               def ⟶*[T <: Resource](objs: Iterable[T]) = -->>(objs)
 
                def ⟶(sub: EasyGraphNode) = -->(sub)
 
                protected def add(obj: Resource) = {
+                       addTriple(obj)
+                       EasyGraphNode.this
+               }
+
+               protected def addTriple(obj: Resource) {
                        graph.add(new TripleImpl(ref, rel, obj))
+               }
+
+               private def toTriples[T <: Resource](headRef: NonLiteral, list: 
List[T]): List[Triple] = {
+                       list match {
+                               case head :: next :: rest => {
+                                       val nextRef = new BNode
+                                       new TripleImpl(headRef, RDF.first, 
head) ::
+                                               new TripleImpl(headRef, 
RDF.rest, nextRef) ::
+                                               toTriples(nextRef, next :: rest)
+                               }
+                               case head :: nil => {
+                                       new TripleImpl(headRef, RDF.first, 
head) :: new TripleImpl(headRef, RDF.rest, RDF.nil) :: Nil
+                               }
+                               case nil => Nil
+                       }
+               }
+
+
+               protected def addList[T <: Resource](list: List[T]) = {
+                       val headNode = new BNode
+                       addTriple(headNode)
+                  val tripleLst = toTriples(headNode,list);
+                       graph.add(new TripleImpl(headNode,RDF.`type`,RDF.List))
+                       
graph.addAll(collection.JavaConversions.asJavaCollection(tripleLst))
                        EasyGraphNode.this
                }
+
+
+
        }
 
 }

Modified: 
incubator/clerezza/trunk/parent/rdf.scala.utils/src/test/scala/org/apache/clerezza/rdf/scala/utils/EasyGraphTest.scala
URL: 
http://svn.apache.org/viewvc/incubator/clerezza/trunk/parent/rdf.scala.utils/src/test/scala/org/apache/clerezza/rdf/scala/utils/EasyGraphTest.scala?rev=1138934&r1=1138933&r2=1138934&view=diff
==============================================================================
--- 
incubator/clerezza/trunk/parent/rdf.scala.utils/src/test/scala/org/apache/clerezza/rdf/scala/utils/EasyGraphTest.scala
 (original)
+++ 
incubator/clerezza/trunk/parent/rdf.scala.utils/src/test/scala/org/apache/clerezza/rdf/scala/utils/EasyGraphTest.scala
 Thu Jun 23 15:31:14 2011
@@ -43,16 +43,22 @@ class EasyGraphTest {
        final val modulus: UriRef = new 
UriRef("http://www.w3.org/ns/auth/rsa#modulus";)
        final val public_exponent: UriRef = new 
UriRef("http://www.w3.org/ns/auth/rsa#public_exponent";)
 
+       val henryUri: String = "http://bblfish.net/#hjs";
+       val retoUri: String = "http://farewellutopia.com/reto/#me";
+       val danbriUri: String = "http://danbri.org/foaf.rdf#danbri";
+
+
 
        private val tinyGraph: Graph = {
                val gr = new SimpleMGraph
                val reto= new BNode()
                val danny = new BNode()
-               val henry = new UriRef("http://bblfish.net/#hjs";)
+               val henry = new UriRef(henryUri)
 
                gr.add(new TripleImpl(reto,RDF.`type`, FOAF.Person))
                gr.add(new TripleImpl(reto,FOAF.name, new 
PlainLiteralImpl("Reto Bachman-Gmür", new Language("rm"))))
-               gr.add(new TripleImpl(reto,FOAF.title, new 
PlainLiteralImpl("Mr")))
+               //it is difficult to remember that one needs to put a string 
literal if one does not want to specify a language
+               gr.add(new TripleImpl(reto,FOAF.title, new 
TypedLiteralImpl("Mr",XSD.string)))
                gr.add(new TripleImpl(reto,FOAF.currentProject, new 
UriRef("http://clerezza.org/";)))
                gr.add(new TripleImpl(reto,FOAF.knows, henry))
                gr.add(new TripleImpl(reto,FOAF.knows, danny))
@@ -62,7 +68,7 @@ class EasyGraphTest {
                gr.add(new TripleImpl(danny,FOAF.knows, henry))
                gr.add(new TripleImpl(danny,FOAF.knows, reto))
 
-               gr.add(new TripleImpl(henry,FOAF.name,new 
PlainLiteralImpl("Henry Story")))
+               gr.add(new TripleImpl(henry,FOAF.name,new 
TypedLiteralImpl("Henry Story",XSD.string))) //It is tricky to remember that 
one needs this for pure strings
                gr.add(new TripleImpl(henry,FOAF.currentProject,new 
UriRef("http://webid.info/";)))
                gr.add(new TripleImpl(henry,RDF.`type`, FOAF.Person))
                gr.add(new TripleImpl(henry,FOAF.knows, danny))
@@ -77,7 +83,7 @@ class EasyGraphTest {
        }
 
        @Test
-       def testEquality {
+       def simpleGraphEquality {
                val gr = new SimpleMGraph
 
                val reto= new BNode()
@@ -91,6 +97,115 @@ class EasyGraphTest {
 
        }
 
+       @Test
+       def testList {
+               val gr = new SimpleMGraph
+               val reto= new UriRef(retoUri)
+               val todoRef = new UriRef("http://clerezza.org/ex/ont/todo";)
+               val holiday = "http://dbpedia.org/resource/Holiday";
+               val list = new BNode
+               val list2 = new BNode
+               val list3 = new BNode
+
+               gr.add(new TripleImpl(reto, todoRef, list ))
+               gr.add(new TripleImpl(list,RDF.`type`, RDF.List))
+               gr.add(new TripleImpl(list,RDF.first, new 
PlainLiteralImpl("SPARQL update support",new Language("en"))))
+               gr.add(new TripleImpl(list,RDF.rest,list2))
+               gr.add(new TripleImpl(list2,RDF.first, new 
PlainLiteralImpl("XSPARQL support",new Language("en"))))
+               gr.add(new TripleImpl(list2,RDF.rest,list3))
+               gr.add(new TripleImpl(list3,RDF.first, new UriRef(holiday)))
+               gr.add(new TripleImpl(list3,RDF.rest,RDF.nil))
+               gr.add(new TripleImpl(reto,RDF.`type`, FOAF.Person))
+
+               val ez = new EasyGraph()
+
+               import org.apache.clerezza.rdf.scala.utils.EasyGraph._
+               import org.apache.clerezza.rdf.scala.utils.Lang._
+               ( ez.u(retoUri) ∈ FOAF.Person
+                           ⟝ todoRef ⟶ List[Resource]("SPARQL update 
support".lang(en),"XSPARQL support".lang(en),holiday.uri))
+
+               Assert.assertEquals("the two graphs should be of same 
size",gr.size(),ez.size())
+               Assert.assertEquals("Both graphs should contain exactly the 
same triples",gr.getGraph,ez.getGraph)
+
+       }
+
+       @Test
+       def oneToMany {
+               val gr = new SimpleMGraph
+               val reto= new UriRef(retoUri)
+               gr.add(new TripleImpl(reto,FOAF.knows,new UriRef(henryUri)))
+               gr.add(new TripleImpl(reto,FOAF.knows,new UriRef(danbriUri)))
+
+               val ez = new EasyGraph()
+               import org.apache.clerezza.rdf.scala.utils.EasyGraph._
+               import org.apache.clerezza.rdf.scala.utils.Lang._
+
+               (ez.u(retoUri) -- FOAF.knows -->> 
List(henryUri.uri,danbriUri.uri))
+
+               Assert.assertEquals("the two graphs should be of same 
size",gr.size(),ez.size())
+               Assert.assertEquals("Both graphs should contain exactly the 
same triples",gr.getGraph,ez.getGraph)
+
+               val ez2 = new EasyGraph()
+               (ez2.u(retoUri) ⟝  FOAF.knows ⟶*  
Set(danbriUri.uri,henryUri.uri))
+
+               Assert.assertEquals("the two graphs should be of same 
size",gr.size(),ez2.size())
+               Assert.assertEquals("Both graphs should contain exactly the 
same triples",gr.getGraph,ez2.getGraph)
+
+       }
+
+       @Test
+       def langEquals {
+               import org.apache.clerezza.rdf.scala.utils.EasyGraph._
+               import org.apache.clerezza.rdf.scala.utils.Lang._
+
+                val lit = new PlainLiteralImpl("SPARQL update support",new 
Language("en"))
+                val lit2 =  "SPARQL update support".lang(en)
+
+               Assert.assertEquals("the two literals should be 
equsl",lit.hashCode(),lit2.hashCode())
+               Assert.assertEquals("the two literals should be equsl",lit,lit2)
+
+               val lit3 = new PlainLiteralImpl("Reto Bachman-Gmür",new 
Language("rm"))
+               val lit4 = "Reto Bachman-Gmür".lang(rm)
+
+               Assert.assertEquals("the two lang literals should have same 
hash",lit3.hashCode(),lit4.hashCode())
+               Assert.assertEquals("the two lang literals should be 
equal",lit3,lit4)
+
+
+       }
+
+       @Test
+       def uriEquals {
+               import org.apache.clerezza.rdf.scala.utils.EasyGraph._
+               import org.apache.clerezza.rdf.scala.utils.Lang._
+
+               val uc = new UriRef("http://clerezza.org/";)
+               val ec = "http://clerezza.org/".uri
+
+               Assert.assertEquals("the two uris should have an equal 
hash",uc.hashCode(),ec.hashCode())
+               Assert.assertEquals("the two uris should be equal",uc,ec)
+
+
+       }
+
+       @Test
+       def literalEquals {
+               val exp = LiteralFactory.getInstance().createTypedLiteral(65537)
+               val mod= new TypedLiteralImpl(bblfishModulus,hex)
+
+               import org.apache.clerezza.rdf.scala.utils.EasyGraph._
+               import org.apache.clerezza.rdf.scala.utils.Lang._
+
+               val modZ: TypedLiteral = bblfishModulus^^hex
+               val expZ: TypedLiteral = 65537
+
+               Assert.assertEquals("the two literals should have an equal 
hash",exp.hashCode(),expZ.hashCode())
+               Assert.assertEquals("the two literals should be equal",exp,expZ)
+
+               Assert.assertEquals("the two literals should have an equal 
hash",mod.hashCode(),modZ.hashCode())
+               Assert.assertEquals("the two literals should be equal",mod,modZ)
+
+       }
+
 
        @Test
        def usingSymbolicArrows {
@@ -112,8 +227,7 @@ class EasyGraphTest {
                                         ⟝ modulus ⟶ 65537
                                         ⟝ public_exponent ⟶ 
(bblfishModulus^^hex) // brackets needed due to precedence
                                  )
-                                 ⟝ FOAF.knows ⟶ ez.bnode("reto")
-                                           ⟝ FOAF.knows ⟶ ez.bnode("danny")
+                                 ⟝ FOAF.knows ⟶* 
Set(ez.bnode("reto").ref,ez.bnode("danny").ref)
                         )
                         ⟝ FOAF.knows ⟶ (
                             ez.bnode("danny") ∈ FOAF.Person
@@ -148,8 +262,7 @@ class EasyGraphTest {
                                               -- modulus --> 65537
                                               -- public_exponent --> 
(bblfishModulus^^hex) // brackets needed due to precedence
                                           )
-                                 -- FOAF.knows --> ez.bnode("reto")
-                                           -- FOAF.knows --> ez.bnode("danny")
+                                 -- FOAF.knows -->> 
List(ez.bnode("reto").ref,ez.bnode("danny").ref)
                         )
                         -- FOAF.knows --> (ez.bnode("danny").a(FOAF.Person)
                                  -- FOAF.name --> "Danny Ayers".lang(en)


Reply via email to