use of com.ibm.cohort.cql.data.CqlDataProvider 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());
}
use of com.ibm.cohort.cql.data.CqlDataProvider 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());
}
use of com.ibm.cohort.cql.data.CqlDataProvider in project quality-measure-and-cohort-service by Alvearie.
the class SparkCqlEvaluator method evaluate.
/**
* Evaluate the input CQL for a single context + data pair.
*
* @param libraryProvider Library provider providing CQL/ELM content
* @param termProvider Terminology provider providing terminology resources
* @param funProvider External function provider providing static CQL functions
* @param contextName Context name corresponding to the library context key
* currently under evaluation.
* @param resultsSchema StructType containing the schema data for the output table
* that will be created.
* @param rowsByContext Data for a single evaluation context
* @param dataTypeAliases Mapping of data type to abstract type
* @param perContextAccum Spark accumulator that tracks each individual context
* evaluation
* @param errorAccum Spark accumulator that tracks CQL evaluation errors
* @param batchRunTime Single unified timestamp for all contexts
* @return Evaluation results for all expressions evaluated keyed by the context
* ID. Expression names are automatically namespaced according to the
* library name to avoid issues arising for expression names matching
* between libraries (e.g. LibraryName.ExpressionName).
* @throws Exception on general failure including CQL library loading issues
*/
protected Iterator<Tuple2<Object, Row>> evaluate(CqlLibraryProvider libraryProvider, CqlTerminologyProvider termProvider, ExternalFunctionProvider funProvider, String contextName, StructType resultsSchema, Tuple2<Object, List<Row>> rowsByContext, Map<String, String> dataTypeAliases, LongAccumulator perContextAccum, CollectionAccumulator<EvaluationError> errorAccum, ZonedDateTime batchRunTime) throws Exception {
// Convert the Spark objects to the cohort Java model
List<DataRow> datarows = rowsByContext._2().stream().map(getDataRowFactory()).collect(Collectors.toList());
Map<String, List<Object>> dataByDataType = new HashMap<>();
for (DataRow datarow : datarows) {
String dataType = (String) datarow.getValue(ContextRetriever.SOURCE_FACT_IDX);
List<Object> mappedRows = dataByDataType.computeIfAbsent(dataType, x -> new ArrayList<>());
mappedRows.add(datarow);
if (dataTypeAliases.containsKey(dataType)) {
String mappedType = dataTypeAliases.get(dataType);
List<Object> aliasedRows = dataByDataType.computeIfAbsent(mappedType, x -> new ArrayList<>());
aliasedRows.add(datarow);
}
}
DataRowRetrieveProvider retrieveProvider = new DataRowRetrieveProvider(dataByDataType, termProvider);
CqlDataProvider dataProvider = new DataRowDataProvider(getDataRowClass(), retrieveProvider);
CqlEvaluator evaluator = new CqlEvaluator().setLibraryProvider(libraryProvider).setDataProvider(dataProvider).setTerminologyProvider(termProvider).setExternalFunctionProvider(funProvider);
CqlEvaluationRequests requests = getFilteredJobSpecificationWithIds();
SparkOutputColumnEncoder columnEncoder = getSparkOutputColumnEncoder();
return evaluate(rowsByContext, contextName, resultsSchema, evaluator, requests, columnEncoder, perContextAccum, errorAccum, batchRunTime);
}
use of com.ibm.cohort.cql.data.CqlDataProvider in project quality-measure-and-cohort-service by Alvearie.
the class DataRowDataProviderTest method testEvaluateSuccess.
@Test
public void testEvaluateSuccess() throws Exception {
CqlLibraryProvider backingProvider = new ClasspathCqlLibraryProvider("cql");
CqlToElmTranslator translator = new CqlToElmTranslator();
try (Reader r = new FileReader(new File("src/test/resources/modelinfo/mock-modelinfo-1.0.0.xml"))) {
translator.registerModelInfo(r);
}
CqlLibraryProvider libraryProvider = new TranslatingCqlLibraryProvider(backingProvider, translator);
CqlVersionedIdentifier topLevelLibraryIdentifier = new CqlVersionedIdentifier("SampleLibrary", "1.0.0");
CqlTerminologyProvider terminologyProvider = new UnsupportedTerminologyProvider();
Map<String, Object> row = new HashMap<>();
row.put("id", "123");
row.put("gender", "female");
row.put("birthDate", new DateTime(OffsetDateTime.now()));
Map<String, Iterable<Object>> data = new HashMap<>();
data.put("Patient", Arrays.asList(new SimpleDataRow(row)));
DataRowRetrieveProvider retrieveProvider = new DataRowRetrieveProvider(data, terminologyProvider);
CqlDataProvider dataProvider = new CompositeCqlDataProvider(new DataRowModelResolver(SimpleDataRow.class), retrieveProvider);
CqlEvaluator evaluator = new CqlEvaluator().setLibraryProvider(libraryProvider).setTerminologyProvider(terminologyProvider).setDataProvider(dataProvider).setCacheContexts(false);
CqlEvaluationResult result = evaluator.evaluate(topLevelLibraryIdentifier);
assertEquals(2, result.getExpressionResults().size());
Object perDefineResult = result.getExpressionResults().get("IsFemale");
assertEquals(Boolean.TRUE, perDefineResult);
}
use of com.ibm.cohort.cql.data.CqlDataProvider in project quality-measure-and-cohort-service by Alvearie.
the class CqlEvaluatorTest method testSetGetSuccess.
@Test
public void testSetGetSuccess() {
CqlTerminologyProvider terminologyProvider = mock(CqlTerminologyProvider.class);
CqlDataProvider dataProvider = mock(CqlDataProvider.class);
CqlLibraryProvider libraryProvider = mock(CqlLibraryProvider.class);
CqlEvaluator evaluator = new CqlEvaluator().setTerminologyProvider(terminologyProvider).setDataProvider(dataProvider).setLibraryProvider(libraryProvider).setCacheContexts(false);
assertSame(terminologyProvider, evaluator.getTerminologyProvider());
assertSame(dataProvider, evaluator.getDataProvider());
assertSame(libraryProvider, evaluator.getLibraryProvider());
}
Aggregations