use of org.cqframework.cql.cql2elm.CqlTranslator in project clinical_quality_language by cqframework.
the class BaseTest method testChoiceDateRangeOptimization.
@Test
public void testChoiceDateRangeOptimization() throws IOException {
CqlTranslator translator = TestUtils.runSemanticTest("fhir/r4/TestChoiceDateRangeOptimization.cql", 0, CqlTranslatorOptions.Options.EnableDateRangeOptimization);
Library library = translator.toELM();
Map<String, ExpressionDef> defs = new HashMap<>();
if (library.getStatements() != null) {
for (ExpressionDef def : library.getStatements().getDef()) {
defs.put(def.getName(), def);
}
}
/*
<expression localId="25" locator="10:23-10:81" xsi:type="Query">
<resultTypeSpecifier xsi:type="ListTypeSpecifier">
<elementType name="fhir:Condition" xsi:type="NamedTypeSpecifier"/>
</resultTypeSpecifier>
<source localId="20" locator="10:23-10:35" alias="C">
<resultTypeSpecifier xsi:type="ListTypeSpecifier">
<elementType name="fhir:Condition" xsi:type="NamedTypeSpecifier"/>
</resultTypeSpecifier>
<expression localId="19" locator="10:23-10:33" dataType="fhir:Condition" dateProperty="recordedDate" xsi:type="Retrieve">
<resultTypeSpecifier xsi:type="ListTypeSpecifier">
<elementType name="fhir:Condition" xsi:type="NamedTypeSpecifier"/>
</resultTypeSpecifier>
<dateRange localId="23" locator="10:65-10:81" name="MeasurementPeriod" xsi:type="ParameterRef">
<resultTypeSpecifier xsi:type="IntervalTypeSpecifier">
<pointType name="t:DateTime" xsi:type="NamedTypeSpecifier"/>
</resultTypeSpecifier>
</dateRange>
</expression>
</source>
</expression>
*/
ExpressionDef expressionDef = defs.get("DateCondition");
assertThat(expressionDef.getExpression(), instanceOf(Query.class));
Query query = (Query) expressionDef.getExpression();
assertThat(query.getSource().size(), is(1));
assertThat(query.getSource().get(0).getExpression(), instanceOf(Retrieve.class));
Retrieve retrieve = (Retrieve) query.getSource().get(0).getExpression();
assertThat(retrieve.getDateProperty(), is("recordedDate"));
assertThat(retrieve.getDateRange(), instanceOf(ParameterRef.class));
/*
<expression localId="35" locator="11:35-11:101" xsi:type="Query">
<resultTypeSpecifier xsi:type="ListTypeSpecifier">
<elementType name="fhir:Condition" xsi:type="NamedTypeSpecifier"/>
</resultTypeSpecifier>
<source localId="28" locator="11:35-11:47" alias="C">
<resultTypeSpecifier xsi:type="ListTypeSpecifier">
<elementType name="fhir:Condition" xsi:type="NamedTypeSpecifier"/>
</resultTypeSpecifier>
<expression localId="27" locator="11:35-11:45" dataType="fhir:Condition" dateProperty="onset" xsi:type="Retrieve">
<resultTypeSpecifier xsi:type="ListTypeSpecifier">
<elementType name="fhir:Condition" xsi:type="NamedTypeSpecifier"/>
</resultTypeSpecifier>
<dateRange localId="33" locator="11:85-11:101" name="MeasurementPeriod" xsi:type="ParameterRef">
<resultTypeSpecifier xsi:type="IntervalTypeSpecifier">
<pointType name="t:DateTime" xsi:type="NamedTypeSpecifier"/>
</resultTypeSpecifier>
</dateRange>
</expression>
</source>
</expression>
*/
expressionDef = defs.get("ChoiceTypePeriodCondition");
assertThat(expressionDef.getExpression(), instanceOf(Query.class));
query = (Query) expressionDef.getExpression();
assertThat(query.getSource().size(), is(1));
assertThat(query.getSource().get(0).getExpression(), instanceOf(Retrieve.class));
retrieve = (Retrieve) query.getSource().get(0).getExpression();
assertThat(retrieve.getDateProperty(), is("onset"));
assertThat(retrieve.getDateRange(), instanceOf(ParameterRef.class));
}
use of org.cqframework.cql.cql2elm.CqlTranslator in project clinical_quality_language by cqframework.
the class BaseTest method testFHIRNamespaces.
@Test
public void testFHIRNamespaces() throws IOException {
CqlTranslator translator = TestUtils.runSemanticTest(new NamespaceInfo("Public", "http://cql.hl7.org/public"), "fhir/r401/TestFHIRNamespaces.cql", 0);
CompiledLibrary library = translator.getTranslatedLibrary();
UsingDef usingDef = library.resolveUsingRef("FHIR");
assertThat(usingDef, notNullValue());
assertThat(usingDef.getLocalIdentifier(), is("FHIR"));
assertThat(usingDef.getUri(), is("http://hl7.org/fhir"));
assertThat(usingDef.getVersion(), is("4.0.1"));
IncludeDef includeDef = library.resolveIncludeRef("FHIRHelpers");
assertThat(includeDef, notNullValue());
assertThat(includeDef.getPath(), is("http://hl7.org/fhir/FHIRHelpers"));
assertThat(includeDef.getVersion(), is("4.0.1"));
}
use of org.cqframework.cql.cql2elm.CqlTranslator in project clinical_quality_language by cqframework.
the class BaseTest method testInclude.
@Test
public void testInclude() throws IOException {
CqlTranslator translator = TestUtils.runSemanticTest("fhir/r401/TestInclude.cql", 0);
CompiledLibrary library = translator.getTranslatedLibrary();
/*
define TestMedicationRequest1:
[MedicationRequest] MR
where MR.medication.reference.resolve().as(Medication).code ~ "aspirin 325 MG / oxycodone hydrochloride 4.84 MG Oral Tablet"
<query>
<retrieve>
<where>
<equivalent>
<functionref "ToConcept">
<property path="code">
<as "Medication">
<functionref "resolve">
<functionref "ToString">
<property path="reference">
<property path="medication" scope="MR"/>
</property>
</functionref>
</functionref>
</as>
</property>
</functionref>
<functionref "ToConcept">
<coderef/>
</functionref>
</equivalent>
</where>
</query>
*/
ExpressionDef expressionDef = library.resolveExpressionRef("TestMedicationRequest1");
assertThat(expressionDef, notNullValue());
Expression expression = expressionDef.getExpression();
assertThat(expression, instanceOf(Query.class));
assertThat(((Query) expression).getWhere(), instanceOf(Equivalent.class));
Equivalent eqv = (Equivalent) ((Query) expression).getWhere();
assertThat(eqv.getOperand().get(0), instanceOf(FunctionRef.class));
FunctionRef fr = (FunctionRef) eqv.getOperand().get(0);
assertThat(fr.getName(), equalTo("ToConcept"));
assertThat(fr.getOperand().size(), equalTo(1));
assertThat(fr.getOperand().get(0), instanceOf(Property.class));
Property p = (Property) fr.getOperand().get(0);
assertThat(p.getPath(), equalTo("code"));
assertThat(p.getSource(), instanceOf(As.class));
As as = (As) p.getSource();
assertThat(as.getAsType().getLocalPart(), equalTo("Medication"));
assertThat(as.getOperand(), instanceOf(FunctionRef.class));
fr = (FunctionRef) as.getOperand();
assertThat(fr.getName(), equalTo("resolve"));
assertThat(fr.getOperand().size(), equalTo(1));
assertThat(fr.getOperand().get(0), instanceOf(FunctionRef.class));
fr = (FunctionRef) fr.getOperand().get(0);
assertThat(fr.getName(), equalTo("ToString"));
assertThat(fr.getOperand().get(0), instanceOf(Property.class));
p = (Property) fr.getOperand().get(0);
assertThat(p.getPath(), equalTo("reference"));
assertThat(p.getSource(), instanceOf(Property.class));
p = (Property) p.getSource();
assertThat(p.getPath(), equalTo("medication"));
assertThat(p.getScope(), equalTo("MR"));
/*
define TestMedicationRequest1A:
[MedicationRequest] MR
with [Medication] M such that
MR.medication = M.reference()
and M.code ~ "aspirin 325 MG / oxycodone hydrochloride 4.84 MG Oral Tablet"
<query>
<retrieve "MedicationRequest" scope="MR"/>
<withRelationship>
<retrieve "Medication" scope="M"/>
<suchThat>
<and>
<equal>
<as type="Reference">
<property path="medication" scope="MR"/>
</as>
<functionref name="reference">
<aliasRef scope="M"/>
</functionref>
</equal>
<equivalent>
<functionref name="ToConcept">
<property path="code" scope="M"/>
</functionref>
<functionref name="ToConcept">
<coderef/>
</functionref>
</equivalent>
</and>
</suchThat>
</withRelationship>
</query>
*/
expressionDef = library.resolveExpressionRef("TestMedicationRequest1A");
assertThat(expressionDef, notNullValue());
expression = expressionDef.getExpression();
assertThat(expression, instanceOf(Query.class));
Query q = (Query) expression;
assertThat(q.getRelationship(), notNullValue());
assertThat(q.getRelationship().size(), equalTo(1));
assertThat(q.getRelationship().get(0), instanceOf(With.class));
With w = (With) q.getRelationship().get(0);
assertThat(w.getSuchThat(), notNullValue());
assertThat(w.getSuchThat(), instanceOf(And.class));
And a = (And) w.getSuchThat();
assertThat(a.getOperand(), notNullValue());
assertThat(a.getOperand().size(), equalTo(2));
assertThat(a.getOperand().get(0), instanceOf(Equal.class));
Equal eq = (Equal) a.getOperand().get(0);
assertThat(eq.getOperand(), notNullValue());
assertThat(eq.getOperand().size(), equalTo(2));
assertThat(eq.getOperand().get(0), instanceOf(As.class));
as = (As) eq.getOperand().get(0);
assertThat(as.getOperand(), instanceOf(Property.class));
p = (Property) as.getOperand();
assertThat(p.getPath(), equalTo("medication"));
assertThat(p.getScope(), equalTo("MR"));
assertThat(eq.getOperand().get(1), instanceOf(FunctionRef.class));
fr = (FunctionRef) eq.getOperand().get(1);
assertThat(fr.getName(), equalTo("reference"));
assertThat(fr.getOperand(), notNullValue());
assertThat(fr.getOperand().size(), equalTo(1));
assertThat(fr.getOperand().get(0), instanceOf(AliasRef.class));
AliasRef ar = (AliasRef) fr.getOperand().get(0);
assertThat(ar.getName(), equalTo("M"));
assertThat(a.getOperand().get(1), instanceOf(Equivalent.class));
eqv = (Equivalent) a.getOperand().get(1);
assertThat(eqv.getOperand().get(0), instanceOf(FunctionRef.class));
fr = (FunctionRef) eqv.getOperand().get(0);
assertThat(fr.getName(), equalTo("ToConcept"));
assertThat(fr.getOperand().size(), equalTo(1));
assertThat(fr.getOperand().get(0), instanceOf(Property.class));
p = (Property) fr.getOperand().get(0);
assertThat(p.getPath(), equalTo("code"));
assertThat(p.getScope(), equalTo("M"));
assertThat(eqv.getOperand().get(1), instanceOf(ToConcept.class));
/*
define TestMedicationRequest1B:
[MedicationRequest] MR
with [MR.medication -> Medication] M
such that M.code ~ "aspirin 325 MG / oxycodone hydrochloride 4.84 MG Oral Tablet"
<query>
<retrieve MedicationRequest/>
<withRelationship>
<retrieve Medication>
<context>
<property path="medication" scope="MR"/>
</context>
</retrieve>
<suchThat>
<equivalent>
<functionRef name="ToConcept">
<property path="code" scope="M"/>
</functionRef>
<functionRef name="ToConcept">
<codeRef/>
</functionRef>
</equivalent>
</suchThat>
</withRelationship>
</query>
*/
expressionDef = library.resolveExpressionRef("TestMedicationRequest1B");
assertThat(expressionDef, notNullValue());
expression = expressionDef.getExpression();
assertThat(expression, instanceOf(Query.class));
q = (Query) expression;
assertThat(q.getRelationship(), notNullValue());
assertThat(q.getRelationship().size(), equalTo(1));
assertThat(q.getRelationship().get(0), instanceOf(With.class));
w = (With) q.getRelationship().get(0);
assertThat(w.getExpression(), instanceOf(Retrieve.class));
Retrieve r = (Retrieve) w.getExpression();
assertThat(r.getContext(), instanceOf(Property.class));
p = (Property) r.getContext();
assertThat(p.getPath(), equalTo("medication"));
assertThat(p.getScope(), equalTo("MR"));
assertThat(w.getSuchThat(), notNullValue());
assertThat(w.getSuchThat(), instanceOf(Equivalent.class));
eqv = (Equivalent) w.getSuchThat();
assertThat(eqv.getOperand().get(0), instanceOf(FunctionRef.class));
fr = (FunctionRef) eqv.getOperand().get(0);
assertThat(fr.getName(), equalTo("ToConcept"));
assertThat(fr.getOperand().size(), equalTo(1));
assertThat(fr.getOperand().get(0), instanceOf(Property.class));
p = (Property) fr.getOperand().get(0);
assertThat(p.getPath(), equalTo("code"));
assertThat(p.getScope(), equalTo("M"));
assertThat(eqv.getOperand().get(1), instanceOf(ToConcept.class));
/*
define TestMedicationRequest1C:
[MedicationRequest] MR
let M: [MR.medication -> Medication]
where M.code ~ "aspirin 325 MG / oxycodone hydrochloride 4.84 MG Oral Tablet"
<query>
<retrieve MedicationRequest/>
<let alias="M">
<singletonFrom>
<retrieve Medication>
<context>
<property path="medication" source="MR"/>
</context>
</retrieve>
</singletonFrom>
</let>
<where>
<equivalent>
<functionRef name="ToConcept">
<property path="code" scope="M"/>
</functionRef>
<functionRef name="ToConcept">
<codeRef/>
</functionRef>
</equivalent>
</where>
</query>
*/
expressionDef = library.resolveExpressionRef("TestMedicationRequest1C");
assertThat(expressionDef, notNullValue());
expression = expressionDef.getExpression();
assertThat(expression, instanceOf(Query.class));
q = (Query) expression;
assertThat(q.getLet(), notNullValue());
assertThat(q.getLet().size(), equalTo(1));
LetClause lc = q.getLet().get(0);
assertThat(lc.getExpression(), instanceOf(SingletonFrom.class));
SingletonFrom sf = (SingletonFrom) lc.getExpression();
assertThat(sf.getOperand(), instanceOf(Retrieve.class));
r = (Retrieve) sf.getOperand();
assertThat(r.getContext(), instanceOf(Property.class));
p = (Property) r.getContext();
assertThat(p.getPath(), equalTo("medication"));
assertThat(p.getScope(), equalTo("MR"));
assertThat(q.getWhere(), instanceOf(Equivalent.class));
eqv = (Equivalent) q.getWhere();
assertThat(eqv.getOperand().get(0), instanceOf(FunctionRef.class));
fr = (FunctionRef) eqv.getOperand().get(0);
assertThat(fr.getName(), equalTo("ToConcept"));
assertThat(fr.getOperand().size(), equalTo(1));
assertThat(fr.getOperand().get(0), instanceOf(Property.class));
p = (Property) fr.getOperand().get(0);
assertThat(p.getPath(), equalTo("code"));
assertThat(p.getSource(), instanceOf(QueryLetRef.class));
QueryLetRef qlr = (QueryLetRef) p.getSource();
assertThat(qlr.getName(), equalTo("M"));
assertThat(eqv.getOperand().get(1), instanceOf(ToConcept.class));
}
use of org.cqframework.cql.cql2elm.CqlTranslator in project clinical_quality_language by cqframework.
the class BaseTest method testConceptConversion.
@Test
public void testConceptConversion() throws IOException {
CqlTranslator translator = TestUtils.runSemanticTest("fhir/r401/TestConceptConversion.cql", 0);
Library library = translator.toELM();
Map<String, ExpressionDef> defs = new HashMap<>();
if (library.getStatements() != null) {
for (ExpressionDef def : library.getStatements().getDef()) {
defs.put(def.getName(), def);
}
}
/*
<expression localId="18" locator="15:3-16:42" xsi:type="Query">
<resultTypeSpecifier xsi:type="ListTypeSpecifier">
<elementType name="fhir:Observation" xsi:type="NamedTypeSpecifier"/>
</resultTypeSpecifier>
<source localId="13" locator="15:3-15:17" alias="O">
<resultTypeSpecifier xsi:type="ListTypeSpecifier">
<elementType name="fhir:Observation" xsi:type="NamedTypeSpecifier"/>
</resultTypeSpecifier>
<expression localId="12" locator="15:3-15:15" dataType="fhir:Observation" xsi:type="Retrieve">
<resultTypeSpecifier xsi:type="ListTypeSpecifier">
<elementType name="fhir:Observation" xsi:type="NamedTypeSpecifier"/>
</resultTypeSpecifier>
</expression>
</source>
<where localId="17" locator="16:5-16:42" resultTypeName="t:Boolean" xsi:type="Equivalent">
<operand name="ToConcept" libraryName="FHIRHelpers" xsi:type="FunctionRef">
<operand localId="15" locator="16:11-16:16" resultTypeName="fhir:CodeableConcept" path="code" scope="O" xsi:type="Property"/>
</operand>
<operand xsi:type="ToConcept">
<operand localId="16" locator="16:20-16:42" resultTypeName="t:Code" name="ECOG performance code" xsi:type="CodeRef"/>
</operand>
</where>
</expression>
*/
ExpressionDef expressionDef = defs.get("TestCodeComparison");
assertThat(expressionDef.getExpression(), instanceOf(Query.class));
Query query = (Query) expressionDef.getExpression();
assertThat(query.getWhere(), instanceOf(Equivalent.class));
Equivalent equivalent = (Equivalent) query.getWhere();
assertThat(equivalent.getOperand().get(0), instanceOf(FunctionRef.class));
FunctionRef functionRef = (FunctionRef) equivalent.getOperand().get(0);
assertThat(functionRef.getLibraryName(), is("FHIRHelpers"));
assertThat(functionRef.getName(), is("ToConcept"));
assertThat(equivalent.getOperand().get(1), instanceOf(ToConcept.class));
expressionDef = defs.get("TestConceptComparison");
/*
<expression localId="26" locator="19:3-20:43" xsi:type="Query">
<resultTypeSpecifier xsi:type="ListTypeSpecifier">
<elementType name="fhir:Observation" xsi:type="NamedTypeSpecifier"/>
</resultTypeSpecifier>
<source localId="21" locator="19:3-19:17" alias="O">
<resultTypeSpecifier xsi:type="ListTypeSpecifier">
<elementType name="fhir:Observation" xsi:type="NamedTypeSpecifier"/>
</resultTypeSpecifier>
<expression localId="20" locator="19:3-19:15" dataType="fhir:Observation" xsi:type="Retrieve">
<resultTypeSpecifier xsi:type="ListTypeSpecifier">
<elementType name="fhir:Observation" xsi:type="NamedTypeSpecifier"/>
</resultTypeSpecifier>
</expression>
</source>
<where localId="25" locator="20:5-20:43" resultTypeName="t:Boolean" xsi:type="Equivalent">
<operand name="ToConcept" libraryName="FHIRHelpers" xsi:type="FunctionRef">
<operand localId="23" locator="20:11-20:16" resultTypeName="fhir:CodeableConcept" path="code" scope="O" xsi:type="Property"/>
</operand>
<operand localId="24" locator="20:20-20:43" resultTypeName="t:Concept" name="ECOG performance score" xsi:type="ConceptRef"/>
</where>
</expression>
*/
assertThat(expressionDef.getExpression(), instanceOf(Query.class));
query = (Query) expressionDef.getExpression();
assertThat(query.getWhere(), instanceOf(Equivalent.class));
equivalent = (Equivalent) query.getWhere();
assertThat(equivalent.getOperand().get(0), instanceOf(FunctionRef.class));
functionRef = (FunctionRef) equivalent.getOperand().get(0);
assertThat(functionRef.getLibraryName(), is("FHIRHelpers"));
assertThat(functionRef.getName(), is("ToConcept"));
assertThat(equivalent.getOperand().get(1), instanceOf(ConceptRef.class));
}
use of org.cqframework.cql.cql2elm.CqlTranslator in project clinical_quality_language by cqframework.
the class BaseTest method testDoubleListPromotion.
@Test
public void testDoubleListPromotion() throws IOException {
CqlTranslator translator = TestUtils.runSemanticTest("fhir/r401/TestDoubleListPromotion.cql", 0);
Library library = translator.toELM();
Map<String, ExpressionDef> defs = new HashMap<>();
if (library.getStatements() != null) {
for (ExpressionDef def : library.getStatements().getDef()) {
defs.put(def.getName(), def);
}
}
ExpressionDef def = defs.get("Observations");
Retrieve retrieve = (Retrieve) def.getExpression();
Expression codes = retrieve.getCodes();
assertThat(codes, instanceOf(ToList.class));
assertThat(((ToList) codes).getOperand(), instanceOf(CodeRef.class));
}
Aggregations