Search in sources :

Example 1 with CqlTerminologyProvider

use of com.ibm.cohort.cql.terminology.CqlTerminologyProvider 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 CqlTerminologyProvider

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

the class CohortEngineRestHandler method evaluateCohort.

@POST
@Path("/cohort-evaluation")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces({ MediaType.APPLICATION_JSON })
@ApiOperation(value = "Evaluates a specific define within a CQL for a set of patients", notes = COHORT_EVALUATION_API_NOTES, response = String.class, tags = { "Cohort Evaluation" }, nickname = "evaluate_cohort", extensions = { @Extension(properties = { @ExtensionProperty(name = DarkFeatureSwaggerFilter.DARK_FEATURE_NAME, value = CohortEngineRestConstants.DARK_LAUNCHED_COHORT_EVALUATION) }) })
@ApiImplicitParams({ // This is necessary for the dark launch feature
@ApiImplicitParam(access = DarkFeatureSwaggerFilter.DARK_FEATURE_CONTROLLED, paramType = "header", dataType = "string"), @ApiImplicitParam(name = REQUEST_DATA_PART, value = EXAMPLE_COHORT_REQUEST_DATA_JSON, dataTypeClass = CohortEvaluation.class, required = true, paramType = "form", type = "file"), @ApiImplicitParam(name = CQL_DEFINITION, value = CQL_REQUIREMENTS, dataTypeClass = File.class, required = true, paramType = "form", type = "file") })
@ApiResponses(value = { @ApiResponse(code = 200, message = "Successful Operation", response = CohortResult.class), @ApiResponse(code = 400, message = "Bad Request", response = ServiceErrorList.class), @ApiResponse(code = 500, message = "Server Error", response = ServiceErrorList.class) })
public Response evaluateCohort(@Context HttpServletRequest request, @ApiParam(value = ServiceBaseConstants.MINOR_VERSION_DESCRIPTION, required = true, defaultValue = ServiceBuildConstants.DATE) @QueryParam(CohortEngineRestHandler.VERSION) String version, @ApiParam(hidden = true, type = "file", required = true) IMultipartBody multipartBody) {
    final String methodName = MethodNames.EVALUATE_COHORT.getName();
    Response response = null;
    // Error out if feature is not enabled
    ServiceBaseUtility.isDarkFeatureEnabled(CohortEngineRestConstants.DARK_LAUNCHED_MEASURE_EVALUATION);
    try {
        // Perform api setup
        Response errorResponse = ServiceBaseUtility.apiSetup(version, logger, methodName);
        if (errorResponse != null) {
            return errorResponse;
        }
        if (multipartBody == null) {
            throw new IllegalArgumentException("A multipart/form-data body is required");
        }
        IAttachment metadataAttachment = multipartBody.getAttachment(REQUEST_DATA_PART);
        if (metadataAttachment == null) {
            throw new IllegalArgumentException(String.format("Missing '%s' MIME attachment", REQUEST_DATA_PART));
        }
        // deserialize the MeasuresEvaluation request
        ObjectMapper om = new ObjectMapper();
        CohortEvaluation evaluationRequest = om.readValue(metadataAttachment.getDataHandler().getInputStream(), CohortEvaluation.class);
        FhirServerConfig dataServerConfig = evaluationRequest.getDataServerConfig();
        FhirServerConfig terminologyServerConfig = evaluationRequest.getTerminologyServerConfig() == null ? evaluationRequest.getDataServerConfig() : evaluationRequest.getTerminologyServerConfig();
        IAttachment cqlAttachment = multipartBody.getAttachment(CQL_DEFINITION);
        if (cqlAttachment == null) {
            throw new IllegalArgumentException(String.format("Missing '%s' MIME attachment", CQL_DEFINITION));
        }
        // validate the contents of the fhirServerConfig
        validateBean(dataServerConfig);
        validateBean(terminologyServerConfig);
        FhirClientBuilderFactory clientBuilderFactory = FhirClientBuilderFactory.newInstance();
        FhirClientBuilder clientBuilder = clientBuilderFactory.newFhirClientBuilder();
        CqlLibraryProvider libraryProvider;
        try (InputStream is = cqlAttachment.getDataHandler().getInputStream()) {
            ZipInputStream zis = new ZipInputStream(is);
            MapCqlLibraryProviderFactory libraryProviderFactory = new MapCqlLibraryProviderFactory();
            String[] attachmentHeaders = cqlAttachment.getHeader("Content-Disposition").split(";");
            String filename = null;
            for (String header : attachmentHeaders) {
                if (header.contains("filename")) {
                    filename = FilenameUtils.removeExtension(header.split("=")[1].replaceAll("\"", ""));
                }
            }
            String[] searchPaths = new String[] { "cql", filename + "/cql" };
            libraryProvider = libraryProviderFactory.fromZipStream(zis, searchPaths);
        }
        CqlLibraryProvider fhirClasspathProvider = new ClasspathCqlLibraryProvider();
        libraryProvider = new PriorityCqlLibraryProvider(libraryProvider, fhirClasspathProvider);
        CqlToElmTranslator translator = new CqlToElmTranslator();
        libraryProvider = new TranslatingCqlLibraryProvider(libraryProvider, translator);
        IGenericClient dataClient = clientBuilder.createFhirClient(dataServerConfig);
        IGenericClient termClient = clientBuilder.createFhirClient(terminologyServerConfig);
        CqlTerminologyProvider termProvider = new R4RestFhirTerminologyProvider(termClient);
        List<String> passingPatients;
        try (RetrieveCacheContext retrieveCacheContext = new DefaultRetrieveCacheContext()) {
            passingPatients = evaluateCohort(dataClient, termProvider, libraryProvider, retrieveCacheContext, evaluationRequest);
        }
        response = Response.status(Response.Status.OK).header("Content-Type", "application/json").entity(new CohortResult(passingPatients)).build();
    } catch (Throwable e) {
        // map any exceptions caught into the proper REST error response objects
        response = new CohortServiceExceptionMapper().toResponse(e);
    } finally {
        // Perform api cleanup
        Response errorResponse = ServiceBaseUtility.apiCleanup(logger, methodName);
        if (errorResponse != null) {
            response = errorResponse;
        }
    }
    return response;
}
Also used : TranslatingCqlLibraryProvider(com.ibm.cohort.cql.translation.TranslatingCqlLibraryProvider) CohortEvaluation(com.ibm.cohort.engine.api.service.model.CohortEvaluation) IGenericClient(ca.uhn.fhir.rest.client.api.IGenericClient) R4RestFhirTerminologyProvider(com.ibm.cohort.engine.terminology.R4RestFhirTerminologyProvider) DefaultRetrieveCacheContext(com.ibm.cohort.engine.measure.cache.DefaultRetrieveCacheContext) RetrieveCacheContext(com.ibm.cohort.engine.measure.cache.RetrieveCacheContext) CqlToElmTranslator(com.ibm.cohort.cql.translation.CqlToElmTranslator) CqlLibraryProvider(com.ibm.cohort.cql.library.CqlLibraryProvider) PriorityCqlLibraryProvider(com.ibm.cohort.cql.library.PriorityCqlLibraryProvider) TranslatingCqlLibraryProvider(com.ibm.cohort.cql.translation.TranslatingCqlLibraryProvider) ClasspathCqlLibraryProvider(com.ibm.cohort.cql.library.ClasspathCqlLibraryProvider) IAttachment(com.ibm.websphere.jaxrs20.multipart.IAttachment) MapCqlLibraryProviderFactory(com.ibm.cohort.cql.library.MapCqlLibraryProviderFactory) CqlTerminologyProvider(com.ibm.cohort.cql.terminology.CqlTerminologyProvider) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) FhirClientBuilder(com.ibm.cohort.fhir.client.config.FhirClientBuilder) ZipInputStream(java.util.zip.ZipInputStream) InputStream(java.io.InputStream) CohortResult(com.ibm.cohort.engine.api.service.model.CohortResult) DefaultRetrieveCacheContext(com.ibm.cohort.engine.measure.cache.DefaultRetrieveCacheContext) PriorityCqlLibraryProvider(com.ibm.cohort.cql.library.PriorityCqlLibraryProvider) Response(javax.ws.rs.core.Response) ApiResponse(io.swagger.annotations.ApiResponse) ZipInputStream(java.util.zip.ZipInputStream) FhirClientBuilderFactory(com.ibm.cohort.fhir.client.config.FhirClientBuilderFactory) FhirServerConfig(com.ibm.cohort.fhir.client.config.FhirServerConfig) ClasspathCqlLibraryProvider(com.ibm.cohort.cql.library.ClasspathCqlLibraryProvider) Path(javax.ws.rs.Path) ApiImplicitParams(io.swagger.annotations.ApiImplicitParams) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 3 with CqlTerminologyProvider

use of com.ibm.cohort.cql.terminology.CqlTerminologyProvider 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 CqlTerminologyProvider

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

Example 5 with CqlTerminologyProvider

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

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