use of com.ibm.cohort.cql.spark.optimizer.ModelUtils.TypeNode in project quality-measure-and-cohort-service by Alvearie.
the class DataTypeRequirementsProcessor method getDescendants.
protected Set<QName> getDescendants(Map<QName, TypeNode> typeMap, QName self) {
Set<QName> names = new HashSet<>();
TypeNode node = typeMap.get(self);
// node will be null when the type is found in a different model
if (node != null) {
for (TypeNode child : node.getChildTypes()) {
names.add(child.getTypeName());
names.addAll(getDescendants(typeMap, child.getTypeName()));
}
}
return names;
}
use of com.ibm.cohort.cql.spark.optimizer.ModelUtils.TypeNode 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 com.ibm.cohort.cql.spark.optimizer.ModelUtils.TypeNode 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 com.ibm.cohort.cql.spark.optimizer.ModelUtils.TypeNode in project quality-measure-and-cohort-service by Alvearie.
the class ModelUtilsTest method testFHIR401Model.
@Test
public void testFHIR401Model() throws Exception {
ModelInfo modelInfo = loadFromClasspath("org/hl7/fhir/fhir-modelinfo-4.0.1.xml");
Map<QName, TypeNode> typeMap = ModelUtils.buildTypeMap(modelInfo);
assertEquals(931, 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(6, node.getChildTypes().size());
assertEquals(1, node.getChildTypes().stream().filter(n -> n.getTypeName().getLocalPart().equals("MoneyQuantity")).collect(Collectors.counting()).longValue());
}
use of com.ibm.cohort.cql.spark.optimizer.ModelUtils.TypeNode in project quality-measure-and-cohort-service by Alvearie.
the class DataTypeRequirementsProcessor method addToChildTypes.
protected <T> void addToChildTypes(Map<String, VersionedIdentifier> models, Map<QName, Set<T>> valuesByDataType) {
for (Map.Entry<String, VersionedIdentifier> modelData : models.entrySet()) {
Map<QName, TypeNode> typeMap = typeMapByModelUri.computeIfAbsent(modelData.getKey(), key -> {
ModelInfo modelInfo = modelManager.resolveModel(modelData.getValue()).getModelInfo();
return ModelUtils.buildTypeMap(modelInfo);
});
// Create a temporary set and then merge later to avoid concurrent modification
Map<QName, Set<T>> newValues = new HashMap<>();
for (Map.Entry<QName, Set<T>> paths : valuesByDataType.entrySet()) {
Set<QName> names = getDescendants(typeMap, paths.getKey());
for (QName name : names) {
Set<T> childValues = newValues.computeIfAbsent(name, key -> new HashSet<>());
childValues.addAll(paths.getValue());
}
}
for (Map.Entry<QName, Set<T>> entry : newValues.entrySet()) {
valuesByDataType.merge(entry.getKey(), entry.getValue(), (oldSet, newSet) -> {
oldSet.addAll(newSet);
return oldSet;
});
}
}
}
Aggregations