~ renames main functions + simplifies lexicalValueSemantics no-hierarchy
authorAlexandre Bertails <bertails@gmail.com>
Sat, 12 Feb 2011 17:33:48 -0500
branchno-hierarchy
changeset 334 f5fd76348339
parent 333 e01de9b8dfcd
child 335 9a828553ea69
~ renames main functions + simplifies lexicalValueSemantics
directmapping-test/src/main/scala/DirectMappingTestSuite.scala
directmapping/src/main/scala/DirectMapping.scala
rdb/src/main/scala/RDB.scala
--- a/directmapping-test/src/main/scala/DirectMappingTestSuite.scala	Sat Feb 12 16:41:13 2011 -0500
+++ b/directmapping-test/src/main/scala/DirectMappingTestSuite.scala	Sat Feb 12 17:33:48 2011 -0500
@@ -18,7 +18,7 @@
 
   def testDirectMapping(testName:String, db:Database, expectedGraph:Graph):Unit =
     test(testName) {
-      val computedGraph = directDB(db)
+      val computedGraph = databaseSemantics(db)
       DirectMapping.NextBNode = 97 // @@ call the "i'd like to reset my fresh variables to 0 so i can have predictable node names" function
       assert (expectedGraph === computedGraph)
     }
--- a/directmapping/src/main/scala/DirectMapping.scala	Sat Feb 12 16:41:13 2011 -0500
+++ b/directmapping/src/main/scala/DirectMapping.scala	Sat Feb 12 17:33:48 2011 -0500
@@ -74,19 +74,19 @@
     }
   
     /** The triples-generating functions start with databasemap: */
