+ first-level SPARQL GGP algebra
authorEric Prud'hommeaux <bertails@w3.org>
Mon, 28 Dec 2009 16:40:45 -0500
changeset 81 2220e911b326
parent 80 b15b4290f0fb
child 82 12f909b4d780
+ first-level SPARQL GGP algebra
src/main/scala/SPARQL.scala
src/test/scala/SparqlTest.scala
--- a/src/main/scala/SPARQL.scala	Mon Dec 28 15:47:21 2009 -0500
+++ b/src/main/scala/SPARQL.scala	Mon Dec 28 16:40:45 2009 -0500
@@ -98,6 +98,15 @@
 	}
 	gpntORf_tbOPT.foldLeft(init)((gp, lentry) => lentry match {
 	  case ~(TableFilter(null, expr), None) => TableFilter(gp, expr)
+	  case ~(TriplesBlock(triples), None) => gp match {
+	    case EmptyGraphPattern() => TriplesBlock(triples)
+	    case TriplesBlock(triples2) => TableConjunction(List(gp, TriplesBlock(triples2)))
+	  }
+	  case ~(TableDisjunction(list), None) => gp match {
+	    case EmptyGraphPattern() => TableDisjunction(list)
+	    case x => TableConjunction(List(gp, x))
+	  }
+	  case ~(OptionalGraphPattern(gp2), None) => TableConjunction(List(gp, OptionalGraphPattern(gp2)))
 	  case x => error("found " + x)
 	})
       }
--- a/src/test/scala/SparqlTest.scala	Mon Dec 28 15:47:21 2009 -0500
+++ b/src/test/scala/SparqlTest.scala	Mon Dec 28 16:40:45 2009 -0500
@@ -182,29 +182,115 @@
     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 nested bgp") {
+    val a = Sparql()
+    val e = """
+SELECT ?x { { ?x <http://hr.example/DB/Employee#manager> ?y} }
+"""
+    val tps =
+      SparqlSelect(
+	SparqlAttributeList(List(Var("x"))),
+	TriplesBlock(
+	  List(
+	    TriplePattern(
+	      SVar(Var("x")),
+	      PUri(Stem("http://hr.example/DB"),Rel("Employee"),Attr("manager")),
+	      OVar(Var("y"))))))
+    assert(tps === 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 conjunction") {
+    val a = Sparql()
+    val e = """
+SELECT ?x { { ?x <http://hr.example/DB/Employee#manager> ?y} { ?x <http://hr.example/DB/Employee#manager> ?y} }
+"""
+    val tps =
+      SparqlSelect(
+	SparqlAttributeList(List(Var("x"))),
+	TableConjunction(List(
+	  TriplesBlock(
+	    List(
+	      TriplePattern(
+		SVar(Var("x")),
+		PUri(Stem("http://hr.example/DB"),Rel("Employee"),Attr("manager")),
+		OVar(Var("y"))))),
+	  TriplesBlock(
+	    List(
+	      TriplePattern(
+		SVar(Var("x")),
+		PUri(Stem("http://hr.example/DB"),Rel("Employee"),Attr("manager")),
+		OVar(Var("y"))))))))
+    assert(tps === 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("parse a disjunction") {
+    val a = Sparql()
+    val e = """
+SELECT ?x { { ?x <http://hr.example/DB/Employee#manager> ?y} UNION { ?x <http://hr.example/DB/Employee#manager> ?y} }
+"""
+    val tps =
+      SparqlSelect(
+	SparqlAttributeList(List(Var("x"))),
+	TableDisjunction(List(
+	  TriplesBlock(
+	    List(
+	      TriplePattern(
+		SVar(Var("x")),
+		PUri(Stem("http://hr.example/DB"),Rel("Employee"),Attr("manager")),
+		OVar(Var("y"))))),
+	  TriplesBlock(
+	    List(
+	      TriplePattern(
+		SVar(Var("x")),
+		PUri(Stem("http://hr.example/DB"),Rel("Employee"),Attr("manager")),
+		OVar(Var("y"))))))))
+    assert(tps === a.parseAll(a.select, e).get)
+  }
+
+  test("parse an optional") {
+    val a = Sparql()
+    val e = """
+SELECT ?x { { ?x <http://hr.example/DB/Employee#manager> ?y} OPTIONAL { ?x <http://hr.example/DB/Employee#manager> ?y} }
+"""
+    val tps =
+      SparqlSelect(
+	SparqlAttributeList(List(Var("x"))),
+	TableConjunction(List(
+	  TriplesBlock(
+	    List(
+	      TriplePattern(
+		SVar(Var("x")),
+		PUri(Stem("http://hr.example/DB"),Rel("Employee"),Attr("manager")),
+		OVar(Var("y"))))),
+	  OptionalGraphPattern(
+	    TriplesBlock(
+	      List(
+		TriplePattern(
+		  SVar(Var("x")),
+		  PUri(Stem("http://hr.example/DB"),Rel("Employee"),Attr("manager")),
+		  OVar(Var("y")))))))))
+    assert(tps === a.parseAll(a.select, e).get)
+  }
+
+  test("parse a leading optional") {
+    val a = Sparql()
+    val e = """
+SELECT ?x { OPTIONAL { ?x <http://hr.example/DB/Employee#manager> ?y} }
+"""
+    val tps =
+      SparqlSelect(
+	SparqlAttributeList(List(Var("x"))),
+	TableConjunction(List(
+	  EmptyGraphPattern(),
+	  OptionalGraphPattern(
+	    TriplesBlock(
+	      List(
+		TriplePattern(
+		  SVar(Var("x")),
+		  PUri(Stem("http://hr.example/DB"),Rel("Employee"),Attr("manager")),
+		  OVar(Var("y")))))))))
+    assert(tps === 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"