--- a/src/main/scala/Main.scala Fri Apr 02 19:47:48 2010 -0400
+++ b/src/main/scala/Main.scala Fri Apr 02 20:04:36 2010 -0400
@@ -8,8 +8,10 @@
case class Relation ( name:RelName, header:Header, body:Body )
type Header = Map[AttrName, (LinkType, SQLDatatype)]
type Body = Set[Tuple]
- type Tuple = Map[AttrName, Either[LexicalValue, ☹]]
- case class ☹ () // SQL NULL value
+ abstract class CellValue
+ case class LexicalValue (s:String) extends CellValue
+ case class ☹ () extends CellValue
+ type Tuple = Map[AttrName, CellValue]
sealed abstract class LinkType
case class Pk () extends LinkType
case class Fk(r:Relation, a:AttrName) extends LinkType
@@ -19,7 +21,6 @@
case class SQLInt () extends SQLDatatype
type RelName = String
type AttrName = String
- type LexicalValue = String
// RDF node types:
type RDFGraph = Set[RDFTriple]
@@ -36,7 +37,7 @@
def header (r:Relation) : Header = r.header
def body (r:Relation) : Body = r.body
- def lexvalue (h:Header, t:Tuple, a:AttrName) : Either[LexicalValue, ☹]
+ def lexvalue (h:Header, t:Tuple, a:AttrName) : CellValue
= t(a)
def sqlDatatype (h:Header, a:AttrName) : SQLDatatype = h(a)._2
def linktype (h:Header, a:AttrName) : LinkType = h(a)._1
@@ -49,15 +50,15 @@
def tuplemap (u:StemURI, t:Tuple, r:Relation) : Set[RDFTriple] = {
val h = header(r)
val k = pk(h)
- val s = nodemap(u, r, k, lexvalue(h, t, k).left.get) // Assume: no NULLs in primary key
+ val s = nodemap(u, r, k, lexvalue(h, t, k).asInstanceOf[LexicalValue]) // Assume: no NULLs in primary key
h.keysIterator.toList.flatMap(a => cellmap(u, r, a, s, t)).toSet
}
def cellmap (u:StemURI, r:Relation, a:AttrName, s:IRI, t:Tuple) : Option[RDFTriple] = {
val h = header(r)
lexvalue(h, t, a) match {
- case Right(☹()) => None
- case Left(l:LexicalValue) => {
+ case (☹()) => None
+ case l:LexicalValue => {
val p = predicatemap (u, r, a)
val o = linktype(h, a) match {
case Fk(r2, a2) => IRIObject(nodemap(u, r2, a2, l))
@@ -69,7 +70,7 @@
}
def nodemap (u:StemURI, r:Relation, a:AttrName, l:LexicalValue) : IRI
- = "<" + u + "/" + relname(r) + "/" + a + "." + l + "#foo>"
+ = "<" + u + "/" + relname(r) + "/" + a + "." + l.s + "#foo>"
def predicatemap (u:StemURI, r:Relation, a:AttrName) : IRI
= "<" + u + "/" + relname(r) + "#" + a + ">"
def XSD (d:SQLDatatype) : IRI =
@@ -78,7 +79,7 @@
case SQLInt() => "<http://www.w3.org/2001/XMLSchema#int>"
}
def literalmap (l:LexicalValue, d:SQLDatatype) : RDFLiteral
- = "\"" + l + "\"^^" + XSD(d)
+ = "\"" + l.s + "\"^^" + XSD(d)
// Utilities:
def toSet[T](list: List[T]) = {
--- a/src/test/scala/Test.scala Fri Apr 02 19:47:48 2010 -0400
+++ b/src/test/scala/Test.scala Fri Apr 02 20:04:36 2010 -0400
@@ -8,20 +8,20 @@
Map("ID" -> (Pk(), SQLInt()),
"city" -> (NotLinked(), SQLString()),
"state" -> (NotLinked(), SQLString())),
- Set(Map("ID" -> Left("18"),
- "city" -> Left("Cambridge"),
- "state" -> Left("MA"))))
+ Set(Map("ID" -> LexicalValue("18"),
+ "city" -> LexicalValue("Cambridge"),
+ "state" -> LexicalValue("MA"))))
val people = Relation("People",
Map("ID" -> (Pk(), SQLInt()),
"fname" -> (NotLinked(), SQLString()),
"addr" -> (Fk(addresses, "ID"), SQLInt())),
- Set(Map("ID" -> Left("7"),
- "fname" -> Left("Bob"),
- "addr" -> Left("18")),
- Map("ID" -> Left("8"),
- "fname" -> Left("Sue"),
- "addr" -> Right(☹()))))
+ Set(Map("ID" -> LexicalValue("7"),
+ "fname" -> LexicalValue("Bob"),
+ "addr" -> LexicalValue("18")),
+ Map("ID" -> LexicalValue("8"),
+ "fname" -> LexicalValue("Sue"),
+ "addr" -> ☹())))
val u:StemURI = "http://foo.example/DB"
val expected:RDFGraph =
Set(
@@ -35,7 +35,7 @@
RDFTriple("<http://foo.example/DB/Addresses/ID.18#foo>","<http://foo.example/DB/Addresses#city>",LiteralObject(""""Cambridge"^^<http://www.w3.org/2001/XMLSchema#string>""")),
RDFTriple("<http://foo.example/DB/Addresses/ID.18#foo>","<http://foo.example/DB/Addresses#state>",LiteralObject(""""MA"^^<http://www.w3.org/2001/XMLSchema#string>""")))
- assert (expected == relationmap(u, people) ++ relationmap(u, addresses))
+ assert (expected === relationmap(u, people) ++ relationmap(u, addresses))
}
}