use of com.ibm.cohort.cql.evaluation.CqlEvaluator 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.evaluation.CqlEvaluator 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.evaluation.CqlEvaluator in project quality-measure-and-cohort-service by Alvearie.
the class CqlEvaluatorIntegrationTest method testUsingUSCoreWithTranslation.
@Test
public void testUsingUSCoreWithTranslation() throws Exception {
Patient patient = getPatient("123", AdministrativeGender.MALE, "1983-12-02");
CqlEvaluator evaluator = setupTestFor(patient, "cql.uscore");
String expression = "QueryByGender";
CqlEvaluationResult actual = evaluator.evaluate(new CqlVersionedIdentifier("TestUSCore", "1.0.0"), null, newPatientContext("123"), Collections.singleton(expression));
Assert.assertEquals(1, actual.getExpressionResults().size());
List<Object> value = (List) actual.getExpressionResults().get(expression);
Assert.assertEquals(1, value.size());
assertFhirEquals(patient, (IBaseResource) value.get(0));
}
use of com.ibm.cohort.cql.evaluation.CqlEvaluator in project quality-measure-and-cohort-service by Alvearie.
the class CqlEvaluatorIntegrationTest method testRequiredCQLParameterSpecifiedPatientInRange.
@Test
public void testRequiredCQLParameterSpecifiedPatientInRange() throws Exception {
Patient patient = getPatient("123", Enumerations.AdministrativeGender.FEMALE, "1978-05-06");
CqlEvaluator evaluator = setupTestFor(patient, "cql.parameters");
String expression = "Female";
Map<String, Parameter> parameters = new HashMap<>();
parameters.put("MaxAge", new IntegerParameter(50));
CqlEvaluationResult actual = evaluator.evaluate(new CqlVersionedIdentifier("TestWithParams", "1.0.0"), parameters, newPatientContext("123"), Collections.singleton(expression));
Map<String, Object> expected = new HashMap<>();
expected.put(expression, true);
Assert.assertEquals(expected, actual.getExpressionResults());
}
use of com.ibm.cohort.cql.evaluation.CqlEvaluator in project quality-measure-and-cohort-service by Alvearie.
the class CqlEvaluatorIntegrationTest method testUOMEquivalence_demonstrateConversionOfBothSides.
@Test
public void testUOMEquivalence_demonstrateConversionOfBothSides() throws Exception {
Patient patient = getPatient("123", Enumerations.AdministrativeGender.FEMALE, "1983-12-02");
CqlEvaluator evaluator = setupTestFor(patient, "cql.uomequivalence");
String expression = "UpConvert";
CqlEvaluationResult actual = evaluator.evaluate(new CqlVersionedIdentifier("TestUOMCompare", "1.0.0"), null, newPatientContext("123"), Collections.singleton(expression));
Map<String, Object> expected = new HashMap<>();
// The safest thing to do is convert the left and right
// values to a known, fixed unit
expected.put(expression, true);
Assert.assertEquals(expected, actual.getExpressionResults());
}
Aggregations