Search in sources :

Example 1 with R4LibraryDependencyGatherer

use of com.ibm.cohort.cql.hapi.R4LibraryDependencyGatherer 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 R4LibraryDependencyGatherer

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

the class MeasureEvaluationSeederTest method create_noLibraryOnMeasure.

@Test(expected = IllegalArgumentException.class)
public void create_noLibraryOnMeasure() {
    Measure measure = createMeasure();
    FhirResourceResolver<Library> libraryResolver = Mockito.mock(FhirResourceResolver.class);
    R4LibraryDependencyGatherer dependencyGatherer = Mockito.mock(R4LibraryDependencyGatherer.class);
    Mockito.when(dependencyGatherer.gatherForMeasure(measure)).thenReturn(Collections.emptyList());
    MeasureEvaluationSeeder seeder = new MeasureEvaluationSeeder(null, null, dependencyGatherer, libraryResolver);
    seeder.create(measure, periodStart, periodEnd, null, null);
}
Also used : Measure(org.hl7.fhir.r4.model.Measure) R4LibraryDependencyGatherer(com.ibm.cohort.cql.hapi.R4LibraryDependencyGatherer) Library(org.hl7.fhir.r4.model.Library) Test(org.junit.Test)

Example 3 with R4LibraryDependencyGatherer

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

the class MeasureEvaluationSeederTest method create_fullContext.

@Test
public void create_fullContext() throws IOException {
    TerminologyProvider terminologyProvider = Mockito.mock(TerminologyProvider.class);
    CqlDataProvider dataProvider = Mockito.mock(CqlDataProvider.class);
    Map<String, CqlDataProvider> dataProviders = new HashMap<>();
    dataProviders.put(fhirUri, dataProvider);
    Measure measure = createMeasure();
    Library library = createLibrary("/cql/seeder/Test-1.0.0.xml");
    FhirResourceResolver<Library> libraryResolver = Mockito.mock(FhirResourceResolver.class);
    Mockito.when(libraryResolver.resolveByName(libraryName, libraryVersion)).thenReturn(library);
    R4LibraryDependencyGatherer dependencyGatherer = Mockito.mock(R4LibraryDependencyGatherer.class);
    Mockito.when(dependencyGatherer.gatherForMeasure(measure)).thenReturn(Collections.singletonList(library));
    MeasureEvaluationSeeder seeder = new MeasureEvaluationSeeder(terminologyProvider, dataProviders, dependencyGatherer, libraryResolver);
    seeder.enableExpressionCaching();
    IMeasureEvaluationSeed actual = seeder.create(measure, periodStart, periodEnd, productLine, null);
    Interval expectedInterval = createInterval();
    Assert.assertEquals(0, expectedInterval.compareTo(actual.getMeasurementPeriod()));
    Assert.assertSame(measure, actual.getMeasure());
    Assert.assertSame(dataProvider, actual.getDataProvider());
    // Attempt to validate the `Context` by looking for key fields under our control.
    Assert.assertSame(terminologyProvider, actual.getContext().resolveTerminologyProvider());
    Assert.assertEquals(productLine, actual.getContext().resolveParameterRef(null, "Product Line"));
    Assert.assertTrue(actual.getContext().isExpressionCachingEnabled());
    Assert.assertNotNull(actual.getContext().getDebugMap());
}
Also used : TerminologyProvider(org.opencds.cqf.cql.engine.terminology.TerminologyProvider) HashMap(java.util.HashMap) Measure(org.hl7.fhir.r4.model.Measure) R4LibraryDependencyGatherer(com.ibm.cohort.cql.hapi.R4LibraryDependencyGatherer) Library(org.hl7.fhir.r4.model.Library) CqlDataProvider(com.ibm.cohort.cql.data.CqlDataProvider) Interval(org.opencds.cqf.cql.engine.runtime.Interval) Test(org.junit.Test)

Example 4 with R4LibraryDependencyGatherer

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

the class MeasureEvaluationSeederTest method create_minimalContext.

