Search in sources :

Example 11 with CqlTerminologyProvider

use of com.ibm.cohort.cql.terminology.CqlTerminologyProvider 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 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 if the model info or CQL libraries cannot be loaded for any
 *                   reason
 */
protected Iterator<Tuple2<Object, Row>> evaluate(String contextName, StructType resultsSchema, Tuple2<Object, List<Row>> rowsByContext, Map<String, String> dataTypeAliases, LongAccumulator perContextAccum, CollectionAccumulator<EvaluationError> errorAccum, ZonedDateTime batchRunTime) throws Exception {
    CqlLibraryProvider provider = libraryProvider.get();
    if (provider == null) {
        provider = createLibraryProvider();
        libraryProvider.set(provider);
    }
    CqlTerminologyProvider termProvider = terminologyProvider.get();
    if (termProvider == null) {
        termProvider = createTerminologyProvider();
        terminologyProvider.set(termProvider);
    }
    ExternalFunctionProvider funProvider = functionProvider.get();
    if (funProvider == null) {
        funProvider = createExternalFunctionProvider();
        functionProvider.set(funProvider);
    }
    return evaluate(provider, termProvider, funProvider, contextName, resultsSchema, rowsByContext, dataTypeAliases, perContextAccum, errorAccum, batchRunTime);
}
Also used : ExternalFunctionProvider(org.opencds.cqf.cql.engine.data.ExternalFunctionProvider) CohortExternalFunctionProvider(com.ibm.cohort.cql.functions.CohortExternalFunctionProvider) CqlLibraryProvider(com.ibm.cohort.cql.library.CqlLibraryProvider) PriorityCqlLibraryProvider(com.ibm.cohort.cql.library.PriorityCqlLibraryProvider) HadoopBasedCqlLibraryProvider(com.ibm.cohort.cql.library.HadoopBasedCqlLibraryProvider) TranslatingCqlLibraryProvider(com.ibm.cohort.cql.translation.TranslatingCqlLibraryProvider) ClasspathCqlLibraryProvider(com.ibm.cohort.cql.library.ClasspathCqlLibraryProvider) CqlTerminologyProvider(com.ibm.cohort.cql.terminology.CqlTerminologyProvider)

Example 12 with CqlTerminologyProvider

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

the class CqlEvaluatorTest method testEvaluationExpressionSet.

@Test
public void testEvaluationExpressionSet() {
    CqlLibraryDescriptor libraryDescriptor = new CqlLibraryDescriptor().setLibraryId("Sample").setVersion("1.0.0").setFormat(Format.CQL);
    CqlLibrary library = new CqlLibrary().setDescriptor(libraryDescriptor).setContent("library \"Sample\" version '1.0.0'\ndefine \"Something\":1<10\ndefine \"OtherThing\":10<1");
    CqlTerminologyProvider terminologyProvider = mock(CqlTerminologyProvider.class);
    CqlDataProvider dataProvider = mock(CqlDataProvider.class);
    CqlLibraryProvider libraryProvider = mock(CqlLibraryProvider.class);
    when(libraryProvider.getLibrary(libraryDescriptor)).thenReturn(library);
    CqlToElmTranslator translator = new CqlToElmTranslator();
    TranslatingCqlLibraryProvider translatingProvider = new TranslatingCqlLibraryProvider(libraryProvider, translator);
    CqlEvaluator evaluator = new CqlEvaluator().setTerminologyProvider(terminologyProvider).setDataProvider(dataProvider).setLibraryProvider(translatingProvider).setCacheContexts(false);
    CqlEvaluationResult result = evaluator.evaluate(libraryDescriptor.getVersionedIdentifier(), null, new HashSet<>(Collections.singletonList("OtherThing")));
    assertNotNull(result);
    assertEquals(1, result.getExpressionResults().size());
    assertEquals(false, result.getExpressionResults().get("OtherThing"));
}
Also used : CqlLibrary(com.ibm.cohort.cql.library.CqlLibrary) TranslatingCqlLibraryProvider(com.ibm.cohort.cql.translation.TranslatingCqlLibraryProvider) CqlToElmTranslator(com.ibm.cohort.cql.translation.CqlToElmTranslator) CqlLibraryProvider(com.ibm.cohort.cql.library.CqlLibraryProvider) TranslatingCqlLibraryProvider(com.ibm.cohort.cql.translation.TranslatingCqlLibraryProvider) CqlLibraryDescriptor(com.ibm.cohort.cql.library.CqlLibraryDescriptor) CqlTerminologyProvider(com.ibm.cohort.cql.terminology.CqlTerminologyProvider) CqlDataProvider(com.ibm.cohort.cql.data.CqlDataProvider) Test(org.junit.Test)

