changeset 38:871a0feead53

~ s/body:Set[Tuple]/body:List[Tuple]/
author Eric Prud'hommeaux <eric@w3.org>
date Fri, 15 Oct 2010 10:24:49 -0400
parents 3e1521c837cb
children a0d50a09566b
files src/main/scala/Main.scala src/test/scala/Test.scala
diffstat 2 files changed, 91 insertions(+), 85 deletions(-) [+]
line wrap: on
line diff
--- a/src/main/scala/Main.scala	Wed Oct 13 22:06:06 2010 -0400
+++ b/src/main/scala/Main.scala	Fri Oct 15 10:24:49 2010 -0400
@@ -10,7 +10,7 @@
     def keySet () = m.keySet
   }
 
-  case class Relation (name:RelName, header:Header, body:Set[Tuple], candidates:List[CandidateKey], pk:Option[CandidateKey], fks:ForeignKeys)
+  case class Relation (name:RelName, header:Header, body:List[Tuple], candidates:List[CandidateKey], pk:Option[CandidateKey], fks:ForeignKeys)
   case class Header (m:Map[AttrName, SQLDatatype]) {
     def apply (a:AttrName) = m(a)
     def keySet () = m.keySet
@@ -74,7 +74,9 @@
   case class LexicalValue (s:String) extends CellValue
   case class ␀ () extends CellValue
 
-  case class RelName(n:String)
+  case class RelName(n:String) {
+    override def toString = n
+  }
   case class AttrName(n:String) {
     override def toString = n
   }
@@ -243,7 +245,11 @@
   }
 
   def directR (u:StemIRI, r:Relation, nodes:NodeMap, db:Database) : RDFGraph =
-    r.body.flatMap(t => directT(u, t, r, nodes, db))
+    /* 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.
+     */
+    r.body.flatMap(t => directT(u, t, r, nodes, db)).toSet
 
   def directT (u:StemIRI, t:Tuple, r:Relation, nodes:NodeMap, db:Database) : Set[Triple] = {
     val s:Node =
--- a/src/test/scala/Test.scala	Wed Oct 13 22:06:06 2010 -0400
+++ b/src/test/scala/Test.scala	Fri Oct 15 10:24:49 2010 -0400
@@ -19,9 +19,9 @@
 			 Header("ID" -> SQLInt(),
 				"city" -> SQLString(),
 				"state" -> SQLString()),
-			 Set(Tuple("ID" -> LexicalValue("18"),
-				   "city" -> LexicalValue("Cambridge"),
-				   "state" -> LexicalValue("MA"))),
+			 List(Tuple("ID" -> LexicalValue("18"),
+				    "city" -> LexicalValue("Cambridge"),
+				    "state" -> LexicalValue("MA"))),
 			 List(CandidateKey("ID")),
 			 Some(CandidateKey("ID")),
 			 ForeignKeys())
@@ -30,12 +30,12 @@
 			  Header("ID" -> SQLInt(),
 				 "fname" -> SQLString(),
 				 "addr" -> SQLInt()),
-			  Set(Tuple("ID" -> LexicalValue("7"),
-				    "fname" -> LexicalValue("Bob"),
-				    "addr" -> LexicalValue("18")),
-			      Tuple("ID" -> LexicalValue("8"),
-				    "fname" -> LexicalValue("Sue"),
-				    "addr" -> ␀())),
+			  List(Tuple("ID" -> LexicalValue("7"),
+				     "fname" -> LexicalValue("Bob"),
+				     "addr" -> LexicalValue("18")),
+			       Tuple("ID" -> LexicalValue("8"),
+				     "fname" -> LexicalValue("Sue"),
+				     "addr" -> ␀())),
 			  List(CandidateKey("ID")),
 			  Some(CandidateKey("ID")),
 			  ForeignKeys(List("addr") -> Target("Addresses", CandidateKey("ID"))))
@@ -65,9 +65,9 @@
 			 Header("ID" -> SQLInt(),
 				"city" -> SQLString(),
 				"state" -> SQLString()),
-			 Set(Tuple("ID" -> LexicalValue("18"),
-				   "city" -> LexicalValue("Cambridge"),
-				   "state" -> LexicalValue("MA"))),
+			 List(Tuple("ID" -> LexicalValue("18"),
+				    "city" -> LexicalValue("Cambridge"),
+				    "state" -> LexicalValue("MA"))),
 			 List(CandidateKey("ID")),
 			 Some(CandidateKey("ID")),
 			 ForeignKeys())
