use of com.ibm.cohort.cql.library.CqlLibraryProvider in project quality-measure-and-cohort-service by Alvearie.
the class SparkCqlEvaluator method createLibraryProvider.
/**
* Initialize a library provider that will load resources from the configured path
* in local storage or from the well-known classpath locations. The library provider
* comes configured with CQL translation enabled and will use custom modelinfo
* definitions if provided in the configuration.
*
* @return configured library provider
* @throws IOException when model info cannot be read
* @throws FileNotFoundException when a specified model info file cannot be found
*/
protected CqlLibraryProvider createLibraryProvider() throws IOException, FileNotFoundException {
CqlLibraryProvider hadoopBasedLp = new HadoopBasedCqlLibraryProvider(new Path(args.cqlPath), this.hadoopConfiguration.value());
// we are excluding the pre-compiled FHIRHelpers libraries because they were not compiled
// with the EnableResultTypes option that is required for some of the features of this program.
ClasspathCqlLibraryProvider cpBasedLp = new ClasspathCqlLibraryProvider();
cpBasedLp.setSupportedFormats(Format.CQL);
CqlLibraryProvider priorityLp = new PriorityCqlLibraryProvider(hadoopBasedLp, cpBasedLp);
return new TranslatingCqlLibraryProvider(priorityLp, getCqlTranslator());
}
use of com.ibm.cohort.cql.library.CqlLibraryProvider 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 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 if the model info or CQL libraries cannot be loaded for any
* reason
*/
protected Iterator<Tuple2<Object, Row>> evaluate(String contextName, StructType resultsSchema, Tuple2<Object, List<Row>> rowsByContext, Map<String, String> dataTypeAliases, LongAccumulator perContextAccum, CollectionAccumulator<EvaluationError> errorAccum, ZonedDateTime batchRunTime) throws Exception {
CqlLibraryProvider provider = libraryProvider.get();
if (provider == null) {
provider = createLibraryProvider();
libraryProvider.set(provider);
}
CqlTerminologyProvider termProvider = terminologyProvider.get();
if (termProvider == null) {
termProvider = createTerminologyProvider();
terminologyProvider.set(termProvider);
}
ExternalFunctionProvider funProvider = functionProvider.get();
if (funProvider == null) {
funProvider = createExternalFunctionProvider();
functionProvider.set(funProvider);
}
return evaluate(provider, termProvider, funProvider, contextName, resultsSchema, rowsByContext, dataTypeAliases, perContextAccum, errorAccum, batchRunTime);
}
use of com.ibm.cohort.cql.library.CqlLibraryProvider in project quality-measure-and-cohort-service by Alvearie.
the class ColumnRuleCreatorTest method testGetFiltersForContext.
@Test
public void testGetFiltersForContext() throws Exception {
CqlToElmTranslator cqlTranslator = new CqlToElmTranslator();
cqlTranslator.registerModelInfo(new File("src/test/resources/alltypes/modelinfo/alltypes-modelinfo-1.0.0.xml"));
ObjectMapper mapper = new ObjectMapper();
CqlEvaluationRequests requests = mapper.readValue(new File("src/test/resources/alltypes/metadata/parent-child-jobs.json"), CqlEvaluationRequests.class);
CqlLibraryProvider backingProvider = new ClasspathCqlLibraryProvider("alltypes.cql");
TranslatingCqlLibraryProvider cqlLibraryProvider = new TranslatingCqlLibraryProvider(backingProvider, cqlTranslator);
ColumnRuleCreator columnRuleCreator = new ColumnRuleCreator(requests.getEvaluations(), cqlTranslator, cqlLibraryProvider);
ContextDefinitions definitions = mapper.readValue(new File("src/test/resources/alltypes/metadata/context-definitions.json"), ContextDefinitions.class);
ContextDefinition context = definitions.getContextDefinitionByName("Patient");
Map<String, Set<StringMatcher>> actual = columnRuleCreator.getDataRequirementsForContext(context);
Map<String, Set<StringMatcher>> expected = new HashMap<>();
expected.put("A", new HashSet<>(Arrays.asList(new EqualsStringMatcher(ContextRetriever.SOURCE_FACT_IDX), new EqualsStringMatcher("pat_id"), new EqualsStringMatcher("code_col"), new EqualsStringMatcher("boolean_col"))));
assertEquals(expected, actual);
}
use of com.ibm.cohort.cql.library.CqlLibraryProvider in project quality-measure-and-cohort-service by Alvearie.
the class BaseDataTypeRequirementsProcessorTest method runPatternTest.
protected Map<String, Set<StringMatcher>> runPatternTest(String cqlPath, String modelInfoPath, Set<String> expressions, Predicate<Path> libraryFilter) throws IOException {
CqlToElmTranslator translator = createCqlTranslator(modelInfoPath);
CqlLibraryProvider sourceProvider = createLibrarySourceProvider(cqlPath, translator);
DataTypeRequirementsProcessor requirementsProcessor = new DataTypeRequirementsProcessor(translator);
Map<String, Set<StringMatcher>> pathMatchersByDataType = new HashMap<>();
if (libraryFilter == null) {
libraryFilter = x -> true;
}
List<Path> paths = Files.list(Paths.get(cqlPath)).filter(libraryFilter).collect(Collectors.toList());
for (Path path : paths) {
System.out.println("Processing " + path.toString());
CqlLibraryDescriptor cld = CqlLibraryHelpers.filenameToLibraryDescriptor(path.toString());
Map<String, Set<StringMatcher>> newPaths = requirementsProcessor.getAnyColumnRequirementsByDataType(sourceProvider, cld);
newPaths.forEach((key, value) -> {
pathMatchersByDataType.merge(key, value, (prev, current) -> {
prev.addAll(current);
return prev;
});
});
}
// System.out.println(pathMatchersByDataType);
return pathMatchersByDataType;
}
use of com.ibm.cohort.cql.library.CqlLibraryProvider in project quality-measure-and-cohort-service by Alvearie.
the class BaseDataTypeRequirementsProcessorTest method runPathTest.
protected Map<String, Set<String>> runPathTest(String cqlPath, String modelInfoPath, Set<String> expressions, Predicate<Path> libraryFilter) throws IOException {
CqlToElmTranslator translator = createCqlTranslator(modelInfoPath);
CqlLibraryProvider sourceProvider = createLibrarySourceProvider(cqlPath, translator);
DataTypeRequirementsProcessor requirementsProcessor = new DataTypeRequirementsProcessor(translator);
Map<String, Set<String>> pathsByDataType = new HashMap<>();
if (libraryFilter == null) {
libraryFilter = x -> true;
}
List<Path> paths = Files.list(Paths.get(cqlPath)).filter(libraryFilter).collect(Collectors.toList());
for (Path path : paths) {
System.out.println("Processing " + path.toString());
CqlLibraryDescriptor cld = CqlLibraryHelpers.filenameToLibraryDescriptor(path.toString());
Map<String, Set<String>> newPaths = requirementsProcessor.getPathRequirementsByDataType(sourceProvider, cld, expressions);
newPaths.forEach((key, value) -> {
pathsByDataType.merge(key, value, (prev, current) -> {
prev.addAll(current);
return prev;
});
});
}
// System.out.println(pathsByDataType);
return pathsByDataType;
}
Aggregations