Search in sources :

Example 6 with CqlDataProvider

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

the class MeasureEvaluationSeederTest method create_fullContext.

@Test
public void create_fullContext() throws IOException {
    TerminologyProvider terminologyProvider = Mockito.mock(TerminologyProvider.class);
    CqlDataProvider dataProvider = Mockito.mock(CqlDataProvider.class);
    Map<String, CqlDataProvider> dataProviders = new HashMap<>();
    dataProviders.put(fhirUri, dataProvider);
    Measure measure = createMeasure();
    Library library = createLibrary("/cql/seeder/Test-1.0.0.xml");
    FhirResourceResolver<Library> libraryResolver = Mockito.mock(FhirResourceResolver.class);
    Mockito.when(libraryResolver.resolveByName(libraryName, libraryVersion)).thenReturn(library);
    R4LibraryDependencyGatherer dependencyGatherer = Mockito.mock(R4LibraryDependencyGatherer.class);
    Mockito.when(dependencyGatherer.gatherForMeasure(measure)).thenReturn(Collections.singletonList(library));
    MeasureEvaluationSeeder seeder = new MeasureEvaluationSeeder(terminologyProvider, dataProviders, dependencyGatherer, libraryResolver);
    seeder.enableExpressionCaching();
    IMeasureEvaluationSeed actual = seeder.create(measure, periodStart, periodEnd, productLine, null);
    Interval expectedInterval = createInterval();
    Assert.assertEquals(0, expectedInterval.compareTo(actual.getMeasurementPeriod()));
    Assert.assertSame(measure, actual.getMeasure());
    Assert.assertSame(dataProvider, actual.getDataProvider());
    // Attempt to validate the `Context` by looking for key fields under our control.
    Assert.assertSame(terminologyProvider, actual.getContext().resolveTerminologyProvider());
    Assert.assertEquals(productLine, actual.getContext().resolveParameterRef(null, "Product Line"));
    Assert.assertTrue(actual.getContext().isExpressionCachingEnabled());
    Assert.assertNotNull(actual.getContext().getDebugMap());
}
Also used : TerminologyProvider(org.opencds.cqf.cql.engine.terminology.TerminologyProvider) HashMap(java.util.HashMap) Measure(org.hl7.fhir.r4.model.Measure) R4LibraryDependencyGatherer(com.ibm.cohort.cql.hapi.R4LibraryDependencyGatherer) Library(org.hl7.fhir.r4.model.Library) CqlDataProvider(com.ibm.cohort.cql.data.CqlDataProvider) Interval(org.opencds.cqf.cql.engine.runtime.Interval) Test(org.junit.Test)

Example 7 with CqlDataProvider

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

the class MeasureEvaluationSeederTest method create_minimalContext.

@Test
public void create_minimalContext() throws IOException {
    TerminologyProvider terminologyProvider = Mockito.mock(TerminologyProvider.class);
    CqlDataProvider dataProvider = Mockito.mock(CqlDataProvider.class);
    Map<String, CqlDataProvider> dataProviders = new HashMap<>();
    dataProviders.put(fhirUri, dataProvider);
    Library library = createLibrary("/cql/seeder/Test-1.0.0.xml");
    FhirResourceResolver<Library> libraryResolver = Mockito.mock(FhirResourceResolver.class);
    Mockito.when(libraryResolver.resolveByName(libraryName, libraryVersion)).thenReturn(library);
    Measure measure = createMeasure();
    R4LibraryDependencyGatherer dependencyGatherer = Mockito.mock(R4LibraryDependencyGatherer.class);
    Mockito.when(dependencyGatherer.gatherForMeasure(measure)).thenReturn(Collections.singletonList(library));
    MeasureEvaluationSeeder seeder = new MeasureEvaluationSeeder(terminologyProvider, dataProviders, dependencyGatherer, libraryResolver);
    seeder.disableDebugLogging();
    IMeasureEvaluationSeed actual = seeder.create(measure, periodStart, periodEnd, null, null);
    Interval expectedInterval = createInterval();
    Assert.assertEquals(0, expectedInterval.compareTo(actual.getMeasurementPeriod()));
    Assert.assertSame(measure, actual.getMeasure());
    Assert.assertSame(dataProvider, actual.getDataProvider());
    // Attempt to validate the `Context` by looking for key fields under our control.
    Assert.assertSame(terminologyProvider, actual.getContext().resolveTerminologyProvider());
    Assert.assertThrows(NullPointerException.class, () -> actual.getContext().resolveParameterRef(null, "Product Line"));
    Assert.assertFalse(actual.getContext().isExpressionCachingEnabled());
    Assert.assertNull(actual.getContext().getDebugMap());
}
Also used : TerminologyProvider(org.opencds.cqf.cql.engine.terminology.TerminologyProvider) HashMap(java.util.HashMap) Measure(org.hl7.fhir.r4.model.Measure) R4LibraryDependencyGatherer(com.ibm.cohort.cql.hapi.R4LibraryDependencyGatherer) Library(org.hl7.fhir.r4.model.Library) CqlDataProvider(com.ibm.cohort.cql.data.CqlDataProvider) Interval(org.opencds.cqf.cql.engine.runtime.Interval) Test(org.junit.Test)

