--- a/src/main/scala/RDB2RDFMain.scala Sun Jan 31 08:10:01 2010 -0500
+++ b/src/main/scala/RDB2RDFMain.scala Sun Jan 31 09:32:43 2010 -0500
@@ -74,6 +74,46 @@
case class RDFNoder(relation:sql.Relation, binding:FullOrPartialBinding) extends SQL2RDFValueMapper(binding)
case class RDFBNoder(relation:sql.Relation, binding:FullOrPartialBinding) extends SQL2RDFValueMapper(binding)
+ case class ObjUri(stem:Stem, rel:Rel, attr:Attr, v:CellValue)
+ case class Stem(s:String) {
+ override def toString = "" + s
+ }
+ case class Rel(s:String) { // !! NUKE
+ override def toString = "" + s
+ }
+ case class Attr(s:String) {
+ override def toString = "" + s
+ }
+ case class CellValue(s:String)
+ case class PUri(stem:Stem, rel:Rel, attr:Attr) {
+ override def toString = "<" + stem + "/" + rel + "#" + attr + ">"
+ }
+ /* stemURI + '/' + (\w+) + '#' (\w+) */
+ def parsePredicateURI(u:sparql.Uri):PUri = {
+ val x:String = u.s
+ val uri = new URI(x)
+ val path = uri.getPath().split("/").toList.filterNot(_ == "")
+ val subPath = path.slice(0, path.size - 1).mkString("/")
+ val stem = uri.getScheme() + "://" + uri.getAuthority + "/" + subPath
+ PUri(Stem(stem), Rel(path.last), Attr(uri.getFragment))
+ }
+
+ /* stemURI + '/' (\w+) '/' (\w+) '.' (\w+) '#record' */
+ def parseObjectURI(u:sparql.Uri):ObjUri = {
+ val x:String = u.s
+ val uri = new URI(x)
+ val path = uri.getPath().split("/").toList.filterNot(_ == "")
+ val subPath = path.slice(0, path.size - 2).mkString("/")
+ val rel = path(path.size - 2)
+ val attrPair = path(path.size-1).split("\\.")
+ val stem = uri.getScheme() + "://" + uri.getAuthority + "/" + subPath
+ assert("record" == uri.getFragment)
+ ObjUri(Stem(stem), Rel(rel), Attr(attrPair(0)), CellValue(attrPair(1)))
+ }
+/*
+Sparql.parseObjectURI(
+Sparql.parsePredicateURI(
+*/
def relAliasFromS(s:sparql.S):sql.RelAlias = {
s match {
case sparql.SUri(ob) => relAliasFromNode(ob)
@@ -89,8 +129,8 @@
}
}
- def relAliasFromNode(u:sparql.ObjUri):sql.RelAlias = {
- val sparql.ObjUri(stem, rel, sparql.Attr(a), sparql.CellValue(v)) = u
+ def relAliasFromNode(u:sparql.Uri):sql.RelAlias = {
+ val ObjUri(stem, rel, Attr(a), CellValue(v)) = parseObjectURI(u)
sql.RelAlias(sql.Name("R_" + a + v))
}
@@ -105,7 +145,7 @@
def attrAliasNameFromVar(v:sparql.Var):sql.Name = sql.Name("" + v.s)
- def uriConstraint(state:R2RState, constrainMe:sql.RelAliasAttribute, u:sparql.ObjUri, enforeForeignKeys:Boolean):R2RState = {
+ def uriConstraint(state:R2RState, constrainMe:sql.RelAliasAttribute, u:ObjUri, enforeForeignKeys:Boolean):R2RState = {
val relvar =
if (enforeForeignKeys)
sql.RelAliasAttribute(constrainMe.relalias, sql.Attribute(sql.Name(u.attr.s)))
@@ -197,7 +237,8 @@
val sparql.TriplePattern(s, p, o) = triple
p match {
case sparql.PVar(v) => error("variable predicates require tedious enumeration; too tedious for me.")
- case sparql.PUri(stem, spRel, spAttr) => {
+ case sparql.PUri(uri) => {
+ val PUri(stem, spRel, spAttr) = parsePredicateURI(uri)
/* Attributes that come from the predicate: */
val rel = sql.Relation(sql.Name(spRel.s))
val attr = sql.Attribute(sql.Name(spAttr.s))
@@ -206,7 +247,7 @@
/* Attributes that come from the subject: */
val objattr = sql.RelAliasAttribute(relalias, attr)
val state_postSubj = s match {
- case sparql.SUri(u) => uriConstraint(stateP, sql.RelAliasAttribute(relalias, db.relationdescs(rel).primarykey.get), u, true)
+ case sparql.SUri(u) => uriConstraint(stateP, sql.RelAliasAttribute(relalias, db.relationdescs(rel).primarykey.get), parseObjectURI(u), true)
case sparql.SVar(v) => varConstraint(stateP, relalias, db.relationdescs(rel).primarykey, v, db, rel)
}
val state_subjJoin = R2RState(state_postSubj.joins + sql.InnerJoin(sql.AliasedResource(rel,relalias), None), state_postSubj.varmap, state_postSubj.exprs)
@@ -244,7 +285,7 @@
}
o match {
case sparql.OLit(l) => literalConstraint(state_fkeys, targetattr, l, dt)
- case sparql.OUri(u) => uriConstraint (state_fkeys, targetattr, u, enforceForeignKeys)
+ case sparql.OUri(u) => uriConstraint (state_fkeys, targetattr, parseObjectURI(u), enforceForeignKeys)
case sparql.OVar(v) => varConstraint (state_fkeys, targetattr.relalias, Some(targetattr.attribute), v, db, targetrel)
}
}
@@ -365,7 +406,7 @@
case sparql.TermVar(v) => { // :sparql.Var
val l = varToAttribute(varmap, v)
val r = rTerm match {
- case sparql.TermUri(obj) => null // :sparql.ObjUri
+ case sparql.TermUri(obj) => null // :sparql.Uri
case sparql.TermVar(v) => { // :sparql.Var
sql.PrimaryExpressionAttr(varToAttribute(varmap, v))
}
--- a/src/test/scala/SparqlTest.scala Sun Jan 31 08:10:01 2010 -0500
+++ b/src/test/scala/SparqlTest.scala Sun Jan 31 09:32:43 2010 -0500
@@ -11,7 +11,7 @@
val e = """
?emp <http://hr.example/DB/Employee#lastName> "bob"^^<http://www.w3.org/2001/XMLSchema#string>
"""
- val expected = TriplesBlock(List(TriplePattern(SVar(Var("emp")),PUri(Stem("http://hr.example/DB"),Rel("Employee"),Attr("lastName")),OLit(Literal(RDFLiteral("bob",Datatype(new URI("http://www.w3.org/2001/XMLSchema#string"))))))))
+ val expected = TriplesBlock(List(TriplePattern(SVar(Var("emp")),PUri(Uri("http://hr.example/DB/Employee#lastName")),OLit(Literal(RDFLiteral("bob",Datatype(new URI("http://www.w3.org/2001/XMLSchema#string"))))))))
assert(expected === (a.parseAll(a.triplesblock, e).get))
}
@@ -20,7 +20,7 @@
val e = """
?emp <http://hr.example/DB/Employee#age> "21"^^<http://www.w3.org/2001/XMLSchema#integer>
"""
- val expected = TriplesBlock(List(TriplePattern(SVar(Var("emp")),PUri(Stem("http://hr.example/DB"),Rel("Employee"),Attr("age")),OLit(Literal(RDFLiteral("21",Datatype(new URI("http://www.w3.org/2001/XMLSchema#integer"))))))))
+ val expected = TriplesBlock(List(TriplePattern(SVar(Var("emp")),PUri(Uri("http://hr.example/DB/Employee#age")),OLit(Literal(RDFLiteral("21",Datatype(new URI("http://www.w3.org/2001/XMLSchema#integer"))))))))
assert(expected === (a.parseAll(a.triplesblock, e).get))
}
@@ -36,15 +36,15 @@
List(
TriplePattern(
SVar(Var("emp")),
- PUri(Stem("http://hr.example/DB"),Rel("Employee"),Attr("lastName")),
+ PUri(Uri("http://hr.example/DB/Employee#lastName")),
OVar(Var("empName"))),
TriplePattern(
SVar(Var("emp")),
- PUri(Stem("http://hr.example/DB"),Rel("Employee"),Attr("manager")),
+ PUri(Uri("http://hr.example/DB/Employee#manager")),
OVar(Var("manager"))),
TriplePattern(
SVar(Var("manager")),
- PUri(Stem("http://hr.example/DB"),Rel("Employee"),Attr("lastName")),
+ PUri(Uri("http://hr.example/DB/Employee#lastName")),
OVar(Var("managName")))))
assert(tps === a.parseAll(a.triplesblock, e).get)
}
@@ -105,7 +105,7 @@
List(
TriplePattern(
SVar(Var("emp")),
- PUri(Stem("http://hr.example/DB"),Rel("Employee"),Attr("lastName")),
+ PUri(Uri("http://hr.example/DB/Employee#lastName")),
OVar(Var("empName"))))))
assert(tps === a.parseAll(a.select, e).get)
}
@@ -126,7 +126,7 @@
List(
TriplePattern(
SVar(Var("emp")),
- PUri(Stem("http://hr.example/DB"),Rel("Employee"),Attr("lastName")),
+ PUri(Uri("http://hr.example/DB/Employee#lastName")),
OVar(Var("empName"))))))
assert(tps === a.parseAll(a.select, e).get)
}
@@ -147,7 +147,7 @@
List(
TriplePattern(
SVar(Var("emp")),
- PUri(Stem("http://hr.example/DB"),Rel("Employee"),Attr("lastName")),
+ PUri(Uri("http://hr.example/DB/Employee#lastName")),
OVar(Var("empName"))))),
Expression(List(
PrimaryExpressionLt(SparqlTermExpression(TermVar(Var("manBday"))),
@@ -173,15 +173,15 @@
List(
TriplePattern(
SVar(Var("emp")),
- PUri(Stem("http://hr.example/DB"),Rel("Employee"),Attr("lastName")),
+ PUri(Uri("http://hr.example/DB/Employee#lastName")),
OVar(Var("empName"))),
TriplePattern(
SVar(Var("emp")),
- PUri(Stem("http://hr.example/DB"),Rel("Employee"),Attr("manager")),
+ PUri(Uri("http://hr.example/DB/Employee#manager")),
OVar(Var("manager"))),
TriplePattern(
SVar(Var("manager")),
- PUri(Stem("http://hr.example/DB"),Rel("Employee"),Attr("lastName")),
+ PUri(Uri("http://hr.example/DB/Employee#lastName")),
OVar(Var("managName"))))))
assert(tps === a.parseAll(a.select, e).get)
}
@@ -219,7 +219,7 @@
List(
TriplePattern(
SVar(Var("x")),
- PUri(Stem("http://hr.example/DB"),Rel("Employee"),Attr("manager")),
+ PUri(Uri("http://hr.example/DB/Employee#manager")),
OVar(Var("y"))))))
assert(tps === a.parseAll(a.select, e).get)
}
@@ -237,13 +237,13 @@
List(
TriplePattern(
SVar(Var("x")),
- PUri(Stem("http://hr.example/DB"),Rel("Employee"),Attr("manager")),
+ PUri(Uri("http://hr.example/DB/Employee#manager")),
OVar(Var("y"))))),
TriplesBlock(
List(
TriplePattern(
SVar(Var("x")),
- PUri(Stem("http://hr.example/DB"),Rel("Employee"),Attr("manager")),
+ PUri(Uri("http://hr.example/DB/Employee#manager")),
OVar(Var("y"))))))))
assert(tps === a.parseAll(a.select, e).get)
}
@@ -261,13 +261,13 @@
List(
TriplePattern(
SVar(Var("x")),
- PUri(Stem("http://hr.example/DB"),Rel("Employee"),Attr("manager")),
+ PUri(Uri("http://hr.example/DB/Employee#manager")),
OVar(Var("y"))))),
TriplesBlock(
List(
TriplePattern(
SVar(Var("x")),
- PUri(Stem("http://hr.example/DB"),Rel("Employee"),Attr("manager")),
+ PUri(Uri("http://hr.example/DB/Employee#manager")),
OVar(Var("y"))))))))
assert(tps === a.parseAll(a.select, e).get)
}
@@ -285,14 +285,14 @@
List(
TriplePattern(
SVar(Var("x")),
- PUri(Stem("http://hr.example/DB"),Rel("Employee"),Attr("manager")),
+ PUri(Uri("http://hr.example/DB/Employee#manager")),
OVar(Var("y"))))),
OptionalGraphPattern(
TriplesBlock(
List(
TriplePattern(
SVar(Var("x")),
- PUri(Stem("http://hr.example/DB"),Rel("Employee"),Attr("manager")),
+ PUri(Uri("http://hr.example/DB/Employee#manager")),
OVar(Var("y")))))))))
assert(tps === a.parseAll(a.select, e).get)
}
@@ -310,7 +310,7 @@
List(
TriplePattern(
SVar(Var("x")),
- PUri(Stem("http://hr.example/DB"),Rel("Employee"),Attr("manager")),
+ PUri(Uri("http://hr.example/DB/Employee#manager")),
OVar(Var("y")))))))
assert(tps === a.parseAll(a.select, e).get)
}
@@ -328,14 +328,14 @@
List(
TriplePattern(
SVar(Var("x")),
- PUri(Stem("http://hr.example/DB"),Rel("Employee"),Attr("manager")),
+ PUri(Uri("http://hr.example/DB/Employee#manager")),
OVar(Var("y"))))),
MinusGraphPattern(
TriplesBlock(
List(
TriplePattern(
SVar(Var("x")),
- PUri(Stem("http://hr.example/DB"),Rel("Employee"),Attr("manager")),
+ PUri(Uri("http://hr.example/DB/Employee#manager")),
OVar(Var("y")))))))))
assert(tps === a.parseAll(a.select, e).get)
}
@@ -361,37 +361,37 @@
List(
TriplePattern(
SVar(Var("who")),
- PUri(Stem("http://hr.example/DB"),Rel("Employee"),Attr("lastName")),
+ PUri(Uri("http://hr.example/DB/Employee#lastName")),
OLit(Literal(RDFLiteral("Smith",Datatype(new URI("http://www.w3.org/2001/XMLSchema#string")))))))),
TableDisjunction(List(
TriplesBlock(
List(
TriplePattern(
SVar(Var("above")),
- PUri(Stem("http://hr.example/DB"),Rel("Manage"),Attr("manages")),
+ PUri(Uri("http://hr.example/DB/Manage#manages")),
OVar(Var("who"))),
TriplePattern(
SVar(Var("above")),
- PUri(Stem("http://hr.example/DB"),Rel("Manage"),Attr("manager")),
+ PUri(Uri("http://hr.example/DB/Manage#manager")),
OVar(Var("manager"))),
TriplePattern(
SVar(Var("manager")),
- PUri(Stem("http://hr.example/DB"),Rel("Employee"),Attr("lastName")),
+ PUri(Uri("http://hr.example/DB/Employee#lastName")),
OVar(Var("name")))
)),
TriplesBlock(
List(
TriplePattern(
SVar(Var("below")),
- PUri(Stem("http://hr.example/DB"),Rel("Manage"),Attr("manager")),
+ PUri(Uri("http://hr.example/DB/Manage#manager")),
OVar(Var("who"))),
TriplePattern(
SVar(Var("below")),
- PUri(Stem("http://hr.example/DB"),Rel("Manage"),Attr("manages")),
+ PUri(Uri("http://hr.example/DB/Manage#manages")),
OVar(Var("managed"))),
TriplePattern(
SVar(Var("managed")),
- PUri(Stem("http://hr.example/DB"),Rel("Employee"),Attr("lastName")),
+ PUri(Uri("http://hr.example/DB/Employee#lastName")),
OVar(Var("name")))
)))))))
assert(tps === a.parseAll(a.select, e).get)
@@ -412,32 +412,15 @@
"""
val tps =
Select(SparqlAttributeList(List(Var("emp1Name"), Var("emp2Name"), Var("emp3Name"))),
- TableFilter(TableConjunction(List(TriplesBlock(List(TriplePattern(SVar(Var("emp1")), PUri(Stem("http://hr.example/DB"),Rel("Employee"),Attr("lastName")), OVar(Var("emp1Name"))))),
- OptionalGraphPattern(TriplesBlock(List(TriplePattern(SVar(Var("emp1")), PUri(Stem("http://hr.example/DB"),Rel("Employee"),Attr("birthday")), OVar(Var("birthday")))))),
- TriplesBlock(List(TriplePattern(SVar(Var("emp2")), PUri(Stem("http://hr.example/DB"),Rel("Employee"),Attr("lastName")), OVar(Var("emp2Name"))))),
- OptionalGraphPattern(TriplesBlock(List(TriplePattern(SVar(Var("emp2")), PUri(Stem("http://hr.example/DB"),Rel("Employee"),Attr("birthday")), OVar(Var("birthday")))))),
- TriplesBlock(List(TriplePattern(SVar(Var("emp4")),PUri(Stem("http://hr.example/DB"),Rel("Employee"),Attr("birthday")),OVar(Var("birthday"))))))),
+ TableFilter(TableConjunction(List(TriplesBlock(List(TriplePattern(SVar(Var("emp1")), PUri(Uri("http://hr.example/DB/Employee#lastName")), OVar(Var("emp1Name"))))),
+ OptionalGraphPattern(TriplesBlock(List(TriplePattern(SVar(Var("emp1")), PUri(Uri("http://hr.example/DB/Employee#birthday")), OVar(Var("birthday")))))),
+ TriplesBlock(List(TriplePattern(SVar(Var("emp2")), PUri(Uri("http://hr.example/DB/Employee#lastName")), OVar(Var("emp2Name"))))),
+ OptionalGraphPattern(TriplesBlock(List(TriplePattern(SVar(Var("emp2")), PUri(Uri("http://hr.example/DB/Employee#birthday")), OVar(Var("birthday")))))),
+ TriplesBlock(List(TriplePattern(SVar(Var("emp4")),PUri(Uri("http://hr.example/DB/Employee#birthday")),OVar(Var("birthday"))))))),
Expression(List(PrimaryExpressionLt(SparqlTermExpression(TermVar(Var("emp1Name"))),SparqlTermExpression(TermVar(Var("emp2Name")))), PrimaryExpressionLt(SparqlTermExpression(TermVar(Var("emp2Name"))),SparqlTermExpression(TermVar(Var("emp3Name")))), PrimaryExpressionLt(SparqlTermExpression(TermVar(Var("emp3Name"))),SparqlTermExpression(TermVar(Var("emp4Name"))))))))
assert(tps === a.parseAll(a.select, e).get)
}
- test("decompose a predicate uri in stem, rel and attr") {
- val uri = "http://hr.example/our/favorite/DB/Employee#lastName"
- val puri:PUri = Sparql.parsePredicateURI(uri)
- assert(puri === PUri(Stem("http://hr.example/our/favorite/DB"),
- Rel("Employee"),
- Attr("lastName")))
- }
-
- test("decompose a object uri in stem, rel and attr") {
- val uri = "http://hr.example/our/favorite/DB/Employee/id.18#record"
- val objuri:ObjUri = Sparql.parseObjectURI(uri)
- assert(objuri === ObjUri(Stem("http://hr.example/our/favorite/DB"),
- Rel("Employee"),
- Attr("id"),
- CellValue("18")))
- }
-
test("CONSTRUCT") {
val a = Sparql()
val e = """
@@ -451,13 +434,13 @@
List(
TriplePattern(
SVar(Var("emp")),
- PUri(Stem("http://foo.example/"),Rel("person"),Attr("lname")),
+ PUri(Uri("http://foo.example/person#lname")),
OVar(Var("empName"))))),
TriplesBlock(
List(
TriplePattern(
SVar(Var("emp")),
- PUri(Stem("http://hr.example/DB"),Rel("Employee"),Attr("lastName")),
+ PUri(Uri("http://hr.example/DB/Employee#lastName")),
OVar(Var("empName"))))))
assert(tps === a.parseAll(a.construct, e).get)
}