-    def directDB (db:Database) : Graph = {
+    def databaseSemantics (db:Database) : Graph = {
       val nodeMap = dbToNodeMap(db)
-      Graph(db.keySet flatMap  { (rn:RelName) => directR(db(rn), nodeMap, db) })
+      Graph(db.keySet flatMap  { (rn:RelName) => relationSemantics(db(rn), nodeMap, db) })
     }
   
-    def directR (r:Relation, nodes:NodeMap, db:Database) : Graph =
+    def relationSemantics (r:Relation, nodes:NodeMap, db:Database) : Graph =
       /* flatMap.toSet assumes that no two triples from directT would be the same.
        * We know this because relations with candidate keys are mapped to unique
        * subjects, and potentially redundant rows get unique blank node subjects.
        */
-      Graph(r.body flatMap { t => directT(t, r, nodes, db) })
+      Graph(r.body flatMap { t => tupleSemantics(t, r, nodes, db) })
   
-    def directT (t:Tuple, r:Relation, nodes:NodeMap, db:Database) : Set[Triple] = {
+    def tupleSemantics (t:Tuple, r:Relation, nodes:NodeMap, db:Database) : Set[Triple] = {
       val s:Node =
         r.candidates.headOption match {
           // Known to have at least one key, so take the first one.
@@ -98,15 +98,12 @@
           case None =>
             NodeBNode(freshbnode())  
         }
-      directS(s, t, r, nodes, db)
-    }
-  
-    def directS (s:Node, t:Tuple, r:Relation, nodes:NodeMap, db:Database) : Set[Triple] = {
-      ( references(t, r)  map { directN(s, _, r, t, nodes) } ) ++
-      ( scalars(t, r) flatMap { directL(r.name, s, _, r.header, t) } ) +
-      Triple(SubjectNode(s),
-	     PredicateIRI(IRI("http://www.w3.org/1999/02/22-rdf-syntax-ns#type")),
-	     ObjectNode(NodeIRI(IRI(UE(r)))))
+      val refs = references(t, r)  map { referenceSemantics(s, _, r, t, nodes) }
+      val scals = scalars(t, r) flatMap { lexicalValueSemantics(r.name, s, _, r.header, t) }
+      val triple = Triple(SubjectNode(s),
+			  PredicateIRI(IRI("http://www.w3.org/1999/02/22-rdf-syntax-ns#type")),
+			  ObjectNode(NodeIRI(IRI(UE(r)))))
+      refs ++ scals + triple
     }
   
     // should be done by BNode
@@ -117,20 +114,24 @@
       BNode(ret.toChar.toString)
     }
   
-    def directL (rn:RelName, s:Node, a:AttrName, h:Header, t:Tuple) : Option[Triple] = {
+    def lexicalValueSemantics (rn:RelName, s:Node, a:AttrName, h:Header, t:Tuple) : Option[Triple] = {
       val p = predicatemap (rn, new AttrList { val attrs = List(a) } )
-      t(a) match {
-	case l:LexicalValue => {
-	  val o = literalmap(l, h.sqlDatatype(a))
-	  Some(Triple(SubjectNode(s),
-		      PredicateIRI(p),
-		      ObjectLiteral(o)))
+      val cellValue = t(a)
+      val datatype = h.datatype(a)
+      (cellValue, datatype)  match {
+	case (LexicalValue(l), Datatype.STRING) => {
+	  val o = PlainLiteral(l, None)
+	  Some(Triple(SubjectNode(s), PredicateIRI(p), ObjectLiteral(o)))
 	}
-	case ␀ => None
+	case (LexicalValue(l), d) => {
+	  val o = TypedLiteral(l, datatypeSemantics(d))
+	  Some(Triple(SubjectNode(s), PredicateIRI(p), ObjectLiteral(o)))
+	}
+	case (␀, _) => None
       }
     }
 
-    def directN (s:Node, fk:ForeignKey, r:Relation, t:Tuple, nodes:NodeMap) : Triple = {
+    def referenceSemantics (s:Node, fk:ForeignKey, r:Relation, t:Tuple, nodes:NodeMap) : Triple = {
       val p = predicatemap (r.name, fk)
       val ls:List[LexicalValue] = t.notNullLexvalues(fk)
       val ForeignKey(as, Target(rel, key)) = fk
@@ -159,7 +160,7 @@
     }
 
     // TODO: aren't they already part of the RDF model?
-    def XSD (d:Datatype) : IRI =
+    def datatypeSemantics (d:Datatype) : IRI =
       d match {
         case Datatype.INTEGER => IRI("http://www.w3.org/2001/XMLSchema#integer")
         case Datatype.FLOAT => IRI("http://www.w3.org/2001/XMLSchema#float")
@@ -171,11 +172,6 @@
         case Datatype.STRING => IRI("http://www.w3.org/2001/XMLSchema#string")
       }
   
-    def literalmap (l:LexicalValue, d:Datatype) : Literal =
-      d match {
-        case Datatype.STRING => PlainLiteral(l.s, None)
-	case _ => TypedLiteral(l.s, XSD(d))
-      }
   
 
   }
--- a/rdb/src/main/scala/RDB.scala	Sat Feb 12 16:41:13 2011 -0500
+++ b/rdb/src/main/scala/RDB.scala	Sat Feb 12 17:33:48 2011 -0500
@@ -22,7 +22,7 @@
   case class Header (m:Map[AttrName, Datatype]) {
     def apply (a:AttrName) = m(a)
     def keySet = m.keySet.toSet
-    def sqlDatatype (a:AttrName) : Datatype = m(a)
+    def datatype (a:AttrName) : Datatype = m(a)
     def contains (a:AttrName) : Boolean = m contains a
   }
   object Header {
@@ -84,10 +84,12 @@
   //   def lexvaluesNoNulls (as:List[AttrName]) = as map { m(_).asInstanceOf[LexicalValue] }
   //   def nullAttributes (h:Header) : Set[AttrName] = h.keySet filter { m(_) == ␀ }
   // }
-  case class Tuple (m:Map[AttrName, CellValue]) {
+  case class Tuple (private val m:Map[AttrName, CellValue]) extends PartialFunction[AttrName, CellValue] {
     def apply (a:AttrName) = m(a)
+    def isDefinedAt(a:AttrName) = m isDefinedAt a
     // assumes that the AttrName does not correspond to null values
     // for example, it's ok to call it with PK attributes
+    // { as | forall a in as, a in Tuple }
     def notNullLexvalues (as:AttrList):List[LexicalValue] =
       as.attrs map {
         m(_) match {