~ hide unmapped vars in rule bodies
authorEric Prud'hommeaux <eric@w3.org>
Mon, 08 Feb 2010 12:52:08 -0500
changeset 158 4e74abad8304
parent 157 8ab6f119e8d8
child 159 c7bf17241c5d
~ hide unmapped vars in rule bodies
src/main/scala/SparqlToSparql.scala
src/test/scala/SparqlToSparqlTest.scala
--- a/src/main/scala/SparqlToSparql.scala	Mon Feb 08 11:50:35 2010 -0500
+++ b/src/main/scala/SparqlToSparql.scala	Mon Feb 08 12:52:08 2010 -0500
@@ -37,11 +37,20 @@
       case _ => error("not implemented" + gp)
     }
   }
-  def substituteGraphPattern (gp:sparql.GraphPattern, vartermmap:Map[sparql.Var, sparql.Term]):sparql.GraphPattern = {
-    vartermmap.foldLeft(gp)((incrementalGP, varterm) => {
+  def substituteGraphPattern (gp:sparql.GraphPattern, vartermmap:Map[sparql.Var, sparql.Term], varPrefix:String):sparql.GraphPattern = {
+    val mapped = vartermmap.foldLeft(gp)((incrementalGP, varterm) => {
       val (varr, term) = varterm
       substitute(incrementalGP, sparql.TermVar(varr), term)
     })
+
+    /* "Uniquely" prefix unmapped vars to void conflict with other rules. */
+    // val bound = Set[sparql.Var](vartermmap.map((varterm) => varterm._1))
+    val bound = vartermmap.foldLeft(Set[sparql.Var]())((s, varterm) => s + varterm._1)
+    val vars = gp.findVars
+    val diff = vars -- bound
+    diff.foldLeft(mapped)((incrementalGP, varr) => {
+      substitute(incrementalGP, sparql.TermVar(varr), sparql.TermVar(sparql.Var(varPrefix + varr.s)))
+    })
   }
   case class HornRule (trigger:sparql.TriplePattern, construct:sparql.Construct) {
     override def toString = "{ \"" + trigger + "\" } => {\"\n  " + _shorten(construct.gp.toString).replace("\n", "\n  ") + "\n\"}"
@@ -51,9 +60,14 @@
   }
   case class Bindings (b:Map[sparql.Construct, List[Map[sparql.Var, sparql.Term]]]) {
     def toGraphPattern ():sparql.GraphPattern = {
+      var conjNo = 0
       val conjuncts = b.foldLeft(List[sparql.GraphPattern]())((conj, constructlist) => {
 	val (construct, l) = constructlist
-	l.map((vartermmap) => substituteGraphPattern(construct.gp, vartermmap))
+	l.map((vartermmap) => {
+	  val unique = "_" + conjNo + "_"
+	  conjNo = conjNo + 1
+	  substituteGraphPattern(construct.gp, vartermmap, unique)
+	})
       })
       if (conjuncts.size == 0)
 	sparql.TriplesBlock(List[sparql.TriplePattern]())
--- a/src/test/scala/SparqlToSparqlTest.scala	Mon Feb 08 11:50:35 2010 -0500
+++ b/src/test/scala/SparqlToSparqlTest.scala	Mon Feb 08 12:52:08 2010 -0500
@@ -86,7 +86,7 @@
 PREFIX empP : <http://hr.example/DB/Employee#>
 PREFIX xsd : <http://www.w3.org/2001/XMLSchema#>
 SELECT ?emp {
-?emp  empP:firstName    ?fname .
+?emp  empP:firstName    ?_0_fname .
 ?emp  empP:lastName    "Smith"^^xsd:string
 }
 """).get
@@ -122,8 +122,8 @@
 PREFIX xsd : <http://www.w3.org/2001/XMLSchema#>
 SELECT ?lname
      { ?who  empP:lastName   ?lname .
-       ?pair task:drone      ?who .
-       ?pair task:manager    ?whom .
+       ?_0_pair task:drone      ?who .
+       ?_0_pair task:manager    ?whom .
        ?whom empP:lastName   "Smith"^^xsd:string }
 """).get
     assert(transformed === expected)