Example 13 with CqlTerminologyProvider

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

the class CqlEvaluatorTest method testSimpleEvaluation.

@Test
public void testSimpleEvaluation() {
    CqlLibraryDescriptor libraryDescriptor = new CqlLibraryDescriptor().setLibraryId("Sample").setVersion("1.0.0").setFormat(Format.CQL);
    CqlLibrary library = new CqlLibrary().setDescriptor(libraryDescriptor).setContent("library \"Sample\" version '1.0.0'\nparameter MinimumAge Integer\n\ndefine \"Something\":1<10\ndefine \"OtherThing\":10<1\ndefine EchoParam: MinimumAge");
    CqlTerminologyProvider terminologyProvider = mock(CqlTerminologyProvider.class);
    CqlDataProvider dataProvider = mock(CqlDataProvider.class);
    CqlLibraryProvider libraryProvider = mock(CqlLibraryProvider.class);
    when(libraryProvider.getLibrary(libraryDescriptor)).thenReturn(library);
    CqlToElmTranslator translator = new CqlToElmTranslator();
    TranslatingCqlLibraryProvider translatingProvider = new TranslatingCqlLibraryProvider(libraryProvider, translator);
    CqlEvaluator evaluator = new CqlEvaluator().setTerminologyProvider(terminologyProvider).setDataProvider(dataProvider).setLibraryProvider(translatingProvider).setCacheContexts(false);
    int expectedMinimumAge = 17;
    Map<String, Parameter> parameters = new HashMap<>();
    parameters.put("MinimumAge", new IntegerParameter(expectedMinimumAge));
    Pair<String, String> context = Pair.of("Patient", "123");
    CqlEvaluationResult result = evaluator.evaluate(libraryDescriptor.getVersionedIdentifier(), parameters, context);
    assertNotNull(result);
    assertEquals(3, result.getExpressionResults().size());
    assertEquals(true, result.getExpressionResults().get("Something"));
    assertEquals(expectedMinimumAge, result.getExpressionResults().get("EchoParam"));
}
Also used : IntegerParameter(com.ibm.cohort.cql.evaluation.parameters.IntegerParameter) TranslatingCqlLibraryProvider(com.ibm.cohort.cql.translation.TranslatingCqlLibraryProvider) HashMap(java.util.HashMap) CqlToElmTranslator(com.ibm.cohort.cql.translation.CqlToElmTranslator) CqlLibraryProvider(com.ibm.cohort.cql.library.CqlLibraryProvider) TranslatingCqlLibraryProvider(com.ibm.cohort.cql.translation.TranslatingCqlLibraryProvider) CqlLibrary(com.ibm.cohort.cql.library.CqlLibrary) Parameter(com.ibm.cohort.cql.evaluation.parameters.Parameter) IntegerParameter(com.ibm.cohort.cql.evaluation.parameters.IntegerParameter) CqlLibraryDescriptor(com.ibm.cohort.cql.library.CqlLibraryDescriptor) CqlTerminologyProvider(com.ibm.cohort.cql.terminology.CqlTerminologyProvider) CqlDataProvider(com.ibm.cohort.cql.data.CqlDataProvider) Test(org.junit.Test)

Example 14 with CqlTerminologyProvider

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

the class CqlEvaluatorTest method testBatchEvaluation.

