Search in sources :

Example 36 with ConceptMap

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

the class NarrativeGenerator method generateDefinition.

private boolean generateDefinition(XhtmlNode x, CodeSystem cs, boolean header) {
    boolean hasExtensions = false;
    Map<ConceptMap, String> mymaps = new HashMap<ConceptMap, String>();
    for (ConceptMap a : context.findMapsForSource(cs.getValueSet())) {
        String url = "";
        ValueSet vsr = context.fetchResource(ValueSet.class, ((Reference) a.getTarget()).getReference());
        if (vsr != null)
            url = (String) vsr.getUserData("filename");
        mymaps.put(a, url);
    }
    // also, look in the contained resources for a concept map
    for (Resource r : cs.getContained()) {
        if (r instanceof ConceptMap) {
            ConceptMap cm = (ConceptMap) r;
            if (((Reference) cm.getSource()).getReference().equals(cs.getValueSet())) {
                String url = "";
                ValueSet vsr = context.fetchResource(ValueSet.class, ((Reference) cm.getTarget()).getReference());
                if (vsr != null)
                    url = (String) vsr.getUserData("filename");
                mymaps.put(cm, url);
            }
        }
    }
    List<String> langs = new ArrayList<String>();
    if (header) {
        XhtmlNode h = x.addTag("h2");
        h.addText(cs.getName());
        XhtmlNode p = x.addTag("p");
        smartAddText(p, cs.getDescription());
        if (cs.hasCopyright())
            generateCopyright(x, cs);
    }
    XhtmlNode p = x.addTag("p");
    p.addText("This code system " + cs.getUrl() + " defines the following codes:");
    XhtmlNode t = x.addTag("table").setAttribute("class", "codes");
    boolean commentS = false;
    boolean deprecated = false;
    boolean display = false;
    boolean hierarchy = false;
    for (ConceptDefinitionComponent c : cs.getConcept()) {
        commentS = commentS || conceptsHaveComments(c);
        deprecated = deprecated || conceptsHaveDeprecated(cs, c);
        display = display || conceptsHaveDisplay(c);
        hierarchy = hierarchy || c.hasConcept();
        scanLangs(c, langs);
    }
    addMapHeaders(addTableHeaderRowStandard(t, hierarchy, display, true, commentS, deprecated), mymaps);
    for (ConceptDefinitionComponent c : cs.getConcept()) {
        hasExtensions = addDefineRowToTable(t, c, 0, hierarchy, display, commentS, deprecated, mymaps, cs.getUrl(), cs) || hasExtensions;
    }
    if (langs.size() > 0) {
        Collections.sort(langs);
        x.addTag("p").addTag("b").addText("Additional Language Displays");
        t = x.addTag("table").setAttribute("class", "codes");
        XhtmlNode tr = t.addTag("tr");
        tr.addTag("td").addTag("b").addText("Code");
        for (String lang : langs) tr.addTag("td").addTag("b").addText(lang);
        for (ConceptDefinitionComponent c : cs.getConcept()) {
            addLanguageRow(c, t, langs);
        }
    }
    return hasExtensions;
}
Also used : ConceptDefinitionComponent(org.hl7.fhir.dstu2016may.model.CodeSystem.ConceptDefinitionComponent) HashMap(java.util.HashMap) Resource(org.hl7.fhir.dstu2016may.model.Resource) DomainResource(org.hl7.fhir.dstu2016may.model.DomainResource) ArrayList(java.util.ArrayList) ConceptMap(org.hl7.fhir.dstu2016may.model.ConceptMap) ValueSet(org.hl7.fhir.dstu2016may.model.ValueSet) XhtmlNode(org.hl7.fhir.utilities.xhtml.XhtmlNode)

Example 37 with ConceptMap

use of org.hl7.fhir.r4b.model.ConceptMap 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("#"))
        lexer.error("Concept Map identifier must start with #");
    map.setId(id.substring(1));
    result.getContained().add(map);
    lexer.token("{");
    lexer.skipComments();
    // 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("}")) {
        SourceElementComponent e = map.addElement();
        e.setSystem(readPrefix(prefixes, lexer));
        lexer.token(":");
        e.setCode(lexer.take());
        TargetElementComponent tgt = e.addTarget();
        tgt.setEquivalence(readEquivalence(lexer));
        if (tgt.getEquivalence() != ConceptMapEquivalence.UNMATCHED) {
            tgt.setSystem(readPrefix(prefixes, lexer));
            lexer.token(":");
            tgt.setCode(lexer.take());
        }
        if (lexer.hasComment())
            tgt.setComments(lexer.take().substring(2).trim());
    }
    lexer.token("}");
}
Also used : HashMap(java.util.HashMap) TargetElementComponent(org.hl7.fhir.dstu2016may.model.ConceptMap.TargetElementComponent) ConceptMap(org.hl7.fhir.dstu2016may.model.ConceptMap) SourceElementComponent(org.hl7.fhir.dstu2016may.model.ConceptMap.SourceElementComponent)

