--- a/src/main/scala/SPARQL.scala Sun Jan 03 23:25:03 2010 -0500
+++ b/src/main/scala/SPARQL.scala Mon Jan 04 15:48:21 2010 -0500
@@ -18,7 +18,9 @@
sealed abstract class GraphPattern
case class TriplesBlock(triplepatterns:List[TriplePattern]) extends GraphPattern
-case class TableConjunction(gps:List[GraphPattern]) extends GraphPattern
+case class TableConjunction(gps:List[GraphPattern]) extends GraphPattern {
+ assert (!(gps exists (x => { x match { case TableConjunction(_) => true case _ => false } })))
+}
case class TableDisjunction(gps:List[GraphPattern]) extends GraphPattern
case class TableFilter(gp:GraphPattern, expr:Expression) extends GraphPattern
case class OptionalGraphPattern(gp:GraphPattern) extends GraphPattern
@@ -104,27 +106,31 @@
// case class OptionalGraphPattern(gp:GraphPattern) extends GraphPattern
// case class GraphGraphPattern(gp:GraphPattern) extends GraphPattern
- // println("groupgraphpattern: " + tbOPT + " " + gpntORf_tbOPT)
+ // println("groupgraphpattern(" + tbOPT + ", " + gpntORf_tbOPT + ")")
val init:Option[GraphPattern] = tbOPT
gpntORf_tbOPT.foldLeft(init)((gp, lentry) => {//println("match: " + (gp, lentry))
-(gp, lentry) match {
- case (Some(TriplesBlock(l)), ~(TableFilter(null, expr), None )) => Some(TableFilter(TriplesBlock(l), expr))
- case (None, ~(TableFilter(null, expr), Some(TriplesBlock(r)))) => Some(TableFilter(TriplesBlock(r), expr))
- case (Some(TriplesBlock(l)), ~(TableFilter(null, expr), Some(TriplesBlock(r)))) => Some(TableFilter(TriplesBlock(l ++ r), expr))
- case (Some(TableFilter(TriplesBlock(l), Expression(lexp))), ~(TableFilter(null, Expression(expr)), Some(TriplesBlock(r)))) => Some(TableFilter(TriplesBlock(l ++ r), Expression(lexp ++ expr)))
+ // print("case (" + gp + ", " + lentry + ")")
+ (gp, lentry) match {
+ case (Some(TableFilter(TriplesBlock(l), Expression(lexp))), ~(TableFilter(null, Expression(expr)), Some(TriplesBlock(r)))) => Some(TableFilter(TriplesBlock(l ++ r), Expression(lexp ++ expr)))
+ case (Some(TriplesBlock(l)), ~(TableFilter(null, expr), Some(TriplesBlock(r)))) => Some(TableFilter(TriplesBlock(l ++ r), expr))
+ case (Some(gp ), ~(TableFilter(null, expr), None )) => Some(TableFilter(gp, expr))
+ case (None, ~(TableFilter(null, expr), Some(TriplesBlock(r)))) => Some(TableFilter(TriplesBlock(r), expr))
- // case (None, ~(TableConjunction(gps), None )) => TableConjunction(gps)
- // case (Some(gp), ~(TableConjunction(gps), None )) => TableConjunction(List(List(gp) ++ gps))
- // case (None, ~(TableConjunction(gps), Some(tb))) => TableConjunction(List(gps ++ List(tb)))
- // case (Some(gp), ~(TableConjunction(gps), Some(tb))) => TableConjunction(List(List(gp) ++ gps ++ List(tb)))
+ // case (None, ~(TableConjunction(gps), None )) => TableConjunction(gps)
+ // case (Some(gp), ~(TableConjunction(gps), None )) => TableConjunction(List(List(gp) ++ gps))
+ // case (None, ~(TableConjunction(gps), Some(tb))) => TableConjunction(List(gps ++ List(tb)))
+ // case (Some(gp), ~(TableConjunction(gps), Some(tb))) => TableConjunction(List(List(gp) ++ gps ++ List(tb)))
- case (None, ~(x, None )) => Some(x )
- case (Some(gp), ~(x, None )) => Some(TableConjunction(List(gp, x )))
- case (None, ~(x, Some(tb))) => Some(TableConjunction(List( x, tb)))
- case (Some(gp), ~(x, Some(tb))) => Some(TableConjunction(List(gp, x, tb)))
+ case (None , ~(x, None )) => Some(x )
+ case (Some(TableConjunction(l)), ~(x, None )) => Some(TableConjunction(l ++ List( x )))
+ case (Some(gp ), ~(x, None )) => Some(TableConjunction( List(gp, x )))
+ case (None , ~(x, Some(tb))) => Some(TableConjunction( List( x, tb)))
+ case (Some(TableConjunction(l)), ~(x, Some(tb))) => Some(TableConjunction(l ++ List( x, tb)))
+ case (Some(gp ), ~(x, Some(tb))) => Some(TableConjunction( List(gp, x, tb)))
- case x => error("found " + x)
- }}).get
+ case x => error("found " + x)
+ }
+ }).get
}
}
)
--- a/src/test/scala/SparqlTest.scala Sun Jan 03 23:25:03 2010 -0500
+++ b/src/test/scala/SparqlTest.scala Mon Jan 04 15:48:21 2010 -0500
@@ -372,6 +372,30 @@
assert(tps === a.parseAll(a.select, e).get)
}
+ test("parse { BGP A BGP FILTER") {
+ 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(SparqlAttributeList(List(Var("emp1Name"), Var("emp2Name"), Var("emp3Name"))),
+ TableFilter(TableConjunction(List(TriplesBlock(List(TriplePattern(SVar(Var("emp1")), PUri(Stem("http://hr.example/DB"),Rel("Employee"),Attr("lastName")), OVar(Var("emp1Name"))))),
+ OptionalGraphPattern(TriplesBlock(List(TriplePattern(SVar(Var("emp1")), PUri(Stem("http://hr.example/DB"),Rel("Employee"),Attr("birthday")), OVar(Var("birthday")))))),
+ TriplesBlock(List(TriplePattern(SVar(Var("emp2")), PUri(Stem("http://hr.example/DB"),Rel("Employee"),Attr("lastName")), OVar(Var("emp2Name"))))),
+ OptionalGraphPattern(TriplesBlock(List(TriplePattern(SVar(Var("emp2")), PUri(Stem("http://hr.example/DB"),Rel("Employee"),Attr("birthday")), OVar(Var("birthday")))))),
+ TriplesBlock(List(TriplePattern(SVar(Var("emp4")),PUri(Stem("http://hr.example/DB"),Rel("Employee"),Attr("birthday")),OVar(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"))))))))
+ 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"
val puri:PUri = Sparql.parsePredicateURI(uri)