Search in sources :

Example 16 with ModelManager

use of org.cqframework.cql.cql2elm.ModelManager in project clinical_quality_language by cqframework.

the class SortingTest method setup.

@BeforeTest
public void setup() throws IOException {
    ModelManager modelManager = new ModelManager();
    CqlTranslator translator = CqlTranslator.fromStream(QueryTest.class.getResourceAsStream("../OperatorTests/Sorting.cql"), modelManager, new LibraryManager(modelManager));
    // The alias test creates an error
    assertThat(translator.getErrors().size(), is(1));
    Library library = translator.toELM();
    defs = new HashMap<>();
    for (ExpressionDef def : library.getStatements().getDef()) {
        defs.put(def.getName(), def);
    }
}
Also used : CqlTranslator(org.cqframework.cql.cql2elm.CqlTranslator) LibraryManager(org.cqframework.cql.cql2elm.LibraryManager) ModelManager(org.cqframework.cql.cql2elm.ModelManager) BeforeTest(org.testng.annotations.BeforeTest)

Example 17 with ModelManager

use of org.cqframework.cql.cql2elm.ModelManager in project clinical_quality_language by cqframework.

the class StringOperatorsTest method setup.

@BeforeTest
public void setup() throws IOException {
    ModelManager modelManager = new ModelManager();
    CqlTranslator translator = CqlTranslator.fromStream(StringOperatorsTest.class.getResourceAsStream("../OperatorTests/StringOperators.cql"), modelManager, new LibraryManager(modelManager));
    assertThat(translator.getErrors().size(), is(0));
    Library library = translator.toELM();
    defs = new HashMap<>();
    for (ExpressionDef def : library.getStatements().getDef()) {
        defs.put(def.getName(), def);
    }
}
Also used : CqlTranslator(org.cqframework.cql.cql2elm.CqlTranslator) LibraryManager(org.cqframework.cql.cql2elm.LibraryManager) ModelManager(org.cqframework.cql.cql2elm.ModelManager) BeforeTest(org.testng.annotations.BeforeTest)

Example 18 with ModelManager

use of org.cqframework.cql.cql2elm.ModelManager in project clinical_quality_language by cqframework.

the class AgeOperatorsTest method setup.

@BeforeTest
public void setup() throws IOException {
    ModelManager modelManager = new ModelManager();
    CqlTranslator translator = CqlTranslator.fromStream(AgeOperatorsTest.class.getResourceAsStream("../OperatorTests/AgeOperators.cql"), modelManager, new LibraryManager(modelManager));
    assertThat(translator.getErrors().size(), is(0));
    Library library = translator.toELM();
    defs = new HashMap<>();
    if (library.getStatements() != null) {
        for (ExpressionDef def : library.getStatements().getDef()) {
            defs.put(def.getName(), def);
        }
    }
}
Also used : CqlTranslator(org.cqframework.cql.cql2elm.CqlTranslator) LibraryManager(org.cqframework.cql.cql2elm.LibraryManager) ModelManager(org.cqframework.cql.cql2elm.ModelManager) BeforeTest(org.testng.annotations.BeforeTest)

Example 19 with ModelManager

use of org.cqframework.cql.cql2elm.ModelManager in project quality-measure-and-cohort-service by Alvearie.

the class SparkSchemaCreator method getDataTypeForContextKey.