@Test
public void testBatchEvaluation() {
    CqlLibraryDescriptor libraryDescriptor = new CqlLibraryDescriptor().setLibraryId("Sample").setVersion("1.0.0").setFormat(Format.CQL);
    CqlLibrary library = new CqlLibrary().setDescriptor(libraryDescriptor).setContent("library \"Sample\" version '1.0.0'\nparameter MinimumAge Integer\n\ndefine \"Something\":1<10\ndefine \"OtherThing\":10<1\ndefine EchoParam: MinimumAge");
    CqlTerminologyProvider terminologyProvider = mock(CqlTerminologyProvider.class);
    CqlDataProvider dataProvider = mock(CqlDataProvider.class);
    CqlLibraryProvider libraryProvider = mock(CqlLibraryProvider.class);
    when(libraryProvider.getLibrary(libraryDescriptor)).thenReturn(library);
    CqlToElmTranslator translator = new CqlToElmTranslator();
    TranslatingCqlLibraryProvider translatingProvider = new TranslatingCqlLibraryProvider(libraryProvider, translator);
    CqlEvaluator evaluator = new CqlEvaluator().setTerminologyProvider(terminologyProvider).setDataProvider(dataProvider).setLibraryProvider(translatingProvider).setCacheContexts(false);
    String parameterName = "MinimumAge";
    int expectedMinimumAge = 17;
    int expectedGlobalMinimumAge = expectedMinimumAge + 10;
    Map<String, Parameter> parameters = new HashMap<>();
    parameters.put(parameterName, new IntegerParameter(expectedMinimumAge));
    Pair<String, String> context = Pair.of("Patient", "123");
    CqlExpressionConfiguration expressionConfiguration = new CqlExpressionConfiguration();
    expressionConfiguration.setName("Something");
    CqlExpressionConfiguration expressionConfiguration2 = new CqlExpressionConfiguration();
    expressionConfiguration2.setName("EchoParam");
    CqlEvaluationRequest request = new CqlEvaluationRequest();
    request.setDescriptor(libraryDescriptor);
    request.setExpressions(Arrays.asList(expressionConfiguration, expressionConfiguration2).stream().collect(Collectors.toSet()));
    request.setParameters(parameters);
    request.setContextKey(context.getKey());
    request.setContextValue(context.getValue());
    CqlEvaluationRequests requests = new CqlEvaluationRequests();
    requests.setEvaluations(Arrays.asList(request));
    requests.setGlobalParameters(Collections.singletonMap(parameterName, new IntegerParameter(expectedGlobalMinimumAge)));
    // First do a full request with parameter override
    List<Pair<CqlEvaluationRequest, CqlEvaluationResult>> results = evaluator.evaluate(requests);
    assertEquals(1, results.size());
    CqlEvaluationResult result = results.get(0).getRight();
    assertNotNull(result);
    assertEquals(2, result.getExpressionResults().size());
    assertEquals(true, result.getExpressionResults().get("Something"));
    assertEquals(expectedMinimumAge, result.getExpressionResults().get("EchoParam"));
    // Now use the global parameter value instead
    request.getParameters().clear();
    results = evaluator.evaluate(requests);
    assertEquals(1, results.size());
    result = results.get(0).getRight();
    assertEquals(expectedGlobalMinimumAge, result.getExpressionResults().get("EchoParam"));
}
Also used : IntegerParameter(com.ibm.cohort.cql.evaluation.parameters.IntegerParameter) TranslatingCqlLibraryProvider(com.ibm.cohort.cql.translation.TranslatingCqlLibraryProvider) HashMap(java.util.HashMap) CqlToElmTranslator(com.ibm.cohort.cql.translation.CqlToElmTranslator) CqlLibraryProvider(com.ibm.cohort.cql.library.CqlLibraryProvider) TranslatingCqlLibraryProvider(com.ibm.cohort.cql.translation.TranslatingCqlLibraryProvider) CqlLibrary(com.ibm.cohort.cql.library.CqlLibrary) Parameter(com.ibm.cohort.cql.evaluation.parameters.Parameter) IntegerParameter(com.ibm.cohort.cql.evaluation.parameters.IntegerParameter) CqlLibraryDescriptor(com.ibm.cohort.cql.library.CqlLibraryDescriptor) CqlTerminologyProvider(com.ibm.cohort.cql.terminology.CqlTerminologyProvider) CqlDataProvider(com.ibm.cohort.cql.data.CqlDataProvider) Pair(org.apache.commons.lang3.tuple.Pair) Test(org.junit.Test)

Example 15 with CqlTerminologyProvider

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

the class CqlContextFactoryTest method testContextCacheKeyEquals.

