~ sanitized a bit
authorEric Prud'hommeaux <eric@w3.org>
Sun, 26 Sep 2010 15:54:09 -0400
changeset 21 402e3f761b4d
parent 20 ae6390ee0fa4
child 22 06b795e0c753
~ sanitized a bit
+ test case with non-fk reference
src/main/scala/Main.scala
src/test/scala/Test.scala
--- a/src/main/scala/Main.scala	Sun Sep 26 12:43:33 2010 -0400
+++ b/src/main/scala/Main.scala	Sun Sep 26 15:54:09 2010 -0400
@@ -24,8 +24,14 @@
   type Tuple = Map[AttrName, CellValue]
 
   sealed abstract class SQLDatatype
+  case class SQLInt () extends SQLDatatype
+  case class SQLFloat () extends SQLDatatype
+  case class SQLDate () extends SQLDatatype
+  case class SQLTime () extends SQLDatatype
+  case class SQLTimestamp () extends SQLDatatype
+  case class SQLChar () extends SQLDatatype
+  case class SQLVarchar () extends SQLDatatype
   case class SQLString () extends SQLDatatype
-  case class SQLInt () extends SQLDatatype
 
   type RelName = String
   type AttrName = String
@@ -89,8 +95,9 @@
   }
 
   // Mapping functions:
-  def databasemap (u:StemIRI, db:Database) : RDFGraph =
+  def databasemap (u:StemIRI, db:Database) : RDFGraph = {
     db.m.flatMap{case(rn, r) => relationmap(u, rn, r)}.toSet
+  }
 
   def relationmap (u:StemIRI, rn:RelName, r:Relation) : RDFGraph =
     body(r).flatMap(t => tuplemap(u, rn, t, r))
@@ -103,22 +110,14 @@
     val allAttrs:Set[AttrName] = h.keySet
     val allFKs:Set[List[AttrName]] = h.fks.keySet
     val unaryFKs:Set[AttrName] = allFKs.flatMap(a => {
-      if (a.length == 1) {
-	a
-      } else {
-	None
-      }
+      if (a.length == 1) a else None
     })
     val nulllist = nulls(h, t)
     val nullFKs:Set[List[AttrName]] = allFKs.flatMap(a => {
       val int:Set[AttrName] = nulllist & a.toSet
-      if (int.toList.length == 0) {
-	None
-      } else {
-	List(a)
-      }
+      if (int.toList.length == 0) None else List(a)
     })
-    val scalarlist = allAttrs -- unaryFKs -- nulllist /* -- h.pk*/
+    val scalarlist = allAttrs -- unaryFKs -- nulllist /* -- h.pk */
     val referencelist = h.fks.keySet -- nullFKs
 
     scalarlist.map(a => scalartriples(u, rn, s, a, h, t)) ++
@@ -126,8 +125,6 @@
 
   }
 
