~ NodeMap is a real partial function no-hierarchy
authorAlexandre Bertails <bertails@gmail.com>
Sat, 12 Feb 2011 16:01:37 -0500
branchno-hierarchy
changeset 331 b13a84b588cf
parent 330 7f4830491c64
child 332 0b6248c67203
~ NodeMap is a real partial function
- FundamentalTest as it was now testing for function equality
directmapping-test/src/main/scala/DirectMappingTestSuite.scala
directmapping-test/src/test/scala/DirectMappingTest.scala
directmapping/src/main/scala/DirectMapping.scala
--- a/directmapping-test/src/main/scala/DirectMappingTestSuite.scala	Sat Feb 12 15:34:32 2011 -0500
+++ b/directmapping-test/src/main/scala/DirectMappingTestSuite.scala	Sat Feb 12 16:01:37 2011 -0500
@@ -8,37 +8,6 @@
 
 import org.scalatest.FunSuite
 
-trait FundamentalTest extends FunSuite with DirectMappingModule with RDFImplicits {
-
-  import DirectMapping._
-
-  test("NodeMap") {
-
-    val ck1:CandidateKey = CandidateKey("name", "ssn")
-    val ck2:CandidateKey = CandidateKey("ID")
-    val v11:List[CellValue] = List(LexicalValue("bob"), LexicalValue("123"))
-    val v21:List[CellValue] = List(LexicalValue("alice"), LexicalValue("8"))
-    val v12:List[CellValue] = List(LexicalValue("18"))
-    val v22:List[CellValue] = List(LexicalValue("23"))
-    val s1:Node = NodeBNode(BNode("1"))
-    val s2:Node = NodeBNode(BNode("2"))
-    val data:Set[(List[(CandidateKey, List[CellValue])], Node)] =
-      Set((List((ck1, v11),(ck2, v21)), s1),
-	  (List((ck1, v12),(ck2, v22)), s2))
-    val test = data.foldLeft(KeyMap(Map[CandidateKey,  Map[List[CellValue], Node]]()))((m, t) => m ++ (t._1, t._2))
-
-    val goal:KeyMap = KeyMap(
-      Map(ck1 -> Map(v11 -> s1,
-      		     v12 -> s2),
-      	  ck2 -> Map(v21 -> s1,
-      		     v22 -> s2))
-    )
-    assert(goal === test)
-  }
-
-}
-
-
 trait DirectMappingTest extends FunSuite with RDFModel with RDFImplicits with DirectMappingModule with TurtleModule {
 
   import DirectMapping._
--- a/directmapping-test/src/test/scala/DirectMappingTest.scala	Sat Feb 12 15:34:32 2011 -0500
+++ b/directmapping-test/src/test/scala/DirectMappingTest.scala	Sat Feb 12 16:01:37 2011 -0500
@@ -2,6 +2,4 @@
 
 import org.w3.rdf.ConcreteModel
 
-class FundamentalTestWithRDF extends FundamentalTest with ConcreteModel
-
 class DirectMappingTestWithConcreteModel extends DirectMappingTest with ConcreteModel
--- a/directmapping/src/main/scala/DirectMapping.scala	Sat Feb 12 15:34:32 2011 -0500
+++ b/directmapping/src/main/scala/DirectMapping.scala	Sat Feb 12 16:01:37 2011 -0500
@@ -15,30 +15,61 @@
      * node for any tuple in a unique relation.
      * CandidateKey -> List[CellValue] -> Node
      */
-    case class KeyMap(private val m:Map[CandidateKey,  Map[List[CellValue], Node]]) extends PartialFunction[CandidateKey, PartialFunction[List[CellValue], Node]] {
-      def apply(i:CandidateKey) : Map[List[CellValue], Node] = m(i)
-      def ++(pairs:List[(CandidateKey, List[CellValue])], n:Node):KeyMap = {
-        val m2:Map[CandidateKey,  Map[List[CellValue], Node]] =
-          pairs.foldLeft(m) { case (m, (ck, cellValues)) => {
-            m.get(ck) match {
-              case Some(byKey) if byKey.get(cellValues).isDefined =>
-                error("tried to set " + ck + cellValues + " = " + n + "(was " + byKey(cellValues) + ")")
-              case Some(byKey) =>
-                m + (ck -> (byKey + (cellValues -> n)))
-              case None =>
-                m + (ck -> Map(cellValues -> n))
-            }
-          } }
-        KeyMap(m2)
+    type KeyMap = PartialFunction[CandidateKey, PartialFunction[List[CellValue], Node]]
+
+    // case class KeyMap(private val m:Map[CandidateKey,  Map[List[CellValue], Node]]) extends PartialFunction[CandidateKey, PartialFunction[List[CellValue], Node]] {
+    //   def ++(pairs:List[(CandidateKey, List[CellValue])], n:Node):KeyMap = {
+    //     val m2:Map[CandidateKey,  Map[List[CellValue], Node]] =
+    //       pairs.foldLeft(m) { case (m, (ck, cellValues)) => {
+    //         m.get(ck) match {
+    //           case Some(byKey) if byKey.get(cellValues).isDefined =>
+    //             error("tried to set " + ck + cellValues + " = " + n + "(was " + byKey(cellValues) + ")")
+    //           case Some(byKey) =>
+    //             m + (ck -> (byKey + (cellValues -> n)))
+    //           case None =>
+    //             m + (ck -> Map(cellValues -> n))
+    //         }
+    //       } }
+    //     KeyMap(m2)
+    //   }
+    // }
+
+    /** The NodeMap-generating functions: */
+    // def relation2KeyMap (r:Relation) : KeyMap = {
+    //   val m = KeyMap(Map[CandidateKey, Map[List[CellValue], Node]]())
+    //   r.body.foldLeft(m) { (m, t) => {
+    //     val (pairs, node) = rdfNodeForTuple(t, r)
+    //     m ++ (pairs, node)
+    //   } }
+    // }
+
+    def KeyMap(r:Relation):KeyMap = {
+      def ++(m:Map[CandidateKey,  Map[List[CellValue], Node]],
+	     pairs:List[(CandidateKey, List[CellValue])],
+	     n:Node):Map[CandidateKey,  Map[List[CellValue], Node]] = {
+	pairs.foldLeft(m) { case (m, (ck, cellValues)) => {
+          m.get(ck) match {
+            case Some(byKey) if byKey.get(cellValues).isDefined =>
+              error("tried to set " + ck + cellValues + " = " + n + "(was " + byKey(cellValues) + ")")
+            case Some(byKey) =>
+              m + (ck -> (byKey + (cellValues -> n)))
+            case None =>
+              m + (ck -> Map(cellValues -> n))
+          }
+        } }  
       }
-      def isDefinedAt(ck:CandidateKey) = m isDefinedAt ck
+      val m = Map[CandidateKey, Map[List[CellValue], Node]]()
+      r.body.foldLeft(m) { (m, t) => {
+        val (pairs, node) = rdfNodeForTuple(t, r)
+        ++(m, pairs, node)
+      } }
     }
 
     type NodeMap = PartialFunction[RelName, KeyMap]
 
     def NodeMap(db:Database):NodeMap = {
       val idxables = db.keySet filter { rn => db(rn).candidates nonEmpty }
-      idxables map { rn:RelName => rn -> relation2KeyMap(db(rn)) } toMap
+      idxables map { rn:RelName => rn -> KeyMap(db(rn)) } toMap
     }
   
     /** The direct mapping requires one parameter: the StemIRI */
@@ -61,16 +92,7 @@
       val notNulls:Set[AttrName] = t.notNullAttributes
       notNulls filterNot { attrName => r.fks definesActuallyUnaryFK attrName }
     }
-  
-    /** The NodeMap-generating functions: */
-    def relation2KeyMap (r:Relation) : KeyMap = {
-      val m = KeyMap(Map[CandidateKey, Map[List[CellValue], Node]]())
-      r.body.foldLeft(m) { (m, t) => {
-        val (pairs, node) = rdfNodeForTuple(t, r)
-        m ++ (pairs, node)
-      } }
-    }
-  
+    
     def rdfNodeForTuple (t:Tuple, r:Relation) : (List[(CandidateKey, List[CellValue])], Node) = {
       val s:Node =
         r.pk match {
@@ -149,7 +171,7 @@
         error("No referent relation \"" + rel + "\" to match " + r.name + t)
       if (!(nodes(rel) isDefinedAt key))
         error("Relation " + rel + " has no attributes (" + key + ") to match " + r.name + t)
-      if (!nodes(rel)(key).contains(ls))
+      if (!(nodes(rel)(key) isDefinedAt ls))
         error("Relation " + rel + "(" + key + ") has no values " + ls + " to match " + r.name + t)
       val o:Object = ObjectNode(nodes(rel)(key)(ls))
        Triple(SubjectNode(s), PredicateIRI(p), o)