Search in sources :

Example 1 with FhirClientBuilderFactory

use of com.ibm.cohort.fhir.client.config.FhirClientBuilderFactory 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 2 with FhirClientBuilderFactory

use of com.ibm.cohort.fhir.client.config.FhirClientBuilderFactory 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 FhirClientBuilderFactory

use of com.ibm.cohort.fhir.client.config.FhirClientBuilderFactory in project health-patterns by LinuxForHealth.

the class CohortService method initializeCQLEngine.

/**
 * Initialized the CQL engine (using the instance's FHIR connection info) runnig over the {@value #CQL_DIRECTORY}.
 */
private void initializeCQLEngine() {
    FhirClientBuilderFactory factory = FhirClientBuilderFactory.newInstance();
    FhirContext fhirContext = FhirContext.forR4();
    // Currently the socket connection  may time out on a FHIR server
    // with several dozen patients so increasing the timeout to avoid that
    fhirContext.getRestfulClientFactory().setSocketTimeout(60 * 1000);
    FhirClientBuilder fhirBuilder = factory.newFhirClientBuilder(fhirContext);
    fhir = fhirBuilder.createFhirClient(fhirConnectionInfo);
    System.out.println("Created FHIR connection to " + fhirConnectionInfo.getEndpoint());
    cqlEngine = new CqlEngineWrapper(fhirBuilder);
    cqlEngine.setDataServerClient(fhir);
    cqlEngine.setMeasureServerClient(fhir);
    cqlEngine.setTerminologyServerClient(fhir);
    resetCQLDirectory();
}
Also used : FhirContext(ca.uhn.fhir.context.FhirContext) FhirClientBuilder(com.ibm.cohort.fhir.client.config.FhirClientBuilder) CqlEngineWrapper(com.ibm.cohort.engine.CqlEngineWrapper) FhirClientBuilderFactory(com.ibm.cohort.fhir.client.config.FhirClientBuilderFactory)

Example 4 with FhirClientBuilderFactory

use of com.ibm.cohort.fhir.client.config.FhirClientBuilderFactory in project quality-measure-and-cohort-service by Alvearie.

the class CohortCLI method runWithArgs.

/**
 * Simulate main method behavior in a non-static context for use in testing
 * tools. This method is intended to be called only once. Multiple calls for the
 * same library path will attempt duplicate library loading.
 *
 * @param args parameter values
 * @param out  location where contents that would normally go to stdout should
 *             be written
 * @return CQLEvaluator
 * @throws IOException IOException
 */
