~ fixed toGraphPattern -- wasn't including earlier conjunctions
authorEric Prud'hommeaux <eric@w3.org>
Thu, 17 Jun 2010 07:15:32 -0700
changeset 225 85376c4dd925
parent 224 d2f793fb52e6
child 226 4247cc69c1e7
~ fixed toGraphPattern -- wasn't including earlier conjunctions
src/main/scala/SparqlToSparql.scala
--- a/src/main/scala/SparqlToSparql.scala	Wed Jun 16 14:04:16 2010 -0700
+++ b/src/main/scala/SparqlToSparql.scala	Thu Jun 17 07:15:32 2010 -0700
@@ -149,14 +149,16 @@
     }
     def toGraphPattern (ident:String):sparql.GraphPattern = {
       var conjNo = 0
-      val conjuncts = b.foldLeft(List[sparql.GraphPattern]())((conj, constructlist) => {
-	val (construct, l) = constructlist
-	l.map((vartermmap) => {
+      val conjuncts = b.keySet.toList.flatMap(construct => {
+	val l = b(construct)
+	val patterns:List[sparql.GraphPattern] = l.map((vartermmap) => {
 	  val unique = ident + conjNo + "_"
 	  conjNo = conjNo + 1
 	  substituteGraphPattern(construct.gp, vartermmap, unique)
 	})
+	patterns
       })
+
       if (conjuncts.size == 0)
 	sparql.TriplesBlock(List[sparql.TriplePattern]())
       else if (conjuncts.size > 1)
@@ -260,11 +262,11 @@
     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))
+	//print(used.size + ":" + _pad + s.replace("\n", "\n" + _pad) + "\n\n")
       }
       def _deepPrint1 (prefix:String, s:String):Unit = {
 	val p = used.size + ":" + prefix + _pad
-	//println(p + s.replace("\n", "\n" + p))
+	//print(p + s.replace("\n", "\n" + p) + "\n\n")
       }
 
       val car = prove(0)
@@ -291,10 +293,19 @@
 	    _deepPrint1(_prefix, _shorten(hornRule.trigger.toString) + "(" + _shorten(car.toString) + ") matches ..." + bindings.toString + ret.toString)
 
 	    /* Magic Huristics */
-	    if (bindings.countEditions == 0) ret
-	    else if (ret.countEditions == 0) bindings
-	    else if (ret.countEditions < bindings.countEditions) ret
-	    else bindings
+	    if (bindings.countEditions == 0) {
+	      //print("choose: bindings.countEditions == 0 => " + ret + "\n\n")
+	      ret
+	    } else if (ret.countEditions == 0) {
+	      //print("choose: ret.countEditions == 0 => " + bindings + "\n\n")
+	      bindings
+	    } else if (ret.countEditions < bindings.countEditions) {
+	      //print("choose: "+ret.countEditions+" < "+bindings.countEditions+" => " + ret + "\n\n")
+	      ret
+	    } else {
+	      //print("choose: "+ret.countEditions+" > "+bindings.countEditions+" => " + bindings + "\n\n")
+	      bindings
+	    }
 	  })
 	case _ => error("not implemented: " + car.p)
       }
@@ -315,7 +326,11 @@
     gp match {
       case sparql.TriplesBlock(tps) => {
 	val emptyBindings = createEmptyBindings
-	ruleMap.transform(tps, Set[sparql.TriplePattern](), emptyBindings).toGraphPattern(ident)
+	val b:Bindings = ruleMap.transform(tps, Set[sparql.TriplePattern](), emptyBindings)
+	//print("mapGraphPattern: " + _shorten(gp.toString) + ") => " + _shorten(b.toString) +  "\n\n")
+	val g:sparql.GraphPattern = b.toGraphPattern(ident)
+	//print("mapGraphPattern: ... instantiated: " + _shorten(g.toString) +  "\n\n")
+	g
       }
       case sparql.TableFilter(gp2:sparql.GraphPattern, expr:sparql.Expression) =>
 	sparql.TableFilter(mapGraphPattern(gp2, ruleMap, ident), expr)