changeset 25:f460e59ebcc2

~ lexvalue returns Option[LexicalValue]
author Eric Prud'hommeaux <eric@w3.org>
date Tue, 28 Sep 2010 09:04:23 -0400
parents 31cf43fcd94b
children 7429859b958b
files src/main/scala/Main.scala src/test/scala/Test.scala
diffstat 2 files changed, 69 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/src/main/scala/Main.scala	Mon Sep 27 17:46:02 2010 -0400
+++ b/src/main/scala/Main.scala	Tue Sep 28 09:04:23 2010 -0400
@@ -41,7 +41,12 @@
   def header (r:Relation) : Header = r.header
   def body (r:Relation) : Body = r.body
 
-  def lexvalue (h:Header, t:Tuple, a:AttrName) : CellValue = t(a)
+  def lexvalue (h:Header, t:Tuple, a:AttrName) : Option[LexicalValue] = 
+    t(a) match {
+      case ␀() => None
+      case v:LexicalValue => Some(v)
+    }
+
   def sqlDatatype (h:Header, a:AttrName) : SQLDatatype = h.types(a)
 
 }
@@ -176,8 +181,9 @@
     val h = header(r)
     val s:Node =
       if (r.pk.isDefined) {
-	val vs = r.pk.get.map(k => lexvalue(h, t, k).asInstanceOf[LexicalValue])
-	nodemap(u, rn, r.pk.get, vs) // Assume: no NULLs in primary key
+	// Assume: no NULLs in primary key
+	val vs = r.pk.get.map(k => lexvalue(h, t, k).get)
+	nodemap(u, rn, r.pk.get, vs)
       } else
 	freshbnode()
     (r.keys.map(k => {
@@ -195,7 +201,8 @@
       if (nodes.get(rn).isDefined) {
 	// Known to have at least one key, so take the first one.
 	val k = r.keys(0)
-	val vs = k.map(a => lexvalue(h, t, a).asInstanceOf[LexicalValue])
+	// Assume: no NULLs in candidate key
+	val vs = k.map(a => lexvalue(h, t, a).get)
 	nodes(rn)(k)(vs)
       } else
 	freshbnode()
@@ -243,7 +250,7 @@
   def nulls (h:Header, t:Tuple) : Set[(AttrName)] = {
     h.keySet.flatMap(a =>
       lexvalue(h, t, a) match {
-	case ␀() => Some(a)
+	case None => Some(a)
 	case _ => None
       })
   }
--- a/src/test/scala/Test.scala	Mon Sep 27 17:46:02 2010 -0400
+++ b/src/test/scala/Test.scala	Tue Sep 28 09:04:23 2010 -0400
@@ -255,6 +255,63 @@
   }
 
 
+  test("1 People 1 Addresses 1 Offices") {
+
+    val addresses = Relation(Header(Map("ID" -> SQLInt(),
+					"city" -> SQLString(),
+					"state" -> SQLString())),
+			     Set(Map("ID" -> LexicalValue("18"),
+				     "city" -> LexicalValue("Cambridge"),
+				     "state" -> LexicalValue("MA"))),
+			     List(List("ID")),
+			     Some(List("ID")),
+			     Map())
+
+    val people = Relation(Header(Map("ID" -> SQLInt(),
+				     "fname" -> SQLString(),
+				     "addr" -> SQLInt())),
+			  Set(Map("ID" -> LexicalValue("7"),
+				  "fname" -> LexicalValue("Bob"),
+				  "addr" -> LexicalValue("18"))),
+			  List(List("ID")),
+			  Some(List("ID")),
+			  Map(List("addr") -> Target("Addresses", List("ID"))))
+
+    val offices = Relation(Header(Map("ID" -> SQLInt(),
+				      "building" -> SQLInt(),
+				      "ofcNumber" -> SQLString())),
+			     Set(Map("ID" -> LexicalValue("18"),
+				     "building" -> LexicalValue("32"),
+				     "ofcNumber" -> LexicalValue("G528"))),
+			     List(List("ID")),
+			     Some(List("ID")),
+			     Map(List("ID") -> Target("Addresses", List("ID"))))
+
+    val db = Database(Map("Addresses" -> addresses, 
+			  "People" -> people, 
+			  "Offices" -> offices))
+    val g = databasemap(StemIRI("http://foo.example/DB"), db)
+
+    val expected:RDFGraph =
+      Set(
+	Triple(IRI("http://foo.example/DB/Addresses/ID.18#_"),IRI("http://foo.example/DB/Offices#ID"),TypedLiteral("8",IRI("http://www.w3.org/2001/XMLSchema#int"))),
+	Triple(IRI("http://foo.example/DB/Addresses/ID.18#_"),IRI("http://foo.example/DB/Offices#building"),TypedLiteral("32",IRI("http://www.w3.org/2001/XMLSchema#int"))),
+	Triple(IRI("http://foo.example/DB/Addresses/ID.18#_"),IRI("http://foo.example/DB/Offices#ofcNumber"),TypedLiteral("G528",IRI("http://www.w3.org/2001/XMLSchema#string"))), 
+
+	Triple(IRI("http://foo.example/DB/People/ID.7#_"),IRI("http://foo.example/DB/People#ID"),TypedLiteral("7",IRI("http://www.w3.org/2001/XMLSchema#int"))),
+	Triple(IRI("http://foo.example/DB/People/ID.7#_"),IRI("http://foo.example/DB/People#fname"),TypedLiteral("Bob",IRI("http://www.w3.org/2001/XMLSchema#string"))),
+	Triple(IRI("http://foo.example/DB/People/ID.7#_"),IRI("http://foo.example/DB/People#addr"),IRI("http://foo.example/DB/Addresses/ID.18#_")),
+
+	Triple(IRI("http://foo.example/DB/Addresses/ID.18#_"),IRI("http://foo.example/DB/Addresses#ID"),TypedLiteral("18",IRI("http://www.w3.org/2001/XMLSchema#int"))),
+	Triple(IRI("http://foo.example/DB/Addresses/ID.18#_"),IRI("http://foo.example/DB/Addresses#city"),TypedLiteral("Cambridge",IRI("http://www.w3.org/2001/XMLSchema#string"))),
+	Triple(IRI("http://foo.example/DB/Addresses/ID.18#_"),IRI("http://foo.example/DB/Addresses#state"),TypedLiteral("MA",IRI("http://www.w3.org/2001/XMLSchema#string")))
+      )
+    println("exp:\n  " + expected.mkString("\n  "))
+    println("got:\n  " + g.mkString("\n  "))
+    assert (expected === g)
+  }
+
+
   test("2 Employees") {
     val employees = Relation(Header(Map("ID" -> SQLInt(),
 					"fname" -> SQLString(),
Set up and maintained by W3C Systems Team, please report bugs to sysreq@w3.org.

W3C would like to thank Microsoft who donated the server that allows us to run this service.