Commit 26a729b8 authored by Georg Fette's avatar Georg Fette
Browse files

fixed bugs

added test cases
parent c8c977be
......@@ -1012,7 +1012,7 @@ public class CQL_2_Graph_Mapper {
String timePrecision = pluralDateTimePrecision.getText();
currentOp.addParameter(timePrecision);
} else {
String unitString = unit.STRING().getText();
String unitString = unit.STRING().getText().replaceAll("'", "");
currentOp.addParameter(unitString);
}
currentOp = lastOp;
......
......@@ -31,19 +31,19 @@ public class Graph_2_CQL_Mapper extends Graph_2_System_Mapper {
private boolean nextPrimitiveIsValueSet;
@SuppressWarnings("unchecked")
private Set<OperatorType> infixOps = new HashSet<OperatorType>(
Arrays.asList(new OperatorType[] { OperatorType.MORE, OperatorType.MORE_OR_EQUAL,
OperatorType.LESS, OperatorType.LESS_OR_EQUAL, OperatorType.AND, OperatorType.EQUALS,
OperatorType.AFTER, OperatorType.ADD, OperatorType.SUBTRACT, OperatorType.MULTIPLY,
OperatorType.DIVIDE, OperatorType.POWER, OperatorType.MODULO, OperatorType.TRUNCATED_DIVIDE }));
private Set<OperatorType> infixOps = new HashSet<OperatorType>(Arrays.asList(
new OperatorType[] { OperatorType.MORE, OperatorType.MORE_OR_EQUAL, OperatorType.LESS,
OperatorType.LESS_OR_EQUAL, OperatorType.AND, OperatorType.EQUALS, OperatorType.AFTER,
OperatorType.ADD, OperatorType.SUBTRACT, OperatorType.MULTIPLY, OperatorType.DIVIDE,
OperatorType.POWER, OperatorType.MODULO, OperatorType.TRUNCATED_DIVIDE }));
@SuppressWarnings("unchecked")
private Set<OperatorType> prefixOps = new HashSet<OperatorType>(Arrays.asList(new OperatorType[] {
OperatorType.FIRST, OperatorType.LAST, OperatorType.NOT, OperatorType.ABS,
OperatorType.ALLTRUE, OperatorType.ANYTRUE, OperatorType.AVG, OperatorType.COUNT,
OperatorType.MAX, OperatorType.MIN, OperatorType.MEDIAN, OperatorType.CEILING,
OperatorType.EXP, OperatorType.FLOOR, OperatorType.LN, OperatorType.LOG, OperatorType.ROUND,
OperatorType.TRUNCATE }));
private Set<OperatorType> prefixOps = new HashSet<OperatorType>(Arrays
.asList(new OperatorType[] { OperatorType.FIRST, OperatorType.LAST, OperatorType.NOT,
OperatorType.ABS, OperatorType.ALLTRUE, OperatorType.ANYTRUE, OperatorType.AVG,
OperatorType.COUNT, OperatorType.MAX, OperatorType.MIN, OperatorType.MEDIAN,
OperatorType.CEILING, OperatorType.EXP, OperatorType.FLOOR, OperatorType.LN,
OperatorType.LOG, OperatorType.ROUND, OperatorType.TRUNCATE, OperatorType.EXISTS }));
public Graph_2_CQL_Mapper() {
super(system);
......@@ -462,10 +462,13 @@ public class Graph_2_CQL_Mapper extends Graph_2_System_Mapper {
String string = writeSomethingSorted(anOp);
builder.append(string);
} else if (anOp.type == OperatorType.QUANTITY) {
builder.append(anOp.getParameters().get(0).asPrimitive().value + " "
+ anOp.getParameters().get(1).asPrimitive().value);
// } else if ((anOp.type == OperatorType.LAST) || (anOp.type == OperatorType.FIRST)) {
// writeFirstLast(builder, opString, anOp);
builder.append(anOp.getParameters().get(0).asPrimitive().value + " ");
String unitString = anOp.getParameters().get(1).asPrimitive().value;
if (unitString.matches("days|months|years|seconds|minutes")) {
builder.append(unitString);
} else {
builder.append("'" + unitString + "'");
}
} else if (anOp.type == OperatorType.OPERATOR_RESULT_PROPERTY) {
String frstExp = writeExpr(anOp.getParameters().get(0));
String sndExp = anOp.getParameters().get(1).asConcept().code;
......
......@@ -296,15 +296,59 @@ public class Graph_2_AQL_Mapper extends Graph_2_System_Mapper {
return result;
}
private String writeInfixOp(QueryOperator anOp) {
String result = "";
String aqlOpString = opMap.graph_2_system_operatorMap.get(anOp.type);
boolean first = true;
for (Expression aParam : anOp.getParameters()) {
if (!first) {
result += " " + aqlOpString + " ";
}
first = false;
String value = getWhereParamPart(aParam);
result += value;
}
return result;
}
private String writePrefixOp(QueryOperator anOp) {
String result = "";
String aqlOpString = opMap.graph_2_system_operatorMap.get(anOp.type);
result += aqlOpString;
for (Expression aParam : anOp.getParameters()) {
String paramValue = getWhereParamPart(aParam);
result += " " + paramValue;
}
return result;
}
private String getWhereParamPart(QueryOperator anOp) {
StringBuilder builder = new StringBuilder();
String aqlOpString = opMap.graph_2_system_operatorMap.get(anOp.type);
if (anOp.isBooleanOp()) {
String opPart = getWhereOpPart(anOp);
builder.append(opPart);
} else if ((anOp.type == OperatorType.EXISTS) && anOp.getParameters().get(0).isConcept()
&& (anOp.source.type != OperatorType.NOT)) {
// do nothing, as this is already handled in the FROM part
} else if (anOp.type == OperatorType.EXISTS) {
Expression param = anOp.getParameters().get(0);
if (param.isConcept()) {
if (
// otherwise this is semantically already included in the SELECT
!param.includeInResult &&
// otherwise this is semantically already included in the CONTAINS
!aliases.containsKey(param)) {
// this is the semantic equivalent for "not NULL" checks
String value = writePrefixOp(anOp);
builder.append(value);
}
} else {
QueryOperator query = param.asOperator();
if (query.getParameters().size() > 1) {
QueryOperator where = query.getParameters().get(1).asOperator();
String value = getWhereParamPart(where);
builder.append(value);
} else {
// do nothing, as this is already handled in the FROM part
}
}
} else if (anOp.type == OperatorType.MATCHES) {
String matchesPart = getMatchesPart(anOp);
builder.append(matchesPart);
......@@ -312,21 +356,11 @@ public class Graph_2_AQL_Mapper extends Graph_2_System_Mapper {
String intervalPart = getIntervalPart(anOp);
builder.append(intervalPart);
} else if (prefixOps.contains(anOp.type)) {
builder.append(aqlOpString);
for (Expression aParam : anOp.getParameters()) {
String paramValue = getWhereParamPart(aParam);
builder.append(" " + paramValue);
}
String value = writePrefixOp(anOp);
builder.append(value);
} else if (infixOps.contains(anOp.type)) {
boolean first = true;
for (Expression aParam : anOp.getParameters()) {
if (!first) {
builder.append(" " + aqlOpString + " ");
}
first = false;
String value = getWhereParamPart(aParam);
builder.append(value);
}
String value = writeInfixOp(anOp);
builder.append(value);
} else {
throw new RuntimeException("unexpected");
}
......
......@@ -5,6 +5,7 @@ import java.io.IOException;
import de.uniwue.query.CQL.CQL_2_Graph_Mapper;
import de.uniwue.query.openEHR.Graph_2_AQL_Mapper;
import de.uniwue.query.transform.Equality_2_Meronym_Transformation;
import de.uniwue.query.transform.QuantityExpander;
import de.uniwue.query.transform.QueryGraphTransformer;
public class CQL_2_AQL_QueryGraphTransformer extends QueryGraphTransformer {
......@@ -17,6 +18,7 @@ public class CQL_2_AQL_QueryGraphTransformer extends QueryGraphTransformer {
protected void initialize() throws IOException {
super.initialize();
Equality_2_Meronym_Transformation.instantiateEquality_2_Meronym_Transformation(transformations);
transformations.add(new QuantityExpander());
}
}
......@@ -3,4 +3,5 @@ EHR Patient
COMPOSITION[openEHR-EHR-COMPOSITION.encounter.v1] Encounter
length2 length
COMPOSITION[openEHR-EHR-COMPOSITION.lab_result.v1] Observation
content[openEHR-EHR-ADMIN_ENTRY.discharge.v1] discharge
\ No newline at end of file
content[openEHR-EHR-ADMIN_ENTRY.discharge.v1] discharge
COMPOSITION[openEHR-EHR-COMPOSITION.HumanName.v1] name
\ No newline at end of file
......@@ -3,4 +3,5 @@ Patient EHR
Encounter COMPOSITION[openEHR-EHR-COMPOSITION.encounter.v1]
Observation COMPOSITION[openEHR-EHR-COMPOSITION.lab_result.v1]
length length2
discharge content[openEHR-EHR-ADMIN_ENTRY.discharge.v1]
\ No newline at end of file
discharge content[openEHR-EHR-ADMIN_ENTRY.discharge.v1]
name COMPOSITION[openEHR-EHR-COMPOSITION.HumanName.v1]
\ No newline at end of file
......@@ -3,7 +3,6 @@ package de.uniwue.query;
import java.io.IOException;
import java.text.ParseException;
import org.junit.Assert;
import org.junit.Test;
import de.uniwue.query.openEHR.AQL_2_Graph_Mapper;
......@@ -50,7 +49,9 @@ public class Test_AQL_2_AQL {
String originalQuery = aMultiQuery.system2QueryString.get(AQL_2_Graph_Mapper.getSystem());
GraphQuery query = aql2Graph.parse(originalQuery);
String result = graph2AQL.visit(query);
Assert.assertEquals(originalQuery, result);
if (!originalQuery.equals(result)) {
throw new RuntimeException("query strings not equal");
}
}
@Test
......
......@@ -7,7 +7,6 @@ import javax.xml.parsers.ParserConfigurationException;
import javax.xml.stream.XMLStreamException;
import org.jdom.JDOMException;
import org.junit.Assert;
import org.junit.Test;
import org.xml.sax.SAXException;
......@@ -57,7 +56,9 @@ public class Test_AQL_2_i2b2_2_AQL {
String aql_query_string2 = graph_2_aql_mapper.visit(query2);
Assert.assertEquals(original_aql_query_string, aql_query_string2);
if (!original_aql_query_string.equals(aql_query_string2)) {
throw new RuntimeException("query strings not equal");
}
}
private void initialize() throws XMLStreamException, IOException {
......
......@@ -7,7 +7,6 @@ import javax.xml.parsers.ParserConfigurationException;
import javax.xml.stream.XMLStreamException;
import org.jdom.JDOMException;
import org.junit.Assert;
import org.junit.Test;
import org.xml.sax.SAXException;
......@@ -38,7 +37,7 @@ public class Test_CQL_2_AQL {
QueryManager queryManager = new QueryManager();
initialize();
for (MultiQuery aMultiQuery : queryManager.queries) {
if (aMultiQuery.name.equals("query_CQL_2_AQL_nameCheck")) {
if (aMultiQuery.name.equals("query_CQL_2_AQL_Calcium_quant_constraint2")) {
processQueryCQL_2_AQL(aMultiQuery);
processQueryAQL_2_CQL(aMultiQuery);
}
......@@ -53,7 +52,9 @@ public class Test_CQL_2_AQL {
String resultAql_query_string = graph_2_aql_mapper.visit(queryGraph);
String desiredAql_query_string = aMultiQuery.system2QueryString
.get(AQL_2_Graph_Mapper.getSystem());
Assert.assertEquals(resultAql_query_string, desiredAql_query_string);
if (!desiredAql_query_string.equals(resultAql_query_string)) {
throw new RuntimeException("query strings not equal");
}
}
private void processQueryAQL_2_CQL(MultiQuery aMultiQuery) throws ParseException, JDOMException,
......@@ -69,7 +70,9 @@ public class Test_CQL_2_AQL {
// differnt syntax but with the same semantics
cql_query_string = aMultiQuery.system2QueryString.get(CQL_2_Graph_Mapper.getSystem());
}
Assert.assertEquals(cql_query_string, result_cql_query_string);
if (!cql_query_string.equals(result_cql_query_string)) {
throw new RuntimeException("query strings not equal");
}
}
private void initialize() throws XMLStreamException, IOException {
......@@ -84,8 +87,8 @@ public class Test_CQL_2_AQL {
public static void main(String[] args) throws IOException, XMLStreamException, ParseException,
JDOMException, ParserConfigurationException, SAXException {
Test_CQL_2_AQL test = new Test_CQL_2_AQL();
test.debugTest();
// test.testAQL_2_i2b2_2_AQL();
// test.debugTest();
test.testAQL_2_i2b2_2_AQL();
}
@Test
......
......@@ -26,7 +26,7 @@ public class Test_CQL_2_CQL {
initialize();
QueryManager queryManager = new QueryManager();
for (MultiQuery aMultiQuery : queryManager.queries) {
if (aMultiQuery.name.equals("query_CQL_doubleCount")) {
if (aMultiQuery.name.equals("query_CQL_name_exists_given")) {
processQuery(aMultiQuery);
return;
}
......@@ -42,8 +42,8 @@ public class Test_CQL_2_CQL {
public static void main(String[] args) throws IOException, ParseException {
Test_CQL_2_CQL test = new Test_CQL_2_CQL();
// test.parseQueries();
// test.debugTest();
test.testCQL_2_CQL();
// test.debugTest();
test.testCQL_2_CQL();
}
private void processQuery(MultiQuery aMultiQuery) throws IOException {
......
......@@ -7,7 +7,6 @@ import javax.xml.parsers.ParserConfigurationException;
import javax.xml.stream.XMLStreamException;
import org.jdom.JDOMException;
import org.junit.Assert;
import org.junit.Test;
import org.xml.sax.SAXException;
......@@ -44,7 +43,9 @@ public class Test_CQL_2_Cypher {
String cypher_query_string = graph_2_cypher_mapper.visit(query);
String desired_cypher_query_string = aMultiQuery.system2QueryString
.get(Graph_2_Cypher_Mapper.getSystem());
Assert.assertEquals(desired_cypher_query_string, cypher_query_string);
if (!desired_cypher_query_string.equals(cypher_query_string)) {
throw new RuntimeException("query strings not equal");
}
}
private void dispose() {
......
......@@ -7,7 +7,6 @@ import javax.xml.parsers.ParserConfigurationException;
import javax.xml.stream.XMLStreamException;
import org.jdom.JDOMException;
import org.junit.Assert;
import org.junit.Test;
import org.xml.sax.SAXException;
......@@ -28,7 +27,9 @@ public class Test_i2B2_2_i2b2 {
GraphQuery query = visitor.parse(originalQuery);
Graph_2_I2B2_Mapper abs2i2b2 = new Graph_2_I2B2_Mapper();
String result = abs2i2b2.visit(query);
Assert.assertEquals(originalQuery, result);
if (!originalQuery.equals(result)) {
throw new RuntimeException("query strings not equal");
}
}
}
}
......
*CQL
[Patient] A with [Observation] B such that B.code = 'Calcium' and B.valueQuantity.value > 10 and B.subject = A
*CQL_XML
<expression xsi:type="Query">
<source alias="A">
<expression dataType="fhir:Patient" templateId="patient-qicore-qicore-patient" xsi:type="Retrieve"/>
</source>
<relationship alias="B" xsi:type="With">
<expression dataType="fhir:Observation" templateId="observation-qicore-qicore-observation" xsi:type="Retrieve"/>
<suchThat xsi:type="And">
<operand xsi:type="And">
<operand xsi:type="Equal">
<operand path="code" scope="B" xsi:type="Property"/>
<operand xsi:type="ToConcept">
<operand name="Calcium" xsi:type="ValueSetRef"/>
</operand>
</operand>
<operand xsi:type="Greater">
<operand path="value" xsi:type="Property">
<source path="valueQuantity" scope="B" xsi:type="Property"/>
</operand>
<operand xsi:type="ToDecimal">
<operand valueType="t:Integer" value="10" xsi:type="Literal"/>
</operand>
</operand>
</operand>
<operand xsi:type="Equal">
<operand path="subject" scope="B" xsi:type="Property"/>
<operand name="A" xsi:type="AliasRef"/>
</operand>
</suchThat>
</relationship>
</expression>
*AQL
SELECT e
FROM EHR e
......
*CQL
[Patient] A with [Observation] B such that B.code = 'Calcium' and B.valueQuantity > 10'mg' and B.subject = A
*CQL2
[Patient] A with [Observation] B such that B.code = 'Calcium' and B.valueQuantity.value > 10 and B.valueQuantity.unit = 'mg' and B.subject = A
*AQL
SELECT e
FROM EHR e
CONTAINS COMPOSITION a[openEHR-EHR-COMPOSITION.lab_result.v1]
WHERE a/code = 'Calcium'
AND a/valueQuantity/value > 10
AND a/valueQuantity/unit = 'mg'
\ No newline at end of file
*CQL
[Patient] A where exists(A.name B where B.given = 'John')
*CQL2
[Patient] A where A.name.given = 'John'
*AQL
SELECT e
FROM EHR e
CONTAINS COMPOSITION a[openEHR-EHR-COMPOSITION.HumanName.v1]
WHERE a/given = 'John'
......@@ -5,4 +5,4 @@ FROM EHR e
WHERE NOT EXISTS a/content[openEHR-EHR-ADMIN_ENTRY.discharge.v1]
*CQL
[Patient] A with [Encounter] B such that not(exists(B.discharge)) and B.subject = A
\ No newline at end of file
[Patient] A with [Encounter] B such that not(exists(B.discharge)) and B.subject = A
*CQL
[Patient] A where A.active = true and A.gender = 'male'
*CQL2
[Patient] A where A.active = true and A.gender = 'male'
*CQL
[Patient] A where exists(A.name B where B.given = 'John')
*CQL2
[Patient] A where exists(A.name B where B.given = 'John')
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment