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);
}
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;
}
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;
}
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);
}
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);
}
Aggregations