+ DOUBLE
authorEric Prud'hommeaux <eric@w3.org>
Sun, 13 Jun 2010 05:21:51 -0400
changeset 209 0bb89ba4ba18
parent 208 b4daf7d3d232
child 210 f92fc8e0599d
+ DOUBLE
+ DATETIME
~ progress on BSBM1
src/main/scala/SPARQL.scala
src/main/scala/SQL.scala
src/test/scala/SparqlToSparqlToSqlTest.scala
--- a/src/main/scala/SPARQL.scala	Sat Jun 12 17:22:57 2010 -0400
+++ b/src/main/scala/SPARQL.scala	Sun Jun 13 05:21:51 2010 -0400
@@ -224,12 +224,23 @@
   override def toString = "_:" + s
 }
 
-case class Expression(conjuncts:List[PrimaryExpression])
+case class Expression(conjuncts:List[PrimaryExpression]) {
+  override def toString = conjuncts.map(e => e.toString()).mkString(" && ")
+}
+
 sealed abstract class PrimaryExpression
-case class PrimaryExpressionEq(left:SparqlTermExpression, right:SparqlTermExpression) extends PrimaryExpression
-case class PrimaryExpressionLt(left:SparqlTermExpression, right:SparqlTermExpression) extends PrimaryExpression
-case class PrimaryExpressionGt(left:SparqlTermExpression, right:SparqlTermExpression) extends PrimaryExpression
-case class SparqlTermExpression(term:Term) extends PrimaryExpression
+case class PrimaryExpressionEq(left:SparqlTermExpression, right:SparqlTermExpression) extends PrimaryExpression {
+  override def toString = left.toString() + " = " + right.toString
+}
+case class PrimaryExpressionLt(left:SparqlTermExpression, right:SparqlTermExpression) extends PrimaryExpression {
+  override def toString = left.toString() + " < " + right.toString
+}
+case class PrimaryExpressionGt(left:SparqlTermExpression, right:SparqlTermExpression) extends PrimaryExpression {
+  override def toString = left.toString() + " > " + right.toString
+}
+case class SparqlTermExpression(term:Term) extends PrimaryExpression {
+  override def toString = term.toString
+}
 
 case class OrderElt(desc:Boolean, expr:Expression) {
   override def toString = { if (desc) "DESC" else "ASC" } + "(" + expr.toString + ")"
--- a/src/main/scala/SQL.scala	Sat Jun 12 17:22:57 2010 -0400
+++ b/src/main/scala/SQL.scala	Sun Jun 13 05:21:51 2010 -0400
@@ -158,7 +158,9 @@
 object Datatype {
   val STRING = Datatype("String")
   val INTEGER = Datatype("Int")
+  val DOUBLE = Datatype("Double")
   val DATE = Datatype("Date")
+  val DATETIME = Datatype("Datetime")
 }
 
 sealed abstract class ValueDescription
@@ -242,7 +244,9 @@
 
   def typpe:Parser[Value] = (
       "INT" ^^ { case _ => Value(Datatype.INTEGER) }
+    | "DOUBLE" ^^ { case _ => Value(Datatype.DOUBLE) }
     | "STRING" ^^ { case _ => Value(Datatype.STRING) }
+    | "DATETIME" ^^ { case _ => Value(Datatype.DATETIME) }
     | "DATE" ^^ { case _ => Value(Datatype.DATE) }
   )
 
--- a/src/test/scala/SparqlToSparqlToSqlTest.scala	Sat Jun 12 17:22:57 2010 -0400
+++ b/src/test/scala/SparqlToSparqlToSqlTest.scala	Sun Jun 13 05:21:51 2010 -0400
@@ -5,6 +5,7 @@
 package w3c.sw
 
 import org.scalatest.FunSuite
+import scala.util.matching.Regex
 import java.net.URI
 import w3c.sw.sparql.Sparql
 import w3c.sw.sparql2sparql.{SparqlToSparql}
@@ -344,18 +345,153 @@
 //     assert(expected === view)
   // }
 
-  /* ========== DiabeticPatient database tests ==========
+  /* ========== BSBM database tests ==========
    *
    */
   val bsbmDdl = """
-CREATE TABLE Person (ID INT PRIMARY KEY, MiddleName STRING, DateOfBirth DATE, SexDE INT, PRIMARY KEY (ID), FOREIGN KEY (SexDE) REFERENCES Sex_DE(ID));
-CREATE TABLE Sex_DE (ID INT PRIMARY KEY, EntryName STRING);
-CREATE TABLE Item_Medication (ID INT PRIMARY KEY, PRIMARY KEY (ID), PatientID INT, FOREIGN KEY (PatientID) REFERENCES Person(ID), PerformedDTTM DATE, EntryName STRING);
-CREATE TABLE Medication (ID INT PRIMARY KEY, PRIMARY KEY (ID), ItemID INT, FOREIGN KEY (ItemID) REFERENCES Item_Medication(ID), MedDictDE INT, FOREIGN KEY (MedDictDE) REFERENCES Medication_DE(ID), DaysToTake INT);
-CREATE TABLE Medication_DE (ID INT PRIMARY KEY, NDC INT);
-CREATE TABLE NDCcodes (ID INT PRIMARY KEY, NDC INT, ingredient INT);
+CREATE TABLE offer (
+  nr INT,
+  product INT,
+  producer INT,
+  vendor INT,
+  price DOUBLE,
+  validFrom DATETIME,
+  PRIMARY KEY (nr)
+);
+
 """
   val bsbmDb:DatabaseDesc = DDLParser.parseAll(DDLParser.ddl, bsbmDdl).get
+
+  val bsbmDdl_orig = """
+CREATE TABLE offer (
+  nr INT,
+  product INT,
+  producer INT,
+  vendor INT,
+  price DOUBLE,
+  validFrom DATETIME,
+  validTo DATETIME,
+  deliveryDays INT,
+  offerWebpage STRING,
+  publisher INT,
+  publishDate DATE,
+  PRIMARY KEY (nr),
+);
+
+
+CREATE TABLE person (
+  nr INT,
+  name STRING,
+  mbox_sha1sum STRING,
+  country STRING,
+  publisher INT,
+  publishDate DATE,
+  PRIMARY KEY (nr)
+);
+
+
+CREATE TABLE producer (
+  nr INT,
+  label STRING,
+  comment STRING,
+  homepage STRING,
+  country STRING,
+  publisher INT,
+  publishDate DATE,
+  PRIMARY KEY (nr)
+);
+
+
+CREATE TABLE product (
+  nr INT,
+  label STRING,
+  comment STRING,
+  producer INT,
+  propertyNum1 INT,
+  propertyNum2 INT,
+  propertyNum3 INT,
+  propertyNum4 INT,
+  propertyNum5 INT,
+  propertyNum6 INT,
+  propertyTex1 STRING,
+  propertyTex2 STRING,
+  propertyTex3 STRING,
+  propertyTex4 STRING,
+  propertyTex5 STRING,
+  propertyTex6 STRING,
+  publisher INT,
+  publishDate DATE,
+  PRIMARY KEY (nr),
+);
+
+
+CREATE TABLE productfeature (
+  nr INT,
+  label STRING,
+  comment SRING,
+  publisher INT,
+  publishDate DATE,
+  PRIMARY KEY (nr)
+);
+
+
+CREATE TABLE productfeatureproduct (
+  product INT,
+  productFeature INT,
+  PRIMARY KEY (product,productFeature)
+);
+
+
+CREATE TABLE producttype (
+  nr INT,
+  label STRING,
+  comment varchar(2000),
+  parent INT,
+  publisher INT,
+  publishDate DATE,
+  PRIMARY KEY (nr)
+);
+
+
+CREATE TABLE producttypeproduct (
+  product INT,
+  productType INT,
+  PRIMARY KEY (product,productType)
+);
+
+
+CREATE TABLE review (
+  nr INT,
+  product INT,
+  producer INT,
+  person INT,
+  reviewDate DATETIME,
+  title STRING,
+  text text CHARACTER SET utf8 COLLATE utf8_bin,
+  language STRING,
+  rating1 INT,
+  rating2 INT,
+  rating3 INT,
+  rating4 INT,
+  publisher INT,
+  publishDate DATE,
+  PRIMARY KEY (nr),
+  KEY product (product),
+  KEY person (person)
+);
+
+
+CREATE TABLE vendor (
+  nr INT,
+  label STRING,
+  comment STRING,
+  homepage STRING,
+  country STRING,
+  publisher INT,
+  publishDate DATE,
+  PRIMARY KEY (nr)
+);
+"""
   val delme = Sparql() // re-use ConstructParser ?
   val db2bsbm = delme.parseAll(delme.construct, """
 PREFIX map: <file:/E:/code/d2r-server-0.4/d2r-mapping.n3#>
@@ -384,14 +520,21 @@
 PREFIX tr: <http://www.w3.org/2008/04/SPARQLfed/#>
 
 CONSTRUCT {
-<s> <p> <o> .
+  ?product    a bsbm:Product .
+     ?product bsbm:productFeature ?pfp .
+     ?product rdf:type ?ptp .
+     ?product rdfs:label ?p_label .
+     ?product bsbm:productPropertyNumeric1 ?lit_propertyNum1 .
+     ?product product:propertyNum1 ?lit_propertyNum1 .
 } WHERE {
-<s> <p> <o> .
+  ?product product:label ?p_label .
+  ?pfp     productfeatureproduct:product ?product .
+  ?ptp     producttypeproduct:product ?product .
 }""" //"
 			      ).get
   test("bsbm1") {
     val sparqlParser = Sparql()
-    val bsbmQuery = sparqlParser.parseAll(sparqlParser.select, """
+    val queryStr = """
 PREFIX bsbm-inst: <http://www4.wiwiss.fu-berlin.de/bizer/bsbm/v01/instances/>
 PREFIX bsbm: <http://www4.wiwiss.fu-berlin.de/bizer/bsbm/v01/vocabulary/>
 PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
@@ -408,28 +551,36 @@
 	}
 ORDER BY ?label
 LIMIT 10
+"""
+    .replaceAll("<http://www4.wiwiss.fu-berlin.de/bizer/bsbm/v01/instances/ProductFeature([0-9]+)>",
+		"<http://bsbm.example/db/productfeatureproduct.ID.$1#record>")
+    .replaceAll("<http://www4.wiwiss.fu-berlin.de/bizer/bsbm/v01/instances/ProductType([0-9]+)>",
+		"<http://bsbm.example/db/producttypeproduct.ID.$1#record>")
+
+    val bsbmQuery = sparqlParser.parseAll(sparqlParser.select, queryStr).get
+
+    val stemQuery = sparqlParser.parseAll(sparqlParser.select, """
+SELECT ?product ?label WHERE {
+  ?product <http://bsbm.example/db/product#label> ?label .
+  <http://bsbm.example/db/productfeatureproduct.ID.5#record> <http://bsbm.example/db/productfeatureproduct#product> ?product .
+  <http://bsbm.example/db/producttypeproduct.ID.59#record> <http://bsbm.example/db/producttypeproduct#product> ?product .
+  <http://bsbm.example/db/productfeatureproduct.ID.7#record> <http://bsbm.example/db/productfeatureproduct#product> ?product .
+  FILTER (?value1 > 578)
+}
 """).get
 
-    val stemQuery = sparqlParser.parseAll(sparqlParser.select, """
-SELECT ?o WHERE { ?s <p> ?o }
-""").get
     val sqlParser = Sql()
     val sqlQuery = sqlParser.parseAll(sqlParser.select, """
 SELECT R_emp.empid AS emp
   FROM Employee AS R_emp
  WHERE R_emp.empid IS NOT NULL AND R_emp.lastName="Smith"
 """).get
-//     val asStem = SparqlToSparql(bsbmQuery, List(db2bsbm))
-//     if (!(asStem == stemQuery)) {
-//       println(asStem.toString())
-//       println("---")
-//       println(stemQuery.toString())
-//     }
-//     assert(asStem == stemQuery)
-//     val asSql = SparqlToSql(bsbmDb, asStem, StemURI("http://hospital.example/DB/"), false, false)._1
-//     assert(asSql === sqlQuery)
-//     val output = """
-// """
+    val asStem = SparqlToSparql(bsbmQuery, List(db2bsbm))
+    // assert(asStem === stemQuery)
+    // val (asSql, _) = SparqlToSql(bsbmDb, asStem, StemURI("http://bsbm.example/db/"), false, false)
+    // assert(asSql === sqlQuery)
+    val output = """
+"""
   }
 
 }