--- a/src/main/scala/SPARQL.scala Sat Jan 30 13:43:11 2010 -0500
+++ b/src/main/scala/SPARQL.scala Sun Jan 31 08:10:01 2010 -0500
@@ -14,6 +14,7 @@
import MyParsers._
case class Select(attrs:SparqlAttributeList, gp:GraphPattern)
+case class Construct(pattern:TriplesBlock, gp:GraphPattern)
case class SparqlAttributeList(attributelist:List[Var])
sealed abstract class GraphPattern
@@ -93,6 +94,12 @@
def select:Parser[Select] =
rep(prefixdecls) ~ "SELECT" ~ attributelist ~ opt("WHERE") ~ groupgraphpattern ^^ { case x~"SELECT"~a~w~gp => Select(a, gp) }
+ def construct:Parser[Construct] =
+ rep(prefixdecls) ~ "CONSTRUCT" ~ constructpattern ~ opt("WHERE") ~ groupgraphpattern ^^ { case x~"CONSTRUCT"~a~w~gp => Construct(a, gp) }
+
+ def constructpattern:Parser[TriplesBlock] =
+ "{" ~ opt(triplesblock) ~ "}" ^^ { case "{"~tbOPT~"}" => tbOPT.getOrElse(TriplesBlock(List[TriplePattern]())) }
+
def prefixdecls:Parser[Unit] =
"PREFIX" ~ name ~ ":" ~ qnameORuri ^^ { case "PREFIX"~pre~":"~u => prefixes += (pre -> u) }
--- a/src/test/scala/SparqlTest.scala Sat Jan 30 13:43:11 2010 -0500
+++ b/src/test/scala/SparqlTest.scala Sun Jan 31 08:10:01 2010 -0500
@@ -438,4 +438,28 @@
CellValue("18")))
}
+ test("CONSTRUCT") {
+ val a = Sparql()
+ val e = """
+PREFIX empP : <http://hr.example/DB/Employee#>
+CONSTRUCT { ?emp <http://foo.example/person#lname> ?empName }
+ WHERE { ?emp empP:lastName ?empName }
+"""
+ val tps =
+ Construct(
+ TriplesBlock(
+ List(
+ TriplePattern(
+ SVar(Var("emp")),
+ PUri(Stem("http://foo.example/"),Rel("person"),Attr("lname")),
+ OVar(Var("empName"))))),
+ TriplesBlock(
+ List(
+ TriplePattern(
+ SVar(Var("emp")),
+ PUri(Stem("http://hr.example/DB"),Rel("Employee"),Attr("lastName")),
+ OVar(Var("empName"))))))
+ assert(tps === a.parseAll(a.construct, e).get)
+ }
+
}