Search in sources :

Example 11 with ConceptMap

use of org.hl7.fhir.dstu3.model.ConceptMap in project bunsen by cerner.

the class ConceptMaps method addToConceptMap.

/**
 * Adds the given mappings to the concept map.
 *
 * @param map the concept map
 * @param mappings the mappings to add
 */
private static void addToConceptMap(ConceptMap map, Dataset<Mapping> mappings) {
    // Sort the items so they are grouped together optimally, and so
    // we consistently produce the same ordering, therefore making
    // inspection and comparison of the concept maps easier.
    List<Mapping> sortedMappings = mappings.sort("sourceSystem", "targetSystem", "sourceValue", "targetValue").collectAsList();
    ConceptMapGroupComponent currentGroup = null;
    SourceElementComponent element = null;
    // Workaround for the decoder producing an immutable array by
    // replacing it with a mutable one.
    map.setGroup(new ArrayList<>(map.getGroup()));
    for (Mapping mapping : sortedMappings) {
        // Add a new group if we don't match the previous one.
        if (currentGroup == null || !mapping.getSourceSystem().equals(currentGroup.getSource()) || !mapping.getTargetSystem().equals(currentGroup.getTarget())) {
            currentGroup = null;
            // Find a matching group.
            for (ConceptMapGroupComponent candidate : map.getGroup()) {
                if (mapping.getSourceSystem().equals(candidate.getSource()) && mapping.getTargetSystem().equals(candidate.getTarget())) {
                    currentGroup = candidate;
                    // Workaround for the decoder producing an immutable array by
                    // replacing it with a mutable one.
                    currentGroup.setElement(new ArrayList<>(currentGroup.getElement()));
                    break;
                }
            }
            // No matching group found, so add it.
            if (currentGroup == null) {
                currentGroup = map.addGroup();
                currentGroup.setSource(mapping.getSourceSystem());
                currentGroup.setTarget(mapping.getTargetSystem());
                // Ensure a new element is created for the newly created group.
                element = null;
            }
        }
        // so add one if it does not match the previous.
        if (element == null || !mapping.getSourceValue().equals(element.getCode())) {
            element = currentGroup.addElement();
            element.setCode(mapping.getSourceValue());
        }
        element.addTarget().setCode(mapping.getTargetValue());
    }
}
Also used : ConceptMapGroupComponent(org.hl7.fhir.dstu3.model.ConceptMap.ConceptMapGroupComponent) SourceElementComponent(org.hl7.fhir.dstu3.model.ConceptMap.SourceElementComponent)

Example 12 with ConceptMap

use of org.hl7.fhir.dstu3.model.ConceptMap in project bunsen by cerner.

the class ConceptMapsTest method testWriteToNewTables.

@Test
public void testWriteToNewTables() {
    spark.sql("create database test_mapping_write");
    ConceptMaps maps = ConceptMaps.getEmpty(spark).withMapsFromDirectory("src/test/resources/xml/conceptmaps");
    maps.writeToDatabase("test_mapping_write");
    ConceptMaps reloadedMaps = ConceptMaps.getFromDatabase(spark, "test_mapping_write");
    ConceptMap genderMap = reloadedMaps.getConceptMap("urn:cerner:poprec:fhir:conceptmap:demographics:gender", "0.0.1");
    Assert.assertNotNull(genderMap);
    Assert.assertEquals("urn:cerner:poprec:fhir:conceptmap:demographics:gender", genderMap.getUrl());
    Assert.assertEquals("0.0.1", genderMap.getVersion());
    Assert.assertEquals(3, genderMap.getGroup().size());
}
Also used : ConceptMap(org.hl7.fhir.dstu3.model.ConceptMap) Test(org.junit.Test)

Example 13 with ConceptMap

use of org.hl7.fhir.dstu3.model.ConceptMap in project bunsen by cerner.

the class ConceptMapsTest method testWithMapsFromDirectoryXml.

