Search in sources :

Example 6 with Library

use of org.cqframework.cql.elm.execution.Library in project quality-measure-and-cohort-service by Alvearie.

the class R4TranslatingLibraryLoader method load.

@Override
public Library load(VersionedIdentifier libraryIdentifier) {
    Library elmLibrary = null;
    org.hl7.fhir.r4.model.Library fhirLibrary = resolver.resolveByName(libraryIdentifier.getId(), libraryIdentifier.getVersion());
    if (fhirLibrary == null) {
        throw new IllegalArgumentException(String.format("Library %s-%s not found", libraryIdentifier.getId(), libraryIdentifier.getVersion()));
    }
    Map<String, Attachment> mimeTypeIndex = new HashMap<>();
    for (Attachment attachment : fhirLibrary.getContent()) {
        if (attachment.hasContentType()) {
            mimeTypeIndex.put(attachment.getContentType(), attachment);
        } else {
            throw new IllegalArgumentException(String.format("Library %s-%s contains an attachment with no content type", libraryIdentifier.getId(), libraryIdentifier.getVersion()));
        }
    }
    Attachment attachment = mimeTypeIndex.get("application/elm+xml");
    if (attachment != null) {
        try (InputStream is = getAttachmentDataAsStream(attachment)) {
            elmLibrary = CqlLibraryReader.read(is);
        } catch (Exception ex) {
            throw new IllegalArgumentException(String.format("Library %s-%s elm attachment failed to deserialize", libraryIdentifier.getId(), libraryIdentifier.getVersion()), ex);
        }
    }
    if (elmLibrary == null) {
        attachment = mimeTypeIndex.get("text/cql");
        if (attachment == null) {
            throw new IllegalArgumentException(String.format("Library %s-%s must contain either a application/elm+xml or text/cql attachment", libraryIdentifier.getId(), libraryIdentifier.getVersion()));
        } else {
            String content = getAttachmentDataAsString(attachment);
            try {
                elmLibrary = OptimizedCqlLibraryReader.read(translateLibrary(content, libraryIdentifier));
            } catch (Exception ex) {
                throw new IllegalArgumentException(String.format("Library %s-%s cql attachment failed to deserialize", libraryIdentifier.getId(), libraryIdentifier.getVersion()), ex);
            }
        }
    }
    return elmLibrary;
}
Also used : HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Attachment(org.hl7.fhir.r4.model.Attachment) CqlLibrary(com.ibm.cohort.cql.library.CqlLibrary) Library(org.cqframework.cql.elm.execution.Library)

Example 7 with Library

use of org.cqframework.cql.elm.execution.Library 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 8 with Library

use of org.cqframework.cql.elm.execution.Library in project quality-measure-and-cohort-service by Alvearie.

the class CqlContextFactory method registerExternalIncludes.

private void registerExternalIncludes(Context context, Library currentLibrary) {
    Library.Includes includes = currentLibrary.getIncludes();
    if (includes != null) {
        for (IncludeDef include : includes.getDef()) {
            VersionedIdentifier vid = new VersionedIdentifier().withId(include.getLocalIdentifier()).withVersion(include.getVersion());
            context.registerExternalFunctionProvider(vid, this.externalFunctionProvider);
        }
    }
}
Also used : VersionedIdentifier(org.cqframework.cql.elm.execution.VersionedIdentifier) CqlVersionedIdentifier(com.ibm.cohort.cql.library.CqlVersionedIdentifier) IncludeDef(org.cqframework.cql.elm.execution.IncludeDef) Library(org.cqframework.cql.elm.execution.Library)

Example 9 with Library

use of org.cqframework.cql.elm.execution.Library in project quality-measure-and-cohort-service by Alvearie.

the class CqlContextFactory method createContext.

/**
 * Initialize a CQL context from the values associated with the provided
 * CQL Context Key. This encapsulates the set of initializations that are
 * static from run to run.
 *
 * @param contextKey container for stable context settings
 * @return initialized CQL Context object
 * @throws CqlLibraryDeserializationException if the specified library cannot be loaded
 */
