Search in sources :

Example 1 with CqlDataProvider

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

the class CohortEngineRestHandler method createMeasureEvaluator.

private MeasureEvaluator createMeasureEvaluator(InputStream inputStream, FhirServerConfig dataServerConfig, FhirServerConfig terminologyServerConfig, Boolean expandValueSets, Integer searchPageSize, RetrieveCacheContext retrieveCacheContext, FhirContext fhirContext) throws IOException {
    FhirClientBuilder clientBuilder = FhirClientBuilderFactory.newInstance().newFhirClientBuilder(fhirContext);
    IGenericClient dataClient = clientBuilder.createFhirClient(dataServerConfig);
    IGenericClient terminologyClient = dataClient;
    if (terminologyServerConfig != null) {
        terminologyClient = clientBuilder.createFhirClient(terminologyServerConfig);
    }
    IParser parser = dataClient.getFhirContext().newJsonParser();
    String[] searchPaths = new String[] { "fhirResources", "fhirResources/libraries" };
    R4QualityMeasureResolverFactory resolverFactory = new R4QualityMeasureResolverFactory(parser);
    R4QualityMeasureResolvers resolvers = resolverFactory.fromZipStream(new ZipInputStream(inputStream), searchPaths);
    FhirResourceResolver<Library> libraryResolver = resolvers.getLibraryResolver();
    FhirResourceResolver<Measure> measureResolver = resolvers.getMeasureResolver();
    R4LibraryDependencyGatherer libraryDependencyGatherer = new R4LibraryDependencyGatherer(libraryResolver);
    CqlTerminologyProvider terminologyProvider = new R4RestFhirTerminologyProvider(terminologyClient);
    if (expandValueSets == null) {
        expandValueSets = R4DataProviderFactory.DEFAULT_IS_EXPAND_VALUE_SETS;
    }
    if (searchPageSize == null) {
        searchPageSize = R4DataProviderFactory.DEFAULT_PAGE_SIZE;
    }
    Map<String, CqlDataProvider> dataProviders = R4DataProviderFactory.createDataProviderMap(dataClient, terminologyProvider, retrieveCacheContext, R4FhirModelResolverFactory.createCachingResolver(), expandValueSets, searchPageSize);
    return new MeasureEvaluator(measureResolver, libraryResolver, libraryDependencyGatherer, terminologyProvider, dataProviders);
}
Also used : FhirClientBuilder(com.ibm.cohort.fhir.client.config.FhirClientBuilder) IGenericClient(ca.uhn.fhir.rest.client.api.IGenericClient) R4QualityMeasureResolverFactory(com.ibm.cohort.cql.hapi.resolver.R4QualityMeasureResolverFactory) R4RestFhirTerminologyProvider(com.ibm.cohort.engine.terminology.R4RestFhirTerminologyProvider) MeasureEvaluator(com.ibm.cohort.engine.measure.MeasureEvaluator) ZipInputStream(java.util.zip.ZipInputStream) Measure(org.hl7.fhir.r4.model.Measure) R4QualityMeasureResolvers(com.ibm.cohort.cql.hapi.resolver.R4QualityMeasureResolvers) R4LibraryDependencyGatherer(com.ibm.cohort.cql.hapi.R4LibraryDependencyGatherer) Library(org.hl7.fhir.r4.model.Library) CqlTerminologyProvider(com.ibm.cohort.cql.terminology.CqlTerminologyProvider) CqlDataProvider(com.ibm.cohort.cql.data.CqlDataProvider) IParser(ca.uhn.fhir.parser.IParser)

Example 2 with CqlDataProvider

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

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

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

the class R4DataProviderFactoryTest method verifyDataProviderMap.

private void verifyDataProviderMap(Map<String, CqlDataProvider> dataProviderMap) {
    Assert.assertEquals(1, dataProviderMap.size());
    DataProvider dataProvider = dataProviderMap.get(R4DataProviderFactory.FHIR_R4_URL);
    Iterable<Object> iterable = dataProvider.retrieve("Patient", "id", PATIENT_ID, "Patient", null, null, null, null, null, null, null, null);
    List<Object> list = new ArrayList<>();
    iterable.forEach(list::add);
    Assert.assertEquals(1, list.size());
    Object possiblePatient = list.get(0);
    Assert.assertTrue("Returned value not type Patient", possiblePatient instanceof Patient);
    Patient patient = (Patient) possiblePatient;
    AdministrativeGender actual = patient.getGender();
    Assert.assertEquals(AdministrativeGender.OTHER, actual);
}
Also used : CqlDataProvider(com.ibm.cohort.cql.data.CqlDataProvider) DataProvider(org.opencds.cqf.cql.engine.data.DataProvider) ArrayList(java.util.ArrayList) Patient(org.hl7.fhir.r4.model.Patient) AdministrativeGender(org.hl7.fhir.r4.model.Enumerations.AdministrativeGender)

Example 5 with CqlDataProvider

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

the class R4DataProviderFactoryTest method createDataProviderMap_noCacheContext.

@Test
public void createDataProviderMap_noCacheContext() {
    IGenericClient client = new Builder().withDefaultClient(getFhirServerConfig()).build().getDataClient();
    CqlTerminologyProvider terminologyProvider = new R4RestFhirTerminologyProvider(client);
    Map<String, CqlDataProvider> map = R4DataProviderFactory.createDataProviderMap(client, terminologyProvider, null);
    verifyDataProviderMap(map);
}
Also used : IGenericClient(ca.uhn.fhir.rest.client.api.IGenericClient) Builder(com.ibm.cohort.engine.measure.FHIRClientContext.Builder) R4RestFhirTerminologyProvider(com.ibm.cohort.engine.terminology.R4RestFhirTerminologyProvider) 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