public CqlEvaluator runWithArgs(String[] args, PrintStream out) throws IOException {
    Arguments arguments = new Arguments();
    Console console = new DefaultConsole(out);
    JCommander jc = JCommander.newBuilder().programName("cql-engine").console(console).addObject(arguments).build();
    jc.parse(args);
    CqlEvaluator wrapper = null;
    if (arguments.isDisplayHelp) {
        jc.usage();
    } else {
        FhirClientBuilderFactory factory = FhirClientBuilderFactory.newInstance();
        FhirClientBuilder fhirClientBuilder = factory.newFhirClientBuilder();
        readConnectionConfiguration(arguments);
        MapCqlLibraryProviderFactory libraryProviderFactory = new MapCqlLibraryProviderFactory();
        String[] filters = null;
        if (arguments.filters != null) {
            filters = arguments.filters.toArray(new String[arguments.filters.size()]);
        }
        CqlLibraryProvider backingLibraryProvider;
        Path libraryFolder = Paths.get(arguments.libraryPath);
        if (libraryFolder.toFile().isDirectory()) {
            out.println(String.format("Loading libraries from folder '%s'", libraryFolder.toString()));
            backingLibraryProvider = libraryProviderFactory.fromDirectory(libraryFolder, filters);
        } else if (FileHelpers.isZip(libraryFolder.toFile())) {
            out.println(String.format("Loading libraries from ZIP '%s'", libraryFolder.toString()));
            backingLibraryProvider = libraryProviderFactory.fromZipFile(libraryFolder, filters);
        } else {
            out.println(String.format("Loading libraries from FHIR Library '%s'", libraryFolder.toString()));
            IGenericClient measureClient = fhirClientBuilder.createFhirClient(measureServerConfig);
            FhirResourceResolver<Library> libraryResolver = R4FhirServerResourceResolverFactory.createLibraryResolver(measureClient);
            R4LibraryDependencyGatherer dependencyGatherer = new R4LibraryDependencyGatherer(libraryResolver);
            List<Library> cqlLibraries = dependencyGatherer.gatherForLibraryId(arguments.libraryPath);
            Map<CqlLibraryDescriptor, CqlLibrary> cqlLibraryMap = toCqlLibraryMap(cqlLibraries);
            backingLibraryProvider = new MapCqlLibraryProvider(cqlLibraryMap);
        }
        CqlLibraryProvider fhirClasspathProvider = new ClasspathCqlLibraryProvider();
        backingLibraryProvider = new PriorityCqlLibraryProvider(backingLibraryProvider, fhirClasspathProvider);
        CqlToElmTranslator translator = new CqlToElmTranslator();
        if (arguments.modelInfoFile != null && arguments.modelInfoFile.exists()) {
            translator.registerModelInfo(arguments.modelInfoFile);
        }
        boolean isForceTranslation = arguments.sourceFormat == Format.CQL;
        CqlLibraryProvider libraryProvider = new TranslatingCqlLibraryProvider(backingLibraryProvider, translator, isForceTranslation);
        IGenericClient dataClient = fhirClientBuilder.createFhirClient(dataServerConfig);
        IGenericClient termClient = fhirClientBuilder.createFhirClient(terminologyServerConfig);
        CqlTerminologyProvider termProvider = new R4RestFhirTerminologyProvider(termClient);
        Map<String, com.ibm.cohort.cql.evaluation.parameters.Parameter> parameters = null;
        if (arguments.parameters != null) {
            parameters = parseParameterArguments(arguments.parameters);
        }
        CqlVersionedIdentifier libraryIdentifier = new CqlVersionedIdentifier(arguments.libraryName, arguments.libraryVersion);
        List<Pair<String, String>> contexts;
        if (arguments.contextIds == null || arguments.contextIds.isEmpty()) {
            // If no context ids are provided, perform one run using a null context
            contexts = Collections.singletonList(null);
        } else {
            contexts = arguments.contextIds.stream().map(x -> new ImmutablePair<>(arguments.contextName, x)).collect(Collectors.toList());
        }
        try (RetrieveCacheContext cacheContext = new DefaultRetrieveCacheContext()) {
            CqlDataProvider dataProvider = R4DataProviderFactory.createDataProvider(dataClient, termProvider, cacheContext, R4FhirModelResolverFactory.createCachingResolver(), !arguments.enableTerminologyOptimization, arguments.searchPageSize);
            wrapper = new CqlEvaluator().setLibraryProvider(libraryProvider).setDataProvider(dataProvider).setTerminologyProvider(termProvider);
            ZonedDateTime evaluationDateTime = ZonedDateTime.now();
            for (Pair<String, String> context : contexts) {
                String contextLabel = context == null ? "null" : context.getRight();
                out.println("Context: " + contextLabel);
                CqlEvaluationResult result = wrapper.evaluate(libraryIdentifier, parameters, context, arguments.expressions, arguments.loggingLevel, evaluationDateTime);
                out.print(prettyPrintResult(result));
                out.println("---");
            }
        }
    }
    return wrapper;
}
Also used : FhirResourceResolver(com.ibm.cohort.cql.fhir.resolver.FhirResourceResolver) DefaultConsole(com.beust.jcommander.internal.DefaultConsole) TranslatingCqlLibraryProvider(com.ibm.cohort.cql.translation.TranslatingCqlLibraryProvider) 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) MapCqlLibraryProvider(com.ibm.cohort.cql.library.MapCqlLibraryProvider) 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) ZonedDateTime(java.time.ZonedDateTime) JCommander(com.beust.jcommander.JCommander) Console(com.beust.jcommander.internal.Console) DefaultConsole(com.beust.jcommander.internal.DefaultConsole) R4LibraryDependencyGatherer(com.ibm.cohort.cql.hapi.R4LibraryDependencyGatherer) List(java.util.List) MapCqlLibraryProviderFactory(com.ibm.cohort.cql.library.MapCqlLibraryProviderFactory) CqlTerminologyProvider(com.ibm.cohort.cql.terminology.CqlTerminologyProvider) CqlDataProvider(com.ibm.cohort.cql.data.CqlDataProvider) Pair(org.apache.commons.lang3.tuple.Pair) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) Path(java.nio.file.Path) FhirClientBuilder(com.ibm.cohort.fhir.client.config.FhirClientBuilder) ParameterHelper.parseParameterArguments(com.ibm.cohort.cli.ParameterHelper.parseParameterArguments) MapCqlLibraryProvider(com.ibm.cohort.cql.library.MapCqlLibraryProvider) CqlEvaluationResult(com.ibm.cohort.cql.evaluation.CqlEvaluationResult) DefaultRetrieveCacheContext(com.ibm.cohort.engine.measure.cache.DefaultRetrieveCacheContext) PriorityCqlLibraryProvider(com.ibm.cohort.cql.library.PriorityCqlLibraryProvider) FhirClientBuilderFactory(com.ibm.cohort.fhir.client.config.FhirClientBuilderFactory) Parameter(com.beust.jcommander.Parameter) ClasspathCqlLibraryProvider(com.ibm.cohort.cql.library.ClasspathCqlLibraryProvider) Map(java.util.Map) HashMap(java.util.HashMap) CqlEvaluator(com.ibm.cohort.cql.evaluation.CqlEvaluator) CqlVersionedIdentifier(com.ibm.cohort.cql.library.CqlVersionedIdentifier)

