Search in sources :

Example 1 with ExpressionDef

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;
}
Also used : ModelInfo(org.hl7.elm_modelinfo.r1.ModelInfo) Element(org.hl7.elm.r1.Element)

Example 2 with ExpressionDef

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;
}
Also used : ElmRequirement(org.cqframework.cql.elm.requirements.ElmRequirement) org.hl7.elm.r1(org.hl7.elm.r1)

Example 3 with ExpressionDef

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;
}
Also used : CompiledLibrary(org.cqframework.cql.cql2elm.model.CompiledLibrary) Library(org.hl7.fhir.r5.model.Library)

Example 4 with ExpressionDef

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);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ValidationMessage(org.hl7.fhir.utilities.validation.ValidationMessage)

Example 5 with ExpressionDef

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);
}
Also used : java.util(java.util) UcumEssenceService(org.fhir.ucum.UcumEssenceService) Logger(org.slf4j.Logger) Date(java.util.Date) LoggerFactory(org.slf4j.LoggerFactory) org.hl7.elm.r1(org.hl7.elm.r1) Test(org.testng.annotations.Test) IOException(java.io.IOException) File(java.io.File) ZoneId(java.time.ZoneId) UcumException(org.fhir.ucum.UcumException) FhirContext(ca.uhn.fhir.context.FhirContext) List(java.util.List) org.cqframework.cql.cql2elm(org.cqframework.cql.cql2elm) Assert(org.testng.Assert) Paths(java.nio.file.Paths) LocalDate(java.time.LocalDate) IParser(ca.uhn.fhir.parser.IParser) org.hl7.fhir.r5.model(org.hl7.fhir.r5.model) UcumService(org.fhir.ucum.UcumService) LocalDate(java.time.LocalDate) org.hl7.fhir.r5.model(org.hl7.fhir.r5.model) Test(org.testng.annotations.Test)

Aggregations

ExpressionDef (org.hl7.elm.r1.ExpressionDef)11 Test (org.testng.annotations.Test)9 Library (org.hl7.elm.r1.Library)7 HashMap (java.util.HashMap)6 CqlTranslator (org.cqframework.cql.cql2elm.CqlTranslator)6 CompiledLibrary (org.cqframework.cql.cql2elm.model.CompiledLibrary)4 Element (org.hl7.elm.r1.Element)4 BeforeTest (org.testng.annotations.BeforeTest)4 CqlLibrary (com.ibm.cohort.cql.library.CqlLibrary)3 HashSet (java.util.HashSet)3 List (java.util.List)3 Set (java.util.Set)3 ModelManager (org.cqframework.cql.cql2elm.ModelManager)3 DataType (org.hl7.cql.model.DataType)3 Annotation (org.hl7.cql_annotations.r1.Annotation)3 CqlLibraryDescriptor (com.ibm.cohort.cql.library.CqlLibraryDescriptor)2 CqlLibraryProvider (com.ibm.cohort.cql.library.CqlLibraryProvider)2 Format (com.ibm.cohort.cql.library.Format)2 IOException (java.io.IOException)2 QuickDataType.quickDataType (org.cqframework.cql.cql2elm.matchers.QuickDataType.quickDataType)2