@@ -78,16 +78,16 @@
 				 "addr" -> SQLInt(),
 				 "deptName" -> SQLString(),
 				 "deptCity" -> SQLString()),
-			  Set(Tuple("ID" -> LexicalValue("7"),
-				    "fname" -> LexicalValue("Bob"),
-				    "addr" -> LexicalValue("18"),
-				    "deptName" -> LexicalValue("accounting"),
-				    "deptCity" -> LexicalValue("Cambridge")),
-			      Tuple("ID" -> LexicalValue("8"),
-				    "fname" -> LexicalValue("Sue"),
-				    "addr" -> ␀(),
-				    "deptName" -> ␀(),
-				    "deptCity" -> ␀())),
+			  List(Tuple("ID" -> LexicalValue("7"),
+				     "fname" -> LexicalValue("Bob"),
+				     "addr" -> LexicalValue("18"),
+				     "deptName" -> LexicalValue("accounting"),
+				     "deptCity" -> LexicalValue("Cambridge")),
+			       Tuple("ID" -> LexicalValue("8"),
+				     "fname" -> LexicalValue("Sue"),
+				     "addr" -> ␀(),
+				     "deptName" -> ␀(),
+				     "deptCity" -> ␀())),
 			  List(CandidateKey("ID")),
 			  Some(CandidateKey("ID")),
 			  ForeignKeys(List("addr") -> Target("Addresses", CandidateKey("ID")),
@@ -98,10 +98,10 @@
 				     "name" -> SQLString(),
 				     "city" -> SQLString(),
 				     "manager" -> SQLInt()),
-			      Set(Tuple("ID" -> LexicalValue("23"),
-					"name" -> LexicalValue("accounting"),
-					"city" -> LexicalValue("Cambridge"),
-					"manager" -> LexicalValue("8"))),
+			      List(Tuple("ID" -> LexicalValue("23"),
+					 "name" -> LexicalValue("accounting"),
+					 "city" -> LexicalValue("Cambridge"),
+					 "manager" -> LexicalValue("8"))),
 			      List(CandidateKey("ID"),
 				   CandidateKey("name", "city")),
 			      Some(CandidateKey("ID")),
@@ -141,9 +141,9 @@
 			 Header("ID" -> SQLInt(),
 				"city" -> SQLString(),
 				"state" -> SQLString()),
-			 Set(Tuple("ID" -> LexicalValue("18"),
-				   "city" -> LexicalValue("Cambridge"),
-				   "state" -> LexicalValue("MA"))),
+			 List(Tuple("ID" -> LexicalValue("18"),
+				    "city" -> LexicalValue("Cambridge"),
+				    "state" -> LexicalValue("MA"))),
 			 List(CandidateKey("ID")),
 			 Some(CandidateKey("ID")),
 			 ForeignKeys())
@@ -154,16 +154,16 @@
 				 "addr" -> SQLInt(),
 				 "deptName" -> SQLString(),
 				 "deptCity" -> SQLString()),
-			  Set(Tuple("ID" -> LexicalValue("7"),
-				    "fname" -> LexicalValue("Bob"),
-				    "addr" -> LexicalValue("18"),
-				    "deptName" -> LexicalValue("accounting"),
-				    "deptCity" -> LexicalValue("Cambridge")),
-			      Tuple("ID" -> LexicalValue("8"),
-				    "fname" -> LexicalValue("Sue"),
-				    "addr" -> ␀(),
-				    "deptName" -> ␀(),
-				    "deptCity" -> ␀())), // no data
+			  List(Tuple("ID" -> LexicalValue("7"),
+				     "fname" -> LexicalValue("Bob"),
+				     "addr" -> LexicalValue("18"),
+				     "deptName" -> LexicalValue("accounting"),
+				     "deptCity" -> LexicalValue("Cambridge")),
+			       Tuple("ID" -> LexicalValue("8"),
+				     "fname" -> LexicalValue("Sue"),
+				     "addr" -> ␀(),
+				     "deptName" -> ␀(),
+				     "deptCity" -> ␀())), // no data
 			  List(CandidateKey("ID")),
 			  Some(CandidateKey("ID")),
 			  ForeignKeys(List("addr") -> Target("Addresses", CandidateKey("ID")),
@@ -174,10 +174,10 @@
 				     "name" -> SQLString(),
 				     "city" -> SQLString(),
 				     "manager" -> SQLInt()),
