Search in sources :

Example 11 with CqlEvaluator

use of com.ibm.cohort.cql.evaluation.CqlEvaluator in project quality-measure-and-cohort-service by Alvearie.

the class CqlTemporalTests method canFindMultipleEncountersFollowingEachOther.

@Test
public void canFindMultipleEncountersFollowingEachOther() throws Exception {
    Patient patient = getPatient("123", Enumerations.AdministrativeGender.FEMALE, null);
    FhirServerConfig fhirConfig = getFhirServerConfig();
    Bundle bundle = new Bundle();
    Bundle.BundleEntryComponent firstEncounter = new Bundle.BundleEntryComponent();
    firstEncounter.setResource(ENCOUNTER_1);
    Bundle.BundleEntryComponent secondEncounter = new Bundle.BundleEntryComponent();
    secondEncounter.setResource(ENCOUNTER_2);
    bundle.addEntry(firstEncounter);
    bundle.addEntry(secondEncounter);
    mockFhirResourceRetrieval("/Encounter?subject=Patient%2F123&_format=json", getFhirParser(), bundle, fhirConfig);
    CqlEvaluator evaluator = setupTestFor(patient, "cql.temporal", ClasspathCqlLibraryProvider.FHIR_HELPERS_CLASSPATH);
    String expression = "ValidEncounters2";
    CqlEvaluationResult actual = evaluator.evaluate(new CqlVersionedIdentifier("Test3", "1.0.0"), null, newPatientContext("123"), Collections.singleton(expression));
    Map<String, Object> expected = new HashMap<>();
    expected.put(expression, true);
    Assert.assertEquals(expected, actual.getExpressionResults());
}
Also used : HashMap(java.util.HashMap) Bundle(org.hl7.fhir.r4.model.Bundle) Patient(org.hl7.fhir.r4.model.Patient) FhirServerConfig(com.ibm.cohort.fhir.client.config.FhirServerConfig) CqlEvaluationResult(com.ibm.cohort.cql.evaluation.CqlEvaluationResult) CqlEvaluator(com.ibm.cohort.cql.evaluation.CqlEvaluator) CqlVersionedIdentifier(com.ibm.cohort.cql.library.CqlVersionedIdentifier) Test(org.junit.Test)

Example 12 with CqlEvaluator

use of com.ibm.cohort.cql.evaluation.CqlEvaluator in project quality-measure-and-cohort-service by Alvearie.

the class CqlTemporalTests method confirmCanFindEventWithExistenceOfThirdEvent.

@Test
public void confirmCanFindEventWithExistenceOfThirdEvent() throws Exception {
    Patient patient = getPatient("123", Enumerations.AdministrativeGender.FEMALE, null);
    Observation observation = new Observation();
    DateTimeType observationEffective = new DateTimeType(new Date());
    observationEffective.setYear(2015);
    observationEffective.setMonth(0);
    observationEffective.setDay(5);
    observation.setEffective(observationEffective);
    FhirServerConfig fhirConfig = getFhirServerConfig();
    mockFhirResourceRetrieval("/Condition?subject=Patient%2F123&_format=json", getFhirParser(), CONDITION_IN, fhirConfig);
    mockFhirResourceRetrieval("/Encounter?subject=Patient%2F123&_format=json", getFhirParser(), ENCOUNTER_1, fhirConfig);
    mockFhirResourceRetrieval("/Observation?subject=Patient%2F123&_format=json", getFhirParser(), observation, fhirConfig);
    CqlEvaluator evaluator = setupTestFor(patient, "cql.temporal", ClasspathCqlLibraryProvider.FHIR_HELPERS_CLASSPATH);
    String expression = "ObservationWithin30DaysOfCondition";
    CqlEvaluationResult actual = evaluator.evaluate(new CqlVersionedIdentifier("Test2", "1.0.0"), null, newPatientContext("123"), Collections.singleton(expression));
    Map<String, Object> expected = new HashMap<>();
    expected.put(expression, true);
    Assert.assertEquals(expected, actual.getExpressionResults());
}
Also used : DateTimeType(org.hl7.fhir.r4.model.DateTimeType) HashMap(java.util.HashMap) Observation(org.hl7.fhir.r4.model.Observation) Patient(org.hl7.fhir.r4.model.Patient) FhirServerConfig(com.ibm.cohort.fhir.client.config.FhirServerConfig) CqlEvaluationResult(com.ibm.cohort.cql.evaluation.CqlEvaluationResult) Date(java.util.Date) CqlEvaluator(com.ibm.cohort.cql.evaluation.CqlEvaluator) CqlVersionedIdentifier(com.ibm.cohort.cql.library.CqlVersionedIdentifier) Test(org.junit.Test)

