migrating towards real SPARQL algebra
authorEric Prud'hommeaux <bertails@w3.org>
Sun, 27 Dec 2009 15:05:58 -0800
changeset 73 853d9bcb1e41
parent 72 ef8cd60aa260
child 74 462bd3ddfc3f
migrating towards real SPARQL algebra
src/main/scala/SPARQL.scala
src/test/scala/RDB2RDFTest.scala
src/test/scala/SparqlTest.scala
--- a/src/main/scala/SPARQL.scala	Sun Dec 27 14:42:03 2009 -0800
+++ b/src/main/scala/SPARQL.scala	Sun Dec 27 15:05:58 2009 -0800
@@ -11,10 +11,10 @@
 
 import MyParsers._
 
-case class SparqlSelect(attrs:SparqlAttributeList, triples:TriplePatterns)
+case class SparqlSelect(attrs:SparqlAttributeList, triples:BasicGraphPattern)
 case class SparqlAttributeList(attributelist:List[Var])
 
-case class TriplePatterns(triplepatterns:List[TriplePattern], filter:SparqlExpression)
+case class BasicGraphPattern(triplepatterns:List[TriplePattern], filter:SparqlExpression)
 case class TriplePattern(s:S, p:P, o:O)
 
 case class ObjUri(stem:Stem, rel:Rel, attr:Attr, v:CellValue)
@@ -57,7 +57,7 @@
 case class Sparql() extends JavaTokenParsers {
 
   def select:Parser[SparqlSelect] =
-    "SELECT" ~ attributelist ~ "{" ~ triplepatterns ~ "}" ^^ { case "SELECT"~a~"{"~t~"}" => SparqlSelect(a, t) }
+    "SELECT" ~ attributelist ~ "{" ~ basicgraphpattern ~ "}" ^^ { case "SELECT"~a~"{"~t~"}" => SparqlSelect(a, t) }
 
   def filter:Parser[SparqlExpression] =
     "FILTER" ~ "(" ~ expression ~ ")" ^^ { case "FILTER"~"("~expression~")" => expression }
@@ -82,14 +82,14 @@
   def attributelist:Parser[SparqlAttributeList] =
     rep(varr) ^^ { SparqlAttributeList(_) }
 
-  def triplepatterns:Parser[TriplePatterns] =
+  def basicgraphpattern:Parser[BasicGraphPattern] =
     repsep(triplepattern, ".") ~ opt(filter) ^^ {
       case pats~filter =>
 	val sparqlExpression:SparqlExpression = filter match {
 	  case None => SparqlExpression(List())
 	  case Some(f) => f
 	}
-      TriplePatterns(pats, sparqlExpression)
+      BasicGraphPattern(pats, sparqlExpression)
     }
 
   def triplepattern:Parser[TriplePattern] =
--- a/src/test/scala/RDB2RDFTest.scala	Sun Dec 27 14:42:03 2009 -0800
+++ b/src/test/scala/RDB2RDFTest.scala	Sun Dec 27 15:05:58 2009 -0800
@@ -204,4 +204,36 @@
     assert(RDB2RDF(db2, sparqlSelect, StemURI("http://hr.example/DB/"), PrimaryKey(Attribute(Name("id")))) === sqlSelect)
   }
 
+//   test("transform disj1") {
+//     val sparqlParser = Sparql()
+//     val sparqlSelect = sparqlParser.parseAll(sparqlParser.select, """
+// SELECT ?name
+//  WHERE { ?who emplP:lastName "Smith"
+//          { ?above   manageP:manages ?who .
+//            ?above   manageP:manager ?manager .
+//            ?manager emplP:lastName  ?name }
+//          UNION
+//          { ?below   manageP:manager ?who .
+//            ?below   manageP:manages ?managed .
+//            ?managed emplP:lastName  ?name } }
+// """).get
+//     val sqlParser = Sql()
+//     val sqlSelect = sqlParser.parseAll(sqlParser.select, """
+// SELECT union1.name
+//   FROM Employee AS who
+//        INNER JOIN (
+//          SELECT manager.lastName AS name, above.manages AS who
+//                 FROM Manage AS above
+//                 INNER JOIN Employee as manager ON above.manager=manager.id
+//           WHERE manager.lastName IS NOT NULL
+//        UNION
+//          SELECT managed.lastName AS name, below.manager AS who
+//                 FROM Manage AS below
+//                 INNER JOIN Employee as managed ON below.manages=managed.id
+//           WHERE managed.lastName IS NOT NULL
+//        ) AS union1 ON union1.who=who.id
+//  WHERE who.lastName="Smith"
+// """).get
+//     assert(RDB2RDF(db2, sparqlSelect, StemURI("http://hr.example/DB/"), PrimaryKey(Attribute(Name("id")))) === sqlSelect)
+//   }
 }