Example 5 with FhirClientBuilderFactory

use of com.ibm.cohort.fhir.client.config.FhirClientBuilderFactory in project quality-measure-and-cohort-service by Alvearie.

the class MeasureCLI method runWithArgs.

public MeasureEvaluator runWithArgs(String[] args, PrintStream out) throws IOException {
    MeasureEvaluator evaluator = null;
    Arguments arguments = new Arguments();
    Console console = new DefaultConsole(out);
    JCommander jc = JCommander.newBuilder().programName("measure-engine").console(console).addObject(arguments).build();
    jc.parse(args);
    if (arguments.isDisplayHelp) {
        jc.usage();
    } else {
        arguments.validate();
        readDataServerConfiguration(arguments);
        readTerminologyServerConfiguration(arguments);
        FhirClientBuilderFactory fhirClientBuilderFactory = getFhirClientBuilderFactory();
        IGenericClient dataServerClient = fhirClientBuilderFactory.newFhirClientBuilder().createFhirClient(dataServerConfig);
        IGenericClient terminologyServerClient = fhirClientBuilderFactory.newFhirClientBuilder().createFhirClient(terminologyServerConfig);
        String[] filters = (arguments.filters != null) ? arguments.filters.toArray(new String[arguments.filters.size()]) : null;
        IParser parser = getFhirContext().newJsonParser().setPrettyPrint(true);
        R4QualityMeasureResolverFactory resolverFactory = new R4QualityMeasureResolverFactory(parser);
        FhirResourceResolver<Library> libraryResolver;
        FhirResourceResolver<Measure> measureResolver;
        if (arguments.measureServerConfigFile != null && FileHelpers.isZip(arguments.measureServerConfigFile)) {
            R4QualityMeasureResolvers resolvers = resolverFactory.fromZipFile(arguments.measureServerConfigFile.toPath(), filters);
            libraryResolver = resolvers.getLibraryResolver();
            measureResolver = resolvers.getMeasureResolver();
        } else if (arguments.measureServerConfigFile != null && arguments.measureServerConfigFile.isDirectory()) {
            R4QualityMeasureResolvers resolvers = resolverFactory.fromDirectory(arguments.measureServerConfigFile.toPath(), filters);
            libraryResolver = resolvers.getLibraryResolver();
            measureResolver = resolvers.getMeasureResolver();
        } else {
            readMeasureServerConfiguration(arguments);
            IGenericClient measureServerClient = fhirClientBuilderFactory.newFhirClientBuilder().createFhirClient(measureServerConfig);
            libraryResolver = R4FhirServerResourceResolverFactory.createLibraryResolver(measureServerClient);
            measureResolver = R4FhirServerResourceResolverFactory.createMeasureResolver(measureServerClient);
        }
        List<MeasureContext> measureContexts;
        if (arguments.measureConfigurationFile != null) {
            measureContexts = MeasureContextProvider.getMeasureContexts(arguments.measureConfigurationFile);
        } else {
            measureContexts = MeasureContextProvider.getMeasureContexts(arguments.resourceId, arguments.parameters);
        }
        validateMeasureContexts(measureContexts);
        R4LibraryDependencyGatherer libraryDependencyGatherer = new R4LibraryDependencyGatherer(libraryResolver);
        CqlTerminologyProvider terminologyProvider = new R4RestFhirTerminologyProvider(terminologyServerClient);
        try (RetrieveCacheContext retrieveCacheContext = arguments.disableRetrieveCache ? null : new DefaultRetrieveCacheContext()) {
            Map<String, CqlDataProvider> dataProviders = R4DataProviderFactory.createDataProviderMap(dataServerClient, terminologyProvider, retrieveCacheContext, R4FhirModelResolverFactory.createCachingResolver(), !arguments.enableTerminologyOptimization, arguments.searchPageSize);
            evaluator = new MeasureEvaluator(measureResolver, libraryResolver, libraryDependencyGatherer, terminologyProvider, dataProviders);
            for (String contextId : arguments.contextIds) {
                out.println("Evaluating: " + contextId);
                // Reports only returned for measures where patient is in initial population
                List<MeasureReport> reports = evaluator.evaluatePatientMeasures(contextId, measureContexts, new MeasureEvidenceOptions(arguments.includeEvaluatedResources, arguments.defineReturnOption));
                for (MeasureReport report : reports) {
                    if (arguments.reportFormat == ReportFormat.TEXT) {
                        out.println("Result for " + report.getMeasure());
                        for (MeasureReport.MeasureReportGroupComponent group : report.getGroup()) {
                            for (MeasureReport.MeasureReportGroupPopulationComponent pop : group.getPopulation()) {
                                String popCode = pop.getCode().getCodingFirstRep().getCode();
                                if (pop.getId() != null) {
                                    popCode += "(" + pop.getId() + ")";
                                }
                                out.println(String.format("Population: %s = %d", popCode, pop.getCount()));
                            }
                        }
                    } else {
                        out.println(parser.encodeResourceToString(report));
                    }
                    out.println("---");
                }
                if (reports.isEmpty()) {
                    out.println("---");
                }
            }
        }
    }
    return evaluator;
}
Also used : DefaultConsole(com.beust.jcommander.internal.DefaultConsole) IGenericClient(ca.uhn.fhir.rest.client.api.IGenericClient) R4QualityMeasureResolverFactory(com.ibm.cohort.cql.hapi.resolver.R4QualityMeasureResolverFactory) R4RestFhirTerminologyProvider(com.ibm.cohort.engine.terminology.R4RestFhirTerminologyProvider) DefaultRetrieveCacheContext(com.ibm.cohort.engine.measure.cache.DefaultRetrieveCacheContext) RetrieveCacheContext(com.ibm.cohort.engine.measure.cache.RetrieveCacheContext) MeasureEvidenceOptions(com.ibm.cohort.engine.measure.evidence.MeasureEvidenceOptions) MeasureContext(com.ibm.cohort.engine.measure.MeasureContext) JCommander(com.beust.jcommander.JCommander) Console(com.beust.jcommander.internal.Console) DefaultConsole(com.beust.jcommander.internal.DefaultConsole) Measure(org.hl7.fhir.r4.model.Measure) R4LibraryDependencyGatherer(com.ibm.cohort.cql.hapi.R4LibraryDependencyGatherer) CqlTerminologyProvider(com.ibm.cohort.cql.terminology.CqlTerminologyProvider) CqlDataProvider(com.ibm.cohort.cql.data.CqlDataProvider) MeasureReport(org.hl7.fhir.r4.model.MeasureReport) MeasureEvaluator(com.ibm.cohort.engine.measure.MeasureEvaluator) DefaultRetrieveCacheContext(com.ibm.cohort.engine.measure.cache.DefaultRetrieveCacheContext) FhirClientBuilderFactory(com.ibm.cohort.fhir.client.config.FhirClientBuilderFactory) R4QualityMeasureResolvers(com.ibm.cohort.cql.hapi.resolver.R4QualityMeasureResolvers) Library(org.hl7.fhir.r4.model.Library) IParser(ca.uhn.fhir.parser.IParser)

