Search in sources :

Example 6 with ModelInfo

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

the class ModelUtilsTest method testFHIR400Model.

@Test
public void testFHIR400Model() throws Exception {
    ModelInfo modelInfo = loadFromClasspath("org/hl7/fhir/fhir-modelinfo-4.0.0.xml");
    Map<QName, TypeNode> typeMap = ModelUtils.buildTypeMap(modelInfo);
    assertEquals(892, typeMap.size());
    TypeNode node = typeMap.get(new QName(modelInfo.getUrl(), "MoneyQuantity"));
    assertNotNull(node);
    assertEquals("Missing parent type", 1, node.getParentTypes().size());
    assertEquals("Quantity", node.getParentTypes().iterator().next().getTypeName().getLocalPart());
    node = typeMap.get(new QName(modelInfo.getUrl(), "Quantity"));
    assertNotNull(node);
    assertEquals(5, node.getChildTypes().size());
    assertEquals(1, node.getChildTypes().stream().filter(n -> n.getTypeName().getLocalPart().equals("MoneyQuantity")).collect(Collectors.counting()).longValue());
    node = typeMap.get(new QName(modelInfo.getUrl(), "Element"));
    assertNotNull(node);
    // The actual parent is System.Any, but we exclude included types from the type map
    assertEquals(0, node.getParentTypes().size());
}
Also used : ModelInfo(org.hl7.elm_modelinfo.r1.ModelInfo) Arrays(java.util.Arrays) TypeInfo(org.hl7.elm_modelinfo.r1.TypeInfo) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertThrows(org.junit.Assert.assertThrows) Collection(java.util.Collection) Assert.assertTrue(org.junit.Assert.assertTrue) Matchers(org.hamcrest.Matchers) IOException(java.io.IOException) Test(org.junit.Test) ClassInfo(org.hl7.elm_modelinfo.r1.ClassInfo) Collectors(java.util.stream.Collectors) File(java.io.File) Assert.assertThat(org.junit.Assert.assertThat) HashSet(java.util.HashSet) Assert.assertNull(org.junit.Assert.assertNull) TypeNode(com.ibm.cohort.cql.spark.optimizer.ModelUtils.TypeNode) Map(java.util.Map) QName(javax.xml.namespace.QName) JAXB(javax.xml.bind.JAXB) Assert.assertEquals(org.junit.Assert.assertEquals) InputStream(java.io.InputStream) ModelSpecifier(org.hl7.elm_modelinfo.r1.ModelSpecifier) ModelInfo(org.hl7.elm_modelinfo.r1.ModelInfo) QName(javax.xml.namespace.QName) TypeNode(com.ibm.cohort.cql.spark.optimizer.ModelUtils.TypeNode) Test(org.junit.Test)

Example 7 with ModelInfo

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

the class ModelUtilsTest method testAllTypesModel.

@Test
public void testAllTypesModel() throws Exception {
    ModelInfo modelInfo = JAXB.unmarshal(new File("src/test/resources/alltypes/modelinfo/alltypes-modelinfo-1.0.0.xml"), ModelInfo.class);
    Map<QName, TypeNode> typeMap = ModelUtils.buildTypeMap(modelInfo);
    assertEquals(6, typeMap.size());
    TypeNode node = typeMap.get(new QName(modelInfo.getUrl(), "B"));
    assertNotNull(node);
    assertEquals("Missing parent type", 1, node.getParentTypes().size());
    assertEquals("ChildBase", node.getParentTypes().iterator().next().getTypeName().getLocalPart());
    node = typeMap.get(new QName(modelInfo.getUrl(), "ChildBase"));
    assertNotNull(node);
    assertEquals(3, node.getChildTypes().size());
    assertEquals(new HashSet<>(Arrays.asList("B", "C", "D")), node.getChildTypes().stream().map(n -> n.getTypeName().getLocalPart()).collect(Collectors.toSet()));
}
Also used : ModelInfo(org.hl7.elm_modelinfo.r1.ModelInfo) QName(javax.xml.namespace.QName) TypeNode(com.ibm.cohort.cql.spark.optimizer.ModelUtils.TypeNode) File(java.io.File) Test(org.junit.Test)

Example 8 with ModelInfo

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

the class ModelUtilsTest method testGetBaseTypeNameSystemNamespacePrefix.

@Test
public void testGetBaseTypeNameSystemNamespacePrefix() {
    ModelInfo modelInfo = new ModelInfo();
    modelInfo.setName("Dummy");
    modelInfo.setUrl("urn:oid:Dummy");
    ClassInfo typeInfo = new ClassInfo();
    typeInfo.setName("MyType");
    typeInfo.setBaseType("System.BaseType");
    QName qname = ModelUtils.getBaseTypeName(modelInfo, typeInfo);
    assertEquals(new QName(CqlConstants.SYSTEM_MODEL_URI, "BaseType"), qname);
}
Also used : ModelInfo(org.hl7.elm_modelinfo.r1.ModelInfo) QName(javax.xml.namespace.QName) ClassInfo(org.hl7.elm_modelinfo.r1.ClassInfo) Test(org.junit.Test)