Example 38 with ConceptMap

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

the class StructureMapUtilities method translate.

public Base translate(TransformContext context, StructureMap map, Base source, String conceptMapUrl, String fieldToReturn) throws FHIRException {
    Coding src = new Coding();
    if (source.isPrimitive()) {
        src.setCode(source.primitiveValue());
    } else if ("Coding".equals(source.fhirType())) {
        Base[] b = source.getProperty("system".hashCode(), "system", true);
        if (b.length == 1)
            src.setSystem(b[0].primitiveValue());
        b = source.getProperty("code".hashCode(), "code", true);
        if (b.length == 1)
            src.setCode(b[0].primitiveValue());
    } else if ("CE".equals(source.fhirType())) {
        Base[] b = source.getProperty("codeSystem".hashCode(), "codeSystem", true);
        if (b.length == 1)
            src.setSystem(b[0].primitiveValue());
        b = source.getProperty("code".hashCode(), "code", true);
        if (b.length == 1)
            src.setCode(b[0].primitiveValue());
    } else
        throw new FHIRException("Unable to translate source " + source.fhirType());
    if (conceptMapUrl.equals("http://hl7.org/fhir/ConceptMap/special-oid2uri")) {
        String uri = worker.oid2Uri(src.getCode());
        if (uri == null)
            uri = "urn:oid:" + src.getCode();
        if ("uri".equals(fieldToReturn))
            return new UriType(uri);
        else
            throw new FHIRException("Error in return code");
    } else {
        ConceptMap cmap = null;
        if (conceptMapUrl.startsWith("#")) {
            for (Resource r : map.getContained()) {
                if (r instanceof ConceptMap && ((ConceptMap) r).getId().equals(conceptMapUrl.substring(1)))
                    cmap = (ConceptMap) r;
            }
        } else
            cmap = worker.fetchResource(ConceptMap.class, conceptMapUrl);
        Coding outcome = null;
        boolean done = false;
        String message = null;
        if (cmap == null) {
            if (services == null)
                message = "No map found for " + conceptMapUrl;
            else {
                outcome = services.translate(context.appInfo, src, conceptMapUrl);
                done = true;
            }
        } else {
            List<SourceElementComponent> list = new ArrayList<SourceElementComponent>();
            for (SourceElementComponent e : cmap.getElement()) {
                if (!src.hasSystem() && src.getCode().equals(e.getCode()))
                    list.add(e);
                else if (src.hasSystem() && src.getSystem().equals(e.getSystem()) && src.getCode().equals(e.getCode()))
                    list.add(e);
            }
            if (list.size() == 0)
                done = true;
            else if (list.get(0).getTarget().size() == 0)
                message = "Concept map " + conceptMapUrl + " found no translation for " + src.getCode();
            else {
                for (TargetElementComponent tgt : list.get(0).getTarget()) {
                    if (tgt.getEquivalence() == ConceptMapEquivalence.EQUAL || tgt.getEquivalence() == ConceptMapEquivalence.EQUIVALENT || tgt.getEquivalence() == ConceptMapEquivalence.WIDER) {
                        if (done) {
                            message = "Concept map " + conceptMapUrl + " found multiple matches for " + src.getCode();
                            done = false;
                        } else {
                            done = true;
                            outcome = new Coding().setCode(tgt.getCode()).setSystem(tgt.getSystem());
                        }
                    } else if (tgt.getEquivalence() == ConceptMapEquivalence.UNMATCHED) {
                        done = true;
                    }
                }
                if (!done)
                    message = "Concept map " + conceptMapUrl + " found no usable translation for " + src.getCode();
            }
        }
        if (!done)
            throw new FHIRException(message);
        if (outcome == null)
            return null;
        if ("code".equals(fieldToReturn))
            return new CodeType(outcome.getCode());
        else
            return outcome;
    }
}
Also used : Resource(org.hl7.fhir.dstu2016may.model.Resource) ArrayList(java.util.ArrayList) FHIRException(org.hl7.fhir.exceptions.FHIRException) Base(org.hl7.fhir.dstu2016may.model.Base) UriType(org.hl7.fhir.dstu2016may.model.UriType) Coding(org.hl7.fhir.dstu2016may.model.Coding) TargetElementComponent(org.hl7.fhir.dstu2016may.model.ConceptMap.TargetElementComponent) CodeType(org.hl7.fhir.dstu2016may.model.CodeType) ConceptMap(org.hl7.fhir.dstu2016may.model.ConceptMap) SourceElementComponent(org.hl7.fhir.dstu2016may.model.ConceptMap.SourceElementComponent)