Example 13 with CqlEvaluator

use of com.ibm.cohort.cql.evaluation.CqlEvaluator in project quality-measure-and-cohort-service by Alvearie.

the class CqlTemporalTests method anEventDoesFollow.

@Test
public void anEventDoesFollow() throws Exception {
    Patient patient = getPatient("123", Enumerations.AdministrativeGender.FEMALE, null);
    FhirServerConfig fhirConfig = getFhirServerConfig();
    mockFhirResourceRetrieval("/Encounter?subject=Patient%2F123&_format=json", getFhirParser(), ENCOUNTER_1, fhirConfig);
    mockFhirResourceRetrieval("/Condition?subject=Patient%2F123&_format=json", getFhirParser(), CONDITION_IN, fhirConfig);
    CqlEvaluator evaluator = setupTestFor(patient, "cql.temporal", ClasspathCqlLibraryProvider.FHIR_HELPERS_CLASSPATH);
    String expression = "NotFollowedByCondition";
    CqlEvaluationResult actual = evaluator.evaluate(new CqlVersionedIdentifier("Test4", "1.0.0"), null, newPatientContext("123"), Collections.singleton(expression));
    Map<String, Object> expected = new HashMap<>();
    expected.put(expression, false);
    Assert.assertEquals(expected, actual.getExpressionResults());
}
Also used : HashMap(java.util.HashMap) Patient(org.hl7.fhir.r4.model.Patient) FhirServerConfig(com.ibm.cohort.fhir.client.config.FhirServerConfig) CqlEvaluationResult(com.ibm.cohort.cql.evaluation.CqlEvaluationResult) CqlEvaluator(com.ibm.cohort.cql.evaluation.CqlEvaluator) CqlVersionedIdentifier(com.ibm.cohort.cql.library.CqlVersionedIdentifier) Test(org.junit.Test)

Example 14 with CqlEvaluator

use of com.ibm.cohort.cql.evaluation.CqlEvaluator in project quality-measure-and-cohort-service by Alvearie.

the class SparkCqlEvaluator method evaluate.

/**
 * Evaluate the input CQL for a single context + data pair.
 *
 * @param libraryProvider Library provider providing CQL/ELM content
 * @param termProvider    Terminology provider providing terminology resources
 * @param funProvider     External function provider providing static CQL functions
 * @param contextName     Context name corresponding to the library context key
 *                        currently under evaluation.
 * @param resultsSchema   StructType containing the schema data for the output table
 *                        that will be created.
 * @param rowsByContext   Data for a single evaluation context
 * @param dataTypeAliases Mapping of data type to abstract type
 * @param perContextAccum Spark accumulator that tracks each individual context
 *                        evaluation
 * @param errorAccum      Spark accumulator that tracks CQL evaluation errors
 * @param batchRunTime    Single unified timestamp for all contexts
 * @return Evaluation results for all expressions evaluated keyed by the context
 *         ID. Expression names are automatically namespaced according to the
 *         library name to avoid issues arising for expression names matching
 *         between libraries (e.g. LibraryName.ExpressionName).
 * @throws Exception on general failure including CQL library loading issues
 */
