Search in sources :

Example 1 with ChoiceType

use of org.hl7.cql.model.ChoiceType in project quality-measure-and-cohort-service by Alvearie.

the class SparkCqlEvaluator method createDataTypeAliases.

private Map<String, String> createDataTypeAliases(List<ContextDefinition> filteredContexts, CqlToElmTranslator translator) {
    Set<String> dataTypes = filteredContexts.stream().map(ContextDefinition::getPrimaryDataType).collect(Collectors.toSet());
    Collection<ModelInfo> modelInfos = translator.getRegisteredModelInfos().values();
    Map<String, String> dataTypeAliases = new HashMap<>();
    for (ModelInfo modelInfo : modelInfos) {
        modelInfo.getTypeInfo().stream().filter(ClassInfo.class::isInstance).map(ClassInfo.class::cast).filter(classInfo -> dataTypes.contains(classInfo.getName())).forEach(info -> {
            String dataType = info.getName();
            QName baseType = ModelUtils.getBaseTypeName(modelInfo, info);
            if (baseType != null) {
                // for inheritance types
                dataTypeAliases.put(dataType, baseType.getLocalPart());
            }
            Collection<String> choiceTypes = ModelUtils.getChoiceTypeNames(info);
            // for choice types
            choiceTypes.forEach(choiceType -> dataTypeAliases.put(dataType, choiceType));
        });
    }
    return dataTypeAliases;
}
Also used : ModelInfo(org.hl7.elm_modelinfo.r1.ModelInfo) Arrays(java.util.Arrays) CqlDataProvider(com.ibm.cohort.cql.data.CqlDataProvider) CqlToElmTranslator(com.ibm.cohort.cql.translation.CqlToElmTranslator) FileSystem(org.apache.hadoop.fs.FileSystem) ZonedDateTime(java.time.ZonedDateTime) LoggerFactory(org.slf4j.LoggerFactory) EncodedParametersCache(com.ibm.cohort.cql.spark.util.EncodedParametersCache) DataRowDataProvider(com.ibm.cohort.datarow.engine.DataRowDataProvider) Format(com.ibm.cohort.cql.library.Format) ClassInfo(org.hl7.elm_modelinfo.r1.ClassInfo) ColumnRuleCreator(com.ibm.cohort.cql.spark.aggregation.ColumnRuleCreator) ConfigurableOutputColumnNameEncoder(com.ibm.cohort.cql.spark.data.ConfigurableOutputColumnNameEncoder) DefaultDatasetRetriever(com.ibm.cohort.cql.spark.data.DefaultDatasetRetriever) SparkSchemaCreator(com.ibm.cohort.cql.spark.data.SparkSchemaCreator) ContextDefinition(com.ibm.cohort.cql.spark.aggregation.ContextDefinition) Map(java.util.Map) Path(org.apache.hadoop.fs.Path) CqlEvaluationRequest(com.ibm.cohort.cql.evaluation.CqlEvaluationRequest) ModelUtils(com.ibm.cohort.cql.spark.optimizer.ModelUtils) ConstraintViolation(javax.validation.ConstraintViolation) StructType(org.apache.spark.sql.types.StructType) HadoopPathOutputMetadataWriter(com.ibm.cohort.cql.spark.metadata.HadoopPathOutputMetadataWriter) Collection(java.util.Collection) Set(java.util.Set) Validator(javax.validation.Validator) CqlLibraryProvider(com.ibm.cohort.cql.library.CqlLibraryProvider) Reader(java.io.Reader) Tuple2(scala.Tuple2) Collectors(java.util.stream.Collectors) FileNotFoundException(java.io.FileNotFoundException) Serializable(java.io.Serializable) ContextRetriever(com.ibm.cohort.cql.spark.aggregation.ContextRetriever) List(java.util.List) CqlEvaluationRequests(com.ibm.cohort.cql.evaluation.CqlEvaluationRequests) CqlTerminologyProvider(com.ibm.cohort.cql.terminology.CqlTerminologyProvider) QName(javax.xml.namespace.QName) SparkOutputColumnEncoder(com.ibm.cohort.cql.spark.data.SparkOutputColumnEncoder) Parameter(com.ibm.cohort.cql.evaluation.parameters.Parameter) MapUtils(com.ibm.cohort.cql.util.MapUtils) ExternalFunctionProvider(org.opencds.cqf.cql.engine.data.ExternalFunctionProvider) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Dataset(org.apache.spark.sql.Dataset) SerializableConfiguration(org.apache.spark.util.SerializableConfiguration) CqlEvaluationResult(com.ibm.cohort.cql.evaluation.CqlEvaluationResult) SparkDataRow(com.ibm.cohort.cql.spark.data.SparkDataRow) PriorityCqlLibraryProvider(com.ibm.cohort.cql.library.PriorityCqlLibraryProvider) R4FileSystemFhirTerminologyProvider(com.ibm.cohort.cql.terminology.R4FileSystemFhirTerminologyProvider) HashMap(java.util.HashMap) ValidatorFactory(javax.validation.ValidatorFactory) HadoopBasedCqlLibraryProvider(com.ibm.cohort.cql.library.HadoopBasedCqlLibraryProvider) SparkTypeConverter(com.ibm.cohort.cql.spark.data.SparkTypeConverter) Function(java.util.function.Function) ArrayList(java.util.ArrayList) CustomMetricSparkPlugin(com.ibm.cohort.cql.spark.metrics.CustomMetricSparkPlugin) CollectionUtils(org.apache.commons.collections.CollectionUtils) EvaluationError(com.ibm.cohort.cql.spark.errors.EvaluationError) DataRowRetrieveProvider(com.ibm.cohort.datarow.engine.DataRowRetrieveProvider) Validation(javax.validation.Validation) TranslatingCqlLibraryProvider(com.ibm.cohort.cql.translation.TranslatingCqlLibraryProvider) NoSuchElementException(java.util.NoSuchElementException) EvaluationSummary(com.ibm.cohort.cql.spark.metadata.EvaluationSummary) DataRow(com.ibm.cohort.datarow.model.DataRow) SparkSession(org.apache.spark.sql.SparkSession) PrintStream(java.io.PrintStream) CqlEvaluator(com.ibm.cohort.cql.evaluation.CqlEvaluator) ClasspathCqlLibraryProvider(com.ibm.cohort.cql.library.ClasspathCqlLibraryProvider) SaveMode(org.apache.spark.sql.SaveMode) Logger(org.slf4j.Logger) CqlDebug(com.ibm.cohort.cql.evaluation.CqlDebug) Iterator(java.util.Iterator) RowFactory(org.apache.spark.sql.RowFactory) JCommander(com.beust.jcommander.JCommander) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException) Row(org.apache.spark.sql.Row) CollectionAccumulator(org.apache.spark.util.CollectionAccumulator) InputStreamReader(java.io.InputStreamReader) JavaPairRDD(org.apache.spark.api.java.JavaPairRDD) LongAccumulator(org.apache.spark.util.LongAccumulator) CqlExpressionConfiguration(com.ibm.cohort.cql.evaluation.CqlExpressionConfiguration) AnyColumnFunctions(com.ibm.cohort.cql.functions.AnyColumnFunctions) MDC(org.slf4j.MDC) UnsupportedTerminologyProvider(com.ibm.cohort.cql.terminology.UnsupportedTerminologyProvider) CohortExternalFunctionProvider(com.ibm.cohort.cql.functions.CohortExternalFunctionProvider) ContextDefinitions(com.ibm.cohort.cql.spark.aggregation.ContextDefinitions) Collections(java.util.Collections) OutputMetadataWriter(com.ibm.cohort.cql.spark.metadata.OutputMetadataWriter) ModelInfo(org.hl7.elm_modelinfo.r1.ModelInfo) HashMap(java.util.HashMap) QName(javax.xml.namespace.QName) ClassInfo(org.hl7.elm_modelinfo.r1.ClassInfo)

