Search in sources :

Example 21 with ExpressionDef

use of org.hl7.elm.r1.ExpressionDef in project cqf-ruler by DBCG.

the class LibraryEvaluationProvider method getLocations.

// TODO: Merge this into the evaluator
@SuppressWarnings("unused")
private Map<String, List<Integer>> getLocations(org.hl7.elm.r1.Library library) {
    Map<String, List<Integer>> locations = new HashMap<>();
    if (library.getStatements() == null)
        return locations;
    for (org.hl7.elm.r1.ExpressionDef def : library.getStatements().getDef()) {
        int startLine = def.getTrackbacks().isEmpty() ? 0 : def.getTrackbacks().get(0).getStartLine();
        int startChar = def.getTrackbacks().isEmpty() ? 0 : def.getTrackbacks().get(0).getStartChar();
        List<Integer> loc = Arrays.asList(startLine, startChar);
        locations.put(def.getName(), loc);
    }
    return locations;
}
Also used : HashMap(java.util.HashMap) List(java.util.List) ArrayList(java.util.ArrayList) Endpoint(org.hl7.fhir.r4.model.Endpoint)

Example 22 with ExpressionDef

use of org.hl7.elm.r1.ExpressionDef in project quality-measure-and-cohort-service by Alvearie.

the class SparkSchemaCreator method calculateSchemaForContext.

public StructType calculateSchemaForContext(String contextName) throws Exception {
    List<CqlEvaluationRequest> filteredRequests = requests.getEvaluationsForContext(contextName);
    StructType resultsSchema = new StructType();
    Set<Tuple2<String, String>> usingInfo = new HashSet<>();
    for (CqlEvaluationRequest filteredRequest : filteredRequests) {
        CqlLibraryDescriptor descriptor = filteredRequest.getDescriptor();
        String libraryId = descriptor.getLibraryId();
        for (String expression : filteredRequest.getExpressionNames()) {
            CqlLibrary providedLibrary = libraryProvider.getLibrary(new CqlLibraryDescriptor().setLibraryId(libraryId).setVersion(descriptor.getVersion()).setFormat(Format.ELM));
            if (providedLibrary == null) {
                throw new IllegalArgumentException("Library not found: " + descriptor.getLibraryId() + "-" + descriptor.getVersion());
            }
            Library library = CqlLibraryReader.read(providedLibrary.getContentAsStream());
            // Track the set of non-system using statements across libraries.
            // Information is used later to access ModelInfos when searching
            // for context key column type information.
            usingInfo.addAll(library.getUsings().getDef().stream().filter(x -> !x.getLocalIdentifier().equals("System")).map(x -> new Tuple2<>(x.getLocalIdentifier(), x.getVersion())).collect(Collectors.toList()));
            List<ExpressionDef> expressionDefs = library.getStatements().getDef().stream().filter(x -> x.getName().equals(expression)).collect(Collectors.toList());
            if (expressionDefs.isEmpty()) {
                throw new IllegalArgumentException("Expression " + expression + " is configured in the CQL jobs file, but not found in " + descriptor.getLibraryId() + "-" + descriptor.getVersion());
            } else if (expressionDefs.size() > 1) {
                throw new IllegalArgumentException("Expression " + expression + " was defined multiple times in library: " + descriptor.getLibraryId() + "-" + descriptor.getVersion());
            }
            QName resultTypeName = expressionDefs.get(0).getExpression().getResultTypeName();
            if (resultTypeName == null) {
                throw new IllegalArgumentException("Expression " + expression + " has a null result type: " + descriptor.getLibraryId() + "-" + descriptor.getVersion());
            }
            // The column name encoder already performed duplicate checking. We just need to make sure
            // we add each uniquely named column to the output one time.
            String columnName = sparkOutputColumnEncoder.getColumnName(filteredRequest, expression);
            if (resultsSchema.getFieldIndex(columnName).isEmpty()) {
                resultsSchema = resultsSchema.add(columnName, QNameToDataTypeConverter.getFieldType(resultTypeName), true);
            }
        }
    }
    if (resultsSchema.fields().length > 0) {
        Tuple2<String, DataType> keyInformation = getDataTypeForContextKey(contextName, usingInfo);
        StructType fullSchema = new StructType().add(keyInformation._1(), keyInformation._2(), false).add(getParametersColumnName(), DataTypes.StringType, false);
        for (StructField field : resultsSchema.fields()) {
            fullSchema = fullSchema.add(field);
        }
        resultsSchema = fullSchema;
    }
    return resultsSchema;
}
Also used : DataType(org.apache.spark.sql.types.DataType) ModelInfo(org.hl7.elm_modelinfo.r1.ModelInfo) CqlToElmTranslator(com.ibm.cohort.cql.translation.CqlToElmTranslator) HashMap(java.util.HashMap) Format(com.ibm.cohort.cql.library.Format) ExpressionDef(org.cqframework.cql.elm.execution.ExpressionDef) ClassInfo(org.hl7.elm_modelinfo.r1.ClassInfo) HashSet(java.util.HashSet) ContextDefinition(com.ibm.cohort.cql.spark.aggregation.ContextDefinition) Map(java.util.Map) CqlLibraryReader(org.opencds.cqf.cql.engine.execution.CqlLibraryReader) CqlEvaluationRequest(com.ibm.cohort.cql.evaluation.CqlEvaluationRequest) ModelUtils(com.ibm.cohort.cql.spark.optimizer.ModelUtils) DataTypes(org.apache.spark.sql.types.DataTypes) StructField(org.apache.spark.sql.types.StructField) StructType(org.apache.spark.sql.types.StructType) ModelManager(org.cqframework.cql.cql2elm.ModelManager) Collection(java.util.Collection) VersionedIdentifier(org.hl7.elm.r1.VersionedIdentifier) Set(java.util.Set) CqlLibrary(com.ibm.cohort.cql.library.CqlLibrary) CqlLibraryProvider(com.ibm.cohort.cql.library.CqlLibraryProvider) Tuple2(scala.Tuple2) Collectors(java.util.stream.Collectors) CqlLibraryDescriptor(com.ibm.cohort.cql.library.CqlLibraryDescriptor) List(java.util.List) CqlEvaluationRequests(com.ibm.cohort.cql.evaluation.CqlEvaluationRequests) Library(org.cqframework.cql.elm.execution.Library) ClassInfoElement(org.hl7.elm_modelinfo.r1.ClassInfoElement) QName(javax.xml.namespace.QName) ContextDefinitions(com.ibm.cohort.cql.spark.aggregation.ContextDefinitions) StructType(org.apache.spark.sql.types.StructType) ExpressionDef(org.cqframework.cql.elm.execution.ExpressionDef) QName(javax.xml.namespace.QName) CqlEvaluationRequest(com.ibm.cohort.cql.evaluation.CqlEvaluationRequest) CqlLibrary(com.ibm.cohort.cql.library.CqlLibrary) StructField(org.apache.spark.sql.types.StructField) Tuple2(scala.Tuple2) DataType(org.apache.spark.sql.types.DataType) CqlLibrary(com.ibm.cohort.cql.library.CqlLibrary) Library(org.cqframework.cql.elm.execution.Library) CqlLibraryDescriptor(com.ibm.cohort.cql.library.CqlLibraryDescriptor) HashSet(java.util.HashSet)