protected Iterator<Tuple2<Object, Row>> evaluate(CqlLibraryProvider libraryProvider, CqlTerminologyProvider termProvider, ExternalFunctionProvider funProvider, String contextName, StructType resultsSchema, Tuple2<Object, List<Row>> rowsByContext, Map<String, String> dataTypeAliases, LongAccumulator perContextAccum, CollectionAccumulator<EvaluationError> errorAccum, ZonedDateTime batchRunTime) throws Exception {
    // Convert the Spark objects to the cohort Java model
    List<DataRow> datarows = rowsByContext._2().stream().map(getDataRowFactory()).collect(Collectors.toList());
    Map<String, List<Object>> dataByDataType = new HashMap<>();
    for (DataRow datarow : datarows) {
        String dataType = (String) datarow.getValue(ContextRetriever.SOURCE_FACT_IDX);
        List<Object> mappedRows = dataByDataType.computeIfAbsent(dataType, x -> new ArrayList<>());
        mappedRows.add(datarow);
        if (dataTypeAliases.containsKey(dataType)) {
            String mappedType = dataTypeAliases.get(dataType);
            List<Object> aliasedRows = dataByDataType.computeIfAbsent(mappedType, x -> new ArrayList<>());
            aliasedRows.add(datarow);
        }
    }
    DataRowRetrieveProvider retrieveProvider = new DataRowRetrieveProvider(dataByDataType, termProvider);
    CqlDataProvider dataProvider = new DataRowDataProvider(getDataRowClass(), retrieveProvider);
    CqlEvaluator evaluator = new CqlEvaluator().setLibraryProvider(libraryProvider).setDataProvider(dataProvider).setTerminologyProvider(termProvider).setExternalFunctionProvider(funProvider);
    CqlEvaluationRequests requests = getFilteredJobSpecificationWithIds();
    SparkOutputColumnEncoder columnEncoder = getSparkOutputColumnEncoder();
    return evaluate(rowsByContext, contextName, resultsSchema, evaluator, requests, columnEncoder, perContextAccum, errorAccum, batchRunTime);
}
Also used : HashMap(java.util.HashMap) DataRowDataProvider(com.ibm.cohort.datarow.engine.DataRowDataProvider) DataRowRetrieveProvider(com.ibm.cohort.datarow.engine.DataRowRetrieveProvider) SparkDataRow(com.ibm.cohort.cql.spark.data.SparkDataRow) DataRow(com.ibm.cohort.datarow.model.DataRow) SparkOutputColumnEncoder(com.ibm.cohort.cql.spark.data.SparkOutputColumnEncoder) List(java.util.List) ArrayList(java.util.ArrayList) CqlEvaluationRequests(com.ibm.cohort.cql.evaluation.CqlEvaluationRequests) CqlDataProvider(com.ibm.cohort.cql.data.CqlDataProvider) CqlEvaluator(com.ibm.cohort.cql.evaluation.CqlEvaluator)

Example 15 with CqlEvaluator

use of com.ibm.cohort.cql.evaluation.CqlEvaluator in project quality-measure-and-cohort-service by Alvearie.

the class DataRowDataProviderTest method testEvaluateSuccess.

