~ refactor -- collect bindings while traversing matching predicates
authorEric Prud'hommeaux <eric@w3.org>
Sun, 14 Feb 2010 07:50:32 -0500
changeset 165 8b545d69368e
parent 164 6a5bfefcfcdb
child 166 5326978aaa16
~ refactor -- collect bindings while traversing matching predicates
src/main/scala/SparqlToSparql.scala
--- a/src/main/scala/SparqlToSparql.scala	Sun Feb 14 07:46:50 2010 -0500
+++ b/src/main/scala/SparqlToSparql.scala	Sun Feb 14 07:50:32 2010 -0500
@@ -100,32 +100,36 @@
     // val varsS:Option[Bindings] = vars.maybeRebind(construct, v, tos)
     // b:Map[sparql.Construct, List[Map[sparql.Var, sparql.Term]]]
     def mustBind (construct:sparql.Construct, vs:sparql.Term, tos:sparql.Term, vo:sparql.Term, too:sparql.Term):Bindings = {
-      /* rediculous traversal for the first viably matching rule edition. */
+      /* ridiculous traversal for the first viably matching rule edition. */
       var matched = false
       val existing:List[Map[sparql.Var, sparql.Term]] = b(construct).map((map) => {
-	def _matches (l:sparql.Term, r:sparql.Term):Boolean = {
-	  l match {
-	    case v:sparql.TermVar =>
+	def _matches (l:sparql.Term, r:sparql.Term):(Boolean, Map[sparql.Var, sparql.Term]) = {
+	  val empty = Map[sparql.Var, sparql.Term]()
+	  (l, r) match {
+	    case (v:sparql.TermVar, x) =>
+	      // println("(v:sparql.TermVar, x)" + v.v + ":" + x)
 	      if (map.contains(v.v)) {
-		if (r == map(v.v)) true
-		else false
+		if (r == map(v.v)) (true, empty)
+		else (false, empty)
 	      } else {
-		true
+		(true, Map(v.v -> r))
 	      }
-	    case u:sparql.TermUri =>
-	      if (r == u.u) true
-	      else false
-	    case l:sparql.TermLit =>
-	      if (r == l.lit) true
-	      else false
+	    case (x, v:sparql.TermVar) => {
+	      // println("query variable " + v + " know equal " + x + " at compile time")
+	      (true, empty)
+	    }
+	    case (x, y) =>
+	      // println("(x, y)" + x + "?=" + y)
+	      if (x == y) (true, empty)
+	      else (false, empty)
 	  }
 	}
-	val sMatches = _matches(vs, tos)
-	val oMatches = _matches(vo, too)
+	val (sMatches, sBindings) = _matches(vs, tos)
+	val (oMatches, oBindings) = _matches(vo, too)
 
 	if (sMatches & oMatches) {
 	  matched = true
-	  map + (vs.asInstanceOf[sparql.TermVar].v -> tos) + (vo.asInstanceOf[sparql.TermVar].v -> too)
+	  map ++ sBindings ++ oBindings
 	} else
 	  map
       })
@@ -135,8 +139,19 @@
 	Bindings(b.map((constructlist) => {
 	  val (oldConstr, l) = constructlist
 	  if (oldConstr == construct) {
-	    val ent = Map[sparql.Var, sparql.Term](vs.asInstanceOf[sparql.TermVar].v -> tos,
-						   vo.asInstanceOf[sparql.TermVar].v -> too)
+	    def _newBinding (l:sparql.Term, r:sparql.Term):Map[sparql.Var, sparql.Term] = {
+	      val empty = Map[sparql.Var, sparql.Term]()
+	      (l, r) match {
+		case (v:sparql.TermVar, _) =>
+		  Map(v.v -> r)
+		case (_, v:sparql.TermVar) => {
+		  println("query variable " + v + " know equal " + l + " at compile time")
+		  Map[sparql.Var, sparql.Term]()
+		}
+		case (_, _) => Map[sparql.Var, sparql.Term]()
+	      }
+	    }
+	    val ent = _newBinding(vs, tos) ++ _newBinding(vo, too)
 	    val list = l ++ List(ent)
 	    (construct -> list)
 	  } else {
@@ -151,10 +166,12 @@
   case class RuleMap (rules:Map[sparql.Uri, List[RuleIndex]]) {
     def transform (prove:List[sparql.TriplePattern], used:Set[sparql.TriplePattern], varsP:Bindings):Bindings = {
       val _pad = used.foldLeft("")((s, x) => s + " ")
-      def _deepPrint (s:String):Unit = { println(used.size + ":" + _pad + s.replace("\n", "\n" + _pad)) }
+      def _deepPrint (s:String):Unit = {
+	//println(used.size + ":" + _pad + s.replace("\n", "\n" + _pad))
+      }
       def _deepPrint1 (prefix:String, s:String):Unit = {
 	val p = used.size + ":" + prefix + _pad
-	println(p + s.replace("\n", "\n" + p))
+	//println(p + s.replace("\n", "\n" + p))
       }
 
       val car = prove(0)
@@ -207,7 +224,18 @@
 	val emptyBindings = createEmptyBindings
 	ruleMap.transform(tps, Set[sparql.TriplePattern](), emptyBindings).toGraphPattern
       }
-      case _ => error("not implemented: " + gp);
+      case sparql.TableFilter(gp2:sparql.GraphPattern, expr:sparql.Expression) =>
+	sparql.TableFilter(mapGraphPattern(gp2, ruleMap), expr)
+      case sparql.MinusGraphPattern(gp2) =>
+	sparql.MinusGraphPattern(mapGraphPattern(gp2, ruleMap))
+      case sparql.OptionalGraphPattern(gp2) =>
+        sparql.OptionalGraphPattern(mapGraphPattern(gp2, ruleMap))
+      case sparql.GraphGraphPattern(gp2) =>
+	sparql.GraphGraphPattern(mapGraphPattern(gp2, ruleMap))
+      case sparql.TableDisjunction(l) =>
+	sparql.TableDisjunction({l.map(gp2 => mapGraphPattern(gp2, ruleMap))})
+      case sparql.TableConjunction(l) =>
+	sparql.TableConjunction({l.map(gp2 => mapGraphPattern(gp2, ruleMap))})
     }
   }
 
@@ -228,7 +256,7 @@
 	}))
       })
     })
-    println("ruleMap: " + ruleMap)
+    //println("ruleMap: " + ruleMap)
     sparql.Select(
       query.attrs,
       mapGraphPattern(query.gp, ruleMap)