@Test
public void testContextCacheKeyEquals() {
    CqlLibraryProvider libraryProvider = mock(CqlLibraryProvider.class);
    CqlTerminologyProvider terminologyProvider = mock(CqlTerminologyProvider.class);
    CqlVersionedIdentifier topLevelLibraryIdentifier = new CqlVersionedIdentifier("Test", "1.0.0");
    Map<String, Parameter> parameters = new HashMap<>();
    ZonedDateTime evaluationDateTime = ZonedDateTime.now();
    CqlContextFactory.ContextCacheKey k1 = new CqlContextFactory.ContextCacheKey(libraryProvider, topLevelLibraryIdentifier, terminologyProvider, null, evaluationDateTime, parameters);
    assertEquals(k1, k1);
    CqlContextFactory.ContextCacheKey k2 = new CqlContextFactory.ContextCacheKey(libraryProvider, topLevelLibraryIdentifier, terminologyProvider, null, evaluationDateTime, parameters);
    assertEquals(k1, k2);
    Map<ContextCacheKey, String> map = new HashMap<>();
    map.put(k1, "Hello,World");
    assertEquals("Hello,World", map.get(k2));
}
Also used : HashMap(java.util.HashMap) ZonedDateTime(java.time.ZonedDateTime) IntegerParameter(com.ibm.cohort.cql.evaluation.parameters.IntegerParameter) StringParameter(com.ibm.cohort.cql.evaluation.parameters.StringParameter) Parameter(com.ibm.cohort.cql.evaluation.parameters.Parameter) ContextCacheKey(com.ibm.cohort.cql.evaluation.CqlContextFactory.ContextCacheKey) PriorityCqlLibraryProvider(com.ibm.cohort.cql.library.PriorityCqlLibraryProvider) TranslatingCqlLibraryProvider(com.ibm.cohort.cql.translation.TranslatingCqlLibraryProvider) ClasspathCqlLibraryProvider(com.ibm.cohort.cql.library.ClasspathCqlLibraryProvider) CqlLibraryProvider(com.ibm.cohort.cql.library.CqlLibraryProvider) CqlTerminologyProvider(com.ibm.cohort.cql.terminology.CqlTerminologyProvider) ContextCacheKey(com.ibm.cohort.cql.evaluation.CqlContextFactory.ContextCacheKey) CqlVersionedIdentifier(com.ibm.cohort.cql.library.CqlVersionedIdentifier) Test(org.junit.Test)

Aggregations

CqlTerminologyProvider (com.ibm.cohort.cql.terminology.CqlTerminologyProvider)16 CqlDataProvider (com.ibm.cohort.cql.data.CqlDataProvider)13 TranslatingCqlLibraryProvider (com.ibm.cohort.cql.translation.TranslatingCqlLibraryProvider)11 CqlLibraryProvider (com.ibm.cohort.cql.library.CqlLibraryProvider)10 Test (org.junit.Test)9 CqlToElmTranslator (com.ibm.cohort.cql.translation.CqlToElmTranslator)8 R4RestFhirTerminologyProvider (com.ibm.cohort.engine.terminology.R4RestFhirTerminologyProvider)8 IGenericClient (ca.uhn.fhir.rest.client.api.IGenericClient)7 ClasspathCqlLibraryProvider (com.ibm.cohort.cql.library.ClasspathCqlLibraryProvider)7 PriorityCqlLibraryProvider (com.ibm.cohort.cql.library.PriorityCqlLibraryProvider)5 HashMap (java.util.HashMap)5 IntegerParameter (com.ibm.cohort.cql.evaluation.parameters.IntegerParameter)4 Parameter (com.ibm.cohort.cql.evaluation.parameters.Parameter)4 R4LibraryDependencyGatherer (com.ibm.cohort.cql.hapi.R4LibraryDependencyGatherer)4 CqlVersionedIdentifier (com.ibm.cohort.cql.library.CqlVersionedIdentifier)4 DefaultRetrieveCacheContext (com.ibm.cohort.engine.measure.cache.DefaultRetrieveCacheContext)4 RetrieveCacheContext (com.ibm.cohort.engine.measure.cache.RetrieveCacheContext)4 FhirClientBuilderFactory (com.ibm.cohort.fhir.client.config.FhirClientBuilderFactory)4 CqlEvaluator (com.ibm.cohort.cql.evaluation.CqlEvaluator)3 CqlLibrary (com.ibm.cohort.cql.library.CqlLibrary)3