--- a/src/main/scala/SPARQL.scala Wed Dec 30 10:25:01 2009 -0500
+++ b/src/main/scala/SPARQL.scala Wed Dec 30 11:19:36 2009 -0500
@@ -7,6 +7,7 @@
val uri = """[a-zA-Z0-9:/#\.]+""".r
val name = """[a-zA-Z_][a-zA-Z0-9_]*""".r
+ var prefixes:Map[String, String] = Map()
}
import MyParsers._
@@ -63,7 +64,10 @@
case class Sparql() extends JavaTokenParsers {
def select:Parser[SparqlSelect] =
- "SELECT" ~ attributelist ~ groupgraphpattern ^^ { case "SELECT"~a~gp => SparqlSelect(a, gp) }
+ rep(prefixdecls) ~ "SELECT" ~ attributelist ~ groupgraphpattern ^^ { case x~"SELECT"~a~gp => SparqlSelect(a, gp) }
+
+ def prefixdecls:Parser[Unit] =
+ "PREFIX" ~ name ~ ":" ~ qnameORuri ^^ { case "PREFIX"~pre~":"~u => prefixes += (pre -> u) }
def filter:Parser[SparqlExpression] =
"FILTER" ~ "(" ~ expression ~ ")" ^^ { case "FILTER"~"("~expression~")" => expression }
@@ -80,7 +84,7 @@
)
def value:Parser[SparqlTermExpression] = (
- "<"~uri~">" ^^ { case "<"~x~">" => SparqlTermExpression(TermUri(Sparql.parseObjectURI(x))) }
+ qnameORuri ^^ { case x => SparqlTermExpression(TermUri(Sparql.parseObjectURI(x))) }
| varr ^^ { x => SparqlTermExpression(TermVar(x)) }
| literal ^^ { x => SparqlTermExpression(TermLit(x)) }
)
@@ -128,25 +132,33 @@
subject ~ predicate ~ objectt ^^ { case s~p~o => TriplePattern(s, p, o) }
def subject:Parser[S] = (
- "<"~uri~">" ^^ { case "<"~x~">" => SUri(Sparql.parseObjectURI(x)) }
+ qnameORuri ^^ { case x => SUri(Sparql.parseObjectURI(x)) }
| varr ^^ { x => SVar(x) }
)
def predicate:Parser[P] =
- "<"~uri~">" ^^ { case "<"~x~">" => Sparql.parsePredicateURI(x) }
+ qnameORuri ^^ { case x => Sparql.parsePredicateURI(x) }
def objectt:Parser[O] = (
- "<"~uri~">" ^^ { case "<"~x~">" => OUri(Sparql.parseObjectURI(x)) }
+ qnameORuri ^^ { case x => OUri(Sparql.parseObjectURI(x)) }
| varr ^^ { x => OVar(x) }
| literal ^^ { x => OLit(x) }
)
- def literal:Parser[SparqlLiteral] = (
- stringLiteral~"^^<http://www.w3.org/2001/XMLSchema#string>" ^^
- { case lit ~ _ => SparqlLiteral(RDFLiteral(lit.substring(1,lit.size - 1), RDFLiteral.StringDatatype)) }
- | stringLiteral~"^^<http://www.w3.org/2001/XMLSchema#integer>" ^^
- { case lit ~ _ => SparqlLiteral(RDFLiteral(lit.substring(1,lit.size - 1), RDFLiteral.IntegerDatatype)) }
-)
+ def qnameORuri:Parser[String] = (
+ "<"~uri~">" ^^ { case "<"~x~">" => x }
+ | name~":"~name ^^ { case prefix~":"~localName => prefixes(prefix) + localName }
+ )
+
+ def literal:Parser[SparqlLiteral] =
+ stringLiteral~"^^"~qnameORuri ^^
+ {
+ case lit~"^^"~dt => SparqlLiteral(RDFLiteral(lit.substring(1,lit.size - 1), dt match {
+ case "http://www.w3.org/2001/XMLSchema#string" => RDFLiteral.StringDatatype
+ case "http://www.w3.org/2001/XMLSchema#integer" => RDFLiteral.IntegerDatatype
+ case x => error("only programed to deal with string and integer, not " + x)
+ }))
+ }
def varr:Parser[Var] = "?"~ident ^^ { case "?"~x => Var(x) }
--- a/src/test/scala/RDB2RDFTest.scala Wed Dec 30 10:25:01 2009 -0500
+++ b/src/test/scala/RDB2RDFTest.scala Wed Dec 30 11:19:36 2009 -0500
@@ -33,8 +33,9 @@
test("?s <p> <x>") {
val sparqlParser = Sparql()
val sparqlSelect = sparqlParser.parseAll(sparqlParser.select, """
+PREFIX empP : <http://hr.example/DB/Employee#>
SELECT ?emp {
-?emp <http://hr.example/DB/Employee#manager> <http://hr.example/DB/Employee/id.18#record>
+?emp empP:manager <http://hr.example/DB/Employee/id.18#record>
}
""").get
val sqlParser = Sql()
@@ -51,8 +52,9 @@
test("<s> <p> ?x") {
val sparqlParser = Sparql()
val sparqlSelect = sparqlParser.parseAll(sparqlParser.select, """
+PREFIX empP : <http://hr.example/DB/Employee#>
SELECT ?manager {
-<http://hr.example/DB/Employee/id.18#record> <http://hr.example/DB/Employee#manager> ?manager
+<http://hr.example/DB/Employee/id.18#record> empP:manager ?manager
}
""").get
val sqlParser = Sql()
@@ -71,8 +73,10 @@
* instead does what user meant. */
val sparqlParser = Sparql()
val sparqlSelect = sparqlParser.parseAll(sparqlParser.select, """
+PREFIX empP : <http://hr.example/DB/Employee#>
+PREFIX xsd : <http://www.w3.org/2001/XMLSchema#>
SELECT ?emp {
-?emp <http://hr.example/DB/Employee#manager> "18"^^<http://www.w3.org/2001/XMLSchema#integer>
+?emp empP:manager "18"^^xsd:integer
}
""").get
val sqlParser = Sql()
@@ -89,9 +93,10 @@
test("?s1 <p> ?x . ?s2 <p> ?x") {
val sparqlParser = Sparql()
val sparqlSelect = sparqlParser.parseAll(sparqlParser.select, """
+PREFIX empP : <http://hr.example/DB/Employee#>
SELECT ?emp1 ?emp2 ?sharedName {
- ?emp1 <http://hr.example/DB/Employee#lastName> ?sharedName .
- ?emp2 <http://hr.example/DB/Employee#lastName> ?sharedName
+ ?emp1 empP:lastName ?sharedName .
+ ?emp2 empP:lastName ?sharedName
}
""").get
val sqlParser = Sql()
@@ -108,10 +113,11 @@
test("transform SQLbgp") {
val sparqlParser = Sparql()
val sparqlSelect = sparqlParser.parseAll(sparqlParser.select, """
+PREFIX empP : <http://hr.example/DB/Employee#>
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> ?manageName
+?emp empP:lastName ?empName .
+?emp empP:manager ?manager .
+?manager empP:lastName ?manageName
}
""").get
val sqlParser = Sql()
@@ -129,9 +135,10 @@
test("transform tup1 no-enforce") {
val sparqlParser = Sparql()
val sparqlSelect = sparqlParser.parseAll(sparqlParser.select, """
+PREFIX empP : <http://hr.example/DB/Employee#>
SELECT ?empName {
- ?emp <http://hr.example/DB/Employee#lastName> ?empName .
- ?emp <http://hr.example/DB/Employee#manager> <http://hr.example/DB/Employee/id.18#record>
+ ?emp empP:lastName ?empName .
+ ?emp empP:manager <http://hr.example/DB/Employee/id.18#record>
}
""").get
val sqlParser = Sql()
@@ -147,9 +154,10 @@
test("transform tup1 enforce") {
val sparqlParser = Sparql()
val sparqlSelect = sparqlParser.parseAll(sparqlParser.select, """
+PREFIX empP : <http://hr.example/DB/Employee#>
SELECT ?empName {
- ?emp <http://hr.example/DB/Employee#lastName> ?empName .
- ?emp <http://hr.example/DB/Employee#manager> <http://hr.example/DB/Employee/id.18#record>
+ ?emp empP:lastName ?empName .
+ ?emp empP:manager <http://hr.example/DB/Employee/id.18#record>
}
""").get
val sqlParser = Sql()
@@ -167,10 +175,12 @@
test("transform litConst1") {
val sparqlParser = Sparql()
val sparqlSelect = sparqlParser.parseAll(sparqlParser.select, """
+PREFIX empP : <http://hr.example/DB/Employee#>
+PREFIX xsd : <http://www.w3.org/2001/XMLSchema#>
SELECT ?empName {
- ?emp <http://hr.example/DB/Employee#lastName> ?empName .
- ?emp <http://hr.example/DB/Employee#manager> ?manager .
- ?manager <http://hr.example/DB/Employee#lastName> "Johnson"^^<http://www.w3.org/2001/XMLSchema#string>
+ ?emp empP:lastName ?empName .
+ ?emp empP:manager ?manager .
+ ?manager empP:lastName "Johnson"^^xsd:string
}
""").get
val sqlParser = Sql()
@@ -188,16 +198,18 @@
test("transform filter1") {
val sparqlParser = Sparql()
val sparqlSelect = sparqlParser.parseAll(sparqlParser.select, """
+PREFIX empP : <http://hr.example/DB/Employee#>
+PREFIX manP : <http://hr.example/DB/Manage#>
SELECT ?empName ?grandManagName {
- ?emp <http://hr.example/DB/Employee#lastName> ?empName .
- ?emp <http://hr.example/DB/Employee#birthday> ?empBday .
- ?lower <http://hr.example/DB/Manage#manages> ?emp .
- ?lower <http://hr.example/DB/Manage#manager> ?manager .
- ?manager <http://hr.example/DB/Employee#birthday> ?manBday .
- ?upper <http://hr.example/DB/Manage#manages> ?manager .
- ?upper <http://hr.example/DB/Manage#manager> ?grandManager .
- ?grandManager <http://hr.example/DB/Employee#birthday> ?grandManBday .
- ?grandManager <http://hr.example/DB/Employee#lastName> ?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)
}
""").get
@@ -225,15 +237,17 @@
test("transform disj1") {
val sparqlParser = Sparql()
val sparqlSelect = sparqlParser.parseAll(sparqlParser.select, """
+PREFIX empP : <http://hr.example/DB/Employee#>
+PREFIX manP : <http://hr.example/DB/Manage#>
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 }
+ { ?who empP:lastName "Smith"^^xsd:string
+ { ?above manP:manages ?who .
+ ?above manP:manager ?manager .
+ ?manager empP: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 } }
+ { ?below manP:manager ?who .
+ ?below manP:manages ?managed .
+ ?managed empP:lastName ?name } }
""").get
val sqlParser = Sql()
val sqlSelect = sqlParser.parseAll(sqlParser.select, """
--- a/src/test/scala/SparqlTest.scala Wed Dec 30 10:25:01 2009 -0500
+++ b/src/test/scala/SparqlTest.scala Wed Dec 30 11:19:36 2009 -0500
@@ -92,8 +92,9 @@
test("SELECT") {
val a = Sparql()
val e = """
+PREFIX empP : <http://hr.example/DB/Employee#>
SELECT ?empName ?manageName {
-?emp <http://hr.example/DB/Employee#lastName> ?empName
+?emp empP:lastName ?empName
}
"""
val tps =
@@ -166,16 +167,18 @@
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 <http://hr.example/DB/Employee#lastName> ?empName .
- ?emp <http://hr.example/DB/Employee#birthday> ?empBday .
- ?lower <http://hr.example/DB/Manage#manages> ?emp .
- ?lower <http://hr.example/DB/Manage#manager> ?manager .
- ?manager <http://hr.example/DB/Employee#birthday> ?manBday .
- ?upper <http://hr.example/DB/Manage#manages> ?manager .
- ?upper <http://hr.example/DB/Manage#manager> ?grandManager .
- ?grandManager <http://hr.example/DB/Employee#birthday> ?grandManBday .
- ?grandManager <http://hr.example/DB/Employee#lastName> ?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)
}
"""