I started writing a turtle DSL for Scala. It's really pretty simple at present. 
I was wondering how we could get it to improve. See 
http://programming-scala.labs.oreilly.com/ch11.html for example

/** Apache licence of course */

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

import org.apache.clerezza.rdf.core._
import collection.mutable.Queue
import impl.{SimpleMGraph, PlainLiteralImpl, TripleImpl}
import org.apache.clerezza.rdf.ontologies.FOAF

object Test {
        val g = new MutableGraphNode(new SimpleMGraph)


        g.bnode.rel(FOAF.knows).to("http://bblfish.net/#hjs";).colon.
                                     to("http://reto.com/#me";).semicolon.
                     rel(FOAF.name).to("Joe Schmidt").dot

}

/*
It would be nice if one could write the above without the dots, like this 
perhaps

  g bnode rel(FOAF.knows) to("http://bblfish.net/#hjs";) colon.
                          to("http://reto.com/#me";) semicolon.
          rel(FOAF.name)  to("Joe Schmidt") dot

 I am trying to get as close to

  [] foaf:knows <http://bblfish.net/#hjs>, <http://reto.com/#me>;
     foaf:name "Joe Schmidt" .

as possible

*/


class MutableGraphNode(val graph: TripleCollection) {
          var currentNode: Resource = _

         def bnode : Subject = {
                new Subject(new BNode())
        }

        class Subject(s: NonLiteral) {

                val predicates = new Queue[Predicate]

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

                private def add(p: Predicate) = {
                        predicates += p
                        p
                }

                private def save {
                        for (p <- predicates) p.save
                }

                class Predicate(r: UriRef) {

                        val statements = new Queue[FlexiTriple]

                        def to(obj: Resource): FlexiTriple = add(new 
FlexiTriple(obj))

                        def to(strlit: String): FlexiTriple = add(new 
FlexiTriple(new PlainLiteralImpl(strlit)))

                        def toRef(uri: String) = add(new FlexiTriple(new 
UriRef(uri)))

                        private def add(tr: FlexiTriple) =  {
                                statements += tr
                                tr
                        }

                        private[Subject] def save {
                                for (s <- statements) s.save
                        }

                        class FlexiTriple(obj: Resource) extends 
TripleImpl(s,r,obj) {

                                 def dot { Subject.this.save }

                                 def semicolon = Subject.this

                                 def colon = Predicate.this

                                 private[Predicate] def save {
                                         graph.add(this)
                                 }
                        }

                }

        }

}


Social Web Architect
http://bblfish.net/

Reply via email to