-/* BEGIN different approaches { */
-
   def scalartriples (u:StemIRI, rn:RelName, s:IRI, a:AttrName, h:Header, t:Tuple) : Triple = {
     val p = predicatemap (u, rn, List(a))
     val l = t(a).asInstanceOf[LexicalValue]
@@ -160,8 +157,14 @@
 
   def XSD (d:SQLDatatype) : IRI =
     d match {
+      case SQLInt() => IRI("http://www.w3.org/2001/XMLSchema#int")
+      case SQLFloat() => IRI("http://www.w3.org/2001/XMLSchema#float")
+      case SQLDate() => IRI("http://www.w3.org/2001/XMLSchema#date")
+      case SQLTime() => IRI("http://www.w3.org/2001/XMLSchema#time")
+      case SQLTimestamp() => IRI("http://www.w3.org/2001/XMLSchema#timestamp")
+      case SQLChar() => IRI("http://www.w3.org/2001/XMLSchema#char")
+      case SQLVarchar() => IRI("http://www.w3.org/2001/XMLSchema#varchar")
       case SQLString() => IRI("http://www.w3.org/2001/XMLSchema#string")
-      case SQLInt() => IRI("http://www.w3.org/2001/XMLSchema#int")
     }
 
   def literalmap (l:LexicalValue, d:SQLDatatype) : TypedLiteral =
--- a/src/test/scala/Test.scala	Sun Sep 26 12:43:33 2010 -0400
+++ b/src/test/scala/Test.scala	Sun Sep 26 15:54:09 2010 -0400
@@ -7,6 +7,7 @@
 import org.scalatest.FunSuite
 
 class Test extends FunSuite {
+
   test("2 People 1 Addresses") {
 
     val addresses = Relation(Header(Map("ID" -> SQLInt(),
@@ -49,6 +50,76 @@
     assert (expected === g)
   }
 
+  test("2 People 1 Addresses 1 Department") {
+
+    val addresses = Relation(Header(Map("ID" -> SQLInt(),
+					"city" -> SQLString(),
+					"state" -> SQLString()),
+				    List("ID"),
+				    Map()),
+			     Set(Map("ID" -> LexicalValue("18"),
+				     "city" -> LexicalValue("Cambridge"),
+				     "state" -> LexicalValue("MA"))))
+
+    val people = Relation(Header(Map("ID" -> SQLInt(),
+				     "fname" -> SQLString(),
+				     "addr" -> SQLInt(),
+				     "deptName" -> SQLString(),
+				     "deptCity" -> SQLString()),
+				 List("ID"),
+				 Map(List("addr") -> Target("Addresses", List("ID")),
+				     List("deptName", "deptCity") -> Target("Department", List("name", "city")))),
+			  Set(Map("ID" -> LexicalValue("7"),
+				  "fname" -> LexicalValue("Bob"),
+				  "addr" -> LexicalValue("18"),
+				  "deptName" -> LexicalValue("accounting"),
+				  "deptCity" -> LexicalValue("Cambridge")),
+			      Map("ID" -> LexicalValue("8"),
+				  "fname" -> LexicalValue("Sue"),
+				  "addr" -> ␀(),
+				  "deptName" -> ␀(),
+				  "deptCity" -> ␀())))
+
+    val department = Relation(Header(Map("ID" -> SQLInt(),
+					"name" -> SQLString(),
+					"city" -> SQLString(),
+					"manager" -> SQLInt()),
+				    List("ID"),
+				    Map(List("manager") -> Target("People", List("ID")))),
+			     Set(Map("ID" -> LexicalValue("23"),
+				     "name" -> LexicalValue("accounting"),
+				     "city" -> LexicalValue("Cambridge"),
+				     "manager" -> LexicalValue("8"))))
+
+    val db = Database(Map("Addresses" -> addresses, 
+			  "People" -> people, 
+			  "Department" -> department))
+    val g = databasemap(StemIRI("http://foo.example/DB"), db)
+
+    val expected:RDFGraph =
+      Set(
+	Triple(IRI("http://foo.example/DB/People/ID.7#_"),IRI("http://foo.example/DB/People#deptName"),TypedLiteral("accounting",IRI("http://www.w3.org/2001/XMLSchema#string"))),
+	Triple(IRI("http://foo.example/DB/People/ID.7#_"),IRI("http://foo.example/DB/People#deptCity"),TypedLiteral("Cambridge",IRI("http://www.w3.org/2001/XMLSchema#string"))),
+	Triple(IRI("http://foo.example/DB/People/ID.7#_"),IRI("http://foo.example/DB/People#deptName_deptCity"),IRI("http://foo.example/DB/Department/ID.23#_")),
+
+	Triple(IRI("http://foo.example/DB/Department/ID.23#_"),IRI("http://foo.example/DB/Department#ID"),TypedLiteral("23",IRI("http://www.w3.org/2001/XMLSchema#int"))),
+	Triple(IRI("http://foo.example/DB/Department/ID.23#_"),IRI("http://foo.example/DB/Department#name"),TypedLiteral("accounting",IRI("http://www.w3.org/2001/XMLSchema#string"))),
+	Triple(IRI("http://foo.example/DB/Department/ID.23#_"),IRI("http://foo.example/DB/Department#city"),TypedLiteral("Cambridge",IRI("http://www.w3.org/2001/XMLSchema#string"))),
+	Triple(IRI("http://foo.example/DB/Department/ID.23#_"),IRI("http://foo.example/DB/Department#manager"),IRI("http://foo.example/DB/People/ID.8#_")),
+
+	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/People/ID.8#_"),IRI("http://foo.example/DB/People#ID"),TypedLiteral("8",IRI("http://www.w3.org/2001/XMLSchema#int"))),
+	Triple(IRI("http://foo.example/DB/People/ID.8#_"),IRI("http://foo.example/DB/People#fname"),TypedLiteral("Sue",IRI("http://www.w3.org/2001/XMLSchema#string"))), 
+
+	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")))
+      )
+    assert (expected === g)
+  }
+
   test("2 Employees") {
     val employees = Relation(Header(Map("ID" -> SQLInt(),
 					"fname" -> SQLString(),