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