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());
}
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()));
}
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);
}
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();
}
}
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;
}
Aggregations