Search in sources :

Example 16 with IntegerParameter

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

the class CqlEvaluationRequestsTest method testSetGet.

@Test
public void testSetGet() {
    Map<String, Parameter> globalParameters = new HashMap<>();
    globalParameters.put("String", new StringParameter("Hello,World"));
    globalParameters.put("Integer", new IntegerParameter(10));
    Map<String, Parameter> localParameters = new HashMap<>();
    localParameters.put("String", new StringParameter("Goodbye, Cruel World"));
    localParameters.put("Float", new DecimalParameter("1.29f"));
    CqlLibraryDescriptor desc = new CqlLibraryDescriptor().setLibraryId("SampleLibrary").setVersion("1.0.0");
    CqlExpressionConfiguration expressionConfiguration = new CqlExpressionConfiguration();
    expressionConfiguration.setName("IsFemale");
    CqlEvaluationRequest r1 = new CqlEvaluationRequest();
    r1.setDescriptor(desc);
    r1.setParameters(localParameters);
    r1.setExpressions(Collections.singleton(expressionConfiguration));
    r1.setContextKey("Patient");
    r1.setContextValue("NA");
    assertEquals(desc, r1.getDescriptor());
    assertEquals(localParameters, r1.getParameters());
    assertEquals(Collections.singleton(expressionConfiguration), r1.getExpressions());
    assertEquals("Patient", r1.getContextKey());
    assertEquals("NA", r1.getContextValue());
    CqlEvaluationRequests requests = new CqlEvaluationRequests();
    requests.setGlobalParameters(globalParameters);
    requests.setEvaluations(Arrays.asList(r1));
    assertEquals(globalParameters, requests.getGlobalParameters());
    assertEquals(1, requests.getEvaluations().size());
}
Also used : IntegerParameter(com.ibm.cohort.cql.evaluation.parameters.IntegerParameter) StringParameter(com.ibm.cohort.cql.evaluation.parameters.StringParameter) DecimalParameter(com.ibm.cohort.cql.evaluation.parameters.DecimalParameter) HashMap(java.util.HashMap) StringParameter(com.ibm.cohort.cql.evaluation.parameters.StringParameter) DecimalParameter(com.ibm.cohort.cql.evaluation.parameters.DecimalParameter) Parameter(com.ibm.cohort.cql.evaluation.parameters.Parameter) IntegerParameter(com.ibm.cohort.cql.evaluation.parameters.IntegerParameter) CqlLibraryDescriptor(com.ibm.cohort.cql.library.CqlLibraryDescriptor) Test(org.junit.Test)

Example 17 with IntegerParameter

use of com.ibm.cohort.cql.evaluation.parameters.IntegerParameter 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 18 with IntegerParameter

use of com.ibm.cohort.cql.evaluation.parameters.IntegerParameter 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 19 with IntegerParameter

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

the class CqlContextFactoryTest method testCreateContextSuccess.