Example 8 with CqlDataProvider

use of com.ibm.cohort.cql.data.CqlDataProvider 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 9 with CqlDataProvider

use of com.ibm.cohort.cql.data.CqlDataProvider 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)

Example 10 with CqlDataProvider

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

the class CqlEvaluatorTest method testSetGetSuccess.

@Test
public void testSetGetSuccess() {
    CqlTerminologyProvider terminologyProvider = mock(CqlTerminologyProvider.class);
    CqlDataProvider dataProvider = mock(CqlDataProvider.class);
    CqlLibraryProvider libraryProvider = mock(CqlLibraryProvider.class);
    CqlEvaluator evaluator = new CqlEvaluator().setTerminologyProvider(terminologyProvider).setDataProvider(dataProvider).setLibraryProvider(libraryProvider).setCacheContexts(false);
    assertSame(terminologyProvider, evaluator.getTerminologyProvider());
    assertSame(dataProvider, evaluator.getDataProvider());
    assertSame(libraryProvider, evaluator.getLibraryProvider());
}
Also used : CqlLibraryProvider(com.ibm.cohort.cql.library.CqlLibraryProvider) TranslatingCqlLibraryProvider(com.ibm.cohort.cql.translation.TranslatingCqlLibraryProvider) CqlTerminologyProvider(com.ibm.cohort.cql.terminology.CqlTerminologyProvider) CqlDataProvider(com.ibm.cohort.cql.data.CqlDataProvider) Test(org.junit.Test)

Aggregations

CqlDataProvider (com.ibm.cohort.cql.data.CqlDataProvider)19 CqlTerminologyProvider (com.ibm.cohort.cql.terminology.CqlTerminologyProvider)13 Test (org.junit.Test)10 HashMap (java.util.HashMap)9 TranslatingCqlLibraryProvider (com.ibm.cohort.cql.translation.TranslatingCqlLibraryProvider)8 CqlLibraryProvider (com.ibm.cohort.cql.library.CqlLibraryProvider)7 CqlToElmTranslator (com.ibm.cohort.cql.translation.CqlToElmTranslator)7 R4RestFhirTerminologyProvider (com.ibm.cohort.engine.terminology.R4RestFhirTerminologyProvider)7 IGenericClient (ca.uhn.fhir.rest.client.api.IGenericClient)6 R4LibraryDependencyGatherer (com.ibm.cohort.cql.hapi.R4LibraryDependencyGatherer)6 CqlEvaluator (com.ibm.cohort.cql.evaluation.CqlEvaluator)5 Library (org.hl7.fhir.r4.model.Library)5 Measure (org.hl7.fhir.r4.model.Measure)5 ClasspathCqlLibraryProvider (com.ibm.cohort.cql.library.ClasspathCqlLibraryProvider)4 CqlLibraryDescriptor (com.ibm.cohort.cql.library.CqlLibraryDescriptor)4 CqlEvaluationResult (com.ibm.cohort.cql.evaluation.CqlEvaluationResult)3 IntegerParameter (com.ibm.cohort.cql.evaluation.parameters.IntegerParameter)3 Parameter (com.ibm.cohort.cql.evaluation.parameters.Parameter)3 CqlLibrary (com.ibm.cohort.cql.library.CqlLibrary)3 CqlVersionedIdentifier (com.ibm.cohort.cql.library.CqlVersionedIdentifier)3