+ the (turtle|SQL) parser can read from files
authorAlexandre Bertails <alexandre@bertails.org>
Fri, 26 Nov 2010 19:43:36 -0500
changeset 282 19086fe774d2
parent 281 60fef14de348
child 283 98b3d35e6175
+ the (turtle|SQL) parser can read from files
- DirectMappingSpec.scala
~ DirectMappingTest.scala depends only on the test files
directmapping/src/test/scala/DirectMappingTest.scala
sharedtestdata/src/main/scala/DirectMappingSpec.scala
sql/src/main/scala/SQL.scala
turtle/src/main/scala/turtle.scala
--- a/directmapping/src/test/scala/DirectMappingTest.scala	Fri Nov 26 19:12:36 2010 -0500
+++ b/directmapping/src/test/scala/DirectMappingTest.scala	Fri Nov 26 19:43:36 2010 -0500
@@ -4,35 +4,13 @@
 import org.w3.sw.rdb.RDB._
 import org.w3.sw.sql
 import org.w3.sw.directmapping.DirectMapping._
+import java.io.File
 
 import org.scalatest.FunSuite
 
-class Test extends FunSuite {
-
-  import DirectMappingSpec._
-
-  implicit def l2db (rs:List[Relation]):Map[RelName, Relation] =
-    rs.map(r => (r.name -> r)).toMap
-  implicit def string2relName (n:String) = RelName(n)
-  implicit def string2attrName (n:String) = AttrName(n)
+class FundamentalTest extends FunSuite {
 
-  def testDirectMapping(testName:String, db:Database, expectedGraph:Graph) =
-    test(testName) {
-      val computedGraph = directDB(db)
-      assert (expectedGraph === computedGraph)
-    }
-
-  testDirectMapping("2 People 1 Addresses", db_emp_adder, directgraph_emp_adder)
-
-  testDirectMapping("2 People 1 Addresses 1 Department", db_multi_key, directgraph_multi_key)
-
-  testDirectMapping("2 People 1 Addresses 1 Department 2 Projects 1 Task", db_ref_no_pk, directgraph_ref_no_pk)
-
-  testDirectMapping("1 People 1 Addresses 1 Offices", db_hier_tabl_proto, directgraph_hier_tabl_proto)
-
-  testDirectMapping("1 People 1 Addresses 1 Offices 1 ExectutiveOffices", db_hier_tabl, directgraph_hier_tabl)
-
-  test("NodeMap") {
+ test("NodeMap") {
 
     val ck1:CandidateKey = CandidateKey("name", "ssn")
     val ck2:CandidateKey = CandidateKey("ID")
@@ -58,3 +36,41 @@
 
 }
 
+class Test extends FunSuite {
+
+  import DirectMappingSpec._
+
+  def testDirectMapping(testName:String, db:Database, expectedGraph:Graph):Unit =
+    test(testName) {
+      val computedGraph = directDB(db)
+      assert (expectedGraph === computedGraph)
+    }
+
+  def testDirectMapping(testName:String, dbFile:File, expectedGraphFile:File):Unit = {
+    val db = SqlParser.toDB(dbFile)
+    val expectedGraph = TurtleParser.toGraph(expectedGraphFile)
+    testDirectMapping(testName, db, expectedGraph)
+  }
+
+  def testDirectMapping(testName:String):Unit =
+    testDirectMapping(testName,
+		      new File("./sharedtestdata/directmappingspec/" + testName + ".sql"),
+		      new File("./sharedtestdata/directmappingspec/" + testName + ".ttl"))
+
+  // 2 People 1 Addresses
+  testDirectMapping("emp_addr")
+
+  // 2 People 1 Addresses 1 Department
+  testDirectMapping("multi_key")
+
+  // 2 People 1 Addresses 1 Department 2 Projects 1 Task
+  testDirectMapping("ref_no_pk")
+
+  // 1 People 1 Addresses 1 Offices
+  testDirectMapping("hier_tabl_proto")
+
+  // 1 People 1 Addresses 1 Offices 1 ExectutiveOffices
+  testDirectMapping("hier_tabl")
+
+}
+
--- a/sharedtestdata/src/main/scala/DirectMappingSpec.scala	Fri Nov 26 19:12:36 2010 -0500
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,216 +0,0 @@
-package org.w3.sw.directmapping
-
-import org.w3.sw.rdf._
-import org.w3.sw.rdb.RDB._
-import org.w3.sw.sql
-import org.w3.sw.turtle._
-
-object DirectMappingSpec {
-
-  val SqlParser = sql.SqlParser()
-
-  val TurtleParser = Turtle()
-
-  // "../../../directmappingspec/emp_addr.sql"
-  val db_emp_adder = SqlParser.toDB("""
-CREATE TABLE Addresses (ID INT PRIMARY KEY, city STRING, state STRING);
-INSERT INTO Addresses (ID, city, state) VALUES (18, "Cambridge", "MA");
-CREATE TABLE People (ID INT PRIMARY KEY, fname STRING, addr INT, FOREIGN KEY (addr) REFERENCES Addresses(ID));
-INSERT INTO People (ID, fname, addr) VALUES (7, "Bob", 18);
-INSERT INTO People (ID, fname, addr) VALUES (8, "Sue", NULL);
-""")
-
-  // "../../../directmappingspec/emp_addr.ttl"
-  val directgraph_emp_adder = TurtleParser.toGraph("""
[email protected] xsd: <http://www.w3.org/2001/XMLSchema#> .
-<People/ID.7#_> <People#ID> 7 .
-<People/ID.7#_> <People#fname> "Bob"^^<http://www.w3.org/2001/XMLSchema#string> .
-<People/ID.7#_> <People#addr> <Addresses/ID.18#_> .
-<People/ID.8#_> <People#ID> 8 .
-<People/ID.8#_> <People#fname> "Sue"^^xsd:string .
-<Addresses/ID.18#_> <Addresses#ID> 18 .
-<Addresses/ID.18#_> <Addresses#city> "Cambridge"^^xsd:string .
-<Addresses/ID.18#_> <Addresses#state> "MA"^^xsd:string .
-""")
-
-
-  // "../../../directmappingspec/multi_key.sql"
-  val db_multi_key = SqlParser.parseAll(SqlParser.ddl, """
-CREATE TABLE Addresses (ID INT PRIMARY KEY, city STRING, state STRING);
-INSERT INTO Addresses (ID, city, state) VALUES (18, "Cambridge", "MA");
-CREATE TABLE Department (ID INT PRIMARY KEY, name STRING, city STRING,
-                         manager INT, FOREIGN KEY (manager) REFERENCES People(ID), UNIQUE (name, city));
-INSERT INTO Department (ID, name, city, manager) VALUES (23, "accounting", "Cambridge", 8);
-CREATE TABLE People (ID INT PRIMARY KEY, fname STRING,
-                     addr INT, FOREIGN KEY (addr) REFERENCES Addresses(ID),
-                     deptName STRING, deptCity STRING, FOREIGN KEY (deptName, deptCity) REFERENCES Department(name, city));
-INSERT INTO People (ID, fname, addr, deptName, deptCity) VALUES (7, "Bob", 18, "accounting", "Cambridge");
-INSERT INTO People (ID, fname, addr, deptName, deptCity) VALUES (8, "Sue", NULL, NULL, NULL);
-""").get
-
-  // "../../../directmappingspec/multi_key.ttl"
-  val directgraph_multi_key = TurtleParser.toGraph("""
[email protected] xsd: <http://www.w3.org/2001/XMLSchema#> .
-<People/ID.7#_> <People#deptName> "accounting"^^xsd:string .
-<People/ID.7#_> <People#deptCity> "Cambridge"^^xsd:string .
-<People/ID.7#_> <People#deptName_deptCity> <Department/ID.23#_> .
-
-<Department/ID.23#_> <Department#ID> 23 .
-<Department/ID.23#_> <Department#name> "accounting"^^xsd:string .
-<Department/ID.23#_> <Department#city> "Cambridge"^^xsd:string .
-<Department/ID.23#_> <Department#manager> <People/ID.8#_> .
-
-<People/ID.7#_> <People#ID> 7 .
-<People/ID.7#_> <People#fname> "Bob"^^xsd:string .
-<People/ID.7#_> <People#addr> <Addresses/ID.18#_> .
-<People/ID.8#_> <People#ID> 8 .
-<People/ID.8#_> <People#fname> "Sue"^^xsd:string . 
-
-<Addresses/ID.18#_> <Addresses#ID> 18 .
-<Addresses/ID.18#_> <Addresses#city> "Cambridge"^^xsd:string .
-<Addresses/ID.18#_> <Addresses#state> "MA"^^xsd:string .
-""")
-
-
-  // "../../../directmappingspec/ref_no_pk.sql"
-  val db_ref_no_pk = SqlParser.parseAll(SqlParser.ddl, """
-CREATE TABLE Addresses (ID INT PRIMARY KEY, city STRING, state STRING);
-INSERT INTO Addresses (ID, city, state) VALUES (18, "Cambridge", "MA");
-CREATE TABLE Department (ID INT PRIMARY KEY, name STRING, city STRING, manager INT,
-                         FOREIGN KEY (manager) REFERENCES People(ID),
-                         UNIQUE (name, city));
-INSERT INTO Department (ID, name, city, manager) VALUES (23, "accounting", "Cambridge", 8);
-CREATE TABLE People (ID INT PRIMARY KEY, fname STRING, addr INT,
-                     FOREIGN KEY (addr) REFERENCES Addresses(ID),
-                     deptName STRING, deptCity STRING,
-                     FOREIGN KEY (deptName, deptCity) REFERENCES Department(name, city));
-INSERT INTO People (ID, fname, addr, deptName, deptCity) VALUES (7, "Bob", 18, "accounting", "Cambridge");
-INSERT INTO People (ID, fname, addr, deptName, deptCity) VALUES (8, "Sue", NULL, NULL, NULL);
-CREATE TABLE Projects (lead INT,
-                       FOREIGN KEY (lead) REFERENCES People(ID),
-                       name STRING, UNIQUE (lead, name), 
-                       deptName STRING, deptCity STRING,
-                       UNIQUE (name, deptName, deptCity),
-                       FOREIGN KEY (deptName, deptCity) REFERENCES Department(name, city));
-INSERT INTO Projects (lead, name, deptName, deptCity) VALUES (8, "pencil survey", "accounting", "Cambridge");
-INSERT INTO Projects (lead, name, deptName, deptCity) VALUES (8, "eraser survey", "accounting", "Cambridge");
-CREATE TABLE TaskAssignments (worker INT,
-                              FOREIGN KEY (worker) REFERENCES People(ID),
-                              project STRING, PRIMARY KEY (worker, project), 
-                              deptName STRING, deptCity STRING,
-                              FOREIGN KEY (worker) REFERENCES People(ID),
-                              FOREIGN KEY (project, deptName, deptCity) REFERENCES Projects(name, deptName, deptCity),
-                              FOREIGN KEY (deptName, deptCity) REFERENCES Department(name, city));
-INSERT INTO TaskAssignments (worker, project, deptName, deptCity) VALUES (7, "pencil survey", "accounting", "Cambridge");
-""").get
-
-  // "../../../directmappingspec/ref_no_pk.ttl"
-  val directgraph_ref_no_pk = TurtleParser.toGraph("""
[email protected] xsd: <http://www.w3.org/2001/XMLSchema#> .
-_:a <Projects#lead> <People/ID.8#_> .
-_:a <Projects#name> "pencil survey"^^xsd:string .
-_:a <Projects#deptName> "accounting"^^xsd:string .
-_:a <Projects#deptCity> "Cambridge"^^xsd:string .
-_:a <Projects#deptName_deptCity> <Department/ID.23#_> .
-
-_:b <Projects#lead> <People/ID.8#_> .
-_:b <Projects#name> "eraser survey"^^xsd:string .
-_:b <Projects#deptName> "accounting"^^xsd:string .
-_:b <Projects#deptCity> "Cambridge"^^xsd:string .
-_:b <Projects#deptName_deptCity> <Department/ID.23#_> .
-
-<TaskAssignments/worker.7_project.pencil+survey#_> <TaskAssignments#worker> <People/ID.7#_> .
-<TaskAssignments/worker.7_project.pencil+survey#_> <TaskAssignments#project> "pencil survey"^^xsd:string .
-<TaskAssignments/worker.7_project.pencil+survey#_> <TaskAssignments#deptName> "accounting"^^xsd:string .
-<TaskAssignments/worker.7_project.pencil+survey#_> <TaskAssignments#deptCity> "Cambridge"^^xsd:string .
-<TaskAssignments/worker.7_project.pencil+survey#_> <TaskAssignments#deptName_deptCity> <Department/ID.23#_> .
-<TaskAssignments/worker.7_project.pencil+survey#_> <TaskAssignments#project_deptName_deptCity> _:a .
-
-<People/ID.7#_> <People#deptName> "accounting"^^xsd:string .
-<People/ID.7#_> <People#deptCity> "Cambridge"^^xsd:string .
-<People/ID.7#_> <People#deptName_deptCity> <Department/ID.23#_> .
-
-<Department/ID.23#_> <Department#ID> 23 .
-<Department/ID.23#_> <Department#name> "accounting"^^xsd:string .
-<Department/ID.23#_> <Department#city> "Cambridge"^^xsd:string .
-<Department/ID.23#_> <Department#manager> <People/ID.8#_> .
-
-<People/ID.7#_> <People#ID> 7 .
-<People/ID.7#_> <People#fname> "Bob"^^xsd:string .
-<People/ID.7#_> <People#addr> <Addresses/ID.18#_> .
-<People/ID.8#_> <People#ID> 8 .
-<People/ID.8#_> <People#fname> "Sue"^^xsd:string .
-
-<Addresses/ID.18#_> <Addresses#ID> 18 .
-<Addresses/ID.18#_> <Addresses#city> "Cambridge"^^xsd:string .
-<Addresses/ID.18#_> <Addresses#state> "MA"^^xsd:string .
-""")
-
-
-  // "../../../directmappingspec/hier_tabl_proto.sql"
-  val db_hier_tabl_proto = SqlParser.parseAll(SqlParser.ddl, """
-CREATE TABLE Addresses (ID INT PRIMARY KEY, city STRING, state STRING);
-INSERT INTO Addresses (ID, city, state) VALUES (18, "Cambridge", "MA");
-CREATE TABLE People (ID INT PRIMARY KEY, fname STRING, addr INT,
-                     FOREIGN KEY (addr) REFERENCES Addresses(ID));
-INSERT INTO People (ID, fname, addr) VALUES (7, "Bob", 18);
-CREATE TABLE Offices (ID INT PRIMARY KEY,
-                      building INT, ofcNumber STRING,
-                      FOREIGN KEY (ID) REFERENCES Addresses(ID));
-INSERT INTO Offices (ID, building, ofcNumber) VALUES (18, 32, "G528");
-""").get
-
-  // "../../../directmappingspec/hier_tabl_proto.ttl"
-  val directgraph_hier_tabl_proto = TurtleParser.toGraph("""
[email protected] xsd: <http://www.w3.org/2001/XMLSchema#> .
-<Addresses/ID.18#_> <Offices#ID> 18 .
-<Addresses/ID.18#_> <Offices#building> 32 .
-<Addresses/ID.18#_> <Offices#ofcNumber> "G528"^^xsd:string . 
-
-<People/ID.7#_> <People#ID> 7 .
-<People/ID.7#_> <People#fname> "Bob"^^xsd:string .
-<People/ID.7#_> <People#addr> <Addresses/ID.18#_> .
-
-<Addresses/ID.18#_> <Addresses#ID> 18 .
-<Addresses/ID.18#_> <Addresses#city> "Cambridge"^^xsd:string .
-<Addresses/ID.18#_> <Addresses#state> "MA"^^xsd:string .
-""")
-
-
-  // "../../../directmappingspec/hier_tabl.sql"
-  val db_hier_tabl = SqlParser.parseAll(SqlParser.ddl, """
-CREATE TABLE Addresses (ID INT PRIMARY KEY, city STRING, state STRING);
-INSERT INTO Addresses (ID, city, state) VALUES (18, "Cambridge", "MA");
-CREATE TABLE People (ID INT PRIMARY KEY, fname STRING, addr INT,
-                     FOREIGN KEY (addr) REFERENCES Addresses(ID));
-INSERT INTO People (ID, fname, addr) VALUES (7, "Bob", 18);
-CREATE TABLE Offices (ID INT PRIMARY KEY,
-                      building INT, ofcNumber STRING,
-                      FOREIGN KEY (ID) REFERENCES Addresses(ID));
-INSERT INTO Offices (ID, building, ofcNumber) VALUES (18, 32, "G528");
-CREATE TABLE ExecutiveOffices (ID INT PRIMARY KEY,
-                               desk STRING,
-                               FOREIGN KEY (ID) REFERENCES Offices(ID));
-INSERT INTO ExecutiveOffices (ID, desk) VALUES (18, "oak");
-""").get
-					
-  // "../../../directmappingspec/hier_tabl.ttl"
-  val directgraph_hier_tabl = TurtleParser.toGraph("""
[email protected] xsd: <http://www.w3.org/2001/XMLSchema#> .
-<Addresses/ID.18#_> <ExecutiveOffices#ID> 18 .
-<Addresses/ID.18#_> <ExecutiveOffices#desk> "oak"^^xsd:string . 
-
-<Addresses/ID.18#_> <Offices#ID> 18 .
-<Addresses/ID.18#_> <Offices#building> 32 .
-<Addresses/ID.18#_> <Offices#ofcNumber> "G528"^^xsd:string . 
-
-<People/ID.7#_> <People#ID> 7 .
-<People/ID.7#_> <People#fname> "Bob"^^xsd:string .
-<People/ID.7#_> <People#addr> <Addresses/ID.18#_> .
-
-<Addresses/ID.18#_> <Addresses#ID> 18 .
-<Addresses/ID.18#_> <Addresses#city> "Cambridge"^^xsd:string .
-<Addresses/ID.18#_> <Addresses#state> "MA"^^xsd:string .
-""")
-
-}
--- a/sql/src/main/scala/SQL.scala	Fri Nov 26 19:12:36 2010 -0500
+++ b/sql/src/main/scala/SQL.scala	Fri Nov 26 19:43:36 2010 -0500
@@ -176,6 +176,11 @@
 
   def toDB(s:String):RDB.Database = parseAll(ddl, s).get
 
+  def toDB(file:java.io.File):RDB.Database = {
+    val s = scala.io.Source.fromFile(file).getLines.reduceLeft(_+_)
+    parseAll(ddl, s).get
+  }
+
   def createview:Parser[View] = // @@@ could stick under ddl
     "CREATE" ~ "VIEW" ~ relation ~ "AS" ~ selectORunion ^^
   { case "CREATE"~"VIEW"~relation~"AS"~defn => View(relation, defn) }
--- a/turtle/src/main/scala/turtle.scala	Fri Nov 26 19:12:36 2010 -0500
+++ b/turtle/src/main/scala/turtle.scala	Fri Nov 26 19:43:36 2010 -0500
@@ -20,6 +20,11 @@
 
   def toGraph(t:String):Graph = parseAll(turtle, t).get
 
+  def toGraph(file:java.io.File):Graph = {
+    val t = scala.io.Source.fromFile(file).getLines.reduceLeft(_+_)
+    parseAll(turtle, t).get
+  }
+
   def turtle:Parser[Graph] =
     opt(triplesblock) ^^ { case tbOPT => tbOPT.getOrElse(Set[Triple]()) }