--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/sparql/src/test/scala/SparqlTest.scala Sun Oct 31 15:58:49 2010 -0400
@@ -0,0 +1,472 @@
+package org.w3.sw.sparql
+
+import org.w3.sw.rdf._
+import org.scalatest.FunSuite
+import java.net.URI
+
+class SparqlTest extends FunSuite {
+
+ test("parse a litstring") {
+ val a = Sparql()
+ val e = """
+?emp <http://hr.example/DB/Employee#lastName> "bob"^^<http://www.w3.org/2001/XMLSchema#string>
+"""
+ val expected = TriplesBlock(List(TriplePattern(TermVar(Var("emp")),TermUri(Uri("http://hr.example/DB/Employee#lastName")),TermLit(Literal(RDFLiteral("bob",Datatype(new URI("http://www.w3.org/2001/XMLSchema#string"))))))))
+ assert(expected === (a.parseAll(a.triplesblock, e).get))
+ }
+
+ test("parse a litint") {
+ val a = Sparql()
+ val e = """
+?emp <http://hr.example/DB/Employee#age> "21"^^<http://www.w3.org/2001/XMLSchema#integer>
+"""
+ val expected = TriplesBlock(List(TriplePattern(TermVar(Var("emp")),TermUri(Uri("http://hr.example/DB/Employee#age")),TermLit(Literal(RDFLiteral("21",Datatype(new URI("http://www.w3.org/2001/XMLSchema#integer"))))))))
+ assert(expected === (a.parseAll(a.triplesblock, e).get))
+ }
+
+ test("parse a triplesblock") {
+ 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 =
+ TriplesBlock(
+ List(
+ TriplePattern(
+ TermVar(Var("emp")),
+ TermUri(Uri("http://hr.example/DB/Employee#lastName")),
+ TermVar(Var("empName"))),
+ TriplePattern(
+ TermVar(Var("emp")),
+ TermUri(Uri("http://hr.example/DB/Employee#manager")),
+ TermVar(Var("manager"))),
+ TriplePattern(
+ TermVar(Var("manager")),
+ TermUri(Uri("http://hr.example/DB/Employee#lastName")),
+ TermVar(Var("managName")))))
+ assert(tps === a.parseAll(a.triplesblock, e).get)
+ }
+
+ // ?manBday < ?empBday && ?grandManBday < ?manBday
+ test("SparqlTermExpression") {
+ val a = Sparql()
+ val e = """
+?emp
+"""
+ val expected = SparqlTermExpression(TermVar(Var("emp")))
+ assert(expected === (a.parseAll(a.value, e).get))
+ }
+
+ test("PrimaryExpressionEq") {
+ val a = Sparql()
+ val e = """
+?emp<?emp
+"""
+ val expected = PrimaryExpressionLt(SparqlTermExpression(TermVar(Var("emp"))), SparqlTermExpression(TermVar(Var("emp"))))
+ assert(expected === (a.parseAll(a.primaryexpression, e).get))
+ }
+
+ test("Expression") {
+ val a = Sparql()
+ val e = """
+?manBday < ?empBday && ?grandManBday < ?manBday
+"""
+ val expected = Expression(List(
+ PrimaryExpressionLt(SparqlTermExpression(TermVar(Var("manBday"))), SparqlTermExpression(TermVar(Var("empBday")))),
+ PrimaryExpressionLt(SparqlTermExpression(TermVar(Var("grandManBday"))), SparqlTermExpression(TermVar(Var("manBday"))))))
+ assert(expected === (a.parseAll(a.expression, e).get))
+ }
+
+ test("FILTER") {
+ val a = Sparql()
+ val e = """
+FILTER(?manBday < ?empBday && ?grandManBday < ?manBday)
+"""
+ val expected = Expression(List(
+ PrimaryExpressionLt(SparqlTermExpression(TermVar(Var("manBday"))), SparqlTermExpression(TermVar(Var("empBday")))),
+ PrimaryExpressionLt(SparqlTermExpression(TermVar(Var("grandManBday"))), SparqlTermExpression(TermVar(Var("manBday"))))))
+ assert(expected === (a.parseAll(a.filter, e).get))
+ }
+
+ test("SELECT") {
+ val a = Sparql()
+ val e = """
+PREFIX empP : <http://hr.example/DB/Employee#>
+SELECT ?empName ?manageName {
+?emp empP:lastName ?empName
+}
+"""
+ val tps =
+ Select(
+ false,
+ SparqlAttributeList(List(Var("empName"), Var("manageName"))),
+ TriplesBlock(
+ List(
+ TriplePattern(
+ TermVar(Var("emp")),
+ TermUri(Uri("http://hr.example/DB/Employee#lastName")),
+ TermVar(Var("empName"))))),
+ List[OrderElt](), None, None)
+ assert(tps === a.parseAll(a.select, e).get)
+ }
+
+ test("WHERE") {
+ val a = Sparql()
+ val e = """
+PREFIX empP : <http://hr.example/DB/Employee#>
+SELECT ?empName ?manageName
+ WHERE {
+ ?emp empP:lastName ?empName
+ }
+"""
+ val tps =
+ Select(
+ false,
+ SparqlAttributeList(List(Var("empName"), Var("manageName"))),
+ TriplesBlock(
+ List(
+ TriplePattern(
+ TermVar(Var("emp")),
+ TermUri(Uri("http://hr.example/DB/Employee#lastName")),
+ TermVar(Var("empName"))))),
+ List[OrderElt](), None, None)
+ assert(tps === a.parseAll(a.select, e).get)
+ }
+
+ test("SELECT with FILTER") {
+ val a = Sparql()
+ val e = """
+SELECT ?empName ?manageName {
+?emp <http://hr.example/DB/Employee#lastName> ?empName
+FILTER(?manBday < ?empBday && ?grandManBday < ?manBday)
+}
+"""
+ val tps =
+ Select(
+ false,
+ SparqlAttributeList(List(Var("empName"), Var("manageName"))),
+ TableFilter(
+ TriplesBlock(
+ List(
+ TriplePattern(
+ TermVar(Var("emp")),
+ TermUri(Uri("http://hr.example/DB/Employee#lastName")),
+ TermVar(Var("empName"))))),
+ Expression(List(
+ PrimaryExpressionLt(SparqlTermExpression(TermVar(Var("manBday"))),
+ SparqlTermExpression(TermVar(Var("empBday")))),
+ PrimaryExpressionLt(SparqlTermExpression(TermVar(Var("grandManBday"))),
+ SparqlTermExpression(TermVar(Var("manBday"))))))),
+ List[OrderElt](), None, None)
+ assert(tps === a.parseAll(a.select, e).get)
+ }
+
+ test("SQLbgp") {
+ val a = Sparql()
+ val e = """
+SELECT ?empName ?manageName {
+?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 =
+ Select(
+ false,
+ SparqlAttributeList(List(Var("empName"), Var("manageName"))),
+ TriplesBlock(
+ List(
+ TriplePattern(
+ TermVar(Var("emp")),
+ TermUri(Uri("http://hr.example/DB/Employee#lastName")),
+ TermVar(Var("empName"))),
+ TriplePattern(
+ TermVar(Var("emp")),
+ TermUri(Uri("http://hr.example/DB/Employee#manager")),
+ TermVar(Var("manager"))),
+ TriplePattern(
+ TermVar(Var("manager")),
+ TermUri(Uri("http://hr.example/DB/Employee#lastName")),
+ TermVar(Var("managName"))))),
+ List[OrderElt](), None, None)
+ assert(tps === a.parseAll(a.select, e).get)
+ }
+
+ test("parse filter1") {
+ val a = Sparql()
+ val e = """
+PREFIX empP : <http://hr.example/DB/Employee#>
+PREFIX manP : <http://hr.example/DB/Manage#>
+SELECT ?empName ?grandManagName {
+ ?emp empP:lastName ?empName .
+ ?emp empP:birthday ?empBday .
+ ?lower manP:manages ?emp .
+ ?lower manP:manager ?manager .
+ ?manager empP:birthday ?manBday .
+ ?upper manP:manages ?manager .
+ ?upper manP:manager ?grandManager .
+ ?grandManager empP:birthday ?grandManBday .
+ ?grandManager empP:lastName ?grandManagName
+ FILTER (?manBday < ?empBday && ?grandManBday < ?manBday)
+}
+"""
+ 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 =
+ Select(
+ false,
+ SparqlAttributeList(List(Var("x"))),
+ TriplesBlock(
+ List(
+ TriplePattern(
+ TermVar(Var("x")),
+ TermUri(Uri("http://hr.example/DB/Employee#manager")),
+ TermVar(Var("y"))))),
+ List[OrderElt](), None, None)
+ assert(tps === 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 =
+ Select(
+ false,
+ SparqlAttributeList(List(Var("x"))),
+ TableConjunction(List(
+ TriplesBlock(
+ List(
+ TriplePattern(
+ TermVar(Var("x")),
+ TermUri(Uri("http://hr.example/DB/Employee#manager")),
+ TermVar(Var("y"))))),
+ TriplesBlock(
+ List(
+ TriplePattern(
+ TermVar(Var("x")),
+ TermUri(Uri("http://hr.example/DB/Employee#manager")),
+ TermVar(Var("y"))))))),
+ List[OrderElt](), None, None)
+ assert(tps === 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 =
+ Select(
+ false,
+ SparqlAttributeList(List(Var("x"))),
+ TableDisjunction(List(
+ TriplesBlock(
+ List(
+ TriplePattern(
+ TermVar(Var("x")),
+ TermUri(Uri("http://hr.example/DB/Employee#manager")),
+ TermVar(Var("y"))))),
+ TriplesBlock(
+ List(
+ TriplePattern(
+ TermVar(Var("x")),
+ TermUri(Uri("http://hr.example/DB/Employee#manager")),
+ TermVar(Var("y"))))))),
+ List[OrderElt](), None, None)
+ 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 =
+ Select(
+ false,
+ SparqlAttributeList(List(Var("x"))),
+ TableConjunction(List(
+ TriplesBlock(
+ List(
+ TriplePattern(
+ TermVar(Var("x")),
+ TermUri(Uri("http://hr.example/DB/Employee#manager")),
+ TermVar(Var("y"))))),
+ OptionalGraphPattern(
+ TriplesBlock(
+ List(
+ TriplePattern(
+ TermVar(Var("x")),
+ TermUri(Uri("http://hr.example/DB/Employee#manager")),
+ TermVar(Var("y")))))))),
+ List[OrderElt](), None, None)
+ 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 =
+ Select(
+ false,
+ SparqlAttributeList(List(Var("x"))),
+ OptionalGraphPattern(
+ TriplesBlock(
+ List(
+ TriplePattern(
+ TermVar(Var("x")),
+ TermUri(Uri("http://hr.example/DB/Employee#manager")),
+ TermVar(Var("y")))))),
+ List[OrderElt](), None, None)
+ assert(tps === a.parseAll(a.select, e).get)
+ }
+
+ test("parse an minus") {
+ val a = Sparql()
+ val e = """
+SELECT ?x { { ?x <http://hr.example/DB/Employee#manager> ?y} MINUS { ?x <http://hr.example/DB/Employee#manager> ?y} }
+"""
+ val tps =
+ Select(
+ false,
+ SparqlAttributeList(List(Var("x"))),
+ TableConjunction(List(
+ TriplesBlock(
+ List(
+ TriplePattern(
+ TermVar(Var("x")),
+ TermUri(Uri("http://hr.example/DB/Employee#manager")),
+ TermVar(Var("y"))))),
+ MinusGraphPattern(
+ TriplesBlock(
+ List(
+ TriplePattern(
+ TermVar(Var("x")),
+ TermUri(Uri("http://hr.example/DB/Employee#manager")),
+ TermVar(Var("y")))))))),
+ List[OrderElt](), None, None)
+ assert(tps === a.parseAll(a.select, e).get)
+ }
+
+ test("parse disj1") {
+ val a = Sparql()
+ val e = """
+SELECT ?name
+ { ?who <http://hr.example/DB/Employee#lastName> "Smith"^^<http://www.w3.org/2001/XMLSchema#string>
+ { ?above <http://hr.example/DB/Manage#manages> ?who .
+ ?above <http://hr.example/DB/Manage#manager> ?manager .
+ ?manager <http://hr.example/DB/Employee#lastName> ?name }
+ UNION
+ { ?below <http://hr.example/DB/Manage#manager> ?who .
+ ?below <http://hr.example/DB/Manage#manages> ?managed .
+ ?managed <http://hr.example/DB/Employee#lastName> ?name } }
+"""
+ val tps =
+ Select(
+ false,
+ SparqlAttributeList(List(Var("name"))),
+ TableConjunction(List(
+ TriplesBlock(
+ List(
+ TriplePattern(
+ TermVar(Var("who")),
+ TermUri(Uri("http://hr.example/DB/Employee#lastName")),
+ TermLit(Literal(RDFLiteral("Smith",Datatype(new URI("http://www.w3.org/2001/XMLSchema#string")))))))),
+ TableDisjunction(List(
+ TriplesBlock(
+ List(
+ TriplePattern(
+ TermVar(Var("above")),
+ TermUri(Uri("http://hr.example/DB/Manage#manages")),
+ TermVar(Var("who"))),
+ TriplePattern(
+ TermVar(Var("above")),
+ TermUri(Uri("http://hr.example/DB/Manage#manager")),
+ TermVar(Var("manager"))),
+ TriplePattern(
+ TermVar(Var("manager")),
+ TermUri(Uri("http://hr.example/DB/Employee#lastName")),
+ TermVar(Var("name")))
+ )),
+ TriplesBlock(
+ List(
+ TriplePattern(
+ TermVar(Var("below")),
+ TermUri(Uri("http://hr.example/DB/Manage#manager")),
+ TermVar(Var("who"))),
+ TriplePattern(
+ TermVar(Var("below")),
+ TermUri(Uri("http://hr.example/DB/Manage#manages")),
+ TermVar(Var("managed"))),
+ TriplePattern(
+ TermVar(Var("managed")),
+ TermUri(Uri("http://hr.example/DB/Employee#lastName")),
+ TermVar(Var("name")))
+ )))))),
+ List[OrderElt](), None, None)
+ assert(tps === a.parseAll(a.select, e).get)
+ }
+
+ test("parse series of OPTs") {
+ val a = Sparql()
+ val e = """
+PREFIX emplP: <http://hr.example/DB/Employee#>
+
+SELECT ?emp1Name ?emp2Name ?emp3Name
+ WHERE { ?emp1 emplP:lastName ?emp1Name
+ OPTIONAL { ?emp1 emplP:birthday ?birthday }
+ ?emp2 emplP:lastName ?emp2Name
+ OPTIONAL { ?emp2 emplP:birthday ?birthday }
+ ?emp4 emplP:birthday ?birthday
+ FILTER ( ?emp1Name < ?emp2Name && ?emp2Name < ?emp3Name && ?emp3Name < ?emp4Name) }
+"""
+ val tps =
+ Select(false, SparqlAttributeList(List(Var("emp1Name"), Var("emp2Name"), Var("emp3Name"))),
+ TableFilter(TableConjunction(List(TriplesBlock(List(TriplePattern(TermVar(Var("emp1")), TermUri(Uri("http://hr.example/DB/Employee#lastName")), TermVar(Var("emp1Name"))))),
+ OptionalGraphPattern(TriplesBlock(List(TriplePattern(TermVar(Var("emp1")), TermUri(Uri("http://hr.example/DB/Employee#birthday")), TermVar(Var("birthday")))))),
+ TriplesBlock(List(TriplePattern(TermVar(Var("emp2")), TermUri(Uri("http://hr.example/DB/Employee#lastName")), TermVar(Var("emp2Name"))))),
+ OptionalGraphPattern(TriplesBlock(List(TriplePattern(TermVar(Var("emp2")), TermUri(Uri("http://hr.example/DB/Employee#birthday")), TermVar(Var("birthday")))))),
+ TriplesBlock(List(TriplePattern(TermVar(Var("emp4")),TermUri(Uri("http://hr.example/DB/Employee#birthday")),TermVar(Var("birthday"))))))),
+ Expression(List(PrimaryExpressionLt(SparqlTermExpression(TermVar(Var("emp1Name"))),SparqlTermExpression(TermVar(Var("emp2Name")))), PrimaryExpressionLt(SparqlTermExpression(TermVar(Var("emp2Name"))),SparqlTermExpression(TermVar(Var("emp3Name")))), PrimaryExpressionLt(SparqlTermExpression(TermVar(Var("emp3Name"))),SparqlTermExpression(TermVar(Var("emp4Name"))))))),
+ List[OrderElt](), None, None)
+ assert(tps === a.parseAll(a.select, e).get)
+ }
+
+ test("CONSTRUCT") {
+ val a = Sparql()
+ val e = """
+PREFIX foaf : <http://xmlns.com/foaf/0.1/>
+PREFIX empP : <http://hr.example/DB/Employee#>
+CONSTRUCT { ?emp foaf:last_name ?empName }
+ WHERE { ?emp empP:lastName ?empName }
+"""
+ val tps =
+ Construct(
+ TriplesBlock(
+ List(
+ TriplePattern(
+ TermVar(Var("emp")),
+ TermUri(Uri("http://xmlns.com/foaf/0.1/last_name")),
+ TermVar(Var("empName"))))),
+ TriplesBlock(
+ List(
+ TriplePattern(
+ TermVar(Var("emp")),
+ TermUri(Uri("http://hr.example/DB/Employee#lastName")),
+ TermVar(Var("empName"))))))
+ assert(tps === a.parseAll(a.construct, e).get)
+ }
+
+}
--- a/src/test/scala/SparqlTest.scala Sun Oct 31 15:56:24 2010 -0400
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,472 +0,0 @@
-package w3c.sw.sparql
-
-import w3c.sw.rdf._
-import org.scalatest.FunSuite
-import java.net.URI
-
-class SparqlTest extends FunSuite {
-
- test("parse a litstring") {
- val a = Sparql()
- val e = """
-?emp <http://hr.example/DB/Employee#lastName> "bob"^^<http://www.w3.org/2001/XMLSchema#string>
-"""
- val expected = TriplesBlock(List(TriplePattern(TermVar(Var("emp")),TermUri(Uri("http://hr.example/DB/Employee#lastName")),TermLit(Literal(RDFLiteral("bob",Datatype(new URI("http://www.w3.org/2001/XMLSchema#string"))))))))
- assert(expected === (a.parseAll(a.triplesblock, e).get))
- }
-
- test("parse a litint") {
- val a = Sparql()
- val e = """
-?emp <http://hr.example/DB/Employee#age> "21"^^<http://www.w3.org/2001/XMLSchema#integer>
-"""
- val expected = TriplesBlock(List(TriplePattern(TermVar(Var("emp")),TermUri(Uri("http://hr.example/DB/Employee#age")),TermLit(Literal(RDFLiteral("21",Datatype(new URI("http://www.w3.org/2001/XMLSchema#integer"))))))))
- assert(expected === (a.parseAll(a.triplesblock, e).get))
- }
-
- test("parse a triplesblock") {
- 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 =
- TriplesBlock(
- List(
- TriplePattern(
- TermVar(Var("emp")),
- TermUri(Uri("http://hr.example/DB/Employee#lastName")),
- TermVar(Var("empName"))),
- TriplePattern(
- TermVar(Var("emp")),
- TermUri(Uri("http://hr.example/DB/Employee#manager")),
- TermVar(Var("manager"))),
- TriplePattern(
- TermVar(Var("manager")),
- TermUri(Uri("http://hr.example/DB/Employee#lastName")),
- TermVar(Var("managName")))))
- assert(tps === a.parseAll(a.triplesblock, e).get)
- }
-
- // ?manBday < ?empBday && ?grandManBday < ?manBday
- test("SparqlTermExpression") {
- val a = Sparql()
- val e = """
-?emp
-"""
- val expected = SparqlTermExpression(TermVar(Var("emp")))
- assert(expected === (a.parseAll(a.value, e).get))
- }
-
- test("PrimaryExpressionEq") {
- val a = Sparql()
- val e = """
-?emp<?emp
-"""
- val expected = PrimaryExpressionLt(SparqlTermExpression(TermVar(Var("emp"))), SparqlTermExpression(TermVar(Var("emp"))))
- assert(expected === (a.parseAll(a.primaryexpression, e).get))
- }
-
- test("Expression") {
- val a = Sparql()
- val e = """
-?manBday < ?empBday && ?grandManBday < ?manBday
-"""
- val expected = Expression(List(
- PrimaryExpressionLt(SparqlTermExpression(TermVar(Var("manBday"))), SparqlTermExpression(TermVar(Var("empBday")))),
- PrimaryExpressionLt(SparqlTermExpression(TermVar(Var("grandManBday"))), SparqlTermExpression(TermVar(Var("manBday"))))))
- assert(expected === (a.parseAll(a.expression, e).get))
- }
-
- test("FILTER") {
- val a = Sparql()
- val e = """
-FILTER(?manBday < ?empBday && ?grandManBday < ?manBday)
-"""
- val expected = Expression(List(
- PrimaryExpressionLt(SparqlTermExpression(TermVar(Var("manBday"))), SparqlTermExpression(TermVar(Var("empBday")))),
- PrimaryExpressionLt(SparqlTermExpression(TermVar(Var("grandManBday"))), SparqlTermExpression(TermVar(Var("manBday"))))))
- assert(expected === (a.parseAll(a.filter, e).get))
- }
-
- test("SELECT") {
- val a = Sparql()
- val e = """
-PREFIX empP : <http://hr.example/DB/Employee#>
-SELECT ?empName ?manageName {
-?emp empP:lastName ?empName
-}
-"""
- val tps =
- Select(
- false,
- SparqlAttributeList(List(Var("empName"), Var("manageName"))),
- TriplesBlock(
- List(
- TriplePattern(
- TermVar(Var("emp")),
- TermUri(Uri("http://hr.example/DB/Employee#lastName")),
- TermVar(Var("empName"))))),
- List[OrderElt](), None, None)
- assert(tps === a.parseAll(a.select, e).get)
- }
-
- test("WHERE") {
- val a = Sparql()
- val e = """
-PREFIX empP : <http://hr.example/DB/Employee#>
-SELECT ?empName ?manageName
- WHERE {
- ?emp empP:lastName ?empName
- }
-"""
- val tps =
- Select(
- false,
- SparqlAttributeList(List(Var("empName"), Var("manageName"))),
- TriplesBlock(
- List(
- TriplePattern(
- TermVar(Var("emp")),
- TermUri(Uri("http://hr.example/DB/Employee#lastName")),
- TermVar(Var("empName"))))),
- List[OrderElt](), None, None)
- assert(tps === a.parseAll(a.select, e).get)
- }
-
- test("SELECT with FILTER") {
- val a = Sparql()
- val e = """
-SELECT ?empName ?manageName {
-?emp <http://hr.example/DB/Employee#lastName> ?empName
-FILTER(?manBday < ?empBday && ?grandManBday < ?manBday)
-}
-"""
- val tps =
- Select(
- false,
- SparqlAttributeList(List(Var("empName"), Var("manageName"))),
- TableFilter(
- TriplesBlock(
- List(
- TriplePattern(
- TermVar(Var("emp")),
- TermUri(Uri("http://hr.example/DB/Employee#lastName")),
- TermVar(Var("empName"))))),
- Expression(List(
- PrimaryExpressionLt(SparqlTermExpression(TermVar(Var("manBday"))),
- SparqlTermExpression(TermVar(Var("empBday")))),
- PrimaryExpressionLt(SparqlTermExpression(TermVar(Var("grandManBday"))),
- SparqlTermExpression(TermVar(Var("manBday"))))))),
- List[OrderElt](), None, None)
- assert(tps === a.parseAll(a.select, e).get)
- }
-
- test("SQLbgp") {
- val a = Sparql()
- val e = """
-SELECT ?empName ?manageName {
-?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 =
- Select(
- false,
- SparqlAttributeList(List(Var("empName"), Var("manageName"))),
- TriplesBlock(
- List(
- TriplePattern(
- TermVar(Var("emp")),
- TermUri(Uri("http://hr.example/DB/Employee#lastName")),
- TermVar(Var("empName"))),
- TriplePattern(
- TermVar(Var("emp")),
- TermUri(Uri("http://hr.example/DB/Employee#manager")),
- TermVar(Var("manager"))),
- TriplePattern(
- TermVar(Var("manager")),
- TermUri(Uri("http://hr.example/DB/Employee#lastName")),
- TermVar(Var("managName"))))),
- List[OrderElt](), None, None)
- assert(tps === a.parseAll(a.select, e).get)
- }
-
- test("parse filter1") {
- val a = Sparql()
- val e = """
-PREFIX empP : <http://hr.example/DB/Employee#>
-PREFIX manP : <http://hr.example/DB/Manage#>
-SELECT ?empName ?grandManagName {
- ?emp empP:lastName ?empName .
- ?emp empP:birthday ?empBday .
- ?lower manP:manages ?emp .
- ?lower manP:manager ?manager .
- ?manager empP:birthday ?manBday .
- ?upper manP:manages ?manager .
- ?upper manP:manager ?grandManager .
- ?grandManager empP:birthday ?grandManBday .
- ?grandManager empP:lastName ?grandManagName
- FILTER (?manBday < ?empBday && ?grandManBday < ?manBday)
-}
-"""
- 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 =
- Select(
- false,
- SparqlAttributeList(List(Var("x"))),
- TriplesBlock(
- List(
- TriplePattern(
- TermVar(Var("x")),
- TermUri(Uri("http://hr.example/DB/Employee#manager")),
- TermVar(Var("y"))))),
- List[OrderElt](), None, None)
- assert(tps === 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 =
- Select(
- false,
- SparqlAttributeList(List(Var("x"))),
- TableConjunction(List(
- TriplesBlock(
- List(
- TriplePattern(
- TermVar(Var("x")),
- TermUri(Uri("http://hr.example/DB/Employee#manager")),
- TermVar(Var("y"))))),
- TriplesBlock(
- List(
- TriplePattern(
- TermVar(Var("x")),
- TermUri(Uri("http://hr.example/DB/Employee#manager")),
- TermVar(Var("y"))))))),
- List[OrderElt](), None, None)
- assert(tps === 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 =
- Select(
- false,
- SparqlAttributeList(List(Var("x"))),
- TableDisjunction(List(
- TriplesBlock(
- List(
- TriplePattern(
- TermVar(Var("x")),
- TermUri(Uri("http://hr.example/DB/Employee#manager")),
- TermVar(Var("y"))))),
- TriplesBlock(
- List(
- TriplePattern(
- TermVar(Var("x")),
- TermUri(Uri("http://hr.example/DB/Employee#manager")),
- TermVar(Var("y"))))))),
- List[OrderElt](), None, None)
- 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 =
- Select(
- false,
- SparqlAttributeList(List(Var("x"))),
- TableConjunction(List(
- TriplesBlock(
- List(
- TriplePattern(
- TermVar(Var("x")),
- TermUri(Uri("http://hr.example/DB/Employee#manager")),
- TermVar(Var("y"))))),
- OptionalGraphPattern(
- TriplesBlock(
- List(
- TriplePattern(
- TermVar(Var("x")),
- TermUri(Uri("http://hr.example/DB/Employee#manager")),
- TermVar(Var("y")))))))),
- List[OrderElt](), None, None)
- 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 =
- Select(
- false,
- SparqlAttributeList(List(Var("x"))),
- OptionalGraphPattern(
- TriplesBlock(
- List(
- TriplePattern(
- TermVar(Var("x")),
- TermUri(Uri("http://hr.example/DB/Employee#manager")),
- TermVar(Var("y")))))),
- List[OrderElt](), None, None)
- assert(tps === a.parseAll(a.select, e).get)
- }
-
- test("parse an minus") {
- val a = Sparql()
- val e = """
-SELECT ?x { { ?x <http://hr.example/DB/Employee#manager> ?y} MINUS { ?x <http://hr.example/DB/Employee#manager> ?y} }
-"""
- val tps =
- Select(
- false,
- SparqlAttributeList(List(Var("x"))),
- TableConjunction(List(
- TriplesBlock(
- List(
- TriplePattern(
- TermVar(Var("x")),
- TermUri(Uri("http://hr.example/DB/Employee#manager")),
- TermVar(Var("y"))))),
- MinusGraphPattern(
- TriplesBlock(
- List(
- TriplePattern(
- TermVar(Var("x")),
- TermUri(Uri("http://hr.example/DB/Employee#manager")),
- TermVar(Var("y")))))))),
- List[OrderElt](), None, None)
- assert(tps === a.parseAll(a.select, e).get)
- }
-
- test("parse disj1") {
- val a = Sparql()
- val e = """
-SELECT ?name
- { ?who <http://hr.example/DB/Employee#lastName> "Smith"^^<http://www.w3.org/2001/XMLSchema#string>
- { ?above <http://hr.example/DB/Manage#manages> ?who .
- ?above <http://hr.example/DB/Manage#manager> ?manager .
- ?manager <http://hr.example/DB/Employee#lastName> ?name }
- UNION
- { ?below <http://hr.example/DB/Manage#manager> ?who .
- ?below <http://hr.example/DB/Manage#manages> ?managed .
- ?managed <http://hr.example/DB/Employee#lastName> ?name } }
-"""
- val tps =
- Select(
- false,
- SparqlAttributeList(List(Var("name"))),
- TableConjunction(List(
- TriplesBlock(
- List(
- TriplePattern(
- TermVar(Var("who")),
- TermUri(Uri("http://hr.example/DB/Employee#lastName")),
- TermLit(Literal(RDFLiteral("Smith",Datatype(new URI("http://www.w3.org/2001/XMLSchema#string")))))))),
- TableDisjunction(List(
- TriplesBlock(
- List(
- TriplePattern(
- TermVar(Var("above")),
- TermUri(Uri("http://hr.example/DB/Manage#manages")),
- TermVar(Var("who"))),
- TriplePattern(
- TermVar(Var("above")),
- TermUri(Uri("http://hr.example/DB/Manage#manager")),
- TermVar(Var("manager"))),
- TriplePattern(
- TermVar(Var("manager")),
- TermUri(Uri("http://hr.example/DB/Employee#lastName")),
- TermVar(Var("name")))
- )),
- TriplesBlock(
- List(
- TriplePattern(
- TermVar(Var("below")),
- TermUri(Uri("http://hr.example/DB/Manage#manager")),
- TermVar(Var("who"))),
- TriplePattern(
- TermVar(Var("below")),
- TermUri(Uri("http://hr.example/DB/Manage#manages")),
- TermVar(Var("managed"))),
- TriplePattern(
- TermVar(Var("managed")),
- TermUri(Uri("http://hr.example/DB/Employee#lastName")),
- TermVar(Var("name")))
- )))))),
- List[OrderElt](), None, None)
- assert(tps === a.parseAll(a.select, e).get)
- }
-
- test("parse series of OPTs") {
- val a = Sparql()
- val e = """
-PREFIX emplP: <http://hr.example/DB/Employee#>
-
-SELECT ?emp1Name ?emp2Name ?emp3Name
- WHERE { ?emp1 emplP:lastName ?emp1Name
- OPTIONAL { ?emp1 emplP:birthday ?birthday }
- ?emp2 emplP:lastName ?emp2Name
- OPTIONAL { ?emp2 emplP:birthday ?birthday }
- ?emp4 emplP:birthday ?birthday
- FILTER ( ?emp1Name < ?emp2Name && ?emp2Name < ?emp3Name && ?emp3Name < ?emp4Name) }
-"""
- val tps =
- Select(false, SparqlAttributeList(List(Var("emp1Name"), Var("emp2Name"), Var("emp3Name"))),
- TableFilter(TableConjunction(List(TriplesBlock(List(TriplePattern(TermVar(Var("emp1")), TermUri(Uri("http://hr.example/DB/Employee#lastName")), TermVar(Var("emp1Name"))))),
- OptionalGraphPattern(TriplesBlock(List(TriplePattern(TermVar(Var("emp1")), TermUri(Uri("http://hr.example/DB/Employee#birthday")), TermVar(Var("birthday")))))),
- TriplesBlock(List(TriplePattern(TermVar(Var("emp2")), TermUri(Uri("http://hr.example/DB/Employee#lastName")), TermVar(Var("emp2Name"))))),
- OptionalGraphPattern(TriplesBlock(List(TriplePattern(TermVar(Var("emp2")), TermUri(Uri("http://hr.example/DB/Employee#birthday")), TermVar(Var("birthday")))))),
- TriplesBlock(List(TriplePattern(TermVar(Var("emp4")),TermUri(Uri("http://hr.example/DB/Employee#birthday")),TermVar(Var("birthday"))))))),
- Expression(List(PrimaryExpressionLt(SparqlTermExpression(TermVar(Var("emp1Name"))),SparqlTermExpression(TermVar(Var("emp2Name")))), PrimaryExpressionLt(SparqlTermExpression(TermVar(Var("emp2Name"))),SparqlTermExpression(TermVar(Var("emp3Name")))), PrimaryExpressionLt(SparqlTermExpression(TermVar(Var("emp3Name"))),SparqlTermExpression(TermVar(Var("emp4Name"))))))),
- List[OrderElt](), None, None)
- assert(tps === a.parseAll(a.select, e).get)
- }
-
- test("CONSTRUCT") {
- val a = Sparql()
- val e = """
-PREFIX foaf : <http://xmlns.com/foaf/0.1/>
-PREFIX empP : <http://hr.example/DB/Employee#>
-CONSTRUCT { ?emp foaf:last_name ?empName }
- WHERE { ?emp empP:lastName ?empName }
-"""
- val tps =
- Construct(
- TriplesBlock(
- List(
- TriplePattern(
- TermVar(Var("emp")),
- TermUri(Uri("http://xmlns.com/foaf/0.1/last_name")),
- TermVar(Var("empName"))))),
- TriplesBlock(
- List(
- TriplePattern(
- TermVar(Var("emp")),
- TermUri(Uri("http://hr.example/DB/Employee#lastName")),
- TermVar(Var("empName"))))))
- assert(tps === a.parseAll(a.construct, e).get)
- }
-
-}