Search in sources :

Example 11 with SourceElementComponent

use of org.hl7.fhir.r5.model.ConceptMap.SourceElementComponent in project org.hl7.fhir.core by hapifhir.

the class StructureMapUtilities method parseConceptMap.

private void parseConceptMap(StructureMap result, FHIRLexer lexer) throws FHIRLexerException {
    lexer.token("conceptmap");
    ConceptMap map = new ConceptMap();
    String id = lexer.readConstant("map id");
    if (id.startsWith("#")) {
        throw lexer.error("Concept Map identifier must start with #");
    }
    map.setId(id);
    // todo: how to add this to the text format
    map.setStatus(PublicationStatus.DRAFT);
    result.getContained().add(map);
    lexer.token("{");
    // lexer.token("source");
    // map.setSource(new UriType(lexer.readConstant("source")));
    // lexer.token("target");
    // map.setSource(new UriType(lexer.readConstant("target")));
    Map<String, String> prefixes = new HashMap<String, String>();
    while (lexer.hasToken("prefix")) {
        lexer.token("prefix");
        String n = lexer.take();
        lexer.token("=");
        String v = lexer.readConstant("prefix url");
        prefixes.put(n, v);
    }
    while (lexer.hasToken("unmapped")) {
        lexer.token("unmapped");
        lexer.token("for");
        String n = readPrefix(prefixes, lexer);
        ConceptMapGroupComponent g = getGroup(map, n, null);
        lexer.token("=");
        String v = lexer.take();
        if (v.equals("provided")) {
            g.getUnmapped().setMode(ConceptMapGroupUnmappedMode.PROVIDED);
        } else {
            throw lexer.error("Only unmapped mode PROVIDED is supported at this time");
        }
    }
    while (!lexer.hasToken("}")) {
        String srcs = readPrefix(prefixes, lexer);
        lexer.token(":");
        String sc = lexer.getCurrent().startsWith("\"") ? lexer.readConstant("code") : lexer.take();
        ConceptMapEquivalence eq = readEquivalence(lexer);
        String tgts = (eq != ConceptMapEquivalence.UNMATCHED) ? readPrefix(prefixes, lexer) : "";
        ConceptMapGroupComponent g = getGroup(map, srcs, tgts);
        SourceElementComponent e = g.addElement();
        e.setCode(sc);
        if (e.getCode().startsWith("\"")) {
            e.setCode(lexer.processConstant(e.getCode()));
        }
        TargetElementComponent tgt = e.addTarget();
        tgt.setEquivalence(eq);
        if (tgt.getEquivalence() != ConceptMapEquivalence.UNMATCHED) {
            lexer.token(":");
            tgt.setCode(lexer.take());
            if (tgt.getCode().startsWith("\"")) {
                tgt.setCode(lexer.processConstant(tgt.getCode()));
            }
        }
        tgt.setComment(lexer.getFirstComment());
    }
    lexer.token("}");
}
Also used : TargetElementComponent(org.hl7.fhir.r4b.model.ConceptMap.TargetElementComponent) ConceptMapEquivalence(org.hl7.fhir.r4b.model.ConceptMap.ConceptMapEquivalence) ConceptMapGroupComponent(org.hl7.fhir.r4b.model.ConceptMap.ConceptMapGroupComponent) SourceElementComponent(org.hl7.fhir.r4b.model.ConceptMap.SourceElementComponent)

Example 12 with SourceElementComponent

use of org.hl7.fhir.r5.model.ConceptMap.SourceElementComponent in project org.hl7.fhir.core by hapifhir.

the class StructureMapUtilities method produceConceptMap.

private static void produceConceptMap(StringBuilder b, ConceptMap cm) {
    b.append("conceptmap \"");
    b.append(cm.getId());
    b.append("\" {\r\n");
    Map<String, String> prefixesSrc = new HashMap<String, String>();
    Map<String, String> prefixesTgt = new HashMap<String, String>();
    char prefix = 's';
    for (ConceptMapGroupComponent cg : cm.getGroup()) {
        if (!prefixesSrc.containsKey(cg.getSource())) {
            prefixesSrc.put(cg.getSource(), String.valueOf(prefix));
            b.append("  prefix ");
            b.append(prefix);
            b.append(" = \"");
            b.append(cg.getSource());
            b.append("\"\r\n");
            prefix++;
        }
        if (!prefixesTgt.containsKey(cg.getTarget())) {
            prefixesTgt.put(cg.getTarget(), String.valueOf(prefix));
            b.append("  prefix ");
            b.append(prefix);
            b.append(" = \"");
            b.append(cg.getTarget());
            b.append("\"\r\n");
            prefix++;
        }
    }
    b.append("\r\n");
    for (ConceptMapGroupComponent cg : cm.getGroup()) {
        if (cg.hasUnmapped()) {
            b.append("  unmapped for ");
            b.append(prefixesSrc.get(cg.getSource()));
            b.append(" = ");
            b.append(cg.getUnmapped().getMode().toCode());
            b.append("\r\n");
        }
    }
    for (ConceptMapGroupComponent cg : cm.getGroup()) {
        for (SourceElementComponent ce : cg.getElement()) {
            b.append("  ");
            b.append(prefixesSrc.get(cg.getSource()));
            b.append(":");
            if (Utilities.isToken(ce.getCode())) {
                b.append(ce.getCode());
            } else {
                b.append("\"");
                b.append(ce.getCode());
                b.append("\"");
            }
            b.append(" ");
            b.append(getChar(ce.getTargetFirstRep().getEquivalence()));
            b.append(" ");
            b.append(prefixesTgt.get(cg.getTarget()));
            b.append(":");
            if (Utilities.isToken(ce.getTargetFirstRep().getCode())) {
                b.append(ce.getTargetFirstRep().getCode());
            } else {
                b.append("\"");
                b.append(ce.getTargetFirstRep().getCode());
                b.append("\"");
            }
            b.append("\r\n");
        }
    }
    b.append("}\r\n\r\n");
}
Also used : ConceptMapGroupComponent(org.hl7.fhir.r4b.model.ConceptMap.ConceptMapGroupComponent) SourceElementComponent(org.hl7.fhir.r4b.model.ConceptMap.SourceElementComponent)