Aggregations

FhirClientBuilderFactory (com.ibm.cohort.fhir.client.config.FhirClientBuilderFactory)9 FhirClientBuilder (com.ibm.cohort.fhir.client.config.FhirClientBuilder)8 IGenericClient (ca.uhn.fhir.rest.client.api.IGenericClient)7 CqlTerminologyProvider (com.ibm.cohort.cql.terminology.CqlTerminologyProvider)4 R4RestFhirTerminologyProvider (com.ibm.cohort.engine.terminology.R4RestFhirTerminologyProvider)4 Before (org.junit.Before)4 CqlDataProvider (com.ibm.cohort.cql.data.CqlDataProvider)3 ClasspathCqlLibraryProvider (com.ibm.cohort.cql.library.ClasspathCqlLibraryProvider)3 CqlLibraryProvider (com.ibm.cohort.cql.library.CqlLibraryProvider)3 CqlToElmTranslator (com.ibm.cohort.cql.translation.CqlToElmTranslator)3 TranslatingCqlLibraryProvider (com.ibm.cohort.cql.translation.TranslatingCqlLibraryProvider)3 DefaultRetrieveCacheContext (com.ibm.cohort.engine.measure.cache.DefaultRetrieveCacheContext)3 RetrieveCacheContext (com.ibm.cohort.engine.measure.cache.RetrieveCacheContext)3 JCommander (com.beust.jcommander.JCommander)2 Console (com.beust.jcommander.internal.Console)2 DefaultConsole (com.beust.jcommander.internal.DefaultConsole)2 CqlEvaluator (com.ibm.cohort.cql.evaluation.CqlEvaluator)2 R4LibraryDependencyGatherer (com.ibm.cohort.cql.hapi.R4LibraryDependencyGatherer)2 MapCqlLibraryProviderFactory (com.ibm.cohort.cql.library.MapCqlLibraryProviderFactory)2 PriorityCqlLibraryProvider (com.ibm.cohort.cql.library.PriorityCqlLibraryProvider)2