Search in sources :

Example 1 with CqlEvaluator

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

the class CohortEngineRestHandler method evaluateCohort.

private List<String> evaluateCohort(IGenericClient dataClient, CqlTerminologyProvider termProvider, CqlLibraryProvider libraryProvider, RetrieveCacheContext retrieveCacheContext, CohortEvaluation evaluationRequest) {
    CqlDataProvider dataProvider = R4DataProviderFactory.createDataProvider(dataClient, termProvider, retrieveCacheContext);
    CqlEvaluator evaluator = new CqlEvaluator().setLibraryProvider(libraryProvider).setDataProvider(dataProvider).setTerminologyProvider(termProvider).setCacheContexts(false);
    Set<String> expressions = new HashSet<>();
    expressions.add(evaluationRequest.getDefineToRun());
    String[] patients = evaluationRequest.getPatientIds().split(",");
    CqlLibraryDescriptor topLevelLibrary = CqlLibraryHelpers.filenameToLibraryDescriptor(evaluationRequest.getEntrypoint());
    List<String> retVal = new ArrayList<>();
    for (String patientId : patients) {
        Pair<String, String> contextPair = new ImmutablePair<>(ContextNames.PATIENT, patientId);
        CqlEvaluationResult result = evaluator.evaluate(topLevelLibrary.getVersionedIdentifier(), evaluationRequest.getParameters(), contextPair, expressions, evaluationRequest.getLoggingLevel(), ZonedDateTime.now());
        for (Map.Entry<String, Object> entry : result.getExpressionResults().entrySet()) {
            String expression = entry.getKey();
            Object rawValue = entry.getValue();
            if (rawValue == null) {
                throw new RuntimeException(String.format("Null result is unsupported! Expression: \"%s\", ContextId: %s", expression, patientId));
            }
            if (!(rawValue instanceof Boolean)) {
                throw new RuntimeException(String.format("Only boolean CQLs are currently supported! Expression: \"%s\", Result: %s, ContextId: %s", expression, result, patientId));
            }
            Boolean booleanResult = (Boolean) rawValue;
            if (booleanResult) {
                retVal.add(patientId);
            }
            logger.info(String.format("Expression: \"%s\", Result: %s, ContextId: %s", expression, booleanResult, patientId));
        }
    }
    return retVal;
}
Also used : ArrayList(java.util.ArrayList) CqlEvaluationResult(com.ibm.cohort.cql.evaluation.CqlEvaluationResult) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) CqlLibraryDescriptor(com.ibm.cohort.cql.library.CqlLibraryDescriptor) CqlDataProvider(com.ibm.cohort.cql.data.CqlDataProvider) Map(java.util.Map) CqlEvaluator(com.ibm.cohort.cql.evaluation.CqlEvaluator) HashSet(java.util.HashSet)

Example 2 with CqlEvaluator

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

the class PatientTestBase method setupTestFor.

protected CqlEvaluator setupTestFor(DomainResource resource, FhirServerConfig fhirConfig, String firstPackage, String... packages) {
    mockFhirResourceRetrieval("/metadata?_format=json", getCapabilityStatement());
    mockFhirResourceRetrieval(resource);
    CqlEvaluator evaluator = null;
    if (firstPackage != null) {
        FhirClientBuilderFactory factory = FhirClientBuilderFactory.newInstance();
        FhirClientBuilder fhirClientBuilder = factory.newFhirClientBuilder();
        CqlLibraryProvider classpathCqlLibraryProvider = new ClasspathCqlLibraryProvider(firstPackage, packages);
        CqlToElmTranslator translator = new CqlToElmTranslator();
        CqlLibraryProvider libraryProvider = new TranslatingCqlLibraryProvider(classpathCqlLibraryProvider, translator);
        IGenericClient testClient = fhirClientBuilder.createFhirClient(fhirConfig);
        CqlTerminologyProvider termProvider = new R4RestFhirTerminologyProvider(testClient);
        CqlDataProvider dataProvider = R4DataProviderFactory.createDataProvider(testClient, termProvider, null, R4FhirModelResolverFactory.createCachingResolver(), true, null);
        evaluator = new CqlEvaluator().setLibraryProvider(libraryProvider).setDataProvider(dataProvider).setTerminologyProvider(termProvider).setCacheContexts(false);
    }
    return evaluator;
}
Also used : FhirClientBuilder(com.ibm.cohort.fhir.client.config.FhirClientBuilder) TranslatingCqlLibraryProvider(com.ibm.cohort.cql.translation.TranslatingCqlLibraryProvider) IGenericClient(ca.uhn.fhir.rest.client.api.IGenericClient) R4RestFhirTerminologyProvider(com.ibm.cohort.engine.terminology.R4RestFhirTerminologyProvider) FhirClientBuilderFactory(com.ibm.cohort.fhir.client.config.FhirClientBuilderFactory) CqlToElmTranslator(com.ibm.cohort.cql.translation.CqlToElmTranslator) ClasspathCqlLibraryProvider(com.ibm.cohort.cql.library.ClasspathCqlLibraryProvider) CqlLibraryProvider(com.ibm.cohort.cql.library.CqlLibraryProvider) TranslatingCqlLibraryProvider(com.ibm.cohort.cql.translation.TranslatingCqlLibraryProvider) ClasspathCqlLibraryProvider(com.ibm.cohort.cql.library.ClasspathCqlLibraryProvider) CqlTerminologyProvider(com.ibm.cohort.cql.terminology.CqlTerminologyProvider) CqlDataProvider(com.ibm.cohort.cql.data.CqlDataProvider) CqlEvaluator(com.ibm.cohort.cql.evaluation.CqlEvaluator)