Example 2 with ChoiceType

use of org.hl7.cql.model.ChoiceType in project clinical_quality_language by cqframework.

the class Main method main.

public static void main(String[] args) throws IOException, JAXBException {
    OptionParser parser = new OptionParser();
    OptionSpec<File> schemaOpt = parser.accepts("schema").withRequiredArg().ofType(File.class).required();
    OptionSpec<String> modelOpt = parser.accepts("model").withRequiredArg().ofType(String.class);
    OptionSpec<File> configOpt = parser.accepts("config").withOptionalArg().ofType(File.class);
    OptionSpec<File> outputOpt = parser.accepts("output").withRequiredArg().ofType(File.class);
    OptionSpec<String> normalizePrefixOpt = parser.accepts("normalize-prefix").withRequiredArg().ofType(String.class);
    OptionSpec<ModelImporterOptions.ChoiceTypePolicy> choiceTypeOpt = parser.accepts("choicetype-policy").withRequiredArg().ofType(ModelImporterOptions.ChoiceTypePolicy.class);
    OptionSpec<ModelImporterOptions.SimpleTypeRestrictionPolicy> stRestrictionsOpt = parser.accepts("simpletype-restriction-policy").withRequiredArg().ofType(ModelImporterOptions.SimpleTypeRestrictionPolicy.class);
    OptionSpec<ModelImporterOptions.ElementRedeclarationPolicy> redeclarationsOpt = parser.accepts("element-redeclaration-policy").withRequiredArg().ofType(ModelImporterOptions.ElementRedeclarationPolicy.class);
    OptionSpec<ModelImporterOptions.VersionPolicy> versionPolicyOpt = parser.accepts("version-policy").withRequiredArg().ofType(ModelImporterOptions.VersionPolicy.class);
    OptionSpec<File> optionsFileOpt = parser.accepts("options-file").withRequiredArg().ofType(File.class);
    OptionSet options = parser.parse(args);
    File schemaFile = schemaOpt.value(options);
    InputStream is = new FileInputStream(schemaFile);
    XmlSchemaCollection schemaCol = new XmlSchemaCollection();
    schemaCol.setBaseUri(schemaFile.getParent());
    XmlSchema schema = schemaCol.read(new StreamSource(is));
    ModelImporterOptions importerOptions;
    if (options.has(optionsFileOpt)) {
        importerOptions = ModelImporterOptions.loadFromProperties(optionsFileOpt.value(options));
    } else {
        importerOptions = new ModelImporterOptions();
    }
    if (options.has(modelOpt)) {
        importerOptions.setModel(modelOpt.value(options));
    }
    if (options.has(choiceTypeOpt)) {
        importerOptions.setChoiceTypePolicy(choiceTypeOpt.value(options));
    }
    if (options.has(stRestrictionsOpt)) {
        importerOptions.setSimpleTypeRestrictionPolicy(stRestrictionsOpt.value(options));
    }
    if (options.has(redeclarationsOpt)) {
        importerOptions.setElementRedeclarationPolicy(redeclarationsOpt.value(options));
    }
    if (options.has(versionPolicyOpt)) {
        importerOptions.setVersionPolicy(versionPolicyOpt.value(options));
    }
    if (options.has(normalizePrefixOpt)) {
        importerOptions.setNormalizePrefix(normalizePrefixOpt.value(options));
    }
    ModelInfo config = null;
    if (configOpt != null) {
        File configFile = configOpt.value(options);
        if (configFile != null) {
            config = JAXB.unmarshal(configFile, ModelInfo.class);
        }
    }
    ModelInfo modelInfo = ModelImporter.fromXsd(schema, importerOptions, config);
    JAXBContext jc = JAXBContext.newInstance(ModelInfo.class);
    Marshaller marshaller = jc.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    File outputfile;
    if (!options.has(outputOpt) || outputOpt.value(options).isDirectory()) {
        // construct output filename using modelinfo
        String name = String.format("%s-modelinfo.xml", modelInfo.getTargetQualifier());
        String basePath = options.has(outputOpt) ? outputOpt.value(options).getAbsolutePath() : schemaFile.getParent();
        outputfile = new File(basePath + File.separator + name);
    } else {
        outputfile = outputOpt.value(options);
    }
    if (outputfile.equals(schemaFile)) {
        throw new IllegalArgumentException("input schema file and output file must be different!");
    }
    OutputStream os = new FileOutputStream(outputfile, false);
    try {
        OutputStreamWriter writer = new OutputStreamWriter(os, "UTF-8");
        marshaller.marshal(new ObjectFactory().createModelInfo(modelInfo), writer);
    } finally {
        os.close();
    }
}
Also used : ModelInfo(org.hl7.elm_modelinfo.r1.ModelInfo) JAXBContext(javax.xml.bind.JAXBContext) OptionParser(joptsimple.OptionParser) ObjectFactory(org.hl7.elm_modelinfo.r1.ObjectFactory) Marshaller(javax.xml.bind.Marshaller) StreamSource(javax.xml.transform.stream.StreamSource) XmlSchemaCollection(org.apache.ws.commons.schema.XmlSchemaCollection) XmlSchema(org.apache.ws.commons.schema.XmlSchema) OptionSet(joptsimple.OptionSet)

