Search in sources :

Example 26 with SourceElementComponent

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

the class ConceptMaps method expandMappingsIterator.

private static Iterator<Mapping> expandMappingsIterator(ConceptMap map) {
    List<Mapping> mappings = new ArrayList<>();
    for (ConceptMapGroupComponent group : map.getGroup()) {
        for (SourceElementComponent element : group.getElement()) {
            for (TargetElementComponent target : element.getTarget()) {
                Mapping mapping = new Mapping();
                mapping.setConceptMapUri(map.getUrl());
                mapping.setConceptMapVersion(map.getVersion());
                try {
                    String sourceValue = map.getSource() instanceof UriType ? map.getSourceUriType().getValue() : map.getSourceReference().getReference();
                    mapping.setSourceValueSet(sourceValue);
                    String targetValue = map.getTarget() instanceof UriType ? map.getTargetUriType().getValue() : map.getTargetReference().getReference();
                    mapping.setTargetValueSet(targetValue);
                } catch (FHIRException fhirException) {
                    // an exception.
                    throw new RuntimeException(fhirException);
                }
                mapping.setSourceSystem(group.getSource());
                mapping.setSourceValue(element.getCode());
                mapping.setTargetSystem(group.getTarget());
                mapping.setTargetValue(target.getCode());
                if (target.getEquivalence() != null) {
                    mapping.setEquivalence(target.getEquivalence().toCode());
                }
                mappings.add(mapping);
            }
        }
    }
    return mappings.iterator();
}
Also used : ArrayList(java.util.ArrayList) TargetElementComponent(org.hl7.fhir.dstu3.model.ConceptMap.TargetElementComponent) Mapping(com.cerner.bunsen.spark.codes.Mapping) FHIRException(org.hl7.fhir.exceptions.FHIRException) ConceptMapGroupComponent(org.hl7.fhir.dstu3.model.ConceptMap.ConceptMapGroupComponent) SourceElementComponent(org.hl7.fhir.dstu3.model.ConceptMap.SourceElementComponent) UriType(org.hl7.fhir.dstu3.model.UriType)

Example 27 with SourceElementComponent

use of org.hl7.fhir.r4b.model.ConceptMap.SourceElementComponent 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 28 with SourceElementComponent

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

the class ConceptMaps method addToConceptMap.

@Override
protected 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 : Mapping(com.cerner.bunsen.codes.Mapping) ConceptMapGroupComponent(org.hl7.fhir.r4.model.ConceptMap.ConceptMapGroupComponent) SourceElementComponent(org.hl7.fhir.r4.model.ConceptMap.SourceElementComponent)

Example 29 with SourceElementComponent

use of org.hl7.fhir.r4b.model.ConceptMap.SourceElementComponent in project kindling by HL7.

the class ResourceNameConceptMapGenerator method genMap.

private static void genMap(String src, String srcV, String dst, String dstV, Sheet xls, String dstFolder, String name, Date date) throws FileNotFoundException, IOException {
    ConceptMap cm = new ConceptMap();
    cm.setId(name);
    cm.setUrl("http://hl7.org/fhir/" + name);
    cm.setName("ResourceNames" + src.toUpperCase() + "to" + dst.toUpperCase());
    cm.setTitle("Resource Names " + src.toUpperCase() + " to " + dst.toUpperCase());
    cm.setDescription("This map contains a mapping between resources from " + src + " to " + dst);
    cm.setStatus(PublicationStatus.ACTIVE);
    cm.setDate(date);
    ConceptMapGroupComponent grp = cm.addGroup();
    grp.setSource("http://hl7.org/fhir/" + srcV + "/resource-types");
    grp.setTarget("http://hl7.org/fhir/" + dstV + "/resource-types");
    for (int row = 0; row < xls.rows.size(); row++) {
        String s = xls.getColumn(row, src);
        String t = xls.getColumn(row, dst);
        if (!Utilities.noString(s) && (Character.isAlphabetic(s.charAt(0)) || s.startsWith("->") || s.startsWith("("))) {
            String c = s.startsWith("->") ? s.substring(2).trim() : s.startsWith("(") ? s.substring(1, s.length() - 1) : s;
            SourceElementComponent map = elementForCode(grp, c);
            if (Utilities.noString(t))
                map.addTarget().setRelationship(ConceptMapRelationship.NOTRELATEDTO);
            else if (t.startsWith("("))
                map.addTarget().setCode(t.substring(1, t.length() - 1)).setRelationship(ConceptMapRelationship.RELATEDTO);
            else if (t.startsWith("->"))
                map.addTarget().setCode(t.substring(2).trim()).setRelationship(ConceptMapRelationship.SOURCEISBROADERTHANTARGET);
            else if (t.startsWith(":"))
                map.addTarget().setComment(t.substring(1).trim()).setRelationship(ConceptMapRelationship.NOTRELATEDTO);
            else if (s.startsWith("-<"))
                map.addTarget().setCode(t).setRelationship(ConceptMapRelationship.SOURCEISNARROWERTHANTARGET);
            else if (s.startsWith("("))
                map.addTarget().setCode(t).setRelationship(ConceptMapRelationship.RELATEDTO);
            else
                map.addTarget().setCode(t).setRelationship(ConceptMapRelationship.EQUIVALENT);
        }
    }
    // 
    new JsonParser().setOutputStyle(OutputStyle.PRETTY).compose(new FileOutputStream(Utilities.path(dstFolder, "ConceptMap-" + name + ".json")), cm);
}
Also used : FileOutputStream(java.io.FileOutputStream) ConceptMap(org.hl7.fhir.r5.model.ConceptMap) ConceptMapGroupComponent(org.hl7.fhir.r5.model.ConceptMap.ConceptMapGroupComponent) SourceElementComponent(org.hl7.fhir.r5.model.ConceptMap.SourceElementComponent) JsonParser(org.hl7.fhir.r5.formats.JsonParser)