Example 3 with CqlEvaluator

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

the class CqlEvaluatorIntegrationTest method testUsingUSCoreWithTranslation.

@Test
public void testUsingUSCoreWithTranslation() throws Exception {
    Patient patient = getPatient("123", AdministrativeGender.MALE, "1983-12-02");
    CqlEvaluator evaluator = setupTestFor(patient, "cql.uscore");
    String expression = "QueryByGender";
    CqlEvaluationResult actual = evaluator.evaluate(new CqlVersionedIdentifier("TestUSCore", "1.0.0"), null, newPatientContext("123"), Collections.singleton(expression));
    Assert.assertEquals(1, actual.getExpressionResults().size());
    List<Object> value = (List) actual.getExpressionResults().get(expression);
    Assert.assertEquals(1, value.size());
    assertFhirEquals(patient, (IBaseResource) value.get(0));
}
Also used : Patient(org.hl7.fhir.r4.model.Patient) CqlEvaluationResult(com.ibm.cohort.cql.evaluation.CqlEvaluationResult) List(java.util.List) CqlEvaluator(com.ibm.cohort.cql.evaluation.CqlEvaluator) CqlVersionedIdentifier(com.ibm.cohort.cql.library.CqlVersionedIdentifier) Test(org.junit.Test)

Example 4 with CqlEvaluator

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

the class CqlEvaluatorIntegrationTest method testRequiredCQLParameterSpecifiedPatientInRange.

@Test
public void testRequiredCQLParameterSpecifiedPatientInRange() throws Exception {
    Patient patient = getPatient("123", Enumerations.AdministrativeGender.FEMALE, "1978-05-06");
    CqlEvaluator evaluator = setupTestFor(patient, "cql.parameters");
    String expression = "Female";
    Map<String, Parameter> parameters = new HashMap<>();
    parameters.put("MaxAge", new IntegerParameter(50));
    CqlEvaluationResult actual = evaluator.evaluate(new CqlVersionedIdentifier("TestWithParams", "1.0.0"), parameters, newPatientContext("123"), Collections.singleton(expression));
    Map<String, Object> expected = new HashMap<>();
    expected.put(expression, true);
    Assert.assertEquals(expected, actual.getExpressionResults());
}
Also used : IntegerParameter(com.ibm.cohort.cql.evaluation.parameters.IntegerParameter) HashMap(java.util.HashMap) Patient(org.hl7.fhir.r4.model.Patient) DatetimeParameter(com.ibm.cohort.cql.evaluation.parameters.DatetimeParameter) IntervalParameter(com.ibm.cohort.cql.evaluation.parameters.IntervalParameter) IntegerParameter(com.ibm.cohort.cql.evaluation.parameters.IntegerParameter) Parameter(com.ibm.cohort.cql.evaluation.parameters.Parameter) 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 5 with CqlEvaluator

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

the class CqlEvaluatorIntegrationTest method testUOMEquivalence_demonstrateConversionOfBothSides.

@Test
public void testUOMEquivalence_demonstrateConversionOfBothSides() throws Exception {
    Patient patient = getPatient("123", Enumerations.AdministrativeGender.FEMALE, "1983-12-02");
    CqlEvaluator evaluator = setupTestFor(patient, "cql.uomequivalence");
    String expression = "UpConvert";
    CqlEvaluationResult actual = evaluator.evaluate(new CqlVersionedIdentifier("TestUOMCompare", "1.0.0"), null, newPatientContext("123"), Collections.singleton(expression));
    Map<String, Object> expected = new HashMap<>();
    // The safest thing to do is convert the left and right
    // values to a known, fixed unit
    expected.put(expression, true);
    Assert.assertEquals(expected, actual.getExpressionResults());
}
Also used : HashMap(java.util.HashMap) Patient(org.hl7.fhir.r4.model.Patient) 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)

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