+ start on turtle serializer
authorEric Prud'hommeaux <eric@w3.org>
Sat, 12 Feb 2011 10:33:58 -0500
changeset 324 ba2fb49e1ac3
parent 323 d912db661b28
child 325 ac3424ce78d8
+ start on turtle serializer
turtle-test/src/main/scala/TurtleTestSuite.scala
turtle/src/main/scala/turtle.scala
--- a/turtle-test/src/main/scala/TurtleTestSuite.scala	Sat Feb 12 10:33:24 2011 -0500
+++ b/turtle-test/src/main/scala/TurtleTestSuite.scala	Sat Feb 12 10:33:58 2011 -0500
@@ -239,3 +239,49 @@
 
 }
 
+class TurtleSerializerTest () extends FunSuite with TurtleModule with ConcreteModel with RDFImplicits {
+
+  val TurtleParser = new TurtleParser {  }
+
+  val Ser = new TurtleSerializer()
+
+  test("directgraph_serializer1") {
+    val turtle_serializer1:String = """
+@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
+<Addresses/ID.18#_> <ExecutiveOffices#ID> "18"^^xsd:integer .
+<Addresses/ID.18#_> <ExecutiveOffices#desk> "oak"^^xsd:string . 
+
+<Addresses/ID.18#_> <Offices#ID> "18"^^xsd:integer .
+<Addresses/ID.18#_> <Offices#building> "32"^^xsd:integer .
+<Addresses/ID.18#_> <Offices#ofcNumber> "G528"^^xsd:string . 
+
+<People/ID.7#_> <People#ID> "7"^^xsd:integer .
+<People/ID.7#_> <People#fname> "Bob"^^xsd:string .
+<People/ID.7#_> <People#addr> <Addresses/ID.18#_> .
+
+<Addresses/ID.18#_> <Addresses#ID> "18"^^xsd:integer .
+<Addresses/ID.18#_> <Addresses#city> "Cambridge"^^xsd:string .
+<Addresses/ID.18#_> <Addresses#state> "MA"^^xsd:string .
+"""
+    val directgraph_serializer1:Graph =
+      Graph(
+	Triple(IRI("Addresses/ID.18#_"),IRI("ExecutiveOffices#ID"),TypedLiteral("18",IRI("http://www.w3.org/2001/XMLSchema#integer"))),
+	Triple(IRI("Addresses/ID.18#_"),IRI("ExecutiveOffices#desk"),TypedLiteral("oak",IRI("http://www.w3.org/2001/XMLSchema#string"))), 
+
+	Triple(IRI("Addresses/ID.18#_"),IRI("Offices#ID"),TypedLiteral("18",IRI("http://www.w3.org/2001/XMLSchema#integer"))),
+	Triple(IRI("Addresses/ID.18#_"),IRI("Offices#building"),TypedLiteral("32",IRI("http://www.w3.org/2001/XMLSchema#integer"))),
+	Triple(IRI("Addresses/ID.18#_"),IRI("Offices#ofcNumber"),TypedLiteral("G528",IRI("http://www.w3.org/2001/XMLSchema#string"))), 
+
+	Triple(IRI("People/ID.7#_"),IRI("People#ID"),TypedLiteral("7",IRI("http://www.w3.org/2001/XMLSchema#integer"))),
+	Triple(IRI("People/ID.7#_"),IRI("People#fname"),TypedLiteral("Bob",IRI("http://www.w3.org/2001/XMLSchema#string"))),
+	Triple(IRI("People/ID.7#_"),IRI("People#addr"),IRI("Addresses/ID.18#_")),
+
+	Triple(IRI("Addresses/ID.18#_"),IRI("Addresses#ID"),TypedLiteral("18",IRI("http://www.w3.org/2001/XMLSchema#integer"))),
+	Triple(IRI("Addresses/ID.18#_"),IRI("Addresses#city"),TypedLiteral("Cambridge",IRI("http://www.w3.org/2001/XMLSchema#string"))),
+	Triple(IRI("Addresses/ID.18#_"),IRI("Addresses#state"),TypedLiteral("MA",IRI("http://www.w3.org/2001/XMLSchema#string")))
+      )
+    assert(Ser.graphString(directgraph_serializer1) === turtle_serializer1)
+  }
+
+}
+
--- a/turtle/src/main/scala/turtle.scala	Sat Feb 12 10:33:24 2011 -0500
+++ b/turtle/src/main/scala/turtle.scala	Sat Feb 12 10:33:58 2011 -0500
@@ -96,6 +96,50 @@
     )
   }
 
+  case class TurtleSerializer () {
+    def graphString (g:Graph):String = {
+      g map { t => {
+	val Triple(SubjectNode(s), PredicateIRI(p), o) = t
+	nodeStr(s) + " " + iriStr(p) + " " + objectStr(o)
+      }+".\n" } mkString("")
+    }
+
+    def objectStr (n:Object) = {
+      n match {
+	// case l:ObjectLiteral => {
+	//   val x:ObjectLiteral = l
+	//   "**ObjectLiteral(" + x + ")**"
+	// }
+	case ObjectNode(n) => nodeStr(n)
+	case ObjectLiteral(l) => literalStr(l)
+	// case x => x
+      }
+    }
+
+    def nodeStr (n:Node) = {
+      n match {
+	case NodeIRI(i) => iriStr(i)
+	case NodeBNode(b) => bnodeStr(b)
+      }
+    }
+
+    def iriStr (i:IRI) =
+      "<" + {
+	val IRI(s) = i
+	s
+      } + ">"
+
+    def bnodeStr (b:BNode) =
+      "_:" + {
+	val BNode(l) = b
+	l
+      }
+ 
+    def literalStr (l:Literal) = { l
+    }
+
+  }
+
 }