Example 3 with ChoiceType

use of org.hl7.cql.model.ChoiceType in project clinical_quality_language by cqframework.

the class SemanticTests method testTypeOperators.

@Test
public void testTypeOperators() throws IOException {
    CqlTranslator translator = runSemanticTest("OperatorTests/TypeOperators.cql");
    org.hl7.elm.r1.Library library = translator.toELM();
    Map<String, ExpressionDef> defs = new HashMap<>();
    if (library.getStatements() != null) {
        for (ExpressionDef def : library.getStatements().getDef()) {
            defs.put(def.getName(), def);
        }
    }
    ExpressionDef def = defs.get("TestIf");
    assertThat(def.getResultType(), instanceOf(ChoiceType.class));
    ChoiceType choiceType = (ChoiceType) def.getResultType();
    DataType type = null;
    for (DataType dt : choiceType.getTypes()) {
        if (type == null) {
            type = dt;
            assertThat(dt, instanceOf(NamedType.class));
            assertThat(((NamedType) dt).getName(), equalTo("System.String"));
        } else {
            assertThat(dt, instanceOf(NamedType.class));
            assertThat(((NamedType) dt).getName(), equalTo("System.Boolean"));
        }
    }
    def = defs.get("TestCase");
    assertThat(def.getResultType(), instanceOf(ChoiceType.class));
    choiceType = (ChoiceType) def.getResultType();
    type = null;
    for (DataType dt : choiceType.getTypes()) {
        if (type == null) {
            type = dt;
            assertThat(dt, instanceOf(NamedType.class));
            assertThat(((NamedType) dt).getName(), equalTo("System.String"));
        } else {
            assertThat(dt, instanceOf(NamedType.class));
            assertThat(((NamedType) dt).getName(), equalTo("System.Boolean"));
        }
    }
}
Also used : HashMap(java.util.HashMap) ChoiceType(org.hl7.cql.model.ChoiceType) NamedType(org.hl7.cql.model.NamedType) DataType(org.hl7.cql.model.DataType) org.hl7.elm.r1(org.hl7.elm.r1) Test(org.testng.annotations.Test)

