Search in sources :

Example 16 with CqlLibraryProvider

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());
}
Also used : Path(org.apache.hadoop.fs.Path) TranslatingCqlLibraryProvider(com.ibm.cohort.cql.translation.TranslatingCqlLibraryProvider) HadoopBasedCqlLibraryProvider(com.ibm.cohort.cql.library.HadoopBasedCqlLibraryProvider) CqlLibraryProvider(com.ibm.cohort.cql.library.CqlLibraryProvider) PriorityCqlLibraryProvider(com.ibm.cohort.cql.library.PriorityCqlLibraryProvider) HadoopBasedCqlLibraryProvider(com.ibm.cohort.cql.library.HadoopBasedCqlLibraryProvider) TranslatingCqlLibraryProvider(com.ibm.cohort.cql.translation.TranslatingCqlLibraryProvider) ClasspathCqlLibraryProvider(com.ibm.cohort.cql.library.ClasspathCqlLibraryProvider) ClasspathCqlLibraryProvider(com.ibm.cohort.cql.library.ClasspathCqlLibraryProvider) PriorityCqlLibraryProvider(com.ibm.cohort.cql.library.PriorityCqlLibraryProvider)

Example 17 with CqlLibraryProvider

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);
}
Also used : ExternalFunctionProvider(org.opencds.cqf.cql.engine.data.ExternalFunctionProvider) CohortExternalFunctionProvider(com.ibm.cohort.cql.functions.CohortExternalFunctionProvider) CqlLibraryProvider(com.ibm.cohort.cql.library.CqlLibraryProvider) PriorityCqlLibraryProvider(com.ibm.cohort.cql.library.PriorityCqlLibraryProvider) HadoopBasedCqlLibraryProvider(com.ibm.cohort.cql.library.HadoopBasedCqlLibraryProvider) TranslatingCqlLibraryProvider(com.ibm.cohort.cql.translation.TranslatingCqlLibraryProvider) ClasspathCqlLibraryProvider(com.ibm.cohort.cql.library.ClasspathCqlLibraryProvider) CqlTerminologyProvider(com.ibm.cohort.cql.terminology.CqlTerminologyProvider)

Example 18 with CqlLibraryProvider

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);
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) TranslatingCqlLibraryProvider(com.ibm.cohort.cql.translation.TranslatingCqlLibraryProvider) HashMap(java.util.HashMap) CqlToElmTranslator(com.ibm.cohort.cql.translation.CqlToElmTranslator) ClasspathCqlLibraryProvider(com.ibm.cohort.cql.library.ClasspathCqlLibraryProvider) CqlLibraryProvider(com.ibm.cohort.cql.library.CqlLibraryProvider) TranslatingCqlLibraryProvider(com.ibm.cohort.cql.translation.TranslatingCqlLibraryProvider) EqualsStringMatcher(com.ibm.cohort.cql.util.EqualsStringMatcher) CqlEvaluationRequests(com.ibm.cohort.cql.evaluation.CqlEvaluationRequests) ClasspathCqlLibraryProvider(com.ibm.cohort.cql.library.ClasspathCqlLibraryProvider) File(java.io.File) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 19 with CqlLibraryProvider

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;
}
Also used : Path(java.nio.file.Path) Set(java.util.Set) HashMap(java.util.HashMap) CqlToElmTranslator(com.ibm.cohort.cql.translation.CqlToElmTranslator) ClasspathCqlLibraryProvider(com.ibm.cohort.cql.library.ClasspathCqlLibraryProvider) PriorityCqlLibraryProvider(com.ibm.cohort.cql.library.PriorityCqlLibraryProvider) CqlLibraryProvider(com.ibm.cohort.cql.library.CqlLibraryProvider) TranslatingCqlLibraryProvider(com.ibm.cohort.cql.translation.TranslatingCqlLibraryProvider) CqlLibraryDescriptor(com.ibm.cohort.cql.library.CqlLibraryDescriptor)

Example 20 with CqlLibraryProvider

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;
}
Also used : Path(java.nio.file.Path) Set(java.util.Set) HashMap(java.util.HashMap) CqlToElmTranslator(com.ibm.cohort.cql.translation.CqlToElmTranslator) ClasspathCqlLibraryProvider(com.ibm.cohort.cql.library.ClasspathCqlLibraryProvider) PriorityCqlLibraryProvider(com.ibm.cohort.cql.library.PriorityCqlLibraryProvider) CqlLibraryProvider(com.ibm.cohort.cql.library.CqlLibraryProvider) TranslatingCqlLibraryProvider(com.ibm.cohort.cql.translation.TranslatingCqlLibraryProvider) CqlLibraryDescriptor(com.ibm.cohort.cql.library.CqlLibraryDescriptor)

Aggregations

CqlLibraryProvider (com.ibm.cohort.cql.library.CqlLibraryProvider)27 ClasspathCqlLibraryProvider (com.ibm.cohort.cql.library.ClasspathCqlLibraryProvider)23 TranslatingCqlLibraryProvider (com.ibm.cohort.cql.translation.TranslatingCqlLibraryProvider)18 Test (org.junit.Test)15 CqlToElmTranslator (com.ibm.cohort.cql.translation.CqlToElmTranslator)13 CqlLibraryDescriptor (com.ibm.cohort.cql.library.CqlLibraryDescriptor)11 PriorityCqlLibraryProvider (com.ibm.cohort.cql.library.PriorityCqlLibraryProvider)10 CqlTerminologyProvider (com.ibm.cohort.cql.terminology.CqlTerminologyProvider)10 CqlLibrary (com.ibm.cohort.cql.library.CqlLibrary)9 HashMap (java.util.HashMap)9 Path (java.nio.file.Path)8 CqlDataProvider (com.ibm.cohort.cql.data.CqlDataProvider)7 MapCqlLibraryProviderFactory (com.ibm.cohort.cql.library.MapCqlLibraryProviderFactory)7 File (java.io.File)4 IGenericClient (ca.uhn.fhir.rest.client.api.IGenericClient)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 CqlEvaluationRequests (com.ibm.cohort.cql.evaluation.CqlEvaluationRequests)3 CqlEvaluator (com.ibm.cohort.cql.evaluation.CqlEvaluator)3 IntegerParameter (com.ibm.cohort.cql.evaluation.parameters.IntegerParameter)3 Parameter (com.ibm.cohort.cql.evaluation.parameters.Parameter)3