--- 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))
- }
}