use of org.hl7.elm.r1.ExpressionDef in project clinical_quality_language by cqframework.
the class Cql2ElmVisitor method visitContextDefinition.
@Override
public Object visitContextDefinition(cqlParser.ContextDefinitionContext ctx) {
String modelIdentifier = parseString(ctx.modelIdentifier());
String unqualifiedIdentifier = parseString(ctx.identifier());
currentContext = modelIdentifier != null ? modelIdentifier + "." + unqualifiedIdentifier : unqualifiedIdentifier;
if (!isUnfilteredContext(unqualifiedIdentifier)) {
ModelContext modelContext = libraryBuilder.resolveContextName(modelIdentifier, unqualifiedIdentifier);
// If this is the first time a context definition is encountered, construct a context definition:
// define <Context> = element of [<Context model type>]
Element modelContextDefinition = contextDefinitions.get(modelContext.getName());
if (modelContextDefinition == null) {
if (libraryBuilder.hasUsings()) {
ModelInfo modelInfo = modelIdentifier == null ? libraryBuilder.getModel(libraryInfo.getDefaultModelName()).getModelInfo() : libraryBuilder.getModel(modelIdentifier).getModelInfo();
// String contextTypeName = modelContext.getName();
// DataType contextType = libraryBuilder.resolveTypeName(modelInfo.getName(), contextTypeName);
DataType contextType = modelContext.getType();
modelContextDefinition = libraryBuilder.resolveParameterRef(modelContext.getName());
if (modelContextDefinition != null) {
contextDefinitions.put(modelContext.getName(), modelContextDefinition);
} else {
Retrieve contextRetrieve = of.createRetrieve().withDataType(libraryBuilder.dataTypeToQName(contextType));
track(contextRetrieve, ctx);
contextRetrieve.setResultType(new ListType(contextType));
String contextClassIdentifier = ((ClassType) contextType).getIdentifier();
if (contextClassIdentifier != null) {
contextRetrieve.setTemplateId(contextClassIdentifier);
}
modelContextDefinition = of.createExpressionDef().withName(unqualifiedIdentifier).withContext(currentContext).withExpression(of.createSingletonFrom().withOperand(contextRetrieve));
track(modelContextDefinition, ctx);
((ExpressionDef) modelContextDefinition).getExpression().setResultType(contextType);
modelContextDefinition.setResultType(contextType);
libraryBuilder.addExpression((ExpressionDef) modelContextDefinition);
contextDefinitions.put(modelContext.getName(), modelContextDefinition);
}
} else {
modelContextDefinition = of.createExpressionDef().withName(unqualifiedIdentifier).withContext(currentContext).withExpression(of.createNull());
track(modelContextDefinition, ctx);
((ExpressionDef) modelContextDefinition).getExpression().setResultType(libraryBuilder.resolveTypeName("System", "Any"));
modelContextDefinition.setResultType(((ExpressionDef) modelContextDefinition).getExpression().getResultType());
libraryBuilder.addExpression((ExpressionDef) modelContextDefinition);
contextDefinitions.put(modelContext.getName(), modelContextDefinition);
}
}
}
ContextDef contextDef = of.createContextDef().withName(currentContext);
track(contextDef, ctx);
if (libraryBuilder.isCompatibleWith("1.5")) {
libraryBuilder.addContext(contextDef);
}
return currentContext;
}
use of org.hl7.elm.r1.ExpressionDef in project clinical_quality_language by cqframework.
the class DataRequirementsProcessor method extractLogicDefinitions.
private List<Extension> extractLogicDefinitions(ElmRequirementsContext context, ElmRequirements requirements) {
List<Extension> result = new ArrayList<Extension>();
int sequence = 0;
for (ElmRequirement req : requirements.getExpressionDefs()) {
ExpressionDef def = (ExpressionDef) req.getElement();
org.hl7.cql_annotations.r1.Annotation a = getAnnotation(def);
if (a != null) {
result.add(toLogicDefinition(req, def, toNarrativeText(a), sequence++));
}
}
return result;
}
use of org.hl7.elm.r1.ExpressionDef in project clinical_quality_language by cqframework.
the class DataRequirementsProcessor method createLibrary.
private Library createLibrary(ElmRequirementsContext context, ElmRequirements requirements, VersionedIdentifier libraryIdentifier, Iterable<ExpressionDef> expressionDefs, boolean includeLogicDefinitions) {
Library returnLibrary = new Library();
returnLibrary.setStatus(Enumerations.PublicationStatus.ACTIVE);
CodeableConcept libraryType = new CodeableConcept();
Coding typeCoding = new Coding().setCode("module-definition");
typeCoding.setSystem("http://terminology.hl7.org/CodeSystem/library-type");
libraryType.addCoding(typeCoding);
returnLibrary.setType(libraryType);
returnLibrary.setSubject(extractSubject(context));
returnLibrary.getExtension().addAll(extractDirectReferenceCodes(context, requirements));
returnLibrary.getRelatedArtifact().addAll(extractRelatedArtifacts(context, requirements));
returnLibrary.getDataRequirement().addAll(extractDataRequirements(context, requirements));
returnLibrary.getParameter().addAll(extractParameters(context, requirements, libraryIdentifier, expressionDefs));
if (includeLogicDefinitions) {
returnLibrary.getExtension().addAll(extractLogicDefinitions(context, requirements));
}
return returnLibrary;
}
use of org.hl7.elm.r1.ExpressionDef in project clinical_quality_language by cqframework.
the class DataRequirementsProcessor method toOutputParameterDefinition.
private ParameterDefinition toOutputParameterDefinition(VersionedIdentifier libraryIdentifier, ExpressionDef def) {
AtomicBoolean isList = new AtomicBoolean(false);
Enumerations.FHIRAllTypes typeCode = null;
try {
typeCode = Enumerations.FHIRAllTypes.fromCode(toFHIRResultTypeCode(def.getResultType(), def.getName(), isList));
} catch (org.hl7.fhir.exceptions.FHIRException fhirException) {
validationMessages.add(new ValidationMessage(ValidationMessage.Source.Publisher, ValidationMessage.IssueType.NOTSUPPORTED, "CQL Library Packaging", String.format("Result type %s of library %s is not supported; implementations may not be able to use the result of this expression", def.getResultType().toLabel(), libraryIdentifier.getId()), ValidationMessage.IssueSeverity.WARNING));
}
return new ParameterDefinition().setName(def.getName()).setUse(Enumerations.OperationParameterUse.OUT).setMin(0).setMax(isList.get() ? "*" : "1").setType(typeCode);
}
use of org.hl7.elm.r1.ExpressionDef in project clinical_quality_language by cqframework.
the class DataRequirementsProcessorTest method TestDataRequirementsAnalysisCase10a.
@Test
public void TestDataRequirementsAnalysisCase10a() throws IOException {
CqlTranslatorOptions translatorOptions = getTranslatorOptions();
CqlTranslator translator = setupDataRequirementsAnalysis("TestCases/TestCase10a.cql", translatorOptions);
org.hl7.fhir.r5.model.Library moduleDefinitionLibrary = getModuleDefinitionLibrary(translator, translatorOptions);
/*
Element that is a dateTime, referenced in a date comparison
define "ESRD Observations":
[Observation: "ESRD Diagnosis"] O
where O.instant same day or after @2022-02-15
{
type: Observation
mustSupport: [ 'code', 'issued' ]
codeFilter: {
path: 'code',
valueSet: 'http://fakeurl.com/ersd-diagnosis'
},
dateFilter: {
path: 'issued',
valuePeriod: {
low: @2022-02-15
}
}
}
*/
// Validate the ELM is correct
ExpressionDef ed = translator.getTranslatedLibrary().resolveExpressionRef("ESRD Observations");
assertTrue(ed.getExpression() instanceof Query);
Query q = (Query) ed.getExpression();
assertTrue(q.getSource() != null && q.getSource().size() == 1);
AliasedQuerySource source = q.getSource().get(0);
assertTrue(source.getExpression() instanceof Retrieve);
Retrieve r = (Retrieve) source.getExpression();
assertTrue(r.getDataType().getLocalPart().equals("Observation"));
// Validate the data requirement is reported in the module definition library
DataRequirement expectedDataRequirement = null;
for (DataRequirement dr : moduleDefinitionLibrary.getDataRequirement()) {
if (dr.getType() == Enumerations.FHIRAllTypes.OBSERVATION) {
expectedDataRequirement = dr;
}
}
assertTrue(expectedDataRequirement != null);
assertTrue(expectedDataRequirement.getMustSupport().size() == 2);
boolean hasCode = false;
assertTrue(expectedDataRequirement.getMustSupport().stream().filter(s -> s.getValue().equals("code")).count() == 1);
assertTrue(expectedDataRequirement.getMustSupport().stream().filter(s -> s.getValue().equals("issued")).count() == 1);
assertTrue(expectedDataRequirement.getCodeFilter().size() == 1);
DataRequirement.DataRequirementCodeFilterComponent drcfc = expectedDataRequirement.getCodeFilter().get(0);
assertTrue(drcfc.getPath().equals("code"));
assertTrue(drcfc.getValueSet().equals("http://fakeurl.com/ersd-diagnosis"));
assertTrue(expectedDataRequirement.getDateFilter().size() == 1);
DataRequirement.DataRequirementDateFilterComponent drdfc = expectedDataRequirement.getDateFilter().get(0);
LocalDate ld = LocalDate.of(2022, 2, 15);
assertTrue(drdfc.getValuePeriod().getStart().compareTo(Date.from(ld.atStartOfDay(ZoneId.systemDefault()).toInstant())) == 0);
// outputModuleDefinitionLibrary(moduleDefinitionLibrary);
}
Aggregations