private Tuple2<String, DataType> getDataTypeForContextKey(String contextName, Set<Tuple2<String, String>> usingInfos) {
    ContextDefinition contextDefinition = contextDefinitions.getContextDefinitionByName(contextName);
    String primaryDataType = contextDefinition.getPrimaryDataType();
    String primaryKeyColumn = contextDefinition.getPrimaryKeyColumn();
    DataType keyType = null;
    ModelManager modelManager = translator.newModelManager();
    // Try to find the key column's type information from a single model info.
    for (Tuple2<String, String> usingInfo : usingInfos) {
        VersionedIdentifier modelInfoIdentifier = new VersionedIdentifier().withId(usingInfo._1()).withVersion(usingInfo._2());
        ModelInfo modelInfo = modelManager.getModelInfoLoader().getModelInfo(modelInfoIdentifier);
        // Look for a ClassInfo element matching primaryDataType for the context
        List<ClassInfo> classInfos = getClassInfos(primaryDataType, modelInfo);
        if (!classInfos.isEmpty()) {
            if (classInfos.size() == 1) {
                ClassInfo classInfo = classInfos.get(0);
                List<ClassInfoElement> elements = classInfo.getElement().stream().filter(x -> x.getName().equals(primaryKeyColumn)).collect(Collectors.toList());
                // check base type
                String baseType = classInfo.getBaseType();
                if (classInfo.getBaseType() != null) {
                    List<ClassInfo> baseClassInfos = getClassInfos(baseType, modelInfo);
                    baseClassInfos.stream().map(ClassInfo::getElement).flatMap(List::stream).filter(element -> element.getName().equals(primaryKeyColumn)).forEach(elements::add);
                }
                // check choice types
                Collection<String> choiceTypes = ModelUtils.getChoiceTypeNames(classInfo);
                choiceTypes.stream().map(type -> getClassInfos(type, modelInfo)).flatMap(List::stream).map(ClassInfo::getElement).flatMap(List::stream).filter(element -> element.getName().equals(primaryKeyColumn)).findFirst().ifPresent(elements::add);
                // A future ModelInfo file may contain the information
                if (elements.isEmpty()) {
                    continue;
                } else if (elements.size() == 1) {
                    String elementType = elements.get(0).getElementType();
                    // store it
                    if (keyType == null) {
                        keyType = getSparkTypeForSystemValue(elementType);
                    } else {
                        throw new IllegalArgumentException("Multiple definitions found for " + primaryDataType + "." + primaryKeyColumn + " in the provided ModelInfo files. Cannot infer key type for context: " + contextName);
                    }
                } else if (elements.size() > 1) {
                    throw new IllegalArgumentException("ModelInfo " + modelInfoIdentifier + " contains multiple element definitions for " + primaryKeyColumn + " for type " + primaryDataType);
                }
            } else {
                throw new IllegalArgumentException("ModelInfo " + modelInfoIdentifier + " contains multiple definitions for type " + primaryDataType);
            }
        }
    }
    if (keyType == null) {
        throw new IllegalArgumentException("Could not locate type information for " + primaryDataType + "." + primaryKeyColumn + " in the provided ModelInfo files. Cannot infer key type for context: " + contextName);
    }
    return new Tuple2<>(contextDefinition.getPrimaryKeyColumn(), keyType);
}
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) ModelInfo(org.hl7.elm_modelinfo.r1.ModelInfo) ContextDefinition(com.ibm.cohort.cql.spark.aggregation.ContextDefinition) ModelManager(org.cqframework.cql.cql2elm.ModelManager) ClassInfoElement(org.hl7.elm_modelinfo.r1.ClassInfoElement) VersionedIdentifier(org.hl7.elm.r1.VersionedIdentifier) Tuple2(scala.Tuple2) DataType(org.apache.spark.sql.types.DataType) List(java.util.List) ClassInfo(org.hl7.elm_modelinfo.r1.ClassInfo)

Example 20 with ModelManager

use of org.cqframework.cql.cql2elm.ModelManager in project quality-measure-and-cohort-service by Alvearie.

the class CqlToElmTranslator method newLibraryManager.

public LibraryManager newLibraryManager(CqlLibrarySourceProvider sourceProvider) {
    ModelManager modelManager = newModelManager();
    LibraryManager libraryManager = new LibraryManager(modelManager);
    libraryManager.getLibrarySourceLoader().registerProvider(sourceProvider);
    return libraryManager;
}
Also used : LibraryManager(org.cqframework.cql.cql2elm.LibraryManager) ModelManager(org.cqframework.cql.cql2elm.ModelManager)

Aggregations

ModelManager (org.cqframework.cql.cql2elm.ModelManager)21 LibraryManager (org.cqframework.cql.cql2elm.LibraryManager)19 CqlTranslator (org.cqframework.cql.cql2elm.CqlTranslator)17 BeforeTest (org.testng.annotations.BeforeTest)14 ByteArrayInputStream (java.io.ByteArrayInputStream)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 CqlTranslatorException (org.cqframework.cql.cql2elm.CqlTranslatorException)2 FhirLibrarySourceProvider (org.cqframework.cql.cql2elm.FhirLibrarySourceProvider)2 Library (org.cqframework.cql.elm.execution.Library)2 ExpressionDef (org.hl7.elm.r1.ExpressionDef)2 Library (org.hl7.elm.r1.Library)2 FhirContext (ca.uhn.fhir.context.FhirContext)1 CqlEvaluationRequest (com.ibm.cohort.cql.evaluation.CqlEvaluationRequest)1 CqlEvaluationRequests (com.ibm.cohort.cql.evaluation.CqlEvaluationRequests)1 CqlLibrary (com.ibm.cohort.cql.library.CqlLibrary)1 CqlLibraryDescriptor (com.ibm.cohort.cql.library.CqlLibraryDescriptor)1 CqlLibraryProvider (com.ibm.cohort.cql.library.CqlLibraryProvider)1 Format (com.ibm.cohort.cql.library.Format)1 ContextDefinition (com.ibm.cohort.cql.spark.aggregation.ContextDefinition)1