fix the SPARQL parser + introduced datatype notion
authorAlexandre Bertails <bertails@w3.org>
Thu, 10 Dec 2009 13:47:27 -0500
changeset 7 039d066f884e
parent 6 12d498689ec8
child 8 db0e7dbf156a
fix the SPARQL parser + introduced datatype notion
src/main/scala/RDB2RDFMain.scala
src/main/scala/SPARQL.scala
src/test/scala/RDB2RDFTest.scala
--- a/src/main/scala/RDB2RDFMain.scala	Wed Dec 09 19:39:58 2009 -0500
+++ b/src/main/scala/RDB2RDFMain.scala	Thu Dec 10 13:47:27 2009 -0500
@@ -10,3 +10,25 @@
 }
 
 import MyParsers._
+
+// case class RDFTriple()
+
+// sealed abstract class RDFSubject()
+// case class RDFSubjectUri(uri:URI) extends RDFSubject
+// case class RDFSubjectBlankNode(debugName:String) extends RDFSubject
+
+// case class RDFPredicate(uri:URI)
+
+// sealed abstract class RDFObject()
+// case class RDFObjectUri(uri:URI) extends RDFObject
+// case class RDFObjectBlankNode(debugName:String) extends RDFObject
+// case class RDFLiteral(lexicalForm:Literal, datatype:URI)
+
+// case class RelationStemGraph(m:Map[String,String])
+// case class RelationMap(Map[(FQAttribute,Tuple),RDFTriple])
+
+// case class Tuple()
+// case class StemURI()
+// case class NodeMap()
+// case class PredicateMap()
+// case class LiteralMap()
--- a/src/main/scala/SPARQL.scala	Wed Dec 09 19:39:58 2009 -0500
+++ b/src/main/scala/SPARQL.scala	Thu Dec 10 13:47:27 2009 -0500
@@ -5,6 +5,12 @@
 
 import MyParsers._
 
+// object SparqlTypeAliases {
+//   type TriplePatterns = List[TriplePattern]
+// }
+
+// import SpqrqlTypeAliases._
+
 case class TriplePatterns(triplepatterns:List[TriplePattern])
 case class TriplePattern(s:S, p:P, o:O)
 
@@ -21,9 +27,16 @@
 case class PUri(stem:Stem, rel:Rel, attr:Attr) extends P
 case class PVar(v:Var) extends P
 
-sealed abstract class Lit
-case class LitInt(i:Int) extends Lit
-case class LitString(s:String) extends Lit
+// sealed abstract class Lit
+// case class LitInt(i:Int) extends Lit
+// case class LitString(s:String) extends Lit
+case class Lit(lexicalForm:String, datatype:Datatype)
+case class Datatype(uri:URI)
+
+object Lit {
+  val StringDatatype = Datatype(new URI("http://www.w3.org/2001/XMLSchema#string"))
+  val IntegerDatatype = Datatype(new URI("http://www.w3.org/2001/XMLSchema#integer"))
+}
 
 case class Stem(s:String)
 case class Attr(s:String)
@@ -53,7 +66,15 @@
     | literal ^^ { x => OLit(x) }
   )
 
-  def literal:Parser[Lit] = stringLiteral ^^ { x => null }
+// case class Lit(lexicalForm:String, datatype:Datatype)
+// case class Datatype(uri:URI)
+
+  def literal:Parser[Lit] = (
+      stringLiteral~"^^<http://www.w3.org/2001/XMLSchema#string>" ^^
+      { case lit ~ _ => Lit(lit.substring(1,lit.size - 1), Lit.StringDatatype) }
+    | stringLiteral~"^^<http://www.w3.org/2001/XMLSchema#integer>" ^^
+      { case lit ~ _ => Lit(lit.substring(1,lit.size - 1), Lit.IntegerDatatype) }
+)
 
   def varr:Parser[Var] = "?"~ident ^^ { case "?"~x => Var(x) }
 
--- a/src/test/scala/RDB2RDFTest.scala	Wed Dec 09 19:39:58 2009 -0500
+++ b/src/test/scala/RDB2RDFTest.scala	Thu Dec 10 13:47:27 2009 -0500
@@ -5,39 +5,6 @@
 
 class TestBank extends FunSuite {
 
-  test("parse a triplepatterns") {
-    val a = Sparql()
-    val e = """
-?emp      <http://hr.example/DB/Employee#lastName>   ?empName .
-?emp      <http://hr.example/DB/Employee#manager>    ?manager .
-?manager  <http://hr.example/DB/Employee#lastName>   ?managName
-"""
-    val tps =
-      TriplePatterns(
-	List(
-	  TriplePattern(
-	    SVar(Var("emp")),
-	    PUri(Stem("http://hr.example/DB"),Rel("Employee"),Attr("lastName")),
-	    OVar(Var("empName"))),
-	  TriplePattern(
-	    SVar(Var("emp")),
-	    PUri(Stem("http://hr.example/DB"),Rel("Employee"),Attr("manager")),
-	    OVar(Var("manager"))),
-	  TriplePattern(
-	    SVar(Var("manager")),
-	    PUri(Stem("http://hr.example/DB"),Rel("Employee"),Attr("lastName")),
-	    OVar(Var("managName")))))
-    assert(tps === a.parseAll(a.triplepatterns, e).get)
-  }
-
-  test("decompose a predicate uri in stem, rel and attr") {
-    val uri = "http://hr.example/DB/Employee#lastName"
-    val puri:PUri = Sparql.parsePredicateURI(uri)
-    assert(puri === PUri(Stem("http://hr.example/DB"),
-			 Rel("Employee"),
-			 Attr("lastName")))
-  }
-
   test("SQLbgp") {
     val a = Sql()
     val e = """