Example 9 with ModelInfo

use of org.hl7.elm_modelinfo.r1.ModelInfo 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 10 with ModelInfo

use of org.hl7.elm_modelinfo.r1.ModelInfo in project clinical_quality_language by cqframework.

the class Cql2ElmVisitor method visitContextDefinition.

@Override
public Object visitContextDefinition(cqlParser.ContextDefinitionContext ctx) {
    String modelIdentifier = parseString(ctx.modelIdentifier());
    String unqualifiedIdentifier = parseString(ctx.identifier());
    currentContext = modelIdentifier != null ? modelIdentifier + "." + unqualifiedIdentifier : unqualifiedIdentifier;
    if (!isUnfilteredContext(unqualifiedIdentifier)) {
        ModelContext modelContext = libraryBuilder.resolveContextName(modelIdentifier, unqualifiedIdentifier);
        // If this is the first time a context definition is encountered, construct a context definition:
        // define <Context> = element of [<Context model type>]
        Element modelContextDefinition = contextDefinitions.get(modelContext.getName());
        if (modelContextDefinition == null) {
            if (libraryBuilder.hasUsings()) {
                ModelInfo modelInfo = modelIdentifier == null ? libraryBuilder.getModel(libraryInfo.getDefaultModelName()).getModelInfo() : libraryBuilder.getModel(modelIdentifier).getModelInfo();
                // String contextTypeName = modelContext.getName();
                // DataType contextType = libraryBuilder.resolveTypeName(modelInfo.getName(), contextTypeName);
                DataType contextType = modelContext.getType();
                modelContextDefinition = libraryBuilder.resolveParameterRef(modelContext.getName());
                if (modelContextDefinition != null) {
                    contextDefinitions.put(modelContext.getName(), modelContextDefinition);
                } else {
                    Retrieve contextRetrieve = of.createRetrieve().withDataType(libraryBuilder.dataTypeToQName(contextType));
                    track(contextRetrieve, ctx);
                    contextRetrieve.setResultType(new ListType(contextType));
                    String contextClassIdentifier = ((ClassType) contextType).getIdentifier();
                    if (contextClassIdentifier != null) {
                        contextRetrieve.setTemplateId(contextClassIdentifier);
                    }
                    modelContextDefinition = of.createExpressionDef().withName(unqualifiedIdentifier).withContext(currentContext).withExpression(of.createSingletonFrom().withOperand(contextRetrieve));
                    track(modelContextDefinition, ctx);
                    ((ExpressionDef) modelContextDefinition).getExpression().setResultType(contextType);
                    modelContextDefinition.setResultType(contextType);
                    libraryBuilder.addExpression((ExpressionDef) modelContextDefinition);
                    contextDefinitions.put(modelContext.getName(), modelContextDefinition);
                }
            } else {
                modelContextDefinition = of.createExpressionDef().withName(unqualifiedIdentifier).withContext(currentContext).withExpression(of.createNull());
                track(modelContextDefinition, ctx);
                ((ExpressionDef) modelContextDefinition).getExpression().setResultType(libraryBuilder.resolveTypeName("System", "Any"));
                modelContextDefinition.setResultType(((ExpressionDef) modelContextDefinition).getExpression().getResultType());
                libraryBuilder.addExpression((ExpressionDef) modelContextDefinition);
                contextDefinitions.put(modelContext.getName(), modelContextDefinition);
            }
        }
    }
    ContextDef contextDef = of.createContextDef().withName(currentContext);
    track(contextDef, ctx);
    if (libraryBuilder.isCompatibleWith("1.5")) {
        libraryBuilder.addContext(contextDef);
    }
    return currentContext;
}
Also used : ModelInfo(org.hl7.elm_modelinfo.r1.ModelInfo) Element(org.hl7.elm.r1.Element)

Aggregations

ModelInfo (org.hl7.elm_modelinfo.r1.ModelInfo)25 Test (org.junit.Test)13 ClassInfo (org.hl7.elm_modelinfo.r1.ClassInfo)12 QName (javax.xml.namespace.QName)11 File (java.io.File)7 Map (java.util.Map)5 TypeNode (com.ibm.cohort.cql.spark.optimizer.ModelUtils.TypeNode)4 IOException (java.io.IOException)4 Collection (java.util.Collection)4 Collectors (java.util.stream.Collectors)4 VersionedIdentifier (org.hl7.elm.r1.VersionedIdentifier)4 ModelSpecifier (org.hl7.elm_modelinfo.r1.ModelSpecifier)4 TypeInfo (org.hl7.elm_modelinfo.r1.TypeInfo)4 Arrays (java.util.Arrays)3 HashMap (java.util.HashMap)3 HashSet (java.util.HashSet)3 List (java.util.List)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 CqlEvaluationRequest (com.ibm.cohort.cql.evaluation.CqlEvaluationRequest)2 CqlEvaluationRequests (com.ibm.cohort.cql.evaluation.CqlEvaluationRequests)2