Example 39 with ConceptMap

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

the class FHIRToolingClient method initializeClosure.

public ConceptMap initializeClosure(String name) {
    Parameters params = new Parameters();
    params.addParameter().setName("name").setValue(new StringType(name));
    List<Header> headers = null;
    ResourceRequest<Resource> result = utils.issuePostRequest(resourceAddress.resolveOperationUri(null, "closure", new HashMap<String, String>()), utils.getResourceAsByteArray(params, false, isJson(getPreferredResourceFormat())), getPreferredResourceFormat(), headers, TIMEOUT_NORMAL);
    // gone
    result.addErrorStatus(410);
    // unknown
    result.addErrorStatus(404);
    result.addErrorStatus(405);
    // Unprocessable Entity
    result.addErrorStatus(422);
    result.addSuccessStatus(200);
    result.addSuccessStatus(201);
    if (result.isUnsuccessfulRequest()) {
        throw new EFhirClientException("Server returned error code " + result.getHttpStatus(), (OperationOutcome) result.getPayload());
    }
    return (ConceptMap) result.getPayload();
}
Also used : Parameters(org.hl7.fhir.dstu2016may.model.Parameters) Header(org.apache.http.Header) StringType(org.hl7.fhir.dstu2016may.model.StringType) HashMap(java.util.HashMap) Resource(org.hl7.fhir.dstu2016may.model.Resource) ConceptMap(org.hl7.fhir.dstu2016may.model.ConceptMap)

Example 40 with ConceptMap

use of org.hl7.fhir.r4b.model.ConceptMap 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("#"))
        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.skipComments();
    // 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
            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();
        if (eq != ConceptMapEquivalence.EQUIVALENT)
            tgt.setEquivalence(eq);
        if (tgt.getEquivalence() != ConceptMapEquivalence.UNMATCHED) {
            lexer.token(":");
            tgt.setCode(lexer.take());
            if (tgt.getCode().startsWith("\""))
                tgt.setCode(lexer.processConstant(tgt.getCode()));
        }
        if (lexer.hasComment())
            tgt.setComment(lexer.take().substring(2).trim());
    }
    lexer.token("}");
}
Also used : HashMap(java.util.HashMap) TargetElementComponent(org.hl7.fhir.dstu3.model.ConceptMap.TargetElementComponent) ConceptMapEquivalence(org.hl7.fhir.dstu3.model.Enumerations.ConceptMapEquivalence) ConceptMap(org.hl7.fhir.dstu3.model.ConceptMap) ConceptMapGroupComponent(org.hl7.fhir.dstu3.model.ConceptMap.ConceptMapGroupComponent) SourceElementComponent(org.hl7.fhir.dstu3.model.ConceptMap.SourceElementComponent)

Aggregations

ConceptMap (org.hl7.fhir.dstu3.model.ConceptMap)34 Test (org.junit.Test)31 ArrayList (java.util.ArrayList)29 HashMap (java.util.HashMap)27 FHIRException (org.hl7.fhir.exceptions.FHIRException)26 ConceptMap (org.hl7.fhir.r4.model.ConceptMap)23 XhtmlNode (org.hl7.fhir.utilities.xhtml.XhtmlNode)23 ConceptMap (org.hl7.fhir.r5.model.ConceptMap)22 ConceptMapGroupComponent (org.hl7.fhir.r5.model.ConceptMap.ConceptMapGroupComponent)17 ConceptMapGroupComponent (org.hl7.fhir.dstu3.model.ConceptMap.ConceptMapGroupComponent)15 HashSet (java.util.HashSet)13 ConceptMapGroupComponent (org.hl7.fhir.r4.model.ConceptMap.ConceptMapGroupComponent)13 StructureDefinition (org.hl7.fhir.r5.model.StructureDefinition)12 FileOutputStream (java.io.FileOutputStream)11 ValueSet (org.hl7.fhir.r5.model.ValueSet)11 SourceElementComponent (org.hl7.fhir.dstu3.model.ConceptMap.SourceElementComponent)10 Test (org.junit.jupiter.api.Test)10 ConceptMap (org.hl7.fhir.dstu2016may.model.ConceptMap)9 Coding (org.hl7.fhir.r4.model.Coding)9 SourceElementComponent (org.hl7.fhir.r4.model.ConceptMap.SourceElementComponent)9