@Test
public void testCreateContextSuccess() {
    boolean expectedDebug = true;
    ZonedDateTime expectedEvaluationDateTime = ZonedDateTime.of(LocalDateTime.of(2001, 10, 2, 11, 12, 13), ZoneId.of("America/New_York"));
    PriorityCqlLibraryProvider libraryProvider = new PriorityCqlLibraryProvider(new ClasspathCqlLibraryProvider("cql", ClasspathCqlLibraryProvider.FHIR_HELPERS_CLASSPATH));
    CqlToElmTranslator translator = new CqlToElmTranslator();
    TranslatingCqlLibraryProvider translatingProvider = new TranslatingCqlLibraryProvider(libraryProvider, translator);
    CqlVersionedIdentifier topLevelLibraryIdentifier = new CqlVersionedIdentifier("MyCQL", "1.0.0");
    CqlTerminologyProvider terminologyProvider = new UnsupportedTerminologyProvider();
    CqlDataProvider dataProvider = mock(CqlDataProvider.class);
    Pair<String, String> contextData = Pair.of("Patient", "123");
    Map<String, Parameter> expectedParams = new HashMap<>();
    expectedParams.put("P1", new StringParameter("MyString"));
    expectedParams.put("P2", new IntegerParameter(10));
    CqlContextFactory cqlContextFactory = spy(CqlContextFactory.class);
    Context context = cqlContextFactory.createContext(translatingProvider, topLevelLibraryIdentifier, terminologyProvider, dataProvider, expectedEvaluationDateTime, contextData, expectedParams, expectedDebug ? CqlDebug.DEBUG : CqlDebug.NONE);
    assertEquals(expectedDebug, context.getDebugMap().getIsLoggingEnabled());
    assertEquals(expectedEvaluationDateTime.toInstant(), context.getEvaluationDateTime().getDateTime().toZonedDateTime().toInstant());
    context.enterContext(contextData.getKey());
    assertEquals(contextData.getValue(), context.getCurrentContextValue());
    assertEquals(topLevelLibraryIdentifier.getId(), context.getCurrentLibrary().getIdentifier().getId());
    assertEquals(topLevelLibraryIdentifier.getVersion(), context.getCurrentLibrary().getIdentifier().getVersion());
    for (Map.Entry<String, Parameter> entry : expectedParams.entrySet()) {
        Object actualValue = context.resolveParameterRef(null, entry.getKey());
        assertEquals(entry.getValue().toCqlType(), actualValue);
    }
    // Once more just to check that caching is working. Using a different data provider because that is
    // how we will actually use it at runtime.
    CqlDataProvider dataProvider2 = mock(CqlDataProvider.class);
    cqlContextFactory.createContext(translatingProvider, topLevelLibraryIdentifier, terminologyProvider, dataProvider2, expectedEvaluationDateTime, contextData, expectedParams, expectedDebug ? CqlDebug.DEBUG : CqlDebug.NONE);
    verify(cqlContextFactory, times(1)).createContext(any(ContextCacheKey.class));
}
Also used : Context(org.opencds.cqf.cql.engine.execution.Context) UnsupportedTerminologyProvider(com.ibm.cohort.cql.terminology.UnsupportedTerminologyProvider) IntegerParameter(com.ibm.cohort.cql.evaluation.parameters.IntegerParameter) StringParameter(com.ibm.cohort.cql.evaluation.parameters.StringParameter) TranslatingCqlLibraryProvider(com.ibm.cohort.cql.translation.TranslatingCqlLibraryProvider) HashMap(java.util.HashMap) CqlToElmTranslator(com.ibm.cohort.cql.translation.CqlToElmTranslator) PriorityCqlLibraryProvider(com.ibm.cohort.cql.library.PriorityCqlLibraryProvider) 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) ClasspathCqlLibraryProvider(com.ibm.cohort.cql.library.ClasspathCqlLibraryProvider) CqlTerminologyProvider(com.ibm.cohort.cql.terminology.CqlTerminologyProvider) ContextCacheKey(com.ibm.cohort.cql.evaluation.CqlContextFactory.ContextCacheKey) CqlDataProvider(com.ibm.cohort.cql.data.CqlDataProvider) HashMap(java.util.HashMap) Map(java.util.Map) CqlVersionedIdentifier(com.ibm.cohort.cql.library.CqlVersionedIdentifier) Test(org.junit.Test)

Aggregations

IntegerParameter (com.ibm.cohort.cql.evaluation.parameters.IntegerParameter)19 Test (org.junit.Test)18 Parameter (com.ibm.cohort.cql.evaluation.parameters.Parameter)17 IntervalParameter (com.ibm.cohort.cql.evaluation.parameters.IntervalParameter)14 HashMap (java.util.HashMap)13 StringParameter (com.ibm.cohort.cql.evaluation.parameters.StringParameter)12 DecimalParameter (com.ibm.cohort.cql.evaluation.parameters.DecimalParameter)11 DateParameter (com.ibm.cohort.cql.evaluation.parameters.DateParameter)9 DatetimeParameter (com.ibm.cohort.cql.evaluation.parameters.DatetimeParameter)8 CqlLibraryDescriptor (com.ibm.cohort.cql.library.CqlLibraryDescriptor)7 CqlEvaluationRequests (com.ibm.cohort.cql.evaluation.CqlEvaluationRequests)6 CqlEvaluationRequest (com.ibm.cohort.cql.evaluation.CqlEvaluationRequest)5 BooleanParameter (com.ibm.cohort.cql.evaluation.parameters.BooleanParameter)5 CodeParameter (com.ibm.cohort.cql.evaluation.parameters.CodeParameter)5 QuantityParameter (com.ibm.cohort.cql.evaluation.parameters.QuantityParameter)5 TimeParameter (com.ibm.cohort.cql.evaluation.parameters.TimeParameter)5 IntegerType (org.hl7.fhir.r4.model.IntegerType)4 CqlDataProvider (com.ibm.cohort.cql.data.CqlDataProvider)3 CqlEvaluationResult (com.ibm.cohort.cql.evaluation.CqlEvaluationResult)3 CqlEvaluator (com.ibm.cohort.cql.evaluation.CqlEvaluator)3