Example 13 with SourceElementComponent

use of org.hl7.fhir.r5.model.ConceptMap.SourceElementComponent in project org.hl7.fhir.core by hapifhir.

the class StructureMapUtilities method produceConceptMap.

private static void produceConceptMap(StringBuilder b, ConceptMap cm) {
    b.append("conceptmap \"");
    b.append(cm.getId());
    b.append("\" {\r\n");
    Map<String, String> prefixesSrc = new HashMap<String, String>();
    Map<String, String> prefixesTgt = new HashMap<String, String>();
    char prefix = 's';
    for (ConceptMapGroupComponent cg : cm.getGroup()) {
        if (!prefixesSrc.containsKey(cg.getSource())) {
            prefixesSrc.put(cg.getSource(), String.valueOf(prefix));
            b.append("  prefix ");
            b.append(prefix);
            b.append(" = \"");
            b.append(cg.getSource());
            b.append("\"\r\n");
            prefix++;
        }
        if (!prefixesTgt.containsKey(cg.getTarget())) {
            prefixesTgt.put(cg.getTarget(), String.valueOf(prefix));
            b.append("  prefix ");
            b.append(prefix);
            b.append(" = \"");
            b.append(cg.getTarget());
            b.append("\"\r\n");
            prefix++;
        }
    }
    b.append("\r\n");
    for (ConceptMapGroupComponent cg : cm.getGroup()) {
        if (cg.hasUnmapped()) {
            b.append("  unmapped for ");
            b.append(prefix);
            b.append(" = ");
            b.append(cg.getUnmapped().getMode());
            b.append("\r\n");
        }
    }
    for (ConceptMapGroupComponent cg : cm.getGroup()) {
        for (SourceElementComponent ce : cg.getElement()) {
            b.append("  ");
            b.append(prefixesSrc.get(cg.getSource()));
            b.append(":");
            b.append(ce.getCode());
            b.append(" ");
            b.append(getChar(ce.getTargetFirstRep().getEquivalence()));
            b.append(" ");
            b.append(prefixesTgt.get(cg.getTarget()));
            b.append(":");
            b.append(ce.getTargetFirstRep().getCode());
            b.append("\r\n");
        }
    }
    b.append("}\r\n\r\n");
}
Also used : HashMap(java.util.HashMap) ConceptMapGroupComponent(org.hl7.fhir.dstu3.model.ConceptMap.ConceptMapGroupComponent) SourceElementComponent(org.hl7.fhir.dstu3.model.ConceptMap.SourceElementComponent)

Example 14 with SourceElementComponent

use of org.hl7.fhir.r5.model.ConceptMap.SourceElementComponent in project org.hl7.fhir.core by hapifhir.

the class ConceptMapEngineTest method codingTranslate.

@Test
@DisplayName("Coding is translated according to ConceptMap")
void codingTranslate() throws IOException {
    final ConceptMap.SourceElementComponent sourceElementComponent = getSourceElementComponent();
    final ConceptMapEngine conceptMapEngine = getConceptMapEngine(Arrays.asList(sourceElementComponent));
    Coding coding = new Coding(null, SOURCE_CODE_STRING, "Body Weight");
    Coding actual = conceptMapEngine.translate(coding, CONCEPT_MAP_URL);
    assertEquals(TARGET_CODE_STRING, actual.getCode());
}
Also used : Coding(org.hl7.fhir.r4.model.Coding) ConceptMap(org.hl7.fhir.r4.model.ConceptMap) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 15 with SourceElementComponent

use of org.hl7.fhir.r5.model.ConceptMap.SourceElementComponent in project org.hl7.fhir.core by hapifhir.

the class ConceptMapSpreadsheetGenerator method renderGroup.

private void renderGroup(ConceptMapGroupComponent grp, int i) {
    Sheet sheet = makeSheet("Mapping Table " + Integer.toString(i));
    addHeaders(sheet, "Source", "Display", "Relationship", "Target", "Display");
    addRow(sheet, grp.getSource(), "", "", grp.getTarget(), "");
    for (SourceElementComponent s : grp.getElement()) {
        for (TargetElementComponent t : s.getTarget()) {
            addRow(sheet, s.getCode(), s.getDisplay(), t.getEquivalenceElement().asStringValue(), t.getCode(), t.getDisplay());
        }
    }
}
Also used : TargetElementComponent(org.hl7.fhir.r4b.model.ConceptMap.TargetElementComponent) Sheet(org.apache.poi.ss.usermodel.Sheet) SourceElementComponent(org.hl7.fhir.r4b.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