--- a/src/main/scala/RDB2RDFMain.scala Mon Dec 14 12:46:17 2009 -0500
+++ b/src/main/scala/RDB2RDFMain.scala Mon Dec 14 13:10:16 2009 -0500
@@ -19,10 +19,10 @@
case class PrimaryKey(attr:Attribute)
sealed abstract class Binding
-case class RDFNode(fqattr:FQAttribute) extends Binding
-case class Str(fqattr:FQAttribute) extends Binding
-case class Int(fqattr:FQAttribute) extends Binding
-case class Enum(fqattr:FQAttribute) extends Binding
+case class RDFNode(fqattr:AliasAttribute) extends Binding
+case class Str(fqattr:AliasAttribute) extends Binding
+case class Int(fqattr:AliasAttribute) extends Binding
+case class Enum(fqattr:AliasAttribute) extends Binding
// case class NodeMap()
// case class PredicateMap()
@@ -64,16 +64,16 @@
*
* */
object RDB2RDF {
- case class R2RState(project:AttributeList, joins:List[Join], exprs:Expression, varmap:Map[Var, FQAttribute])
+ case class R2RState(project:AttributeList, joins:List[Join], exprs:Expression, varmap:Map[Var, AliasAttribute])
- def AliasFromS(s:S):Relation = {
+ def AliasFromS(s:S):Alias = {
s match {
case SUri(ob) => AliasFromNode(ob)
case SVar(v) => AliasFromVar(v)
}
}
- def AliasFromO(o:O):Option[Relation] = {
+ def AliasFromO(o:O):Option[Alias] = {
o match {
case OUri(ob) => Some(AliasFromNode(ob))
case OVar(v) => Some(AliasFromVar(v))
@@ -81,20 +81,20 @@
}
}
- def AliasFromNode(u:ObjUri):Relation = {
+ def AliasFromNode(u:ObjUri):Alias = {
val ObjUri(stem, rel, Attr(a), CellValue(v)) = u
- Relation(Name(a + v))
+ Alias(Name(a + v))
}
- def AliasFromVar(vr:Var):Relation = {
+ def AliasFromVar(vr:Var):Alias = {
val Var(v) = vr
- Relation(Name("_" + v))
+ Alias(Name("_" + v))
}
def URIconstraint(u:ObjUri, pk:PrimaryKey) = {
val alias = AliasFromNode(u)
val ObjUri(stem, rel, attr, value) = u
- val fqattr = FQAttribute(alias, pk.attr)
+ val fqattr = AliasAttribute(alias, pk.attr)
println("equiv+= " + toString(fqattr) + "=" + value)
}
@@ -118,7 +118,7 @@
* type String -> RDFStringConstructor // adds ^^xsd:string
* type primary key -> RDFNodeConstructor // prefixes with stemURL + relation + attribute and adds #record
* */
- def varConstraint(v:Var, db:DatabaseDesc, rel:Relation, alias:Relation, attr:Attribute) = {
+ def varConstraint(v:Var, db:DatabaseDesc, rel:Relation, alias:Alias, attr:Attribute) = {
/* e.g. Employee _emp id
** Employee _emp lastName
** Employee _emp manager
@@ -139,13 +139,16 @@
null
}
- def LiteralConstraint(lit:SparqlLiteral, attr:FQAttribute) = {
+ def LiteralConstraint(lit:SparqlLiteral, attr:AliasAttribute) = {
println("equiv+= " + toString(attr) + "=" + lit)
}
- def toString(fqattr:FQAttribute) : String = {
- fqattr.relation.n.s + "." + fqattr.attribute.n.s
+ def toString(fqattr:AliasAttribute) : String = {
+ fqattr.alias.n.s + "." + fqattr.attribute.n.s
}
+ // def toString(fqattr:RelAttribute) : String = {
+ // "[" + fqattr.relation.n.s + "]" + fqattr.attribute.n.s
+ // }
def acc(db:DatabaseDesc, state:R2RState, triple:TriplePattern, pk:PrimaryKey):R2RState = {
val R2RState(project, joins, exprs, varmap) = state
@@ -161,20 +164,19 @@
case SVar(v) => varConstraint(v, db, rel, alias, pk.attr)
null
}
- val objattr = FQAttribute(alias, attr)
+ val objattr = AliasAttribute(alias, attr)
val target = db.relationdescs(rel).attributes(attr) match {
case ForeignKey(fkrel, fkattr) => {
- val fqattr = FQAttribute(fkrel, fkattr)
o match {
case OUri(u) => {
val oAlias = AliasFromNode(u)
- println(toString(objattr) + "->" + toString(FQAttribute(oAlias, fqattr.attribute)))
+ println(toString(objattr) + "->" + toString(AliasAttribute(oAlias, fkattr)))
URIconstraint(u, pk)
}
case OVar(v) => {
val oAlias = AliasFromVar(v)
- println(toString(objattr) + "->" + toString(FQAttribute(oAlias, fqattr.attribute)))
- varConstraint(v, db, fqattr.relation, oAlias, fqattr.attribute)
+ println(toString(objattr) + "->" + toString(AliasAttribute(oAlias, fkattr)))
+ varConstraint(v, db, fkrel, oAlias, fkattr)
}
case OLit(l) => LiteralConstraint(l, objattr)
}
@@ -199,23 +201,23 @@
var r2rState = R2RState(
// AttributeList(List()),
AttributeList(List(
- NamedAttribute(FQAttribute(Relation(Name("emp")),Attribute(Name("lastName"))),Attribute(Name("empName"))),
- NamedAttribute(FQAttribute(Relation(Name("manager")),Attribute(Name("lastName"))),Attribute(Name("managName"))))),
+ NamedAttribute(AliasAttribute(Alias(Name("emp")),Attribute(Name("lastName"))),Attribute(Name("empName"))),
+ NamedAttribute(AliasAttribute(Alias(Name("manager")),Attribute(Name("lastName"))),Attribute(Name("managName"))))),
// List[Join](),
List(
- Join(RelAsAlias(Relation(Name("Employee")),Relation(Name("emp"))),None),
- Join(RelAsAlias(Relation(Name("Employee")),Relation(Name("manager"))),
+ Join(RelAsAlias(Relation(Name("Employee")),Alias(Name("emp"))),None),
+ Join(RelAsAlias(Relation(Name("Employee")),Alias(Name("manager"))),
Some(Expression(List(
- PrimaryExpressionEq(FQAttribute(Relation(Name("manager")),Attribute(Name("id"))),
- RValueAttr(FQAttribute(Relation(Name("emp")),Attribute(Name("manager"))))))
+ PrimaryExpressionEq(AliasAttribute(Alias(Name("manager")),Attribute(Name("id"))),
+ RValueAttr(AliasAttribute(Alias(Name("emp")),Attribute(Name("manager"))))))
)))
),
// Expression(List()),
Expression(List(
- PrimaryExpressionNotNull(FQAttribute(Relation(Name("emp")),Attribute(Name("lastName")))),
- PrimaryExpressionNotNull(FQAttribute(Relation(Name("manager")),Attribute(Name("lastName"))))
+ PrimaryExpressionNotNull(AliasAttribute(Alias(Name("emp")),Attribute(Name("lastName")))),
+ PrimaryExpressionNotNull(AliasAttribute(Alias(Name("manager")),Attribute(Name("lastName"))))
)),
- Map[Var, FQAttribute]()
+ Map[Var, AliasAttribute]()
)
triples.triplepatterns.foreach(s => r2rState = acc(db, r2rState, s, pk))
--- a/src/main/scala/SQL.scala Mon Dec 14 12:46:17 2009 -0500
+++ b/src/main/scala/SQL.scala Mon Dec 14 13:10:16 2009 -0500
@@ -7,20 +7,22 @@
case class Select(attributelist:AttributeList, tablelist:TableList, expression:Option[Expression])
case class AttributeList(attributes:List[NamedAttribute])
-case class NamedAttribute(fqattribute:FQAttribute, attribute:Attribute)
-case class FQAttribute(relation:Relation, attribute:Attribute)
+case class NamedAttribute(fqattribute:AliasAttribute, attribute:Attribute)
+//case class RelAttribute(relation:Relation, attribute:Attribute)
+case class AliasAttribute(alias:Alias, attribute:Attribute)
case class Attribute(n:Name)
case class Relation(n:Name)
+case class Alias(n:Name)
case class TableList(joins:List[Join])
case class Join(relasalias:RelAsAlias, expression:Option[Expression])
-case class RelAsAlias(rel:Relation, as:Relation)
+case class RelAsAlias(rel:Relation, as:Alias)
case class Expression(conjuncts:List[PrimaryExpression])
sealed abstract class PrimaryExpression
-case class PrimaryExpressionEq(l:FQAttribute, r:RValue) extends PrimaryExpression
-case class PrimaryExpressionLt(l:FQAttribute, r:RValue) extends PrimaryExpression
-case class PrimaryExpressionNotNull(l:FQAttribute) extends PrimaryExpression
+case class PrimaryExpressionEq(l:AliasAttribute, r:RValue) extends PrimaryExpression
+case class PrimaryExpressionLt(l:AliasAttribute, r:RValue) extends PrimaryExpression
+case class PrimaryExpressionNotNull(l:AliasAttribute) extends PrimaryExpression
sealed abstract class RValue
-case class RValueAttr(fqattribute:FQAttribute) extends RValue
+case class RValueAttr(fqattribute:AliasAttribute) extends RValue
case class RValueTyped(datatype:SQLDatatype, i:Name) extends RValue
case class Name(s:String)
@@ -60,9 +62,9 @@
{ case fqattribute ~ "AS" ~ attribute =>
NamedAttribute(fqattribute, attribute) }
- def fqattribute:Parser[FQAttribute] =
- relation ~ "." ~ attribute ^^
- { case relation ~ "." ~ attribute => FQAttribute(relation, attribute) }
+ def fqattribute:Parser[AliasAttribute] =
+ alias ~ "." ~ attribute ^^
+ { case alias ~ "." ~ attribute => AliasAttribute(alias, attribute) }
def attribute:Parser[Attribute] =
"""[a-zA-Z_]\w*""".r ^^ { x => Attribute(Name(x)) }
@@ -70,6 +72,9 @@
def relation:Parser[Relation] =
"""[a-zA-Z_]\w*""".r ^^ { x => Relation(Name(x)) }
+ def alias:Parser[Alias] =
+ """[a-zA-Z_]\w*""".r ^^ { x => Alias(Name(x)) }
+
def tablelist:Parser[TableList] =
repsep(join, "INNER" ~ "JOIN") ^^ { TableList(_) }
@@ -81,7 +86,7 @@
"ON" ~ expression ^^ { case "ON"~expression => expression }
def relasalias:Parser[RelAsAlias] =
- relation ~ "AS" ~ relation ^^
+ relation ~ "AS" ~ alias ^^
{ case rel1 ~ "AS" ~ rel2 => RelAsAlias(rel1, rel2) }
def expression:Parser[Expression] =
--- a/src/test/scala/SQLTest.scala Mon Dec 14 12:46:17 2009 -0500
+++ b/src/test/scala/SQLTest.scala Mon Dec 14 13:10:16 2009 -0500
@@ -13,7 +13,22 @@
INNER JOIN Employee AS manager ON manager.id=emp.manager
WHERE emp.lastName IS NOT NULL AND manager.lastName IS NOT NULL
"""
- val expected = Select(AttributeList(List(NamedAttribute(FQAttribute(Relation(Name("emp")),Attribute(Name("lastName"))),Attribute(Name("empName"))), NamedAttribute(FQAttribute(Relation(Name("manager")),Attribute(Name("lastName"))),Attribute(Name("managName"))))),TableList(List(Join(RelAsAlias(Relation(Name("Employee")),Relation(Name("emp"))),None),Join(RelAsAlias(Relation(Name("Employee")),Relation(Name("manager"))),Some(Expression(List(PrimaryExpressionEq(FQAttribute(Relation(Name("manager")),Attribute(Name("id"))),RValueAttr(FQAttribute(Relation(Name("emp")),Attribute(Name("manager"))))))))))),Some(Expression(List(PrimaryExpressionNotNull(FQAttribute(Relation(Name("emp")),Attribute(Name("lastName")))), PrimaryExpressionNotNull(FQAttribute(Relation(Name("manager")),Attribute(Name("lastName"))))))))
+ val expected = Select(AttributeList(List(NamedAttribute(AliasAttribute(Alias(Name("emp")),
+ Attribute(Name("lastName"))),
+ Attribute(Name("empName"))),
+ NamedAttribute(AliasAttribute(Alias(Name("manager")),
+ Attribute(Name("lastName"))),
+ Attribute(Name("managName"))))),
+ TableList(List(Join(RelAsAlias(Relation(Name("Employee")),Alias(Name("emp"))),None),
+ Join(RelAsAlias(Relation(Name("Employee")),Alias(Name("manager"))),
+ Some(Expression(List(PrimaryExpressionEq(AliasAttribute(Alias(Name("manager")),
+ Attribute(Name("id"))),
+ RValueAttr(AliasAttribute(Alias(Name("emp")),
+ Attribute(Name("manager"))))))))))),
+ Some(Expression(List(PrimaryExpressionNotNull(AliasAttribute(Alias(Name("emp")),
+ Attribute(Name("lastName")))),
+ PrimaryExpressionNotNull(AliasAttribute(Alias(Name("manager")),
+ Attribute(Name("lastName"))))))))
assert(expected === (a.parseAll(a.select, e).get))
}
@@ -24,7 +39,15 @@
FROM Employee AS emp
WHERE emp.manager=18 AND emp.lastName IS NOT NULL
"""
- val expected = Select(AttributeList(List(NamedAttribute(FQAttribute(Relation(Name("emp")),Attribute(Name("lastName"))),Attribute(Name("empName"))))),TableList(List(Join(RelAsAlias(Relation(Name("Employee")),Relation(Name("emp"))),None))),Some(Expression(List(PrimaryExpressionEq(FQAttribute(Relation(Name("emp")),Attribute(Name("manager"))),RValueTyped(SQLDatatype.INTEGER,Name("18"))), PrimaryExpressionNotNull(FQAttribute(Relation(Name("emp")),Attribute(Name("lastName"))))))))
+ val expected = Select(AttributeList(List(NamedAttribute(AliasAttribute(Alias(Name("emp")),
+ Attribute(Name("lastName"))),
+ Attribute(Name("empName"))))),
+ TableList(List(Join(RelAsAlias(Relation(Name("Employee")),Alias(Name("emp"))),None))),
+ Some(Expression(List(PrimaryExpressionEq(AliasAttribute(Alias(Name("emp")),
+ Attribute(Name("manager"))),
+ RValueTyped(SQLDatatype.INTEGER,Name("18"))),
+ PrimaryExpressionNotNull(AliasAttribute(Alias(Name("emp")),
+ Attribute(Name("lastName"))))))))
assert(expected === (a.parseAll(a.select, e).get))
}
@@ -37,7 +60,19 @@
AND manager.lastName="Johnson"
WHERE emp.lastName IS NOT NULL
"""
- val expected = Select(AttributeList(List(NamedAttribute(FQAttribute(Relation(Name("emp")),Attribute(Name("lastName"))),Attribute(Name("empName"))))),TableList(List(Join(RelAsAlias(Relation(Name("Employee")),Relation(Name("emp"))),None),Join(RelAsAlias(Relation(Name("Employee")),Relation(Name("manager"))),Some(Expression(List(PrimaryExpressionEq(FQAttribute(Relation(Name("emp")),Attribute(Name("manager"))),RValueAttr(FQAttribute(Relation(Name("manager")),Attribute(Name("id"))))), PrimaryExpressionEq(FQAttribute(Relation(Name("manager")),Attribute(Name("lastName"))),RValueTyped(SQLDatatype.STRING,Name("Johnson"))))))))),Some(Expression(List(PrimaryExpressionNotNull(FQAttribute(Relation(Name("emp")),Attribute(Name("lastName"))))))))
+ val expected = Select(AttributeList(List(NamedAttribute(AliasAttribute(Alias(Name("emp")),
+ Attribute(Name("lastName"))),
+ Attribute(Name("empName"))))),
+ TableList(List(Join(RelAsAlias(Relation(Name("Employee")),Alias(Name("emp"))),None),
+ Join(RelAsAlias(Relation(Name("Employee")),Alias(Name("manager"))),
+ Some(Expression(List(PrimaryExpressionEq(AliasAttribute(Alias(Name("emp")),
+ Attribute(Name("manager"))),
+ RValueAttr(AliasAttribute(Alias(Name("manager")),
+ Attribute(Name("id"))))),
+ PrimaryExpressionEq(AliasAttribute(Alias(Name("manager")),
+ Attribute(Name("lastName"))),
+ RValueTyped(SQLDatatype.STRING,Name("Johnson"))))))))),
+ Some(Expression(List(PrimaryExpressionNotNull(AliasAttribute(Alias(Name("emp")),Attribute(Name("lastName"))))))))
assert(expected === (a.parseAll(a.select, e).get))
}
@@ -54,7 +89,27 @@
AND grandManager.birthday < manager.birthday
WHERE emp.lastName IS NOT NULL AND grandManager.lastName IS NOT NULL
"""
- val expected = Select(AttributeList(List(NamedAttribute(FQAttribute(Relation(Name("emp")),Attribute(Name("lastName"))),Attribute(Name("empName"))), NamedAttribute(FQAttribute(Relation(Name("grandManager")),Attribute(Name("lastName"))),Attribute(Name("grandManagName"))))),TableList(List(Join(RelAsAlias(Relation(Name("Employee")),Relation(Name("emp"))),None),Join(RelAsAlias(Relation(Name("Manage")),Relation(Name("lower"))),Some(Expression(List(PrimaryExpressionEq(FQAttribute(Relation(Name("lower")),Attribute(Name("manages"))),RValueAttr(FQAttribute(Relation(Name("emp")),Attribute(Name("id"))))))))), Join(RelAsAlias(Relation(Name("Employee")),Relation(Name("manager"))),Some(Expression(List(PrimaryExpressionEq(FQAttribute(Relation(Name("manager")),Attribute(Name("id"))),RValueAttr(FQAttribute(Relation(Name("lower")),Attribute(Name("manager"))))), PrimaryExpressionLt(FQAttribute(Relation(Name("manager")),Attribute(Name("birthday"))),RValueAttr(FQAttribute(Relation(Name("emp")),Attribute(Name("birthday"))))))))), Join(RelAsAlias(Relation(Name("Manage")),Relation(Name("upper"))),Some(Expression(List(PrimaryExpressionEq(FQAttribute(Relation(Name("upper")),Attribute(Name("manages"))),RValueAttr(FQAttribute(Relation(Name("manager")),Attribute(Name("id"))))))))), Join(RelAsAlias(Relation(Name("Employee")),Relation(Name("grandManager"))),Some(Expression(List(PrimaryExpressionEq(FQAttribute(Relation(Name("grandManager")),Attribute(Name("id"))),RValueAttr(FQAttribute(Relation(Name("upper")),Attribute(Name("manager"))))), PrimaryExpressionLt(FQAttribute(Relation(Name("grandManager")),Attribute(Name("birthday"))),RValueAttr(FQAttribute(Relation(Name("manager")),Attribute(Name("birthday"))))))))))),Some(Expression(List(PrimaryExpressionNotNull(FQAttribute(Relation(Name("emp")),Attribute(Name("lastName")))), PrimaryExpressionNotNull(FQAttribute(Relation(Name("grandManager")),Attribute(Name("lastName"))))))))
+ val expected = Select(AttributeList(List(NamedAttribute(AliasAttribute(Alias(Name("emp")), Attribute(Name("lastName"))),
+ Attribute(Name("empName"))),
+ NamedAttribute(AliasAttribute(Alias(Name("grandManager")),Attribute(Name("lastName"))),
+ Attribute(Name("grandManagName"))))),
+ TableList(List(Join(RelAsAlias(Relation(Name("Employee")),Alias(Name("emp"))),None),
+ Join(RelAsAlias(Relation(Name("Manage")),Alias(Name("lower"))),Some(Expression(List(PrimaryExpressionEq(AliasAttribute(Alias(Name("lower")),Attribute(Name("manages"))),RValueAttr(AliasAttribute(Alias(Name("emp")),Attribute(Name("id"))))))))),
+ Join(RelAsAlias(Relation(Name("Employee")),Alias(Name("manager"))),Some(Expression(List(PrimaryExpressionEq(AliasAttribute(Alias(Name("manager")),Attribute(Name("id"))),RValueAttr(AliasAttribute(Alias(Name("lower")),Attribute(Name("manager"))))), PrimaryExpressionLt(AliasAttribute(Alias(Name("manager")),Attribute(Name("birthday"))),RValueAttr(AliasAttribute(Alias(Name("emp")),Attribute(Name("birthday"))))))))),
+ Join(RelAsAlias(Relation(Name("Manage")),Alias(Name("upper"))),Some(Expression(List(PrimaryExpressionEq(AliasAttribute(Alias(Name("upper")),Attribute(Name("manages"))),RValueAttr(AliasAttribute(Alias(Name("manager")),Attribute(Name("id"))))))))),
+ Join(RelAsAlias(Relation(Name("Employee")),Alias(Name("grandManager"))),
+ Some(Expression(List(PrimaryExpressionEq(AliasAttribute(Alias(Name("grandManager")),
+ Attribute(Name("id"))),
+ RValueAttr(AliasAttribute(Alias(Name("upper")),
+ Attribute(Name("manager"))))),
+ PrimaryExpressionLt(AliasAttribute(Alias(Name("grandManager")),
+ Attribute(Name("birthday"))),
+ RValueAttr(AliasAttribute(Alias(Name("manager")),
+ Attribute(Name("birthday"))))))))))),
+ Some(Expression(List(PrimaryExpressionNotNull(AliasAttribute(Alias(Name("emp")),
+ Attribute(Name("lastName")))),
+ PrimaryExpressionNotNull(AliasAttribute(Alias(Name("grandManager")),
+ Attribute(Name("lastName"))))))))
assert(expected === (a.parseAll(a.select, e).get))
}