Example 23 with ExpressionDef

use of org.hl7.elm.r1.ExpressionDef in project quality-measure-and-cohort-service by Alvearie.

the class DataTypeRequirementsProcessor method getDataRequirements.

public DataTypeRequirements getDataRequirements(CqlLibraryProvider sourceProvider, CqlLibraryDescriptor libraryDescriptor, Set<String> expressions) {
    AnyColumnVisitor visitor = new AnyColumnVisitor(sourceProvider);
    AnyColumnContext context = new AnyColumnContext();
    Library elmLibrary = getElmLibrary(sourceProvider, libraryDescriptor);
    // Build a list of all the expressions in the Library that we care about
    List<ExpressionDef> expressionDefs = elmLibrary.getStatements().getDef();
    if (expressions != null) {
        expressionDefs = expressionDefs.stream().filter(def -> expressions.contains(def.getName())).collect(Collectors.toList());
        if (expressionDefs.size() != expressions.size()) {
            throw new IllegalArgumentException(String.format("One or more requested expressions %s not found in library %s", expressions, libraryDescriptor.toString()));
        }
    }
    visitor.enterLibrary(elmLibrary.getIdentifier());
    try {
        for (UsingDef usingDef : elmLibrary.getUsings().getDef()) {
            visitor.visitElement(usingDef, context);
        }
        for (ExpressionDef expressionDef : expressionDefs) {
            visitor.visitElement(expressionDef, context);
        }
    } finally {
        visitor.exitLibrary();
    }
    addToChildTypes(context.getModels(), context.getPathsByQName());
    addToChildTypes(context.getModels(), context.getMatchers());
    Map<String, Set<String>> pathsByDataType = mapToLocalPart(context.getPathsByQName());
    Map<String, Set<StringMatcher>> matchersByDataType = mapToLocalPart(context.getMatchers());
    return new DataTypeRequirements(context.getModels(), pathsByDataType, matchersByDataType);
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) ExpressionDef(org.hl7.elm.r1.ExpressionDef) UsingDef(org.hl7.elm.r1.UsingDef) CqlLibrary(com.ibm.cohort.cql.library.CqlLibrary) Library(org.hl7.elm.r1.Library)

