+ many factoriings of cellMap
authorEric Prud'hommeaux <eric@w3.org>
Fri, 04 Jun 2010 08:53:09 -0400
changeset 11 37a1a69e954f
parent 10 76e6d541cd7d
child 12 c77e2043f54e
+ many factoriings of cellMap
src/main/scala/Main.scala
--- a/src/main/scala/Main.scala	Thu Jun 03 19:56:02 2010 -0400
+++ b/src/main/scala/Main.scala	Fri Jun 04 08:53:09 2010 -0400
@@ -92,9 +92,105 @@
     val h = header(r)
     val k = pk(h)
     val s = nodemap(u, rn, k, lexvalue(h, t, k).asInstanceOf[LexicalValue]) // Assume: no NULLs in primary key
-    h.keySet.flatMap(a => cellmap(u, rn, h, a, s, t))
+
+    scalartriples(u, rn, s, scalars(h, nonnulls(h, t))) ++
+    referencetriples(u, rn, s, references(h, nonnulls(h, t)))
+
+    // scalars(h, nonnulls(h, t)).map{case(a, l, dt) => scalarmap(u, rn, a, l, dt, s)} ++
+    // references(h, nonnulls(h, t)).map{case(a, l, fk) => referencemap(u, rn, a, l, fk, s)}
+
+    // nonnullscalars(h, t).map{case(a, l, dt) => scalarmap(u, rn, a, l, dt, s)} ++
+    // nonnullreferences(h, t).map{case(a, l, fk) => referencemap(u, rn, a, l, fk, s)}
+
+    // h.keySet.flatMap(a => cellmap(u, rn, h, a, s, t))
   }
 
+/* BEGIN different approaches { */
+
+  def scalartriples (u:StemIRI, rn:RelName, s:IRI, objs:Set[(AttrName, LexicalValue, SQLDatatype)]) : Set[Triple] = {
+    objs.map{
+      case(a, l, dt) => {
+	val p = predicatemap (u, rn, a)
+	val o = ObjectLiteral(literalmap(l, dt))
+	Triple(s, p, o)
+      }
+    }
+  }
+  def referencetriples (u:StemIRI, rn:RelName, s:IRI, objs:Set[(AttrName, LexicalValue, Fk)]) : Set[Triple] = {
+    objs.map{
+      case(a, l, fk) => {
+	val p = predicatemap (u, rn, a)
+	val Fk(rn2, a2) = fk
+	val o = ObjectIRI(nodemap(u, rn2, a2, l))
+	Triple(s, p, o)
+      }
+    }
+  }
+
+  def scalarmap (u:StemIRI, rn:RelName, a:AttrName, l:LexicalValue, dt:SQLDatatype, s:IRI) : Triple = {
+    val p = predicatemap (u, rn, a)
+    val o = ObjectLiteral(literalmap(l, dt))
+    Triple(s, p, o)
+  }
+  def referencemap (u:StemIRI, rn:RelName, a:AttrName, l:LexicalValue, fk:Fk, s:IRI) : Triple = {
+    val p = predicatemap (u, rn, a)
+    val Fk(rn2, a2) = fk
+    val o = ObjectIRI(nodemap(u, rn2, a2, l))
+    Triple(s, p, o)
+  }
+
+  def nonnulls (h:Header, t:Tuple) : Set[(AttrName, LexicalValue)] = {
+    h.keySet.flatMap(a =>
+      lexvalue(h, t, a) match {
+	case ☹() => None
+	case l:LexicalValue => Some((a, l))
+      })
+  }
+
+  def scalars (h:Header, s:Set[(AttrName, LexicalValue)]) : Set[(AttrName, LexicalValue, SQLDatatype)] = {
+    s.flatMap(elt =>
+      linktype(h, elt._1) match {
+	case Fk(rn2, a2) => None
+	case _ => Some((elt._1, elt._2, sqlDatatype(h, elt._1)))
+      })
+  }
+
+  def references (h:Header, s:Set[(AttrName, LexicalValue)]) : Set[(AttrName, LexicalValue, Fk)] = {
+    s.flatMap(elt =>
+      linktype(h, elt._1) match {
+  	case fk:Fk => Some((elt._1, elt._2, fk))
+	case _ => None
+      })
+  }
+
+  def nonnullscalars (h:Header, t:Tuple) : Set[(AttrName, LexicalValue, SQLDatatype)] = {
+    h.keySet.flatMap(a =>
+      lexvalue(h, t, a) match {
+	case ☹() => None
+	case l:LexicalValue => {
+	  linktype(h, a) match {
+  	    case Fk(rn2, a2) => None
+	    case _ => Some((a, l, sqlDatatype(h, a)))
+	  }
+	}
+      })
+  }
+
+  def nonnullreferences (h:Header, t:Tuple) : Set[(AttrName, LexicalValue, Fk)] = {
+    h.keySet.flatMap(a =>
+      lexvalue(h, t, a) match {
+      case ☹() => None
+      case l:LexicalValue => {
+	linktype(h, a) match {
+  	  case fk:Fk => Some((a, l, fk))
+	  case _ => None
+	}
+      }
+    })
+  }
+
+/* } END different approaches */
+
   def cellmap (u:StemIRI, rn:RelName, h:Header, a:AttrName, s:IRI, t:Tuple) : Option[Triple] = {
     lexvalue(h, t, a) match {
       case ☹() => None