--- a/src/test/scala/SparqlTest.scala	Sun Dec 27 14:42:03 2009 -0800
+++ b/src/test/scala/SparqlTest.scala	Sun Dec 27 15:05:58 2009 -0800
@@ -10,8 +10,8 @@
     val e = """
 ?emp      <http://hr.example/DB/Employee#lastName>   "bob"^^<http://www.w3.org/2001/XMLSchema#string>
 """
-    val expected = TriplePatterns(List(TriplePattern(SVar(Var("emp")),PUri(Stem("http://hr.example/DB"),Rel("Employee"),Attr("lastName")),OLit(SparqlLiteral(RDFLiteral("bob",Datatype(new URI("http://www.w3.org/2001/XMLSchema#string"))))))), SparqlExpression(List()))
-    assert(expected === (a.parseAll(a.triplepatterns, e).get))
+    val expected = BasicGraphPattern(List(TriplePattern(SVar(Var("emp")),PUri(Stem("http://hr.example/DB"),Rel("Employee"),Attr("lastName")),OLit(SparqlLiteral(RDFLiteral("bob",Datatype(new URI("http://www.w3.org/2001/XMLSchema#string"))))))), SparqlExpression(List()))
+    assert(expected === (a.parseAll(a.basicgraphpattern, e).get))
   }
 
   test("parse a litint") {
@@ -19,11 +19,11 @@
     val e = """
 ?emp      <http://hr.example/DB/Employee#age>   "21"^^<http://www.w3.org/2001/XMLSchema#integer>
 """
-    val expected = TriplePatterns(List(TriplePattern(SVar(Var("emp")),PUri(Stem("http://hr.example/DB"),Rel("Employee"),Attr("age")),OLit(SparqlLiteral(RDFLiteral("21",Datatype(new URI("http://www.w3.org/2001/XMLSchema#integer"))))))), SparqlExpression(List()))
-    assert(expected === (a.parseAll(a.triplepatterns, e).get))
+    val expected = BasicGraphPattern(List(TriplePattern(SVar(Var("emp")),PUri(Stem("http://hr.example/DB"),Rel("Employee"),Attr("age")),OLit(SparqlLiteral(RDFLiteral("21",Datatype(new URI("http://www.w3.org/2001/XMLSchema#integer"))))))), SparqlExpression(List()))
+    assert(expected === (a.parseAll(a.basicgraphpattern, e).get))
   }
 
-  test("parse a triplepatterns") {
+  test("parse a basicgraphpattern") {
     val a = Sparql()
     val e = """
 ?emp      <http://hr.example/DB/Employee#lastName>   ?empName .
@@ -31,7 +31,7 @@
 ?manager  <http://hr.example/DB/Employee#lastName>   ?managName
 """
     val tps =
-      TriplePatterns(
+      BasicGraphPattern(
 	List(
 	  TriplePattern(
 	    SVar(Var("emp")),
@@ -45,7 +45,7 @@
 	    SVar(Var("manager")),
 	    PUri(Stem("http://hr.example/DB"),Rel("Employee"),Attr("lastName")),
 	    OVar(Var("managName")))), SparqlExpression(List()))
-    assert(tps === a.parseAll(a.triplepatterns, e).get)
+    assert(tps === a.parseAll(a.basicgraphpattern, e).get)
   }
 
   // ?manBday < ?empBday && ?grandManBday < ?manBday
@@ -100,7 +100,7 @@
     val tps =
       SparqlSelect(
 	SparqlAttributeList(List(Var("empName"), Var("manageName"))),
-	TriplePatterns(
+	BasicGraphPattern(
 	  List(
 	    TriplePattern(
 	      SVar(Var("emp")),
@@ -126,7 +126,7 @@
     val tps =
       SparqlSelect(
 	SparqlAttributeList(List(Var("empName"), Var("manageName"))),
-	TriplePatterns(
+	BasicGraphPattern(
 	  List(
 	    TriplePattern(
 	      SVar(Var("emp")),
@@ -162,6 +162,30 @@
     a.parseAll(a.select, e).get
   }
 
+//   test("parse a nested bgp") {
+//     val a = Sparql()
+//     val e = """
+// SELECT ?x { { ?x <p> ?y} }
+// """
+//     a.parseAll(a.select, e).get
+//   }
+
+//   test("parse a conjunction") {
+//     val a = Sparql()
+//     val e = """
+// SELECT ?x { { ?x <p> ?y} { ?x <p> ?y} }
+// """
+//     a.parseAll(a.select, e).get
+//   }
+
+//   test("parse a disjunction") {
+//     val a = Sparql()
+//     val e = """
+// SELECT ?x { { ?x <p> ?y} UNION { ?x <p> ?y} }
+// """
+//     a.parseAll(a.select, e).get
+//   }
+
   test("decompose a predicate uri in stem, rel and attr") {
     val uri = "http://hr.example/our/favorite/DB/Employee#lastName"
     val puri:PUri = Sparql.parsePredicateURI(uri)