Search in sources :

Example 1 with TypeNode

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;
}
Also used : QName(javax.xml.namespace.QName) TypeNode(com.ibm.cohort.cql.spark.optimizer.ModelUtils.TypeNode) HashSet(java.util.HashSet)

Example 2 with TypeNode

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());
}
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 3 with TypeNode

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()));
}
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 4 with TypeNode

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());
}
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 5 with TypeNode

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;
            });
        }
    }
}
Also used : ModelInfo(org.hl7.elm_modelinfo.r1.ModelInfo) Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) QName(javax.xml.namespace.QName) VersionedIdentifier(org.hl7.elm.r1.VersionedIdentifier) TypeNode(com.ibm.cohort.cql.spark.optimizer.ModelUtils.TypeNode) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

TypeNode (com.ibm.cohort.cql.spark.optimizer.ModelUtils.TypeNode)5 QName (javax.xml.namespace.QName)5 HashSet (java.util.HashSet)4 ModelInfo (org.hl7.elm_modelinfo.r1.ModelInfo)4 File (java.io.File)3 Map (java.util.Map)3 Test (org.junit.Test)3 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 Arrays (java.util.Arrays)2 Collection (java.util.Collection)2 Collectors (java.util.stream.Collectors)2 JAXB (javax.xml.bind.JAXB)2 Matchers (org.hamcrest.Matchers)2 ClassInfo (org.hl7.elm_modelinfo.r1.ClassInfo)2 ModelSpecifier (org.hl7.elm_modelinfo.r1.ModelSpecifier)2 TypeInfo (org.hl7.elm_modelinfo.r1.TypeInfo)2 Assert.assertEquals (org.junit.Assert.assertEquals)2 Assert.assertNotNull (org.junit.Assert.assertNotNull)2 Assert.assertNull (org.junit.Assert.assertNull)2