-			      Set(Tuple("ID" -> LexicalValue("23"),
-					"name" -> LexicalValue("accounting"),
-					"city" -> LexicalValue("Cambridge"),
-					"manager" -> LexicalValue("8"))),
+			      List(Tuple("ID" -> LexicalValue("23"),
+					 "name" -> LexicalValue("accounting"),
+					 "city" -> LexicalValue("Cambridge"),
+					 "manager" -> LexicalValue("8"))),
 			      List(CandidateKey("ID"),
 				   CandidateKey("name", "city")),
 			      Some(CandidateKey("ID")),
@@ -188,14 +188,14 @@
 				   "name" -> SQLString(),
 				   "deptName" -> SQLString(),
 				   "deptCity" -> SQLString()),
-			    Set(Tuple("lead" -> LexicalValue("8"),
-				      "name" -> LexicalValue("pencil survey"),
-				      "deptName" -> LexicalValue("accounting"),
-				      "deptCity" -> LexicalValue("Cambridge")),
-				Tuple("lead" -> LexicalValue("8"),
-				      "name" -> LexicalValue("eraser survey"),
-				      "deptName" -> LexicalValue("accounting"),
-				      "deptCity" -> LexicalValue("Cambridge"))),
+			    List(Tuple("lead" -> LexicalValue("8"),
+				       "name" -> LexicalValue("pencil survey"),
+				       "deptName" -> LexicalValue("accounting"),
+				       "deptCity" -> LexicalValue("Cambridge")),
+				 Tuple("lead" -> LexicalValue("8"),
+				       "name" -> LexicalValue("eraser survey"),
+				       "deptName" -> LexicalValue("accounting"),
+				       "deptCity" -> LexicalValue("Cambridge"))),
 			    List(CandidateKey("lead", "name"),
 				 CandidateKey("name", "deptName", "deptCity")),
 			    None,
@@ -209,10 +209,10 @@
 				"project" -> SQLString(),
 				"deptName" -> SQLString(),
 				"deptCity" -> SQLString()),
-			 Set(Tuple("worker" -> LexicalValue("7"),
-				   "project" -> LexicalValue("pencil survey"),
-				   "deptName" -> LexicalValue("accounting"),
-				   "deptCity" -> LexicalValue("Cambridge"))),
+			 List(Tuple("worker" -> LexicalValue("7"),
+				    "project" -> LexicalValue("pencil survey"),
+				    "deptName" -> LexicalValue("accounting"),
+				    "deptCity" -> LexicalValue("Cambridge"))),
 			 List(CandidateKey("worker", "project")),
 			 Some(CandidateKey("worker", "project")),
 			 ForeignKeys(List("worker") -> Target("People", CandidateKey("ID")),
@@ -272,9 +272,9 @@
 			 Header("ID" -> SQLInt(),
 				"city" -> SQLString(),
 				"state" -> SQLString()),
-			 Set(Tuple("ID" -> LexicalValue("18"),
-				   "city" -> LexicalValue("Cambridge"),
-				   "state" -> LexicalValue("MA"))),
+			 List(Tuple("ID" -> LexicalValue("18"),
+				    "city" -> LexicalValue("Cambridge"),
+				    "state" -> LexicalValue("MA"))),
 			 List(CandidateKey("ID")),
 			 Some(CandidateKey("ID")),
 			 ForeignKeys())
@@ -283,9 +283,9 @@
 			  Header("ID" -> SQLInt(),
 				 "fname" -> SQLString(),
 				 "addr" -> SQLInt()),
-			  Set(Tuple("ID" -> LexicalValue("7"),
-				    "fname" -> LexicalValue("Bob"),
-				    "addr" -> LexicalValue("18"))),
+			  List(Tuple("ID" -> LexicalValue("7"),
+				     "fname" -> LexicalValue("Bob"),
+				     "addr" -> LexicalValue("18"))),
 			  List(CandidateKey("ID")),
 			  Some(CandidateKey("ID")),
 			  ForeignKeys(List("addr") -> Target("Addresses", CandidateKey("ID"))))
@@ -294,9 +294,9 @@
 			   Header("ID" -> SQLInt(),
 				  "building" -> SQLInt(),
 				  "ofcNumber" -> SQLString()),