Example 4 with ChoiceType

use of org.hl7.cql.model.ChoiceType in project clinical_quality_language by cqframework.

the class OperatorEntry method expandChoices.

public List<Signature> expandChoices(Signature callSignature) {
    ArrayList<Signature> signatures = new ArrayList<Signature>();
    if (callSignature.containsChoices()) {
        ArrayList<ArrayList<DataType>> operandList = new ArrayList<ArrayList<DataType>>();
        for (DataType operand : callSignature.getOperandTypes()) {
            ArrayList<DataType> list = new ArrayList<DataType>();
            if (operand instanceof ChoiceType) {
                for (DataType type : ((ChoiceType) operand).getTypes()) {
                    list.add(type);
                }
            } else {
                list.add(operand);
            }
            operandList.add(list);
        }
        DataType[] result = new DataType[callSignature.getSize()];
        collectSignatures(operandList, result, 0, signatures);
    } else {
        signatures.add(callSignature);
    }
    return signatures;
}
Also used : ChoiceType(org.hl7.cql.model.ChoiceType) DataType(org.hl7.cql.model.DataType)

Example 5 with ChoiceType

use of org.hl7.cql.model.ChoiceType in project quality-measure-and-cohort-service by Alvearie.

the class ModelUtils method getChoiceTypeNames.

