--- a/turtle/src/main/scala/turtle.scala Wed Dec 29 19:03:39 2010 -0500
+++ b/turtle/src/main/scala/turtle.scala Wed Dec 29 19:04:43 2010 -0500
@@ -1,22 +1,24 @@
package org.w3.sw.turtle
-import org.w3.sw.rdf._
+import org.w3.sw.RDFModel
+//import org.w3.sw.{rdf => RDFModel}
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 name = """[a-zA-Z][a-zA-Z0-9_-]*|[a-zA-Z_][a-zA-Z0-9_]+""".r
var prefixes:Map[String, String] = Map()
- var bnodes:Map[String, BNode] = Map()
var nextBNode = 1
}
import MyParsers._
-case class Turtle() extends JavaTokenParsers {
+case class Turtle(RDFModel:RDFModel) extends JavaTokenParsers {
+//case class Turtle() extends JavaTokenParsers {
+
+ import RDFModel._
def toGraph(t:String):Graph = parseAll(turtle, t).get
@@ -26,13 +28,13 @@
}
def turtle:Parser[Graph] =
- opt(triplesblock) ^^ { case tbOPT => tbOPT.getOrElse(Set[Triple]()) }
+ opt(triplesblock) ^^ { case tbOPT => tbOPT.getOrElse(Graph.empty) }
def prefixdecl:Parser[Unit] =
- "@prefix" ~ name ~ ":" ~ qnameORuri ~ "." ^^ { case "@prefix"~pre~":"~u~"." => prefixes += (pre -> u.iri) }
+ "@prefix" ~ name ~ ":" ~ qnameORuri ~ "." ^^ { case "@prefix"~pre~":"~u~"." => prefixes += (pre -> { val IRI(iri) = u ; iri }) }
def triplesblock:Parser[Graph] =
- rep(triplepatternOrPrefixOrBase) ^^ { case pats => pats.flatten.toSet }
+ rep(triplepatternOrPrefixOrBase) ^^ { case pats => Graph(pats.flatten) }
def triplepatternOrPrefixOrBase:Parser[Option[Triple]] = (
triplepattern ^^ { case p => Some(p) }
@@ -49,12 +51,12 @@
def predicate:Parser[Predicate] = (
qnameORuri ^^ { case x => PredicateIRI(x) }
- | "a" ^^ { x => IRI("http://www.w3.org/1999/02/22-rdf-syntax-ns#type") }
+ | "a" ^^ { x => PredicateIRI(IRI("http://www.w3.org/1999/02/22-rdf-syntax-ns#type")) }
)
def objectt:Parser[Object] = (
- qnameORuri ^^ { case x => ObjectNode(x) }
- | bnode ^^ { case x => ObjectNode(x) }
+ qnameORuri ^^ { case x => ObjectNode(NodeIRI(x)) }
+ | bnode ^^ { case x => ObjectNode(NodeBNode(x)) }
| literal ^^ { case x => ObjectLiteral(x) }
)
@@ -76,11 +78,11 @@
def literal:Parser[Literal] = (
stringLiteral~"^^"~qnameORuri ^^
{
- case lit~"^^"~dt => TypedLiteral(lit.substring(1,lit.size - 1), dt.iri match {
- case "http://www.w3.org/2001/XMLSchema#string" => StringDatatype
- case "http://www.w3.org/2001/XMLSchema#integer" => IntegerDatatype
- case "http://www.w3.org/2001/XMLSchema#date" => DateDatatype
- // case "http://www.w3.org/2001/XMLSchema#dateTime" => DateTimeDatatype
+ case lit~"^^"~dt => TypedLiteral(lit.substring(1,lit.size - 1), dt match {
+ case IRI("http://www.w3.org/2001/XMLSchema#string") => StringDatatype
+ case IRI("http://www.w3.org/2001/XMLSchema#integer") => IntegerDatatype
+ case IRI("http://www.w3.org/2001/XMLSchema#date" ) => DateDatatype
+ case "http://www.w3.org/2001/XMLSchema#dateTime" => DateTimeDatatype
case x => error("only programed to deal with string and integer, not " + x)
})
}
--- a/turtle/src/test/scala/turtleTest.scala Wed Dec 29 19:03:39 2010 -0500
+++ b/turtle/src/test/scala/turtleTest.scala Wed Dec 29 19:04:43 2010 -0500
@@ -1,16 +1,19 @@
package org.w3.sw.turtle
-import org.w3.sw.rdf._
-
+import org.w3.sw.{RDF, RDFModel, RDFImplicits}
import org.scalatest.FunSuite
-class TurtleTest extends FunSuite {
+class TurtleTestWithRDF extends TurtleTest(new RDF with RDFImplicits { })
- val turtleParser = Turtle()
+abstract class TurtleTest(RDFModel:RDFModel with RDFImplicits) extends FunSuite {
+
+ import RDFModel._
+
+ val turtleParser = Turtle(RDFModel)
test("directgraph_emp_adder") {
val directgraph_emp_adder:Graph =
- Set(
+ Graph(
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#_")),
@@ -38,7 +41,7 @@
test("directgraph_multi_key") {
val directgraph_multi_key:Graph =
- Set(
+ Graph(
Triple(IRI("People/ID.7#_"),IRI("People#deptName"),TypedLiteral("accounting",IRI("http://www.w3.org/2001/XMLSchema#string"))),
Triple(IRI("People/ID.7#_"),IRI("People#deptCity"),TypedLiteral("Cambridge",IRI("http://www.w3.org/2001/XMLSchema#string"))),
Triple(IRI("People/ID.7#_"),IRI("People#deptName_deptCity"),IRI("Department/ID.23#_")),
@@ -84,7 +87,7 @@
test("directgraph_ref_no_pk") {
val directgraph_ref_no_pk:Graph =
- Set(
+ Graph(
Triple(BNode("a"), IRI("Projects#lead"), IRI("People/ID.8#_")),
Triple(BNode("a"), IRI("Projects#name"), TypedLiteral("pencil survey", IRI("http://www.w3.org/2001/XMLSchema#string"))),
Triple(BNode("a"), IRI("Projects#deptName"), TypedLiteral("accounting", IRI("http://www.w3.org/2001/XMLSchema#string"))),
@@ -169,7 +172,7 @@
test("directgraph_hier_tabl_proto") {
val directgraph_hier_tabl_proto:Graph =
- Set(
+ Graph(
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"))),
@@ -201,7 +204,7 @@
test("directgraph_hier_tabl") {
val directgraph_hier_tabl:Graph =
- Set(
+ 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"))),