use of com.ibm.cohort.cql.data.CqlDataProvider 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;
}
use of com.ibm.cohort.cql.data.CqlDataProvider 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;
}
use of com.ibm.cohort.cql.data.CqlDataProvider in project quality-measure-and-cohort-service by Alvearie.
the class R4DataProviderFactory method createDataProviderMap.
public static Map<String, CqlDataProvider> createDataProviderMap(IGenericClient client, CqlTerminologyProvider terminologyProvider, RetrieveCacheContext retrieveCacheContext, ModelResolver modelResolver, boolean isExpandValueSets, Integer pageSize) {
CqlDataProvider dataProvider = createDataProvider(client, terminologyProvider, retrieveCacheContext, modelResolver, isExpandValueSets, pageSize);
Map<String, CqlDataProvider> retVal = new HashMap<>();
retVal.put(FHIR_R4_URL, dataProvider);
return retVal;
}
use of com.ibm.cohort.cql.data.CqlDataProvider in project quality-measure-and-cohort-service by Alvearie.
the class R4MeasureEvaluatorBuilder method build.
public MeasureEvaluator build() {
if (clientContext == null) {
throw new IllegalArgumentException("Client context not provided");
}
CqlTerminologyProvider terminologyProvider = new R4RestFhirTerminologyProvider(clientContext.getTerminologyClient());
Map<String, CqlDataProvider> dataProviders = R4DataProviderFactory.createDataProviderMap(clientContext.getDataClient(), terminologyProvider, cacheContext, isCachingModelResolver ? new CachingModelResolverDecorator(modelResolver) : modelResolver, isExpandValueSets, pageSize);
FhirResourceResolver<Measure> measureResolver = R4FhirServerResourceResolverFactory.createMeasureResolver(clientContext.getMeasureClient());
FhirResourceResolver<Library> libraryResolver = R4FhirServerResourceResolverFactory.createLibraryResolver(clientContext.getLibraryClient());
R4LibraryDependencyGatherer libraryDependencyGatherer = new R4LibraryDependencyGatherer(libraryResolver);
return new MeasureEvaluator(measureResolver, libraryResolver, libraryDependencyGatherer, terminologyProvider, dataProviders);
}
use of com.ibm.cohort.cql.data.CqlDataProvider in project quality-measure-and-cohort-service by Alvearie.
the class R4DataProviderFactoryTest method createDataProviderMap_withCacheContext.
@Test
public void createDataProviderMap_withCacheContext() throws Exception {
IGenericClient client = new Builder().withDefaultClient(getFhirServerConfig()).build().getDataClient();
CqlTerminologyProvider terminologyProvider = new R4RestFhirTerminologyProvider(client);
try (RetrieveCacheContext cacheContext = new DefaultRetrieveCacheContext(new CaffeineConfiguration<>())) {
Map<String, CqlDataProvider> map = R4DataProviderFactory.createDataProviderMap(client, terminologyProvider, cacheContext);
verifyDataProviderMap(map);
}
}
Aggregations