Example 30 with SourceElementComponent

use of org.hl7.fhir.r4b.model.ConceptMap.SourceElementComponent in project kindling by HL7.

the class CodeListToValueSetParser method processV3Map.

private void processV3Map(ConceptMap cm, String url, String code, String v3map) throws Exception {
    if (Utilities.noString(v3map))
        return;
    for (String m : v3map.split(",")) {
        // analyse m
        String[] n = m.split("\\(");
        String comm = (n.length > 1) ? n[1].substring(0, n[1].length() - 1) : null;
        n = n[0].split("\\.");
        if (n.length != 2)
            throw new Exception("Error processing v3 map value for " + cm.getName() + "." + code + " '" + m + "' - format should be CodeSystem.code (comment) - the comment bit is optional");
        String rel = null;
        String tbl = n[0];
        if (Utilities.existsInList(n[0].substring(0, 1), "=", "~", ">", "<")) {
            rel = n[0].substring(0, 1);
            tbl = n[0].substring(1);
        }
        String cd = n[1];
        ConceptMapGroupComponent grp = getGroup(cm, url, "http://terminology.hl7.org/CodeSystem/v3-" + tbl);
        SourceElementComponent src = getSource(grp, code);
        TargetElementComponent tgt = src.addTarget();
        tgt.setCode(cd.trim());
        tgt.setComment(comm);
        if (rel == null || rel.equals("="))
            tgt.setRelationship(ConceptMapRelationship.EQUIVALENT);
        else if (rel.equals("~"))
            tgt.setRelationship(ConceptMapRelationship.EQUIVALENT);
        else if (rel.equals("<"))
            tgt.setRelationship(ConceptMapRelationship.SOURCEISNARROWERTHANTARGET);
        else if (rel.equals(">")) {
            tgt.setRelationship(ConceptMapRelationship.SOURCEISBROADERTHANTARGET);
            if (!tgt.hasComment())
                throw new Exception("Missing comment for narrower match on " + cm.getName() + "/" + code);
        } else
            throw new Exception("Unable to understand relationship character " + rel);
    }
}
Also used : TargetElementComponent(org.hl7.fhir.r5.model.ConceptMap.TargetElementComponent) ConceptMapGroupComponent(org.hl7.fhir.r5.model.ConceptMap.ConceptMapGroupComponent) SourceElementComponent(org.hl7.fhir.r5.model.ConceptMap.SourceElementComponent)

Aggregations

SourceElementComponent (org.hl7.fhir.r5.model.ConceptMap.SourceElementComponent)14 FHIRException (org.hl7.fhir.exceptions.FHIRException)11 ConceptMapGroupComponent (org.hl7.fhir.r5.model.ConceptMap.ConceptMapGroupComponent)11 HashMap (java.util.HashMap)10 ConceptMapGroupComponent (org.hl7.fhir.dstu3.model.ConceptMap.ConceptMapGroupComponent)9 SourceElementComponent (org.hl7.fhir.dstu3.model.ConceptMap.SourceElementComponent)9 TargetElementComponent (org.hl7.fhir.r5.model.ConceptMap.TargetElementComponent)9 ConceptMapGroupComponent (org.hl7.fhir.r4.model.ConceptMap.ConceptMapGroupComponent)8 SourceElementComponent (org.hl7.fhir.r4.model.ConceptMap.SourceElementComponent)8 SourceElementComponent (org.hl7.fhir.r4b.model.ConceptMap.SourceElementComponent)8 ArrayList (java.util.ArrayList)7 TargetElementComponent (org.hl7.fhir.dstu3.model.ConceptMap.TargetElementComponent)6 TargetElementComponent (org.hl7.fhir.r4.model.ConceptMap.TargetElementComponent)6 XhtmlNode (org.hl7.fhir.utilities.xhtml.XhtmlNode)6 HashSet (java.util.HashSet)5 ConceptMap (org.hl7.fhir.r4.model.ConceptMap)5 ConceptMapGroupComponent (org.hl7.fhir.r4b.model.ConceptMap.ConceptMapGroupComponent)5 TargetElementComponent (org.hl7.fhir.r4b.model.ConceptMap.TargetElementComponent)5 Coding (org.hl7.fhir.r4.model.Coding)4 ConceptMap (org.hl7.fhir.r5.model.ConceptMap)4