public static Collection<String> getChoiceTypeNames(TypeInfo typeInfo) {
    if (typeInfo != null && typeInfo.getBaseTypeSpecifier() != null && (typeInfo.getBaseTypeSpecifier() instanceof ChoiceTypeSpecifier)) {
        List<String> choiceTypes = new ArrayList<>();
        ChoiceTypeSpecifier choiceType = (ChoiceTypeSpecifier) typeInfo.getBaseTypeSpecifier();
        for (TypeSpecifier typeSpecifier : choiceType.getChoice()) {
            if (typeSpecifier instanceof NamedTypeSpecifier) {
                String typeName = ((NamedTypeSpecifier) typeSpecifier).getName();
                choiceTypes.add(typeName);
            }
        }
        return choiceTypes;
    } else {
        return Collections.emptyList();
    }
}
Also used : NamedTypeSpecifier(org.hl7.elm_modelinfo.r1.NamedTypeSpecifier) ArrayList(java.util.ArrayList) NamedTypeSpecifier(org.hl7.elm_modelinfo.r1.NamedTypeSpecifier) ChoiceTypeSpecifier(org.hl7.elm_modelinfo.r1.ChoiceTypeSpecifier) TypeSpecifier(org.hl7.elm_modelinfo.r1.TypeSpecifier) ChoiceTypeSpecifier(org.hl7.elm_modelinfo.r1.ChoiceTypeSpecifier)

Aggregations

ChoiceType (org.hl7.cql.model.ChoiceType)2 DataType (org.hl7.cql.model.DataType)2 ModelInfo (org.hl7.elm_modelinfo.r1.ModelInfo)2 JCommander (com.beust.jcommander.JCommander)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 CqlDataProvider (com.ibm.cohort.cql.data.CqlDataProvider)1 CqlDebug (com.ibm.cohort.cql.evaluation.CqlDebug)1 CqlEvaluationRequest (com.ibm.cohort.cql.evaluation.CqlEvaluationRequest)1 CqlEvaluationRequests (com.ibm.cohort.cql.evaluation.CqlEvaluationRequests)1 CqlEvaluationResult (com.ibm.cohort.cql.evaluation.CqlEvaluationResult)1 CqlEvaluator (com.ibm.cohort.cql.evaluation.CqlEvaluator)1 CqlExpressionConfiguration (com.ibm.cohort.cql.evaluation.CqlExpressionConfiguration)1 Parameter (com.ibm.cohort.cql.evaluation.parameters.Parameter)1 AnyColumnFunctions (com.ibm.cohort.cql.functions.AnyColumnFunctions)1 CohortExternalFunctionProvider (com.ibm.cohort.cql.functions.CohortExternalFunctionProvider)1 ClasspathCqlLibraryProvider (com.ibm.cohort.cql.library.ClasspathCqlLibraryProvider)1 CqlLibraryProvider (com.ibm.cohort.cql.library.CqlLibraryProvider)1 Format (com.ibm.cohort.cql.library.Format)1 HadoopBasedCqlLibraryProvider (com.ibm.cohort.cql.library.HadoopBasedCqlLibraryProvider)1 PriorityCqlLibraryProvider (com.ibm.cohort.cql.library.PriorityCqlLibraryProvider)1