@Test
public void testEvaluateSuccess() throws Exception {
    CqlLibraryProvider backingProvider = new ClasspathCqlLibraryProvider("cql");
    CqlToElmTranslator translator = new CqlToElmTranslator();
    try (Reader r = new FileReader(new File("src/test/resources/modelinfo/mock-modelinfo-1.0.0.xml"))) {
        translator.registerModelInfo(r);
    }
    CqlLibraryProvider libraryProvider = new TranslatingCqlLibraryProvider(backingProvider, translator);
    CqlVersionedIdentifier topLevelLibraryIdentifier = new CqlVersionedIdentifier("SampleLibrary", "1.0.0");
    CqlTerminologyProvider terminologyProvider = new UnsupportedTerminologyProvider();
    Map<String, Object> row = new HashMap<>();
    row.put("id", "123");
    row.put("gender", "female");
    row.put("birthDate", new DateTime(OffsetDateTime.now()));
    Map<String, Iterable<Object>> data = new HashMap<>();
    data.put("Patient", Arrays.asList(new SimpleDataRow(row)));
    DataRowRetrieveProvider retrieveProvider = new DataRowRetrieveProvider(data, terminologyProvider);
    CqlDataProvider dataProvider = new CompositeCqlDataProvider(new DataRowModelResolver(SimpleDataRow.class), retrieveProvider);
    CqlEvaluator evaluator = new CqlEvaluator().setLibraryProvider(libraryProvider).setTerminologyProvider(terminologyProvider).setDataProvider(dataProvider).setCacheContexts(false);
    CqlEvaluationResult result = evaluator.evaluate(topLevelLibraryIdentifier);
    assertEquals(2, result.getExpressionResults().size());
    Object perDefineResult = result.getExpressionResults().get("IsFemale");
    assertEquals(Boolean.TRUE, perDefineResult);
}
Also used : UnsupportedTerminologyProvider(com.ibm.cohort.cql.terminology.UnsupportedTerminologyProvider) TranslatingCqlLibraryProvider(com.ibm.cohort.cql.translation.TranslatingCqlLibraryProvider) HashMap(java.util.HashMap) Reader(java.io.Reader) FileReader(java.io.FileReader) CompositeCqlDataProvider(com.ibm.cohort.cql.data.CompositeCqlDataProvider) CqlToElmTranslator(com.ibm.cohort.cql.translation.CqlToElmTranslator) CqlEvaluationResult(com.ibm.cohort.cql.evaluation.CqlEvaluationResult) ClasspathCqlLibraryProvider(com.ibm.cohort.cql.library.ClasspathCqlLibraryProvider) CqlLibraryProvider(com.ibm.cohort.cql.library.CqlLibraryProvider) TranslatingCqlLibraryProvider(com.ibm.cohort.cql.translation.TranslatingCqlLibraryProvider) OffsetDateTime(java.time.OffsetDateTime) DateTime(org.opencds.cqf.cql.engine.runtime.DateTime) SimpleDataRow(com.ibm.cohort.datarow.model.SimpleDataRow) FileReader(java.io.FileReader) ClasspathCqlLibraryProvider(com.ibm.cohort.cql.library.ClasspathCqlLibraryProvider) CqlTerminologyProvider(com.ibm.cohort.cql.terminology.CqlTerminologyProvider) File(java.io.File) CqlDataProvider(com.ibm.cohort.cql.data.CqlDataProvider) CompositeCqlDataProvider(com.ibm.cohort.cql.data.CompositeCqlDataProvider) CqlEvaluator(com.ibm.cohort.cql.evaluation.CqlEvaluator) CqlVersionedIdentifier(com.ibm.cohort.cql.library.CqlVersionedIdentifier) Test(org.junit.Test)

Aggregations

CqlEvaluator (com.ibm.cohort.cql.evaluation.CqlEvaluator)34 CqlVersionedIdentifier (com.ibm.cohort.cql.library.CqlVersionedIdentifier)31 Patient (org.hl7.fhir.r4.model.Patient)29 Test (org.junit.Test)29 CqlEvaluationResult (com.ibm.cohort.cql.evaluation.CqlEvaluationResult)28 HashMap (java.util.HashMap)26 FhirServerConfig (com.ibm.cohort.fhir.client.config.FhirServerConfig)14 Date (java.util.Date)7 CqlDataProvider (com.ibm.cohort.cql.data.CqlDataProvider)5 DateTimeType (org.hl7.fhir.r4.model.DateTimeType)5 Observation (org.hl7.fhir.r4.model.Observation)5 DatetimeParameter (com.ibm.cohort.cql.evaluation.parameters.DatetimeParameter)4 IntegerParameter (com.ibm.cohort.cql.evaluation.parameters.IntegerParameter)4 IntervalParameter (com.ibm.cohort.cql.evaluation.parameters.IntervalParameter)4 Parameter (com.ibm.cohort.cql.evaluation.parameters.Parameter)4 List (java.util.List)4 Condition (org.hl7.fhir.r4.model.Condition)4 Reference (org.hl7.fhir.r4.model.Reference)4 ClasspathCqlLibraryProvider (com.ibm.cohort.cql.library.ClasspathCqlLibraryProvider)3 CqlLibraryProvider (com.ibm.cohort.cql.library.CqlLibraryProvider)3