@Test
public void testWithMapsFromDirectoryXml() {
    ConceptMaps maps = ConceptMaps.getEmpty(spark).withMapsFromDirectory("src/test/resources/xml/conceptmaps");
    ConceptMap genderMap = maps.getConceptMap("urn:cerner:poprec:fhir:conceptmap:demographics:gender", "0.0.1");
    Assert.assertNotNull(genderMap);
    Assert.assertEquals("urn:cerner:poprec:fhir:conceptmap:demographics:gender", genderMap.getUrl());
    Assert.assertEquals("0.0.1", genderMap.getVersion());
}
Also used : ConceptMap(org.hl7.fhir.dstu3.model.ConceptMap) Test(org.junit.Test)

Example 14 with ConceptMap

use of org.hl7.fhir.dstu3.model.ConceptMap in project bunsen by cerner.

the class ConceptMapsTest method testLoadExpandedMappings.

@Test
public void testLoadExpandedMappings() throws FHIRException {
    ConceptMap map = conceptMap("urn:cerner:map:testmap", "1");
    // Explicitly create a mapping dataset to simulate an ETL load from an external source.
    Mapping mapping = new Mapping();
    mapping.setConceptMapUri(map.getUrl());
    mapping.setConceptMapVersion(map.getVersion());
    mapping.setSourceValueSet("urn:source:valueset");
    mapping.setTargetValue("urn:target:valueset");
    mapping.setSourceSystem("urn:source:system");
    mapping.setSourceValue("urn:source:code:a");
    mapping.setTargetSystem("urn:target:system");
    mapping.setTargetValue("urn:target:code:1");
    Dataset<Mapping> mappings = spark.createDataset(Arrays.asList(mapping), ConceptMaps.getMappingEncoder());
    ConceptMaps maps = ConceptMaps.getEmpty(spark).withExpandedMap(map, mappings);
    Dataset<Mapping> loadedMappings = maps.getMappings();
    Assert.assertEquals(1, loadedMappings.count());
    Mapping loadedMapping = loadedMappings.head();
    Assert.assertEquals(mapping, loadedMapping);
}
Also used : ConceptMap(org.hl7.fhir.dstu3.model.ConceptMap) Test(org.junit.Test)

Example 15 with ConceptMap

use of org.hl7.fhir.dstu3.model.ConceptMap in project bunsen by cerner.

the class ConceptMapsTest method conceptMap.

private static final ConceptMap conceptMap(String url, String version) {
    ConceptMap conceptMap = new ConceptMap();
    conceptMap.setUrl(url);
    conceptMap.setVersion(version);
    conceptMap.setExperimental(true);
    conceptMap.setSource(new UriType("urn:source:valueset"));
    conceptMap.setTarget(new UriType("urn:target:valueset"));
    conceptMap.addGroup().setSource("urn:source:system").setTarget("urn:target:system").addElement().setCode("urn:source:code:a").addTarget().setCode("urn:target:code:1");
    return conceptMap;
}
Also used : ConceptMap(org.hl7.fhir.dstu3.model.ConceptMap) UriType(org.hl7.fhir.dstu3.model.UriType)

Aggregations

ConceptMap (org.hl7.fhir.dstu3.model.ConceptMap)14 Test (org.junit.Test)8 ArrayList (java.util.ArrayList)3 ConceptMapGroupComponent (org.hl7.fhir.dstu3.model.ConceptMap.ConceptMapGroupComponent)3 UriType (org.hl7.fhir.dstu3.model.UriType)3 ArrayDeque (java.util.ArrayDeque)2 HashSet (java.util.HashSet)2 SourceElementComponent (org.hl7.fhir.dstu3.model.ConceptMap.SourceElementComponent)2 ConceptMaps (com.cerner.bunsen.codes.ConceptMaps)1 Mapping (com.cerner.bunsen.codes.Mapping)1 Serializable (java.io.Serializable)1 Timestamp (java.sql.Timestamp)1 Collections (java.util.Collections)1 Deque (java.util.Deque)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 Function (java.util.function.Function)1 Collectors (java.util.stream.Collectors)1