Example 24 with ExpressionDef

use of org.hl7.elm.r1.ExpressionDef in project quality-measure-and-cohort-service by Alvearie.

the class GraphWalkingElmVisitor method visitRef.

protected R visitRef(ExpressionRef ref, C context, R defaultResult) {
    R result = defaultResult;
    if (!visited.contains(ref)) {
        visited.add(ref);
        Library library = prepareLibraryVisit(getCurrentLibraryIdentifier(), ref.getLibraryName(), context);
        try {
            // The TranslatedLibrary class that the CqlTranslator produces has this already indexed. If we want to
            // use any ELM without relying on the translator, we need to do the lookup ourselves. It is worth
            // considering whether or not we build our own indexing helper instead.
            Optional<ExpressionDef> optionalEd = library.getStatements().getDef().stream().filter(def -> def.getName().equals(ref.getName())).findAny();
            if (optionalEd.isPresent()) {
                result = visitElement(optionalEd.get(), context);
            } else {
                throw new IllegalArgumentException("Could not find definition for reference " + ref.getName());
            }
        } finally {
            unprepareLibraryVisit(ref.getLibraryName());
        }
    }
    return result;
}
Also used : Logger(org.slf4j.Logger) ElmBaseLibraryVisitor(org.cqframework.cql.elm.visiting.ElmBaseLibraryVisitor) NamespaceManager(org.cqframework.cql.cql2elm.NamespaceManager) VersionedIdentifier(org.hl7.elm.r1.VersionedIdentifier) LoggerFactory(org.slf4j.LoggerFactory) Set(java.util.Set) CqlLibrary(com.ibm.cohort.cql.library.CqlLibrary) CqlLibraryProvider(com.ibm.cohort.cql.library.CqlLibraryProvider) Format(com.ibm.cohort.cql.library.Format) Deque(java.util.Deque) ExpressionRef(org.hl7.elm.r1.ExpressionRef) CqlLibraryDescriptor(com.ibm.cohort.cql.library.CqlLibraryDescriptor) HashSet(java.util.HashSet) Library(org.hl7.elm.r1.Library) FunctionRef(org.hl7.elm.r1.FunctionRef) Optional(java.util.Optional) IncludeDef(org.hl7.elm.r1.IncludeDef) ArrayDeque(java.util.ArrayDeque) JAXB(javax.xml.bind.JAXB) Element(org.hl7.elm.r1.Element) ExpressionDef(org.hl7.elm.r1.ExpressionDef) ExpressionDef(org.hl7.elm.r1.ExpressionDef) CqlLibrary(com.ibm.cohort.cql.library.CqlLibrary) Library(org.hl7.elm.r1.Library)

Aggregations

ExpressionDef (org.hl7.elm.r1.ExpressionDef)11 Test (org.testng.annotations.Test)9 Library (org.hl7.elm.r1.Library)7 HashMap (java.util.HashMap)6 CqlTranslator (org.cqframework.cql.cql2elm.CqlTranslator)6 CompiledLibrary (org.cqframework.cql.cql2elm.model.CompiledLibrary)4 Element (org.hl7.elm.r1.Element)4 BeforeTest (org.testng.annotations.BeforeTest)4 CqlLibrary (com.ibm.cohort.cql.library.CqlLibrary)3 HashSet (java.util.HashSet)3 List (java.util.List)3 Set (java.util.Set)3 ModelManager (org.cqframework.cql.cql2elm.ModelManager)3 DataType (org.hl7.cql.model.DataType)3 Annotation (org.hl7.cql_annotations.r1.Annotation)3 CqlLibraryDescriptor (com.ibm.cohort.cql.library.CqlLibraryDescriptor)2 CqlLibraryProvider (com.ibm.cohort.cql.library.CqlLibraryProvider)2 Format (com.ibm.cohort.cql.library.Format)2 IOException (java.io.IOException)2 QuickDataType.quickDataType (org.cqframework.cql.cql2elm.matchers.QuickDataType.quickDataType)2