Thanks Paul, for your help. I ended up getting it. It's a question of being 
careful about 
what you point out and of using some signs like - which have various binding 
strengths.

So here is the code now. It looks quite good I think, and it shows how easy rdf 
is to write
out. 

I am not sure if the arrow notation is better than the pure () one or the 

/*
 * Apache licence
 */

package org.apache.clerezza.rdf.scala.utils

import org.apache.clerezza.rdf.core._
import collection.mutable.Queue
import impl._
import org.apache.clerezza.rdf.ontologies.{RDF, RDFS, FOAF}

object EasyGraph {
        def lit(str: String) =  new PlainLiteralImpl(str)
        final val en = "en"
        final val de = "de"
        final val fr = "fr"

        implicit def string2lit(str: String) = new PlainLiteralScala(str)


        // example using old graph notation
        // of course the add method could be overloaded to take triples, but it 
is still very repetitive

        val gr = new SimpleMGraph
        val subj= new BNode()
        gr.add(new TripleImpl(sub,RDF.`type`, FOAF.Person))
        gr.add(new TripleImpl(sub,FOAF.knows, new 
UriRef("http://bblfish.net/#hjs";)))
        gr.add(new TripleImpl(sub,FOAF.name, new PlainLiteralImpl("Henry 
Story","en")))
        val other = new BNode()
        gr.add(new TripleImpl(sub,FOAF.knows, other))
        gr.add(new TripleImpl(sub,FOAF.name,new PlainLiteralImpl("Danny 
Ayers")))

        val g = new EasyGraph(new SimpleMGraph)
        
        // example using arrows
                (
                        g.bnode a FOAF.Person
                           ⟝ FOAF.knows ⟶  "http://bblfish.net/#hjs".uri
                           ⟝ FOAF.name ⟶ "Henry Story"(en) 
                           ⟝ FOAF.knows ⟶ ( g.bnode ⟝ FOAF.name ⟶ "Danny Ayers" 
)
                )

        // example using just brackets ( the apply() method )
        ( 
g.bnode(FOAF.knows)("http://bblfish.net/#hjs".uri,"http://farewellutopia.com/#me".uri)
                      (FOAF.knows)(g.bnode(FOAF.name)("Danny Ayers"(en)))
        )

        //Example using english easy to type non unicode syntax
        ( g.u("http://bblfish.net/#hjs";) a FOAF.Person
                 has FOAF.knows toUris 
Seq("http://www.w3.org/People/Connolly/#me";, "http://farewellutopia.com/#me";)
                 has FOAF.name to "Henry Story"
                )
// should work like http://programming-scala.labs.oreilly.com/ch11.html

}

class PlainLiteralScala(string: String) extends PlainLiteralImpl(string) {

        def apply(lang: String) = new PlainLiteralImpl(string, lang)
        def ^^(typ: UriRef) = new TypedLiteralImpl(string, typ)
        def uri = new UriRef(string)

}

/**
 *  This is really a TripleCollection
 *
 * @author hjs
 * @created: 20/04/2011
 */

class EasyGraph(val graph: TripleCollection) {

        def +=(sub: SubjectGraph) = {
                  if (graph ne  sub.graph) graph.addAll(sub.graph)
        }

         def bnode : SubjectGraph = {
                new SubjectGraph(new BNode(), graph)
         }

        def u(url: String) = new SubjectGraph(new UriRef(url),graph)

}

/**
 * This is really a GraphNode
 *
 */
class SubjectGraph(val ref: NonLiteral, val graph: TripleCollection) {

        def this(s: NonLiteral)  = this(s,new SimpleMGraph())
        def this() = this(new BNode)

        def apply(rel: UriRef): Predicate = has(rel)
        def apply(rel: String): Predicate = has(rel)

        def has(rel: UriRef) = new Predicate(rel)
        def has(rel: String) = new Predicate(new UriRef(rel))

        def ⟝(rel: UriRef) = apply(rel)
        def ⟝(rel: String) = apply(rel)


        def +(sub: SubjectGraph) = {
                  if (graph ne sub.graph) graph.addAll(sub.graph)
                  this
        }

        def a(rdfclass: UriRef) : SubjectGraph = {
                graph.add(new TripleImpl(ref,RDF.`type`,rdfclass))
                return SubjectGraph.this
        }

        class Predicate(rel: UriRef) {

                //
                // methods that do the work
                //
                def to(obj: Resource): SubjectGraph = add(obj)
                def to(objs: Resource*): SubjectGraph = {
                        for (o <- objs) add(o)
                        SubjectGraph.this
                }
                def to(uri: String) : SubjectGraph = add(new 
PlainLiteralImpl(uri))
                def to (sub: SubjectGraph): SubjectGraph = {
                        SubjectGraph.this + sub
                        add(sub.ref)
                }

                def toUri(uri: String) = add(new UriRef(uri))
                def toUris(uris: Seq[String]) = {
                        for (u <- uris) add(new UriRef(u))
                        SubjectGraph.this
                }


                //
                //apply method allows turns brackets () into an equivalent of 
<rel>
                //
                def apply(obj: Resource) = to(obj)
                def apply(objs: Resource*) = to(objs: _*)
                def apply(uri: String) = to(uri)
                def apply(sub: SubjectGraph) = to(sub)


                //
                // arrow notation
                //

                def ⟶ (obj: Resource) = apply(obj)
                def ⟶ (sub: SubjectGraph) = to(sub)

                private def add(obj: Resource) =  {
                        graph.add(new TripleImpl(ref,rel,obj))
                        SubjectGraph.this
                }

        }

}



On 22 Apr 2011, at 18:06, Paul Phillips wrote:

> On 4/22/11 8:44 AM, bblfish wrote:
>> So it does not like the "to" in "colon to"
> 
> It does not like your attempt to pass "to" as an argument to "colon".
> 
>> But if I cut it up into a smaller piece, and stop right after the
>> colon, giving that object a name, giving me what I expect.
> 
> It isn't that you gave it a name, it's that you gave it a newline.
> 
> As detailed elsewhere, it always parses the same:
> 
>  a.b(c)
>  a.b(c).d
>  a.b(c).d(e)
>  a.b(c).d(e).f
> 
> With "colon to" you are hoping it will do this:
> 
>  a.b(c).d(e).f.g
> 
> And it won't.  All your nullary methods:
> 
>>                                 def dot  =
>>                                 def semicolon = Subject.this
>>                                 def colon = Predicate.this
> 
> are effectively terminators when it comes to punctuation free parsing.

Attachment: smime.p7s
Description: S/MIME cryptographic signature

Reply via email to