~ reorganized
authorEric Prud'hommeaux <eric@w3.org>
Sun, 14 Feb 2010 07:52:35 -0500
changeset 166 5326978aaa16
parent 165 8b545d69368e
child 167 23a6870f1c3f
~ reorganized
src/test/scala/SparqlToSparqlTest.scala
--- a/src/test/scala/SparqlToSparqlTest.scala	Sun Feb 14 07:50:32 2010 -0500
+++ b/src/test/scala/SparqlToSparqlTest.scala	Sun Feb 14 07:52:35 2010 -0500
@@ -1,4 +1,4 @@
-/* SparqlToSparqlTest: transform SPARQL to SQL and compare against a reference query.
+/* SparqlToSparqlTest: transform SPARQL to SPARQL and compare against a reference query.
  * $Id$
  */
 
@@ -92,6 +92,26 @@
     assert(transformed === expected)
   }
 
+  val GlobalSparqlParser = Sparql()
+  val Emp_TaskToFoaf = GlobalSparqlParser.parseAll(GlobalSparqlParser.construct, """
+  PREFIX foaf : <http://xmlns.com/foaf/0.1/>
+  PREFIX empP : <http://hr.example/DB/Employee#>
+  PREFIX task : <http://hr.example/DB/Task#>
+  CONSTRUCT { ?emp  foaf:last_name  ?wname .
+             ?emp  foaf:knows      ?man .
+             ?man  foaf:last_name  ?mname }
+  WHERE { ?emp  empP:lastName   ?wname .
+         ?pair task:drone      ?emp .
+         ?pair task:manager    ?man .
+         ?man  empP:lastName   ?mname }
+  """).get // "
+  val Emp_TaskToFoaf_abbr = Map[String,String]("<http://xmlns.com/foaf/0.1/last_name>" -> "foaf:last_name", 
+                                               "<http://xmlns.com/foaf/0.1/knows>" -> "foaf:knows",
+                                               "<http://hr.example/DB/Employee#lastName>" -> "Employee:lastName",
+                                               "<http://hr.example/DB/Task#drone>" -> "Task:drone",
+                                               "<http://hr.example/DB/Task#manager>" -> "Task:manager",
+                                               "^^http://www.w3.org/2001/XMLSchema#string" -> "")
+
   test("to many-to-many") {
     val sparqlParser = Sparql()
     val query = sparqlParser.parseAll(sparqlParser.select, """
@@ -102,19 +122,7 @@
   ?who  foaf:knows        ?whom  .
   ?whom foaf:last_name    "Smith"^^xsd:string }
 """).get
-    val rule1 = sparqlParser.parseAll(sparqlParser.construct, """
-PREFIX foaf : <http://xmlns.com/foaf/0.1/>
-PREFIX empP : <http://hr.example/DB/Employee#>
-PREFIX task : <http://hr.example/DB/Task#>
-CONSTRUCT { ?emp  foaf:last_name  ?wname .
-            ?emp  foaf:knows      ?man .
-            ?man  foaf:last_name  ?mname }
-    WHERE { ?emp  empP:lastName   ?wname .
-            ?pair task:drone      ?emp .
-            ?pair task:manager    ?man .
-            ?man  empP:lastName   ?mname }
-""").get
-    val transformed = SparqlToSparql(query, List(rule1))
+    val transformed = SparqlToSparql(query, List(Emp_TaskToFoaf))
     val expected = sparqlParser.parseAll(sparqlParser.select, """
 PREFIX empP : <http://hr.example/DB/Employee#>
 PREFIX task : <http://hr.example/DB/Task#>
@@ -139,26 +147,9 @@
   ?whom foaf:knows        ?whom2 .
   ?whom2 foaf:last_name    "Smith"^^xsd:string }
 """).get
-    val rule1 = sparqlParser.parseAll(sparqlParser.construct, """
-PREFIX foaf : <http://xmlns.com/foaf/0.1/>
-PREFIX empP : <http://hr.example/DB/Employee#>
-PREFIX task : <http://hr.example/DB/Task#>
-CONSTRUCT { ?emp  foaf:last_name  ?wname .
-            ?emp  foaf:knows      ?man .
-            ?man  foaf:last_name  ?mname }
-    WHERE { ?emp  empP:lastName   ?wname .
-            ?pair task:drone      ?emp .
-            ?pair task:manager    ?man .
-            ?man  empP:lastName   ?mname }
-""").get
-    SparqlToSparql.Abbreviations.update("<http://xmlns.com/foaf/0.1/last_name>", "foaf:last_name")
-    SparqlToSparql.Abbreviations.update("<http://xmlns.com/foaf/0.1/knows>", "foaf:knows")
-    SparqlToSparql.Abbreviations.update("<http://hr.example/DB/Employee#lastName>", "Employee:lastName")
-    SparqlToSparql.Abbreviations.update("<http://hr.example/DB/Task#drone>", "Task:drone")
-    SparqlToSparql.Abbreviations.update("<http://hr.example/DB/Task#manager>", "Task:manager")
-    SparqlToSparql.Abbreviations.update("^^http://www.w3.org/2001/XMLSchema#string", "")
+    SparqlToSparql.Abbreviations ++= Emp_TaskToFoaf_abbr
 
-    val transformed = SparqlToSparql(query, List(rule1))
+    val transformed = SparqlToSparql(query, List(Emp_TaskToFoaf))
     val expected = sparqlParser.parseAll(sparqlParser.select, """
 PREFIX empP : <http://hr.example/DB/Employee#>
 PREFIX task : <http://hr.example/DB/Task#>
@@ -194,4 +185,232 @@
     println("of interest: " + useful)
   }
 
+  test("XXX") { // ~/swobjects/tests/healthCare/lists-notBound/hl7.rq short
+    val sparqlParser = Sparql()
+    val query = sparqlParser.parseAll(sparqlParser.select, """
+PREFIX hl7: <http://www.hl7.org/v3ballot/xml/infrastructure/vocabulary/vocabulary#> 
+PREFIX xsd : <http://www.w3.org/2001/XMLSchema#>
+SELECT ?patient ?dob ?sex
+WHERE
+{
+  ?patient a hl7:Person .
+  ?patient hl7:entityName ?middleName .
+  ?patient hl7:livingSubjectBirthTime ?dob .
+  ?patient hl7:administrativeGenderCodePrintName ?sex .
+
+  ?subs_admin a hl7:SubstanceAdministration .
+  ?subs_admin hl7:consumable ?consumable .
+  ?consumable hl7:displayName "asdf"^^xsd:string .
 }
+""").get
+    SparqlToSparql.Abbreviations ++= Emp_TaskToFoaf_abbr
+    val rule1 = sparqlParser.parseAll(sparqlParser.construct, """
+PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
+PREFIX Person: <http://hospital.example/DB/Person#>
+PREFIX Sex_DE: <http://hospital.example/DB/Sex_DE#>
+PREFIX Item_Medication: <http://hospital.example/DB/Item_Medication#>
+PREFIX Medication: <http://hospital.example/DB/Medication#>
+PREFIX Medication_DE: <http://hospital.example/DB/Medication_DE#>
+PREFIX NDCcodes: <http://hospital.example/DB/NDCcodes#>
+
+PREFIX hl7: <http://www.hl7.org/v3ballot/xml/infrastructure/vocabulary/vocabulary#>
+PREFIX spl: <http://www.hl7.org/v3ballot/xml/infrastructure/vocabulary/vocabulary#>
+
+CONSTRUCT {
+?person     a					  hl7:Person .
+?person     hl7:entityName			  ?middleName .
+?person     hl7:livingSubjectBirthTime		  ?dob .
+?person     hl7:administrativeGenderCodePrintName ?sex .
+?person     hl7:substanceAdministration		  ?subst .
+?subst	a	       hl7:SubstanceAdministration .
+?subst 	hl7:consumable ?cons .
+?cons	    hl7:displayName	 ?takes .
+?cons	    spl:activeIngredient ?ingred .
+?ingred		spl:classCode ?ingred .
+?subst	hl7:effectiveTime ?interval .
+?interval	    hl7:start ?indicDate 
+} WHERE {
+  ?person     Person:MiddleName		     ?middleName .
+  ?person     Person:DateOfBirth	     ?dob .
+  ?person     Person:SexDE		     ?sexEntry   .
+
+              ?sexEntry   Sex_DE:EntryName   ?sex .
+
+  ?indicItem  Item_Medication:PatientID	     ?person .
+  ?indicItem  Item_Medication:PerformedDTTM  ?indicDate .
+  ?indicItem  Item_Medication:EntryName	     ?takes .
+  ?indicMed   Medication:ItemID		     ?indicItem .
+  ?indicMed   Medication:DaysToTake	     ?indicDuration .
+  ?indicMed   Medication:MedDictDE	     ?indicDE .
+  ?indicDE    Medication_DE:NDC		     ?indicNDC .
+  ?indicCode  NDCcodes:NDC         ?indicNDC .
+  ?indicCode  NDCcodes:ingredient  ?ingred }
+""").get
+    val transformed = SparqlToSparql(query, List(rule1))
+    val expected = sparqlParser.parseAll(sparqlParser.select, """
+PREFIX Person: <http://hospital.example/DB/Person#>
+PREFIX Sex_DE: <http://hospital.example/DB/Sex_DE#>
+PREFIX Item_Medication: <http://hospital.example/DB/Item_Medication#>
+PREFIX Medication: <http://hospital.example/DB/Medication#>
+PREFIX Medication_DE: <http://hospital.example/DB/Medication_DE#>
+PREFIX NDCcodes: <http://hospital.example/DB/NDCcodes#>
+PREFIX xsd : <http://www.w3.org/2001/XMLSchema#>
+
+SELECT ?patient ?dob ?sex
+ WHERE {
+    ?patient Person:MiddleName ?middleName .
+    ?patient Person:DateOfBirth ?dob .
+    ?patient Person:SexDE ?_0_sexEntry .
+    ?_0_sexEntry Sex_DE:EntryName ?sex .
+
+    ?_0_indicItem Item_Medication:PatientID ?patient .
+    ?_0_indicItem Item_Medication:EntryName "asdf"^^xsd:string .
+}
+""").get
+    assert(transformed === expected)
+  }
+
+  test("YYY") { // ~/swobjects/tests/healthCare/lists-notBound/hl7.rq short
+    val sparqlParser = Sparql()
+    val query = sparqlParser.parseAll(sparqlParser.select, """
+PREFIX hl7: <http://www.hl7.org/v3ballot/xml/infrastructure/vocabulary/vocabulary#> 
+PREFIX xsd : <http://www.w3.org/2001/XMLSchema#>
+SELECT ?patient ?dob ?sex
+WHERE { ?consumable hl7:displayName "asdf"^^xsd:string .
+        ?consumable hl7:activeIngredient ?ingredient .
+        ?ingredient hl7:classCode 6809 }
+""").get
+    SparqlToSparql.Abbreviations ++= Emp_TaskToFoaf_abbr
+    val rule1 = sparqlParser.parseAll(sparqlParser.construct, """
+PREFIX Item_Medication: <http://hospital.example/DB/Item_Medication#>
+PREFIX Medication: <http://hospital.example/DB/Medication#>
+PREFIX Medication_DE: <http://hospital.example/DB/Medication_DE#>
+PREFIX NDCcodes: <http://hospital.example/DB/NDCcodes#>
+PREFIX hl7: <http://www.hl7.org/v3ballot/xml/infrastructure/vocabulary/vocabulary#>
+CONSTRUCT {
+?cons	    hl7:displayName	 ?takes .
+?cons	    hl7:activeIngredient ?ingred .
+?ingred	    hl7:classCode        ?ingredCode .
+} WHERE {
+  ?indicItem  Item_Medication:EntryName	     ?takes .
+  ?indicMed   Medication:ItemID		     ?indicItem .
+  ?indicMed   Medication:MedDictDE	     ?indicDE .
+  ?indicDE    Medication_DE:NDC		     ?indicNDC .
+  ?indicCode  NDCcodes:NDC         ?indicNDC .
+  ?indicCode  NDCcodes:ingredient  ?ingredCode }
+""").get
+    val transformed = SparqlToSparql(query, List(rule1))
+    val expected = sparqlParser.parseAll(sparqlParser.select, """
+PREFIX Item_Medication: <http://hospital.example/DB/Item_Medication#>
+PREFIX Medication: <http://hospital.example/DB/Medication#>
+PREFIX Medication_DE: <http://hospital.example/DB/Medication_DE#>
+PREFIX NDCcodes: <http://hospital.example/DB/NDCcodes#>
+PREFIX xsd : <http://www.w3.org/2001/XMLSchema#>
+
+SELECT ?patient ?dob ?sex
+ WHERE {
+    ?_0_indicItem Item_Medication:EntryName "asdf"^^xsd:string .
+    ?_0_indicMed Medication:ItemID ?_0_indicItem .
+    ?_0_indicMed Medication:MedDictDE ?_0_indicDE .
+    ?_0_indicDE Medication_DE:NDC ?_0_indicNDC .
+    ?_0_indicCode NDCcodes:NDC ?_0_indicNDC .
+    ?_0_indicCode NDCcodes:ingredient 6809
+}
+""").get
+    assert(transformed === expected)
+  }
+
+  test("ZZZ") { // ~/swobjects/tests/healthCare/lists-notBound/hl7.rq short
+    val sparqlParser = Sparql()
+    val query = sparqlParser.parseAll(sparqlParser.select, """
+PREFIX hl7: <http://www.hl7.org/v3ballot/xml/infrastructure/vocabulary/vocabulary#> 
+SELECT ?patient ?dob ?sex
+WHERE
+{
+  ?patient a hl7:Person .
+  ?patient hl7:entityName ?middleName .
+  ?patient hl7:livingSubjectBirthTime ?dob .
+  ?patient hl7:administrativeGenderCodePrintName ?sex .
+  ?patient hl7:substanceAdministration ?subs_admin .
+  ?subs_admin a hl7:SubstanceAdministration .
+  ?subs_admin hl7:consumable ?consumable .
+  ?consumable hl7:displayName ?takes .
+  ?consumable hl7:activeIngredient ?ingredient .
+  ?ingredient hl7:classCode 6809 .
+  ?subs_admin hl7:effectiveTime ?indic_span .
+}
+""").get
+    SparqlToSparql.Abbreviations ++= Emp_TaskToFoaf_abbr
+    val rule1 = sparqlParser.parseAll(sparqlParser.construct, """
+PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
+PREFIX Person: <http://hospital.example/DB/Person#>
+PREFIX Sex_DE: <http://hospital.example/DB/Sex_DE#>
+PREFIX Item_Medication: <http://hospital.example/DB/Item_Medication#>
+PREFIX Medication: <http://hospital.example/DB/Medication#>
+PREFIX Medication_DE: <http://hospital.example/DB/Medication_DE#>
+PREFIX NDCcodes: <http://hospital.example/DB/NDCcodes#>
+
+PREFIX hl7: <http://www.hl7.org/v3ballot/xml/infrastructure/vocabulary/vocabulary#>
+PREFIX spl: <http://www.hl7.org/v3ballot/xml/infrastructure/vocabulary/vocabulary#>
+
+CONSTRUCT {
+?person     a					  hl7:Person .
+?person     hl7:entityName			  ?middleName .
+?person     hl7:livingSubjectBirthTime		  ?dob .
+?person     hl7:administrativeGenderCodePrintName ?sex .
+?person     hl7:substanceAdministration		  ?subst .
+?subst	a	       hl7:SubstanceAdministration .
+?subst 	hl7:consumable ?cons .
+?cons	    hl7:displayName	 ?takes .
+?cons	    spl:activeIngredient ?ingred .
+?ingred		spl:classCode ?ingredCode .
+?subst	hl7:effectiveTime ?interval .
+?interval	    hl7:start ?indicDate 
+} WHERE {
+  ?person     Person:MiddleName		     ?middleName .
+  ?person     Person:DateOfBirth	     ?dob .
+  ?person     Person:SexDE		     ?sexEntry   .
+
+              ?sexEntry   Sex_DE:EntryName   ?sex .
+
+  ?indicItem  Item_Medication:PatientID	     ?person .
+  ?indicItem  Item_Medication:PerformedDTTM  ?indicDate .
+  ?indicItem  Item_Medication:EntryName	     ?takes .
+  ?indicMed   Medication:ItemID		     ?indicItem .
+  ?indicMed   Medication:DaysToTake	     ?indicDuration .
+  ?indicMed   Medication:MedDictDE	     ?indicDE .
+  ?indicDE    Medication_DE:NDC		     ?indicNDC .
+  ?indicCode  NDCcodes:NDC         ?indicNDC .
+  ?indicCode  NDCcodes:ingredient  ?ingredCode }
+""").get
+    val transformed = SparqlToSparql(query, List(rule1))
+    val expected = sparqlParser.parseAll(sparqlParser.select, """
+PREFIX Person: <http://hospital.example/DB/Person#>
+PREFIX Sex_DE: <http://hospital.example/DB/Sex_DE#>
+PREFIX Item_Medication: <http://hospital.example/DB/Item_Medication#>
+PREFIX Medication: <http://hospital.example/DB/Medication#>
+PREFIX Medication_DE: <http://hospital.example/DB/Medication_DE#>
+PREFIX NDCcodes: <http://hospital.example/DB/NDCcodes#>
+PREFIX xsd : <http://www.w3.org/2001/XMLSchema#>
+
+SELECT ?patient ?dob ?sex
+ WHERE {
+    ?patient Person:MiddleName ?middleName .
+    ?patient Person:DateOfBirth ?dob .
+    ?patient Person:SexDE ?_0_sexEntry .
+    ?_0_sexEntry Sex_DE:EntryName ?sex .
+
+    ?_0_indicItem Item_Medication:PatientID ?patient .
+    ?_0_indicItem Item_Medication:EntryName ?takes .
+    ?_0_indicMed Medication:ItemID ?_0_indicItem .
+    ?_0_indicMed Medication:MedDictDE ?_0_indicDE .
+    ?_0_indicDE Medication_DE:NDC ?_0_indicNDC .
+    ?_0_indicCode NDCcodes:NDC ?_0_indicNDC .
+    ?_0_indicCode NDCcodes:ingredient 6809
+}
+""").get
+    assert(transformed === expected)
+  }
+
+
+}