use of org.hl7.elm_modelinfo.r1.ClassInfo 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.elm_modelinfo.r1.ClassInfo in project quality-measure-and-cohort-service by Alvearie.
the class ModelUtilsTest method testGetBaseTypeNameIncludedNamespacePrefixMissing.
@Test
public void testGetBaseTypeNameIncludedNamespacePrefixMissing() {
ModelSpecifier otherModel = new ModelSpecifier().withName("Other").withVersion("1.2.3").withUrl("urn:oid:Other");
ModelInfo modelInfo = new ModelInfo();
modelInfo.setName("Dummy");
modelInfo.setVersion("5.4.3");
modelInfo.setUrl("urn:oid:Dummy");
modelInfo.getRequiredModelInfo().add(otherModel);
ClassInfo typeInfo = new ClassInfo();
typeInfo.setName("MyType");
typeInfo.setBaseType("Missing.BaseType");
IllegalArgumentException iex = assertThrows(IllegalArgumentException.class, () -> ModelUtils.getBaseTypeName(modelInfo, typeInfo));
assertTrue(iex.getMessage(), iex.getMessage().contains("Missing"));
assertTrue(iex.getMessage(), iex.getMessage().contains(modelInfo.getName()));
assertTrue(iex.getMessage(), iex.getMessage().contains(modelInfo.getVersion()));
}
use of org.hl7.elm_modelinfo.r1.ClassInfo in project quality-measure-and-cohort-service by Alvearie.
the class ModelUtilsTest method testGetBaseTypeNameNoNamespacePrefix.
@Test
public void testGetBaseTypeNameNoNamespacePrefix() {
ModelInfo modelInfo = new ModelInfo();
modelInfo.setName("Dummy");
modelInfo.setUrl("urn:oid:Dummy");
ClassInfo typeInfo = new ClassInfo();
typeInfo.setName("MyType");
typeInfo.setBaseType("BaseType");
QName qname = ModelUtils.getBaseTypeName(modelInfo, typeInfo);
assertEquals(new QName(modelInfo.getUrl(), "BaseType"), qname);
}
use of org.hl7.elm_modelinfo.r1.ClassInfo 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.ClassInfo 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);
}
Aggregations