--- a/src/main/scala/SPARQL.scala Sun Jan 31 20:30:34 2010 -0500
+++ b/src/main/scala/SPARQL.scala Mon Feb 01 09:15:57 2010 -0500
@@ -13,22 +13,33 @@
import MyParsers._
-case class Select(attrs:SparqlAttributeList, gp:GraphPattern)
+case class Select(attrs:SparqlAttributeList, gp:GraphPattern) {
+ override def toString = attrs+"\n"+gp
+}
case class Construct(head:TriplesBlock, gp:GraphPattern)
-case class SparqlAttributeList(attributelist:List[Var])
+case class SparqlAttributeList(attributelist:List[Var]) {
+ override def toString = "SELECT "+(attributelist.toList.sortWith((l, r) => l.s < r.s).mkString(", "))
+}
sealed abstract class GraphPattern
-case class TriplesBlock(triplepatterns:List[TriplePattern]) extends GraphPattern
+case class TriplesBlock(triplepatterns:List[TriplePattern]) extends GraphPattern {
+ override def toString = "{\n " + (triplepatterns.toList.map(s => s.toString.replace("\n", "\n ")).mkString(".\n "))
+}
case class TableConjunction(gps:List[GraphPattern]) extends GraphPattern {
assert (!(gps exists (x => { x match { case TableConjunction(_) => true case _ => false } })))
+ override def toString = " " + (gps.toList.map(s => s.toString.replace("\n", "\n ")).mkString("\n "))
}
-case class TableDisjunction(gps:List[GraphPattern]) extends GraphPattern
+case class TableDisjunction(gps:List[GraphPattern]) extends GraphPattern {
+ override def toString = " " + (gps.toList.map(s => s.toString.replace("\n", "\n ")).mkString("\nUNION\n "))
+}
case class TableFilter(gp:GraphPattern, expr:Expression) extends GraphPattern
case class OptionalGraphPattern(gp:GraphPattern) extends GraphPattern
case class MinusGraphPattern(gp:GraphPattern) extends GraphPattern
case class GraphGraphPattern(gp:GraphPattern) extends GraphPattern
-case class TriplePattern(s:S, p:P, o:O)
+case class TriplePattern(s:S, p:P, o:O) {
+ override def toString = s + " " + p + " " + o
+}
sealed abstract class S
case class SUri(u:Uri) extends S {
--- a/src/test/scala/SparqlToSparqlTest.scala Sun Jan 31 20:30:34 2010 -0500
+++ b/src/test/scala/SparqlToSparqlTest.scala Mon Feb 01 09:15:57 2010 -0500
@@ -32,7 +32,7 @@
assert(transformed === expected)
}
- test("foaf:last_name") {
+ test("foaf:last_name single") {
val sparqlParser = Sparql()
val query = sparqlParser.parseAll(sparqlParser.select, """
PREFIX foaf : <http://xmlns.com/foaf/0.1/>
@@ -41,7 +41,39 @@
?emp foaf:last_name "Smith"^^xsd:string
}
""").get
- val rule1 = sparqlParser.parseAll(sparqlParser.construct, """
+ val lname = sparqlParser.parseAll(sparqlParser.construct, """
+PREFIX foaf : <http://xmlns.com/foaf/0.1/>
+PREFIX empP : <http://hr.example/DB/Employee#>
+CONSTRUCT { ?who foaf:last_name ?lname }
+ WHERE { ?who empP:lastName ?lname }
+""").get
+ val fname = sparqlParser.parseAll(sparqlParser.construct, """
+PREFIX foaf : <http://xmlns.com/foaf/0.1/>
+PREFIX empP : <http://hr.example/DB/Employee#>
+CONSTRUCT { ?who foaf:first_name ?fname }
+ WHERE { ?who empP:firstName ?fname }
+""").get
+ val transformed = SparqlToSparql(query, List(fname, lname))
+ val expected = sparqlParser.parseAll(sparqlParser.select, """
+PREFIX empP : <http://hr.example/DB/Employee#>
+PREFIX xsd : <http://www.w3.org/2001/XMLSchema#>
+SELECT ?emp {
+?emp empP:lastName "Smith"^^xsd:string
+}
+""").get
+ assert(transformed === expected)
+ }
+
+ test("foaf:last_name multi") {
+ val sparqlParser = Sparql()
+ val query = sparqlParser.parseAll(sparqlParser.select, """
+PREFIX foaf : <http://xmlns.com/foaf/0.1/>
+PREFIX xsd : <http://www.w3.org/2001/XMLSchema#>
+SELECT ?emp {
+?emp foaf:last_name "Smith"^^xsd:string
+}
+""").get
+ val flname = sparqlParser.parseAll(sparqlParser.construct, """
PREFIX foaf : <http://xmlns.com/foaf/0.1/>
PREFIX empP : <http://hr.example/DB/Employee#>
CONSTRUCT { ?who foaf:first_name ?fname .
@@ -49,7 +81,7 @@
WHERE { ?who empP:firstName ?fname .
?who empP:lastName ?lname }
""").get
- val transformed = SparqlToSparql(query, List(rule1))
+ val transformed = SparqlToSparql(query, List(flname))
val expected = sparqlParser.parseAll(sparqlParser.select, """
PREFIX empP : <http://hr.example/DB/Employee#>
PREFIX xsd : <http://www.w3.org/2001/XMLSchema#>
@@ -61,4 +93,40 @@
assert(transformed === expected)
}
+// test("to many-to-many") {
+// val sparqlParser = Sparql()
+// val query = sparqlParser.parseAll(sparqlParser.select, """
+// PREFIX foaf : <http://xmlns.com/foaf/0.1/>
+// PREFIX xsd : <http://www.w3.org/2001/XMLSchema#>
+// SELECT ?lname {
+// ?who foaf:last_name ?lname .
+// ?who foaf:knows ?whom .
+// ?whom foaf:last_name "Smith"^^xsd:string }
+// """).get
+// val rule1 = sparqlParser.parseAll(sparqlParser.construct, """
+// PREFIX foaf : <http://xmlns.com/foaf/0.1/>
+// PREFIX empP : <http://hr.example/DB/Employee#>
+// PREFIX task : <http://hr.example/DB/Task#>
+// CONSTRUCT { ?emp foaf:last_name ?wname .
+// ?emp foaf:knows ?man .
+// ?man foaf:last_name ?mname }
+// WHERE { ?emp empP:lastName ?wname .
+// ?pair task:drone ?emp .
+// ?pair task:manager ?man .
+// ?man empP:lastName ?mname }
+// """).get
+// val transformed = SparqlToSparql(query, List(rule1))
+// val expected = sparqlParser.parseAll(sparqlParser.select, """
+// PREFIX empP : <http://hr.example/DB/Employee#>
+// PREFIX task : <http://hr.example/DB/Task#>
+// PREFIX xsd : <http://www.w3.org/2001/XMLSchema#>
+// SELECT ?lname
+// { ?who empP:lastName ?lname .
+// ?pair task:drone ?who .
+// ?pair task:manager ?whom .
+// ?whom empP:lastName "Smith"^^xsd:string }
+// """).get
+// assert(transformed === expected)
+// }
+
}