-			   Set(Tuple("ID" -> LexicalValue("18"),
-				     "building" -> LexicalValue("32"),
-				     "ofcNumber" -> LexicalValue("G528"))),
+			   List(Tuple("ID" -> LexicalValue("18"),
+				      "building" -> LexicalValue("32"),
+				      "ofcNumber" -> LexicalValue("G528"))),
 			   List(CandidateKey("ID")),
 			   Some(CandidateKey("ID")),
 			   ForeignKeys(List("ID") -> Target("Addresses", CandidateKey("ID"))))
@@ -328,9 +328,9 @@
 			 Header("ID" -> SQLInt(),
 				"city" -> SQLString(),
 				"state" -> SQLString()),
-			 Set(Tuple("ID" -> LexicalValue("18"),
-				   "city" -> LexicalValue("Cambridge"),
-				   "state" -> LexicalValue("MA"))),
+			 List(Tuple("ID" -> LexicalValue("18"),
+				    "city" -> LexicalValue("Cambridge"),
+				    "state" -> LexicalValue("MA"))),
 			 List(CandidateKey("ID")),
 			 Some(CandidateKey("ID")),
 			 ForeignKeys())
@@ -339,9 +339,9 @@
 			  Header("ID" -> SQLInt(),
 				 "fname" -> SQLString(),
 				 "addr" -> SQLInt()),
-			  Set(Tuple("ID" -> LexicalValue("7"),
-				    "fname" -> LexicalValue("Bob"),
-				    "addr" -> LexicalValue("18"))),
+			  List(Tuple("ID" -> LexicalValue("7"),
+				     "fname" -> LexicalValue("Bob"),
+				     "addr" -> LexicalValue("18"))),
 			  List(CandidateKey("ID")),
 			  Some(CandidateKey("ID")),
 			  ForeignKeys(List("addr") -> Target("Addresses", CandidateKey("ID"))))
@@ -350,9 +350,9 @@
 			   Header("ID" -> SQLInt(),
 				  "building" -> SQLInt(),
 				  "ofcNumber" -> SQLString()),
-			   Set(Tuple("ID" -> LexicalValue("18"),
-				     "building" -> LexicalValue("32"),
-				     "ofcNumber" -> LexicalValue("G528"))),
+			   List(Tuple("ID" -> LexicalValue("18"),
+				      "building" -> LexicalValue("32"),
+				      "ofcNumber" -> LexicalValue("G528"))),
 			     List(CandidateKey("ID")),
 			     Some(CandidateKey("ID")),
 			     ForeignKeys(List("ID") -> Target("Addresses", CandidateKey("ID"))))
@@ -360,8 +360,8 @@
     val execoffices = Relation("ExecutiveOffices",
 			       Header("ID" -> SQLInt(),
 				      "desk" -> SQLString()),
-			       Set(Tuple("ID" -> LexicalValue("18"),
-					 "desk" -> LexicalValue("oak"))),
+			       List(Tuple("ID" -> LexicalValue("18"),
+					  "desk" -> LexicalValue("oak"))),
 			       List(CandidateKey("ID")),
 			       Some(CandidateKey("ID")),
 			       ForeignKeys(List("ID") -> Target("Offices", CandidateKey("ID"))))
@@ -420,13 +420,13 @@
 			     Header("ID" -> SQLInt(),
 				    "fname" -> SQLString(),
 				    "boss" -> SQLInt()),
-			     Set(Tuple("ID" -> LexicalValue("1"),
-				       "fname" -> LexicalValue("Alice"),
-				       "boss" -> ␀()),
-				 Tuple("ID" -> LexicalValue("2"),
-				       "fname" -> LexicalValue("Bob"),
-				       "boss" -> LexicalValue("1"))
-			       ),
+			     List(Tuple("ID" -> LexicalValue("1"),
+					"fname" -> LexicalValue("Alice"),
+					"boss" -> ␀()),
+				  Tuple("ID" -> LexicalValue("2"),
+					"fname" -> LexicalValue("Bob"),
+					"boss" -> LexicalValue("1"))
+				),
 			     List(CandidateKey("ID")),
 			     Some(CandidateKey("ID")),
 			     ForeignKeys(List("boss") -> Target("Employees", CandidateKey("ID"))))
Set up and maintained by W3C Systems Team, please report bugs to sysreq@w3.org.

W3C would like to thank Microsoft who donated the server that allows us to run this service.