--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/sparql2sparql/src/test/scala/SparqlToSparqlTest.scala Sun Oct 31 16:06:05 2010 -0400
@@ -0,0 +1,525 @@
+/* SparqlToSparqlTest: transform SPARQL to SPARQL and compare against a reference query.
+ * $Id$
+ */
+
+package org.w3.sw.sparql2sparql
+
+import org.scalatest.FunSuite
+import java.net.URI
+import org.w3.sw.sparql.{Sparql, Var}
+
+/* The SparqlToSparqlTest class transforms SPARQL queries to a relational data
+ * structure and compares them to a structure parsed from SQL.
+ */
+class SparqlToSparqlTest extends FunSuite {
+
+ val emptyPatternMap = NodePatternMap(Map[Var, NodePattern]())
+
+ test("foaf:last_name simple head") {
+ val sparqlParser = Sparql()
+ /* Query to be fired over view created by some transformation rules. */
+ val query = sparqlParser.parseAll(sparqlParser.select, """
+PREFIX foaf : <http://xmlns.com/foaf/0.1/>
+PREFIX xsd : <http://www.w3.org/2001/XMLSchema#>
+SELECT ?emp {
+?emp foaf:last_name "Smith"^^xsd:string
+}
+""").get
+
+ /* This transformation rule maps last_name to empP:lastName. */
+ val lname = sparqlParser.parseAll(sparqlParser.construct, """
+PREFIX foaf : <http://xmlns.com/foaf/0.1/>
+PREFIX empP : <http://hr.example/DB/Employee#>
+CONSTRUCT { ?who foaf:last_name ?lname }
+ WHERE { ?who empP:lastName ?lname }
+""").get
+
+ /* The query doesn't reference the graph produced by this transformation
+ * so the rule antecedent (empP:firstName) should not appear in result. */
+ val fname = sparqlParser.parseAll(sparqlParser.construct, """
+PREFIX foaf : <http://xmlns.com/foaf/0.1/>
+PREFIX empP : <http://hr.example/DB/Employee#>
+CONSTRUCT { ?who foaf:first_name ?fname }
+ WHERE { ?who empP:firstName ?fname }
+""").get
+ val transformed = SparqlToSparql(query, List(SparqlMap(fname, emptyPatternMap), SparqlMap(lname, emptyPatternMap)))
+ val expected = sparqlParser.parseAll(sparqlParser.select, """
+PREFIX empP : <http://hr.example/DB/Employee#>
+PREFIX xsd : <http://www.w3.org/2001/XMLSchema#>
+SELECT ?emp {
+?emp empP:lastName "Smith"^^xsd:string
+}
+""").get
+ assert(transformed === expected)
+ }
+
+ test("foaf:last_name conjunctive head") {
+ val sparqlParser = Sparql()
+ val query = sparqlParser.parseAll(sparqlParser.select, """
+PREFIX foaf : <http://xmlns.com/foaf/0.1/>
+PREFIX xsd : <http://www.w3.org/2001/XMLSchema#>
+SELECT ?emp {
+?emp foaf:last_name "Smith"^^xsd:string
+}
+""").get
+ val flname = sparqlParser.parseAll(sparqlParser.construct, """
+PREFIX foaf : <http://xmlns.com/foaf/0.1/>
+PREFIX empP : <http://hr.example/DB/Employee#>
+CONSTRUCT { ?who foaf:first_name ?fname .
+ ?who foaf:last_name ?lname }
+ WHERE { ?who empP:firstName ?fname .
+ ?who empP:lastName ?lname }
+""").get
+ val transformed = SparqlToSparql(query, List(SparqlMap(flname, emptyPatternMap)))
+ val expected = sparqlParser.parseAll(sparqlParser.select, """
+PREFIX empP : <http://hr.example/DB/Employee#>
+PREFIX xsd : <http://www.w3.org/2001/XMLSchema#>
+SELECT ?emp {
+?emp empP:lastName "Smith"^^xsd:string
+}
+""").get
+ assert(transformed === expected)
+ }
+
+ test("match only const in rule") {
+ val sparqlParser = Sparql()
+ val query = sparqlParser.parseAll(sparqlParser.select, """
+PREFIX foaf : <http://xmlns.com/foaf/0.1/>
+PREFIX xsd : <http://www.w3.org/2001/XMLSchema#>
+SELECT ?emp {
+?emp a foaf:Person
+}
+""").get
+ val flname = sparqlParser.parseAll(sparqlParser.construct, """
+PREFIX foaf : <http://xmlns.com/foaf/0.1/>
+PREFIX empP : <http://hr.example/DB/Employee#>
+CONSTRUCT { ?who a foaf:Person .
+ ?who foaf:first_name ?fname .
+ ?who foaf:last_name ?lname }
+ WHERE { ?who empP:firstName ?fname .
+ ?who empP:lastName ?lname }
+""").get
+ val transformed = SparqlToSparql(query, List(SparqlMap(flname, emptyPatternMap)))
+ val expected = sparqlParser.parseAll(sparqlParser.select, """
+PREFIX empP : <http://hr.example/DB/Employee#>
+PREFIX xsd : <http://www.w3.org/2001/XMLSchema#>
+SELECT ?emp {
+?emp empP:firstName ?_0_fname .
+?emp empP:lastName ?_0_lname .
+}
+""").get
+ assert(transformed == expected)
+ }
+
+ test("foaf:last_name FILTER") {
+ val sparqlParser = Sparql()
+ val query = sparqlParser.parseAll(sparqlParser.select, """
+PREFIX foaf : <http://xmlns.com/foaf/0.1/>
+PREFIX xsd : <http://www.w3.org/2001/XMLSchema#>
+SELECT ?emp {
+?emp foaf:last_name ?name
+FILTER (?name = "Smith"^^xsd:string)
+}
+""").get
+ val lname = sparqlParser.parseAll(sparqlParser.construct, """
+PREFIX foaf : <http://xmlns.com/foaf/0.1/>
+PREFIX empP : <http://hr.example/DB/Employee#>
+CONSTRUCT { ?who foaf:last_name ?lname }
+ WHERE { ?who empP:lastName ?lname }
+""").get
+ val transformed = SparqlToSparql(query, List(SparqlMap(lname, emptyPatternMap)))
+ val expected = sparqlParser.parseAll(sparqlParser.select, """
+PREFIX empP : <http://hr.example/DB/Employee#>
+PREFIX xsd : <http://www.w3.org/2001/XMLSchema#>
+SELECT ?emp {
+?emp empP:lastName ?name
+FILTER (?name = "Smith"^^xsd:string)
+}
+""").get
+ assert(transformed === expected)
+ }
+
+ /* Rule used for several examples. */
+ 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 // "
+ /* Make error output a bit more terse. */
+ 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, """
+PREFIX foaf : <http://xmlns.com/foaf/0.1/>
+PREFIX xsd : <http://www.w3.org/2001/XMLSchema#>
+SELECT ?lname {
+ ?who foaf:last_name ?lname .
+ ?who foaf:knows ?whom .
+ ?whom foaf:last_name "Smith"^^xsd:string }
+""").get
+ val transformed = SparqlToSparql(query, List(SparqlMap(Emp_TaskToFoaf, emptyPatternMap)))
+ val expected = sparqlParser.parseAll(sparqlParser.select, """
+PREFIX empP : <http://hr.example/DB/Employee#>
+PREFIX task : <http://hr.example/DB/Task#>
+PREFIX xsd : <http://www.w3.org/2001/XMLSchema#>
+SELECT ?lname
+ { ?who empP:lastName ?lname .
+ ?_0_pair task:drone ?who .
+ ?_0_pair task:manager ?whom .
+ ?whom empP:lastName "Smith"^^xsd:string }
+""").get
+ assert(transformed === expected)
+ }
+
+ test("trans head") {
+ val sparqlParser = Sparql()
+ val query = sparqlParser.parseAll(sparqlParser.select, """
+PREFIX foaf : <http://xmlns.com/foaf/0.1/>
+PREFIX xsd : <http://www.w3.org/2001/XMLSchema#>
+SELECT ?lname {
+ ?who foaf:last_name ?lname .
+ ?who foaf:knows ?whom .
+ ?whom foaf:knows ?whom2 .
+ ?whom2 foaf:last_name "Smith"^^xsd:string }
+""").get
+ SparqlToSparql.Abbreviations ++= Emp_TaskToFoaf_abbr
+
+ val transformed = SparqlToSparql(query, List(SparqlMap(Emp_TaskToFoaf, emptyPatternMap)))
+ val expected = sparqlParser.parseAll(sparqlParser.select, """
+PREFIX empP : <http://hr.example/DB/Employee#>
+PREFIX task : <http://hr.example/DB/Task#>
+PREFIX xsd : <http://www.w3.org/2001/XMLSchema#>
+SELECT ?lname
+ { ?who empP:lastName ?lname .
+ ?_0_pair task:drone ?who .
+ ?_0_pair task:manager ?whom .
+ ?_1_pair task:drone ?whom .
+ ?_1_pair task:manager ?whom2 .
+ ?whom2 empP:lastName "Smith"^^xsd:string
+ }
+""").get
+ assert(transformed === expected)
+ }
+
+ val Emp_TaskToFoaf_split_knows = GlobalSparqlParser.parseAll(GlobalSparqlParser.construct, """
+ PREFIX mark : <tag://eric.prud.name/2010/mark>
+ PREFIX foaf : <http://xmlns.com/foaf/0.1/>
+ PREFIX task : <http://hr.example/DB/Task#>
+ CONSTRUCT { ?emp foaf:knows ?man . }
+ WHERE { ?pair task:drone ?emp .
+ ?pair task:manager ?man }
+ """).get // "
+/* ?emp mark:a mark:a .
+ ?man mark:a mark:a } */
+ val Emp_TaskToFoaf_split_empLastName = GlobalSparqlParser.parseAll(GlobalSparqlParser.construct, """
+ PREFIX mark : <tag://eric.prud.name/2010/mark>
+ PREFIX foaf : <http://xmlns.com/foaf/0.1/>
+ PREFIX empP : <http://hr.example/DB/Employee#>
+ CONSTRUCT { ?person foaf:last_name ?person_name }
+ WHERE { ?person empP:lastName ?person_name }
+ """).get // "
+ val Emp_TaskToFoaf_split_projLastName = GlobalSparqlParser.parseAll(GlobalSparqlParser.construct, """
+ PREFIX mark : <tag://eric.prud.name/2010/mark>
+ PREFIX foaf : <http://xmlns.com/foaf/0.1/>
+ PREFIX projP : <http://hr.example/DB/Project#>
+ CONSTRUCT { ?project foaf:last_name ?proj_name }
+ WHERE { ?project projP:lastName ?proj_name }
+ """).get // "
+
+ test("markers0") {
+ val sparqlParser = Sparql()
+ val query = sparqlParser.parseAll(sparqlParser.select, """
+PREFIX foaf : <http://xmlns.com/foaf/0.1/>
+PREFIX xsd : <http://www.w3.org/2001/XMLSchema#>
+SELECT ?lname {
+ ?who foaf:last_name ?lname .
+ ?who foaf:knows ?whom .
+ ?whom foaf:knows ?whom2 .
+ ?whom2 foaf:last_name "Smith"^^xsd:string }
+""").get
+ SparqlToSparql.Abbreviations ++= Emp_TaskToFoaf_abbr
+
+ val transformed = SparqlToSparql(query, List(SparqlMap(Emp_TaskToFoaf_split_knows, emptyPatternMap)
+// , SparqlMap(Emp_TaskToFoaf_split_projLastName, emptyPatternMap)
+ , SparqlMap(Emp_TaskToFoaf_split_empLastName, emptyPatternMap)
+ ))
+
+ val expected = sparqlParser.parseAll(sparqlParser.select, """
+PREFIX empP : <http://hr.example/DB/Employee#>
+PREFIX task : <http://hr.example/DB/Task#>
+PREFIX xsd : <http://www.w3.org/2001/XMLSchema#>
+SELECT ?lname
+ { ?who empP:lastName ?lname .
+ ?_2_pair task:drone ?who .
+ ?_2_pair task:manager ?whom .
+ ?_3_pair task:drone ?whom .
+ ?_3_pair task:manager ?whom2 .
+ ?whom2 empP:lastName "Smith"^^xsd:string
+ }
+""").get
+ assert(expected === transformed)
+ }
+
+ test("reaches0") {
+ val graph = Set[(String,String)](
+ ("a", "b"),
+ ("c", "a"),
+ ("b", "d"),
+ ("c", "e"),
+ ("e", "a"),
+ ("e", "f"))
+ val r0 = new org.w3.sw.util.GraphAnalyzer[String](Map[String, Set[String]]())
+ val r = graph.foldLeft(r0)((r, pair) => r.pair(pair._1, pair._2))
+ val useful = graph.foldLeft(Set[(String,String)]())((s, pair) => {
+ if (r.neededFor(Set("a", "f"), pair._1, Set(pair._2)) &&
+ r.neededFor(Set("a", "f"), pair._2, Set(pair._1))) s + pair
+ else s
+ })
+ val expected = Set[(String,String)](
+ ("e","f"), ("c","a"), ("e","a"), ("c","e"))
+ assert(expected === 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(SparqlMap(rule1, emptyPatternMap)))
+ 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(SparqlMap(rule1, emptyPatternMap)))
+ 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(SparqlMap(rule1, emptyPatternMap)))
+ 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)
+ }
+
+
+}
--- a/src/test/scala/SparqlToSparqlTest.scala Sun Oct 31 16:01:55 2010 -0400
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,526 +0,0 @@
-/* SparqlToSparqlTest: transform SPARQL to SPARQL and compare against a reference query.
- * $Id$
- */
-
-package w3c.sw
-
-import org.scalatest.FunSuite
-import java.net.URI
-import w3c.sw.sparql.Sparql
-import w3c.sw.sparql2sparql.{SparqlToSparql,NodePatternMap,NodePattern,SparqlMap}
-
-/* The SparqlToSparqlTest class transforms SPARQL queries to a relational data
- * structure and compares them to a structure parsed from SQL.
- */
-class SparqlToSparqlTest extends FunSuite {
-
- val emptyPatternMap = NodePatternMap(Map[sparql.Var, NodePattern]())
-
- test("foaf:last_name simple head") {
- val sparqlParser = Sparql()
- /* Query to be fired over view created by some transformation rules. */
- val query = sparqlParser.parseAll(sparqlParser.select, """
-PREFIX foaf : <http://xmlns.com/foaf/0.1/>
-PREFIX xsd : <http://www.w3.org/2001/XMLSchema#>
-SELECT ?emp {
-?emp foaf:last_name "Smith"^^xsd:string
-}
-""").get
-
- /* This transformation rule maps last_name to empP:lastName. */
- val lname = sparqlParser.parseAll(sparqlParser.construct, """
-PREFIX foaf : <http://xmlns.com/foaf/0.1/>
-PREFIX empP : <http://hr.example/DB/Employee#>
-CONSTRUCT { ?who foaf:last_name ?lname }
- WHERE { ?who empP:lastName ?lname }
-""").get
-
- /* The query doesn't reference the graph produced by this transformation
- * so the rule antecedent (empP:firstName) should not appear in result. */
- val fname = sparqlParser.parseAll(sparqlParser.construct, """
-PREFIX foaf : <http://xmlns.com/foaf/0.1/>
-PREFIX empP : <http://hr.example/DB/Employee#>
-CONSTRUCT { ?who foaf:first_name ?fname }
- WHERE { ?who empP:firstName ?fname }
-""").get
- val transformed = SparqlToSparql(query, List(SparqlMap(fname, emptyPatternMap), SparqlMap(lname, emptyPatternMap)))
- val expected = sparqlParser.parseAll(sparqlParser.select, """
-PREFIX empP : <http://hr.example/DB/Employee#>
-PREFIX xsd : <http://www.w3.org/2001/XMLSchema#>
-SELECT ?emp {
-?emp empP:lastName "Smith"^^xsd:string
-}
-""").get
- assert(transformed === expected)
- }
-
- test("foaf:last_name conjunctive head") {
- val sparqlParser = Sparql()
- val query = sparqlParser.parseAll(sparqlParser.select, """
-PREFIX foaf : <http://xmlns.com/foaf/0.1/>
-PREFIX xsd : <http://www.w3.org/2001/XMLSchema#>
-SELECT ?emp {
-?emp foaf:last_name "Smith"^^xsd:string
-}
-""").get
- val flname = sparqlParser.parseAll(sparqlParser.construct, """
-PREFIX foaf : <http://xmlns.com/foaf/0.1/>
-PREFIX empP : <http://hr.example/DB/Employee#>
-CONSTRUCT { ?who foaf:first_name ?fname .
- ?who foaf:last_name ?lname }
- WHERE { ?who empP:firstName ?fname .
- ?who empP:lastName ?lname }
-""").get
- val transformed = SparqlToSparql(query, List(SparqlMap(flname, emptyPatternMap)))
- val expected = sparqlParser.parseAll(sparqlParser.select, """
-PREFIX empP : <http://hr.example/DB/Employee#>
-PREFIX xsd : <http://www.w3.org/2001/XMLSchema#>
-SELECT ?emp {
-?emp empP:lastName "Smith"^^xsd:string
-}
-""").get
- assert(transformed === expected)
- }
-
- test("match only const in rule") {
- val sparqlParser = Sparql()
- val query = sparqlParser.parseAll(sparqlParser.select, """
-PREFIX foaf : <http://xmlns.com/foaf/0.1/>
-PREFIX xsd : <http://www.w3.org/2001/XMLSchema#>
-SELECT ?emp {
-?emp a foaf:Person
-}
-""").get
- val flname = sparqlParser.parseAll(sparqlParser.construct, """
-PREFIX foaf : <http://xmlns.com/foaf/0.1/>
-PREFIX empP : <http://hr.example/DB/Employee#>
-CONSTRUCT { ?who a foaf:Person .
- ?who foaf:first_name ?fname .
- ?who foaf:last_name ?lname }
- WHERE { ?who empP:firstName ?fname .
- ?who empP:lastName ?lname }
-""").get
- val transformed = SparqlToSparql(query, List(SparqlMap(flname, emptyPatternMap)))
- val expected = sparqlParser.parseAll(sparqlParser.select, """
-PREFIX empP : <http://hr.example/DB/Employee#>
-PREFIX xsd : <http://www.w3.org/2001/XMLSchema#>
-SELECT ?emp {
-?emp empP:firstName ?_0_fname .
-?emp empP:lastName ?_0_lname .
-}
-""").get
- assert(transformed == expected)
- }
-
- test("foaf:last_name FILTER") {
- val sparqlParser = Sparql()
- val query = sparqlParser.parseAll(sparqlParser.select, """
-PREFIX foaf : <http://xmlns.com/foaf/0.1/>
-PREFIX xsd : <http://www.w3.org/2001/XMLSchema#>
-SELECT ?emp {
-?emp foaf:last_name ?name
-FILTER (?name = "Smith"^^xsd:string)
-}
-""").get
- val lname = sparqlParser.parseAll(sparqlParser.construct, """
-PREFIX foaf : <http://xmlns.com/foaf/0.1/>
-PREFIX empP : <http://hr.example/DB/Employee#>
-CONSTRUCT { ?who foaf:last_name ?lname }
- WHERE { ?who empP:lastName ?lname }
-""").get
- val transformed = SparqlToSparql(query, List(SparqlMap(lname, emptyPatternMap)))
- val expected = sparqlParser.parseAll(sparqlParser.select, """
-PREFIX empP : <http://hr.example/DB/Employee#>
-PREFIX xsd : <http://www.w3.org/2001/XMLSchema#>
-SELECT ?emp {
-?emp empP:lastName ?name
-FILTER (?name = "Smith"^^xsd:string)
-}
-""").get
- assert(transformed === expected)
- }
-
- /* Rule used for several examples. */
- 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 // "
- /* Make error output a bit more terse. */
- 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, """
-PREFIX foaf : <http://xmlns.com/foaf/0.1/>
-PREFIX xsd : <http://www.w3.org/2001/XMLSchema#>
-SELECT ?lname {
- ?who foaf:last_name ?lname .
- ?who foaf:knows ?whom .
- ?whom foaf:last_name "Smith"^^xsd:string }
-""").get
- val transformed = SparqlToSparql(query, List(SparqlMap(Emp_TaskToFoaf, emptyPatternMap)))
- val expected = sparqlParser.parseAll(sparqlParser.select, """
-PREFIX empP : <http://hr.example/DB/Employee#>
-PREFIX task : <http://hr.example/DB/Task#>
-PREFIX xsd : <http://www.w3.org/2001/XMLSchema#>
-SELECT ?lname
- { ?who empP:lastName ?lname .
- ?_0_pair task:drone ?who .
- ?_0_pair task:manager ?whom .
- ?whom empP:lastName "Smith"^^xsd:string }
-""").get
- assert(transformed === expected)
- }
-
- test("trans head") {
- val sparqlParser = Sparql()
- val query = sparqlParser.parseAll(sparqlParser.select, """
-PREFIX foaf : <http://xmlns.com/foaf/0.1/>
-PREFIX xsd : <http://www.w3.org/2001/XMLSchema#>
-SELECT ?lname {
- ?who foaf:last_name ?lname .
- ?who foaf:knows ?whom .
- ?whom foaf:knows ?whom2 .
- ?whom2 foaf:last_name "Smith"^^xsd:string }
-""").get
- SparqlToSparql.Abbreviations ++= Emp_TaskToFoaf_abbr
-
- val transformed = SparqlToSparql(query, List(SparqlMap(Emp_TaskToFoaf, emptyPatternMap)))
- val expected = sparqlParser.parseAll(sparqlParser.select, """
-PREFIX empP : <http://hr.example/DB/Employee#>
-PREFIX task : <http://hr.example/DB/Task#>
-PREFIX xsd : <http://www.w3.org/2001/XMLSchema#>
-SELECT ?lname
- { ?who empP:lastName ?lname .
- ?_0_pair task:drone ?who .
- ?_0_pair task:manager ?whom .
- ?_1_pair task:drone ?whom .
- ?_1_pair task:manager ?whom2 .
- ?whom2 empP:lastName "Smith"^^xsd:string
- }
-""").get
- assert(transformed === expected)
- }
-
- val Emp_TaskToFoaf_split_knows = GlobalSparqlParser.parseAll(GlobalSparqlParser.construct, """
- PREFIX mark : <tag://eric.prud.name/2010/mark>
- PREFIX foaf : <http://xmlns.com/foaf/0.1/>
- PREFIX task : <http://hr.example/DB/Task#>
- CONSTRUCT { ?emp foaf:knows ?man . }
- WHERE { ?pair task:drone ?emp .
- ?pair task:manager ?man }
- """).get // "
-/* ?emp mark:a mark:a .
- ?man mark:a mark:a } */
- val Emp_TaskToFoaf_split_empLastName = GlobalSparqlParser.parseAll(GlobalSparqlParser.construct, """
- PREFIX mark : <tag://eric.prud.name/2010/mark>
- PREFIX foaf : <http://xmlns.com/foaf/0.1/>
- PREFIX empP : <http://hr.example/DB/Employee#>
- CONSTRUCT { ?person foaf:last_name ?person_name }
- WHERE { ?person empP:lastName ?person_name }
- """).get // "
- val Emp_TaskToFoaf_split_projLastName = GlobalSparqlParser.parseAll(GlobalSparqlParser.construct, """
- PREFIX mark : <tag://eric.prud.name/2010/mark>
- PREFIX foaf : <http://xmlns.com/foaf/0.1/>
- PREFIX projP : <http://hr.example/DB/Project#>
- CONSTRUCT { ?project foaf:last_name ?proj_name }
- WHERE { ?project projP:lastName ?proj_name }
- """).get // "
-
- test("markers0") {
- val sparqlParser = Sparql()
- val query = sparqlParser.parseAll(sparqlParser.select, """
-PREFIX foaf : <http://xmlns.com/foaf/0.1/>
-PREFIX xsd : <http://www.w3.org/2001/XMLSchema#>
-SELECT ?lname {
- ?who foaf:last_name ?lname .
- ?who foaf:knows ?whom .
- ?whom foaf:knows ?whom2 .
- ?whom2 foaf:last_name "Smith"^^xsd:string }
-""").get
- SparqlToSparql.Abbreviations ++= Emp_TaskToFoaf_abbr
-
- val transformed = SparqlToSparql(query, List(SparqlMap(Emp_TaskToFoaf_split_knows, emptyPatternMap)
-// , SparqlMap(Emp_TaskToFoaf_split_projLastName, emptyPatternMap)
- , SparqlMap(Emp_TaskToFoaf_split_empLastName, emptyPatternMap)
- ))
-
- val expected = sparqlParser.parseAll(sparqlParser.select, """
-PREFIX empP : <http://hr.example/DB/Employee#>
-PREFIX task : <http://hr.example/DB/Task#>
-PREFIX xsd : <http://www.w3.org/2001/XMLSchema#>
-SELECT ?lname
- { ?who empP:lastName ?lname .
- ?_2_pair task:drone ?who .
- ?_2_pair task:manager ?whom .
- ?_3_pair task:drone ?whom .
- ?_3_pair task:manager ?whom2 .
- ?whom2 empP:lastName "Smith"^^xsd:string
- }
-""").get
- assert(expected === transformed)
- }
-
- test("reaches0") {
- val graph = Set[(String,String)](
- ("a", "b"),
- ("c", "a"),
- ("b", "d"),
- ("c", "e"),
- ("e", "a"),
- ("e", "f"))
- val r0 = new w3c.sw.util.GraphAnalyzer[String](Map[String, Set[String]]())
- val r = graph.foldLeft(r0)((r, pair) => r.pair(pair._1, pair._2))
- val useful = graph.foldLeft(Set[(String,String)]())((s, pair) => {
- if (r.neededFor(Set("a", "f"), pair._1, Set(pair._2)) &&
- r.neededFor(Set("a", "f"), pair._2, Set(pair._1))) s + pair
- else s
- })
- val expected = Set[(String,String)](
- ("e","f"), ("c","a"), ("e","a"), ("c","e"))
- assert(expected === 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(SparqlMap(rule1, emptyPatternMap)))
- 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(SparqlMap(rule1, emptyPatternMap)))
- 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(SparqlMap(rule1, emptyPatternMap)))
- 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)
- }
-
-
-}