protected Context createContext(ContextCacheKey contextKey) throws CqlLibraryDeserializationException {
    LibraryLoader libraryLoader = new ProviderBasedLibraryLoader(contextKey.libraryProvider);
    VersionedIdentifier vid = new VersionedIdentifier().withId(contextKey.topLevelLibraryIdentifier.getId()).withVersion(contextKey.topLevelLibraryIdentifier.getVersion());
    Library entryPoint = libraryLoader.load(vid);
    Context cqlContext;
    if (contextKey.evaluationDateTime != null) {
        cqlContext = new Context(entryPoint, contextKey.evaluationDateTime, new CqlSystemDataProvider());
    } else {
        cqlContext = new Context(entryPoint, new CqlSystemDataProvider());
    }
    cqlContext.registerExternalFunctionProvider(vid, this.externalFunctionProvider);
    registerExternalIncludes(cqlContext, cqlContext.getCurrentLibrary());
    cqlContext.registerLibraryLoader(libraryLoader);
    cqlContext.registerTerminologyProvider(contextKey.terminologyProvider);
    if (contextKey.parameters != null) {
        Library library = cqlContext.getCurrentLibrary();
        for (Map.Entry<String, Parameter> entry : contextKey.parameters.entrySet()) {
            Object cqlValue = entry.getValue().toCqlType();
            cqlContext.setParameter(library.getLocalId(), entry.getKey(), cqlValue);
        }
        if (library.getIncludes() != null && library.getIncludes().getDef() != null) {
            for (IncludeDef def : library.getIncludes().getDef()) {
                String name = def.getLocalIdentifier();
                for (Map.Entry<String, Parameter> parameterValue : contextKey.parameters.entrySet()) {
                    Object cqlValue = parameterValue.getValue().toCqlType();
                    cqlContext.setParameter(name, parameterValue.getKey(), cqlValue);
                }
            }
        }
    }
    return cqlContext;
}
Also used : Context(org.opencds.cqf.cql.engine.execution.Context) VersionedIdentifier(org.cqframework.cql.elm.execution.VersionedIdentifier) CqlVersionedIdentifier(com.ibm.cohort.cql.library.CqlVersionedIdentifier) ProviderBasedLibraryLoader(com.ibm.cohort.cql.library.ProviderBasedLibraryLoader) IncludeDef(org.cqframework.cql.elm.execution.IncludeDef) Parameter(com.ibm.cohort.cql.evaluation.parameters.Parameter) Library(org.cqframework.cql.elm.execution.Library) LibraryLoader(org.opencds.cqf.cql.engine.execution.LibraryLoader) ProviderBasedLibraryLoader(com.ibm.cohort.cql.library.ProviderBasedLibraryLoader) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) DebugMap(org.opencds.cqf.cql.engine.debug.DebugMap) CqlSystemDataProvider(com.ibm.cohort.cql.data.CqlSystemDataProvider)

Example 10 with Library

use of org.cqframework.cql.elm.execution.Library in project quality-measure-and-cohort-service by Alvearie.

the class OptimizedCqlLibraryReaderTest method testOverrideAndOrEvaluators.

@Test
public void testOverrideAndOrEvaluators() throws JAXBException {
    InputStream is = this.getClass().getClassLoader().getResourceAsStream("cql/short-circuit-and-or.xml");
    String xml = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8)).lines().collect(Collectors.joining(System.lineSeparator()));
    Library library = OptimizedCqlLibraryReader.read(xml);
    List<ExpressionDef> defs = library.getStatements().getDef();
    Optional<ExpressionDef> orEvaluator = defs.stream().filter(def -> def.getName().equals("TrueOrTrue")).findFirst();
    assertTrue(orEvaluator.isPresent());
    assertThat(orEvaluator.get().getExpression(), instanceOf(ShortOrEvaluator.class));
    Optional<ExpressionDef> andEvaluator = defs.stream().filter(def -> def.getName().equals("TrueAndTrue")).findFirst();
    assertTrue(andEvaluator.isPresent());
    assertThat(andEvaluator.get().getExpression(), instanceOf(ShortAndEvaluator.class));
}
Also used : Assert.assertTrue(org.junit.Assert.assertTrue) ShortOrEvaluator(com.ibm.cohort.cql.evaluator.ShortOrEvaluator) Test(org.junit.Test) ExpressionDef(org.cqframework.cql.elm.execution.ExpressionDef) InputStreamReader(java.io.InputStreamReader) Collectors(java.util.stream.Collectors) JAXBException(javax.xml.bind.JAXBException) StandardCharsets(java.nio.charset.StandardCharsets) CoreMatchers.instanceOf(org.hamcrest.CoreMatchers.instanceOf) List(java.util.List) ShortAndEvaluator(com.ibm.cohort.cql.evaluator.ShortAndEvaluator) Library(org.cqframework.cql.elm.execution.Library) Optional(java.util.Optional) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) BufferedReader(java.io.BufferedReader) InputStream(java.io.InputStream) InputStreamReader(java.io.InputStreamReader) ExpressionDef(org.cqframework.cql.elm.execution.ExpressionDef) InputStream(java.io.InputStream) BufferedReader(java.io.BufferedReader) ShortAndEvaluator(com.ibm.cohort.cql.evaluator.ShortAndEvaluator) ShortOrEvaluator(com.ibm.cohort.cql.evaluator.ShortOrEvaluator) Library(org.cqframework.cql.elm.execution.Library) Test(org.junit.Test)

Aggregations

Library (org.cqframework.cql.elm.execution.Library)14 VersionedIdentifier (org.cqframework.cql.elm.execution.VersionedIdentifier)6 LibraryLoader (org.opencds.cqf.cql.engine.execution.LibraryLoader)5 CqlLibrary (com.ibm.cohort.cql.library.CqlLibrary)4 HashMap (java.util.HashMap)4 Map (java.util.Map)4 Test (org.junit.Test)4 CqlToElmTranslator (com.ibm.cohort.cql.translation.CqlToElmTranslator)3 JAXBException (javax.xml.bind.JAXBException)3 CqlLibraryDescriptor (com.ibm.cohort.cql.library.CqlLibraryDescriptor)2 CqlLibraryProvider (com.ibm.cohort.cql.library.CqlLibraryProvider)2 CqlVersionedIdentifier (com.ibm.cohort.cql.library.CqlVersionedIdentifier)2 ProviderBasedLibraryLoader (com.ibm.cohort.cql.library.ProviderBasedLibraryLoader)2 TranslatingCqlLibraryProvider (com.ibm.cohort.cql.translation.TranslatingCqlLibraryProvider)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 StringReader (java.io.StringReader)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2