@Test
public void create_minimalContext() throws IOException {
    TerminologyProvider terminologyProvider = Mockito.mock(TerminologyProvider.class);
    CqlDataProvider dataProvider = Mockito.mock(CqlDataProvider.class);
    Map<String, CqlDataProvider> dataProviders = new HashMap<>();
    dataProviders.put(fhirUri, dataProvider);
    Library library = createLibrary("/cql/seeder/Test-1.0.0.xml");
    FhirResourceResolver<Library> libraryResolver = Mockito.mock(FhirResourceResolver.class);
    Mockito.when(libraryResolver.resolveByName(libraryName, libraryVersion)).thenReturn(library);
    Measure measure = createMeasure();
    R4LibraryDependencyGatherer dependencyGatherer = Mockito.mock(R4LibraryDependencyGatherer.class);
    Mockito.when(dependencyGatherer.gatherForMeasure(measure)).thenReturn(Collections.singletonList(library));
    MeasureEvaluationSeeder seeder = new MeasureEvaluationSeeder(terminologyProvider, dataProviders, dependencyGatherer, libraryResolver);
    seeder.disableDebugLogging();
    IMeasureEvaluationSeed actual = seeder.create(measure, periodStart, periodEnd, null, null);
    Interval expectedInterval = createInterval();
    Assert.assertEquals(0, expectedInterval.compareTo(actual.getMeasurementPeriod()));
    Assert.assertSame(measure, actual.getMeasure());
    Assert.assertSame(dataProvider, actual.getDataProvider());
    // Attempt to validate the `Context` by looking for key fields under our control.
    Assert.assertSame(terminologyProvider, actual.getContext().resolveTerminologyProvider());
    Assert.assertThrows(NullPointerException.class, () -> actual.getContext().resolveParameterRef(null, "Product Line"));
    Assert.assertFalse(actual.getContext().isExpressionCachingEnabled());
    Assert.assertNull(actual.getContext().getDebugMap());
}
Also used : TerminologyProvider(org.opencds.cqf.cql.engine.terminology.TerminologyProvider) HashMap(java.util.HashMap) Measure(org.hl7.fhir.r4.model.Measure) R4LibraryDependencyGatherer(com.ibm.cohort.cql.hapi.R4LibraryDependencyGatherer) Library(org.hl7.fhir.r4.model.Library) CqlDataProvider(com.ibm.cohort.cql.data.CqlDataProvider) Interval(org.opencds.cqf.cql.engine.runtime.Interval) Test(org.junit.Test)

Example 5 with R4LibraryDependencyGatherer

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

Aggregations

R4LibraryDependencyGatherer (com.ibm.cohort.cql.hapi.R4LibraryDependencyGatherer)8 Library (org.hl7.fhir.r4.model.Library)7 Measure (org.hl7.fhir.r4.model.Measure)7 CqlDataProvider (com.ibm.cohort.cql.data.CqlDataProvider)6 CqlTerminologyProvider (com.ibm.cohort.cql.terminology.CqlTerminologyProvider)4 R4RestFhirTerminologyProvider (com.ibm.cohort.engine.terminology.R4RestFhirTerminologyProvider)4 IGenericClient (ca.uhn.fhir.rest.client.api.IGenericClient)3 HashMap (java.util.HashMap)3 Test (org.junit.Test)3 IParser (ca.uhn.fhir.parser.IParser)2 JCommander (com.beust.jcommander.JCommander)2 Console (com.beust.jcommander.internal.Console)2 DefaultConsole (com.beust.jcommander.internal.DefaultConsole)2 R4QualityMeasureResolverFactory (com.ibm.cohort.cql.hapi.resolver.R4QualityMeasureResolverFactory)2 R4QualityMeasureResolvers (com.ibm.cohort.cql.hapi.resolver.R4QualityMeasureResolvers)2 MeasureEvaluator (com.ibm.cohort.engine.measure.MeasureEvaluator)2 DefaultRetrieveCacheContext (com.ibm.cohort.engine.measure.cache.DefaultRetrieveCacheContext)2 RetrieveCacheContext (com.ibm.cohort.engine.measure.cache.RetrieveCacheContext)2 FhirClientBuilder (com.ibm.cohort.fhir.client.config.FhirClientBuilder)2 FhirClientBuilderFactory (com.ibm.cohort.fhir.client.config.FhirClientBuilderFactory)2