- turtle.scala.egp webacl
authorAlexandre Bertails <bertails@w3.org>
Mon, 21 Nov 2011 17:37:09 -0500
branchwebacl
changeset 138 caf19fefff4b
parent 137 771587831866
- turtle.scala.egp
src/main/scala/rdf/main.scala
src/main/scala/rdf/turtle.scala.egp
--- a/src/main/scala/rdf/main.scala	Mon Nov 21 15:31:17 2011 -0500
+++ b/src/main/scala/rdf/main.scala	Mon Nov 21 17:37:09 2011 -0500
@@ -3,78 +3,11 @@
 import java.io.File
 import com.hp.hpl.jena.graph.Graph
 
-// afaik, the compiler doesn't not expose the unapply method
-// for a companion object
-trait Isomorphic[A, B] {
-  def apply(x: A): B
-  def unapply(x: B): Option[A]
-}
-
-// abstract module
-trait Module {
-  // 3 types with some contraints
-  type X
-  type Y <: X
-  type Z <: X
-  // and their "companion" objects
-  def X: Isomorphic[Int, X]
-  def Y: Isomorphic[X, Y]
-  def Z: Isomorphic[Y, Z]
-}
-
-// an implementation relying on case classes
-object ConcreteModule extends Module {
-  sealed trait X { val i: Int = 42 }
-  object X extends Isomorphic[Int, X] {
-    def apply(_s: Int): X = new X { }
-    def unapply(x: X): Option[Int] = Some(x.i)
-  }
-  case class Y(x: X) extends X
-  // I guess the compiler could do that for me
-  object Y extends Isomorphic[X, Y]
-  case class Z(y: Y) extends X
-  object Z extends Isomorphic[Y, Z]
-}
-
-object Main2 {
-  def foo(t: Module)(x: t.X): Unit = {
-    import t._
-    // the output depends on the order of the first 3 lines
-    // I'm not sure what's happening here...
-    x match {
-      // unchecked since it is eliminated by erasure
-      case Y(_y) => println("y "+_y)
-      // unchecked since it is eliminated by erasure
-      case Z(_z) => println("z "+_z)
-      // this one is fine
-      case X(_x) => println("x "+_x)
-      case xyz => println("xyz "+xyz)
-    }
-  }
-  def bar(t: Module): Unit = {
-    import t._
-    val x: X = X(42)
-    val y: Y = Y(x)
-    val z: Z = Z(y)
-    foo(t)(x)
-    foo(t)(y)
-    foo(t)(z)
-  }
-  def main(args: Array[String]) = {
-    // call bar with the concrete module
-    bar(ConcreteModule)
-  }
-}
 
 object Main {
   
   def main(args: Array[String]) = {
     
-//    
-//    val turtle = new org.w3.rdf.turtle.TurtleParser(org.w3.rdf.jena.JenaModel)
-//    
-//    val g = turtle.toGraph(new File("/tmp/card.n3"))
-
     //import org.w3.rdf.jena.JenaModel
     //val rdf = ConcreteRDFModel
     val rdf = org.w3.rdf.jena.JenaModel
@@ -88,13 +21,6 @@
     
     println(s.asString(g, new java.net.URL("http://www.w3.org/People/Berners-Lee/card")))
     
-//    import rdf._
-//    val ol = ObjectLiteral(PlainLiteral("The Next Wave of the Web (Plenary Panel)",None))
-//    
-//    println(s.objectStr(ol))
-    
-    
-    
   }
   
   
--- a/src/main/scala/rdf/turtle.scala.egp	Mon Nov 21 15:31:17 2011 -0500
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,145 +0,0 @@
-package org.w3.rdf.turtle
-
-import org.w3.rdf._
-import scala.util.parsing.combinator._
-import java.net.URI
-
-object MyParsers extends RegexParsers {
-  val uri = """[a-zA-Z0-9:/#_\.\-\+]+""".r
-  val integer = """[0-9]+""".r
-  val float = """([0-9]+)?\.([0-9]+)?""".r
-  val name = """[a-zA-Z][a-zA-Z0-9_-]*|[a-zA-Z_][a-zA-Z0-9_]+""".r
-  var prefixes:Map[String, String] = Map()
-  var nextBNode = 1
-}
-
-import MyParsers._
-
-class TurtleParser extends JavaTokenParsers {
-  
-  import rdf._
-  
-  def toGraph(t:String)(implicit rdf: RDFModel): rdf.Graph = parseAll(turtle, t).get
-
-  def toGraph(file:java.io.File)(implicit rdf: RDFModel): rdf.Graph = {
-    val t = scala.io.Source.fromFile(file).getLines.reduceLeft(_+_)
-    parseAll(turtle, t).get
-  }
- 
-  def turtle(implicit rdf: RDFModel): Parser[rdf.Graph] =
-    opt(triplesblock) ^^ { case tbOPT => tbOPT.getOrElse(rdf.Graph.empty) }
-    
-  def prefixdecl(implicit rdf: RDFModel): Parser[Unit] =
-    "@prefix" ~ name ~ ":" ~ qnameORuri ~ "." ^^ { case "@prefix"~pre~":"~u~"." => prefixes += (pre -> { val rdf.IRI(i: String) = u ; i }) }
-    
-  def triplesblock(implicit rdf: RDFModel): Parser[rdf.Graph] =
-    rep(triplepatternOrPrefixOrBase) ^^ {
-      case pats => rdf.Graph(pats.flatten(_.toTraversable))
-    }
-    
-    def triplepatternOrPrefixOrBase(implicit rdf: RDFModel): Parser[Option[rdf.Triple]] = (
-        triplepattern ^^ { case p => Some(p) }
-      | prefixdecl ^^ { case _ => None }
-    )
-      
-    def triplepattern(implicit rdf: RDFModel): Parser[rdf.Triple] =
-      subject ~ predicate ~ objectt ~ "." ^^ { case s~p~o~"." => rdf.Triple(s, p, o) }
-  
-    def bnode(implicit rdf: RDFModel): Parser[rdf.BNode] =
-      "_:"~name ^^ { case "_:"~name => rdf.BNode(name) }
-
-    def literal(implicit rdf: RDFModel): Parser[rdf.Literal] = (
-        stringLiteral~"^^"~qnameORuri ^^ {
-        case lit~"^^"~dt => rdf.TypedLiteral(lit.substring(1,lit.size - 1), dt match {
-          case rdf.IRI("http://www.w3.org/2001/XMLSchema#string") => rdf.StringDatatype
-          case rdf.IRI("http://www.w3.org/2001/XMLSchema#integer") => rdf.IntegerDatatype
-          case rdf.IRI("http://www.w3.org/2001/XMLSchema#float") => rdf.FloatDatatype
-          case rdf.IRI("http://www.w3.org/2001/XMLSchema#date") => rdf.DateDatatype
-          case rdf.IRI("http://www.w3.org/2001/XMLSchema#dateTime") => rdf.DateTimeDatatype
-          case x => sys.error("only programed to deal with string and integer, not " + x)
-        })
-      } |
-        stringLiteral ^^ {
-        case lit => rdf.PlainLiteral(lit.substring(1,lit.size - 1), None)
-      } |
-        float ^^ { l => rdf.TypedLiteral(l, rdf.FloatDatatype) } |
-        integer ^^ { l => rdf.TypedLiteral(l, rdf.IntegerDatatype) }
-    )
-
-    def subject(implicit rdf: RDFModel): Parser[rdf.Subject] = {
-        qnameORuri ^^ { case x => rdf.SubjectNode(rdf.NodeIRI(x)) } |
-        bnode ^^ { case x => rdf.SubjectNode(rdf.NodeBNode(x)) }
-    }
-      
-    def predicate(implicit rdf: RDFModel): Parser[rdf.Predicate] = (
-        qnameORuri ^^ { case x => rdf.PredicateIRI(x) }
-      | "a" ^^ { x => rdf.PredicateIRI(rdf.IRI("http://www.w3.org/1999/02/22-rdf-syntax-ns#type")) }
-    )
-      
-    def objectt(implicit rdf: RDFModel): Parser[rdf.Object] = {
-        qnameORuri ^^ { case x => rdf.ObjectNode(rdf.NodeIRI(x)) } |
-        bnode ^^ { case x => rdf.ObjectNode(rdf.NodeBNode(x)) } |
-        literal ^^ { case x => rdf.ObjectLiteral(x) }
-    }
-  
-    def qnameORuri(implicit rdf: RDFModel): Parser[rdf.IRI] = (
-        "<"~uri~">" ^^ { case "<"~x~">" => rdf.IRI(x) } |
-        name~":"~name ^^ {
-        case prefix~":"~localName => try {
-          rdf.IRI(prefixes(prefix) + localName)
-        } catch {
-          case e:java.util.NoSuchElementException =>
-            throw new Exception("unknown prefix " + prefix)
-        }
-      }
-    )
-  
-}
-
-// as the Module type does not escape from any method here, we can pass it at the constructor level
-class TurtleSerializer[M <: RDFModel] {
-  
-  type M = ConcreteRDFModel
-  
-  def showAsString(rdf: M)(g: rdf.Graph): String = {
-    g map {
-      t => {
-        println(t)
-        val rdf.Triple(rdf.SubjectNode(s), rdf.PredicateIRI(p), o) = t
-        println(s)
-        println(p)
-        println(o)
-        "%s %s %s" format (nodeStr(rdf)(s), iriStr(rdf)(p), objectStr(rdf)(o))
-      }
-    } mkString "\n"
-  }
-
-  private def objectStr(rdf: M)(n: rdf.Object): String = {
-    n match {
-      // case l:ObjectLiteral => {
-      //   val x:ObjectLiteral = l
-      //   "**ObjectLiteral(" + x + ")**"
-      // }
-      case rdf.ObjectNode(n) => nodeStr(rdf)(n)
-      case rdf.ObjectLiteral(l) => literalStr(rdf)(l)
-      // case x => x
-    }
-  }
-
-  private def nodeStr(rdf: M)(n: rdf.Node): String = {
-    n match {
-      case rdf.NodeIRI(i) => iriStr(rdf)(i)
-      case rdf.NodeBNode(b) => bnodeStr(rdf)(b)
-    }
-  }
-
-  private def iriStr(rdf: M)(i: rdf.IRI): String =
-    "<%s>" format { val rdf.IRI(s) = i; s }
-
-  private def bnodeStr(rdf: M)(b: rdf.BNode): String =
-    "_:" + { val rdf.BNode(l) = b; l }
- 
-  private def literalStr(rdf: M)(l: rdf.Literal): String = l.toString
-
-}
-