Author: bblfish
Date: Fri Jun 24 07:45:13 2011
New Revision: 1139186

URL: http://svn.apache.org/viewvc?rev=1139186&view=rev
Log:
CLEREZZA-510 added back linguistic ascii notation, as it has easier to 
understand precedence rules

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=1139186&r1=1139185&r2=1139186&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
 Fri Jun 24 07:45:13 2011
@@ -193,6 +193,12 @@ class EasyGraphNode(val ref: NonLiteral,
 //             case other: TripleCollection => new EasyGraph(graph)
 //     }
 
+       def +(sub: EasyGraphNode) = {
+               if (graph ne sub.graph) graph.addAll(sub.graph)
+               this
+       }
+
+
        /*
         * create an EasyGraphNode from this one where the backing graph is 
protected from writes by a new
         * SimpleGraph.
@@ -203,6 +209,12 @@ class EasyGraphNode(val ref: NonLiteral,
 
        def this() = this (new BNode)
 
+       //
+       // Ascii arrow notation
+       //
+       // (easier to write but has odd precedence implications)
+
+
        def --(rel: UriRef): Predicate = new Predicate(rel)
 
        def --(rel: String): Predicate = new Predicate(new UriRef(rel))
@@ -217,6 +229,10 @@ class EasyGraphNode(val ref: NonLiteral,
        //              if (yes) func(has(rel))
        //              else this
 
+       //
+       // symbolic notation
+       //
+       // (shorter and more predictable precedence rules, but unicode issues)
 
        def ⟝(rel: UriRef): Predicate = --(rel)
 
@@ -225,21 +241,30 @@ class EasyGraphNode(val ref: NonLiteral,
        /* For inverse relationships */
        def ⟵(rel: UriRef) = -<-(rel)
 
-       // does not work as hoped
-       //      def ⟝?(yes: Boolean, uri: UriRef)(func: Predicate => 
EasyGraphNode): EasyGraphNode = hasQ(yes,uri)(func)
-
-       def +(sub: EasyGraphNode) = {
-               if (graph ne sub.graph) graph.addAll(sub.graph)
-               this
+       def ∈(rdfclass: UriRef): EasyGraphNode = {
+               graph.add(new TripleImpl(ref, RDF.`type`, rdfclass))
+               return EasyGraphNode.this
        }
 
+
+
+
+       //
+       // symbolic notation
+       //
+       // (shorter and more predictable precedence rules - they are always the 
weakest, and so very few brakets are need
+       // when symbolic operators are used. But sometimes this notation is 
grammatically awkward)
+
        def a(rdfclass: UriRef) = ∈(rdfclass)
 
+       def has(rel: UriRef): Predicate = --(rel)
+
+       def has(rel: String): Predicate = --(rel)
+
+       /* For inverse relationships */
+       def is(rel: UriRef) = -<-(rel)
+
 
-       def ∈(rdfclass: UriRef): EasyGraphNode = {
-               graph.add(new TripleImpl(ref, RDF.`type`, rdfclass))
-               return EasyGraphNode.this
-       }
 
        class InversePredicate(rel: UriRef) {
                def ⟞(subj: NonLiteral) = add(subj)
@@ -256,6 +281,10 @@ class EasyGraphNode(val ref: NonLiteral,
                def --(subj: EasyGraphNode) = ⟞(subj)
                // since we can only have inverses from non literals (howto 
deal with bndoes?)
 
+               def of(subj: NonLiteral) = ⟞(subj)
+               def of(subj: String) = ⟞(subj)
+               def of(subj: EasyGraphNode) = ⟞(subj)
+
                protected def add(subj: NonLiteral) = {
                        graph.add(new TripleImpl(subj, rel, ref))
                        EasyGraphNode.this
@@ -294,7 +323,6 @@ class EasyGraphNode(val ref: NonLiteral,
                //
                // arrow notation
                //
-               // todo: a relation to a list
 
                def ⟶(obj: String) = -->(obj)
 
@@ -309,6 +337,25 @@ class EasyGraphNode(val ref: NonLiteral,
 
                def ⟶(sub: EasyGraphNode) = -->(sub)
 
+
+               //
+               // text notation
+               //
+
+               def to(obj: String) = -->(obj)
+
+               def to(obj: Resource): EasyGraphNode = -->(obj)
+
+               def to(list: List[Resource]): EasyGraphNode = addList(list)
+
+               /**
+                * Add one relation for each member of the iterable collection
+                */
+               def toEach[T <: Resource](objs: Iterable[T]) = -->>(objs)
+
+               def to(sub: EasyGraphNode) = -->(sub)
+
+
                protected def add(obj: Resource) = {
                        addTriple(obj)
                        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=1139186&r1=1139185&r2=1139186&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
 Fri Jun 24 07:45:13 2011
@@ -292,4 +292,39 @@ class EasyGraphTest {
        }
 
 
+       @Test
+       def usingWordOperators {
+               import org.apache.clerezza.rdf.scala.utils.EasyGraph._
+               import org.apache.clerezza.rdf.scala.utils.Lang._
+                val ez = new EasyGraph()
+                // example using arrows
+                (
+                  ez.bnode("reto") a FOAF.Person
+                        has FOAF.name to "Reto Bachman-Gmür".lang(rm)
+                        has FOAF.title to "Mr"
+                        has FOAF.currentProject to "http://clerezza.org/".uri
+                        has FOAF.knows to (
+                            ez.u("http://bblfish.net/#hjs";) a FOAF.Person
+                                 has FOAF.name to "Henry Story"
+                                 has FOAF.currentProject to 
"http://webid.info/".uri
+                                 is identity of (
+                                          ez.bnode a RSAPublicKey //. notation 
because of precedence of operators
+                                              has modulus to 65537
+                                              has public_exponent to 
bblfishModulus^^hex // brackets needed due to precedence
+                                          )
+                                 has FOAF.knows toEach 
List(ez.bnode("reto").ref,ez.bnode("danny").ref)
+                        )
+                        has FOAF.knows to ( ez.bnode("danny") a FOAF.Person
+                                 has FOAF.name to "Danny Ayers".lang(en)
+                            has FOAF.knows to "http://bblfish.net/#hjs".uri 
//knows
+                                           has FOAF.knows to ez.bnode("reto")
+                        )
+                )
+               Assert.assertEquals("the two graphs should be of same 
size",tinyGraph.size(),ez.size())
+               Assert.assertEquals("Both graphs should contain exactly the 
same triples",tinyGraph,ez.getGraph)
+               ez.bnode("danny") has FOAF.name to "George"
+               Assert.assertNotSame("Added one more triple, so graphs should 
no longer be equal",tinyGraph,ez.getGraph)
+
+       }
+
 }


Reply via email to