Search in sources :

Example 41 with SourceElementComponent

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

the class ConceptMap method setProperty.

@Override
public void setProperty(String name, Base value) throws FHIRException {
    if (name.equals("url"))
        // UriType
        this.url = castToUri(value);
    else if (name.equals("identifier"))
        // Identifier
        this.identifier = castToIdentifier(value);
    else if (name.equals("version"))
        // StringType
        this.version = castToString(value);
    else if (name.equals("name"))
        // StringType
        this.name = castToString(value);
    else if (name.equals("status"))
        // Enumeration<ConformanceResourceStatus>
        this.status = new ConformanceResourceStatusEnumFactory().fromType(value);
    else if (name.equals("experimental"))
        // BooleanType
        this.experimental = castToBoolean(value);
    else if (name.equals("publisher"))
        // StringType
        this.publisher = castToString(value);
    else if (name.equals("contact"))
        this.getContact().add((ConceptMapContactComponent) value);
    else if (name.equals("date"))
        // DateTimeType
        this.date = castToDateTime(value);
    else if (name.equals("description"))
        // StringType
        this.description = castToString(value);
    else if (name.equals("useContext"))
        this.getUseContext().add(castToCodeableConcept(value));
    else if (name.equals("requirements"))
        // StringType
        this.requirements = castToString(value);
    else if (name.equals("copyright"))
        // StringType
        this.copyright = castToString(value);
    else if (name.equals("source[x]"))
        // Type
        this.source = (Type) value;
    else if (name.equals("target[x]"))
        // Type
        this.target = (Type) value;
    else if (name.equals("element"))
        this.getElement().add((SourceElementComponent) value);
    else
        super.setProperty(name, value);
}
Also used : ConformanceResourceStatusEnumFactory(org.hl7.fhir.dstu2.model.Enumerations.ConformanceResourceStatusEnumFactory)

Example 42 with SourceElementComponent

use of org.hl7.fhir.r5.model.ConceptMap.SourceElementComponent 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());
    String su = conceptMapUrl;
    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;
                    su = map.getUrl() + conceptMapUrl;
                }
            }
            if (cmap == null)
                throw new FHIRException("Unable to translate - cannot find map " + conceptMapUrl);
        } 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<SourceElementComponentWrapper> list = new ArrayList<SourceElementComponentWrapper>();
            for (ConceptMapGroupComponent g : cmap.getGroup()) {
                for (SourceElementComponent e : g.getElement()) {
                    if (!src.hasSystem() && src.getCode().equals(e.getCode()))
                        list.add(new SourceElementComponentWrapper(g, e));
                    else if (src.hasSystem() && src.getSystem().equals(g.getSource()) && src.getCode().equals(e.getCode()))
                        list.add(new SourceElementComponentWrapper(g, e));
                }
            }
            if (list.size() == 0)
                done = true;
            else if (list.get(0).comp.getTarget().size() == 0)
                message = "Concept map " + su + " found no translation for " + src.getCode();
            else {
                for (TargetElementComponent tgt : list.get(0).comp.getTarget()) {
                    if (tgt.getEquivalence() == null || EnumSet.of(ConceptMapEquivalence.EQUAL, ConceptMapEquivalence.RELATEDTO, ConceptMapEquivalence.EQUIVALENT, ConceptMapEquivalence.WIDER).contains(tgt.getEquivalence())) {
                        if (done) {
                            message = "Concept map " + su + " found multiple matches for " + src.getCode();
                            done = false;
                        } else {
                            done = true;
                            outcome = new Coding().setCode(tgt.getCode()).setSystem(list.get(0).group.getTarget());
                        }
                    } else if (tgt.getEquivalence() == ConceptMapEquivalence.UNMATCHED) {
                        done = true;
                    }
                }
                if (!done)
                    message = "Concept map " + su + " 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.dstu3.model.Resource) ArrayList(java.util.ArrayList) FHIRException(org.hl7.fhir.exceptions.FHIRException) Base(org.hl7.fhir.dstu3.model.Base) UriType(org.hl7.fhir.dstu3.model.UriType) Coding(org.hl7.fhir.dstu3.model.Coding) TargetElementComponent(org.hl7.fhir.dstu3.model.ConceptMap.TargetElementComponent) CodeType(org.hl7.fhir.dstu3.model.CodeType) ConceptMap(org.hl7.fhir.dstu3.model.ConceptMap) ConceptMapGroupComponent(org.hl7.fhir.dstu3.model.ConceptMap.ConceptMapGroupComponent) SourceElementComponent(org.hl7.fhir.dstu3.model.ConceptMap.SourceElementComponent)

Example 43 with SourceElementComponent

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

the class ConceptMapEngineTest method codingTranslateFailsForMultipleCandidateMatches.

@Test
@DisplayName("Coding fails to translate due to multiple candidate matches in ConceptMap")
void codingTranslateFailsForMultipleCandidateMatches() throws IOException {
    final ConceptMap.SourceElementComponent sourceElementComponent = getSourceElementComponent();
    final ConceptMapEngine conceptMapEngine = getConceptMapEngine(Arrays.asList(sourceElementComponent, sourceElementComponent));
    Coding coding = new Coding(null, SOURCE_CODE_STRING, "Body Weight");
    assertThrows(FHIRException.class, () -> {
        conceptMapEngine.translate(coding, CONCEPT_MAP_URL);
    });
}
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 44 with SourceElementComponent

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

the class ConceptMapEngine method translateByJustCode.

private Coding translateByJustCode(ConceptMap cm, String code) throws FHIRException {
    SourceElementComponent ct = null;
    ConceptMapGroupComponent cg = null;
    for (ConceptMapGroupComponent g : cm.getGroup()) {
        for (SourceElementComponent e : g.getElement()) {
            if (code.equals(e.getCode())) {
                if (e != null)
                    throw new FHIRException("Unable to process translate " + code + " because multiple candidate matches were found in concept map " + cm.getUrl());
                ct = e;
                cg = g;
            }
        }
    }
    if (ct == null)
        return null;
    TargetElementComponent tt = null;
    for (TargetElementComponent t : ct.getTarget()) {
        if (!t.hasDependsOn() && !t.hasProduct() && isOkEquivalence(t.getEquivalence())) {
            if (tt != null)
                throw new FHIRException("Unable to process translate " + code + " because multiple targets were found in concept map " + cm.getUrl());
            tt = t;
        }
    }
    if (tt == null)
        return null;
    CanonicalPair cp = new CanonicalPair(cg.getTarget());
    return new Coding().setSystem(cp.getUrl()).setVersion(cp.getVersion()).setCode(tt.getCode()).setDisplay(tt.getDisplay());
}
Also used : Coding(org.hl7.fhir.r4b.model.Coding) TargetElementComponent(org.hl7.fhir.r4b.model.ConceptMap.TargetElementComponent) FHIRException(org.hl7.fhir.exceptions.FHIRException) SourceElementComponent(org.hl7.fhir.r4b.model.ConceptMap.SourceElementComponent) ConceptMapGroupComponent(org.hl7.fhir.r4b.model.ConceptMap.ConceptMapGroupComponent) CanonicalPair(org.hl7.fhir.utilities.CanonicalPair)

Example 45 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.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
            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()));
        }
        if (lexer.hasComment())
            tgt.setComment(lexer.take().substring(2).trim());
    }
    lexer.token("}");
}
Also used : HashMap(java.util.HashMap) TargetElementComponent(org.hl7.fhir.r4.model.ConceptMap.TargetElementComponent) ConceptMapEquivalence(org.hl7.fhir.r4.model.Enumerations.ConceptMapEquivalence) ConceptMap(org.hl7.fhir.r4.model.ConceptMap) ConceptMapGroupComponent(org.hl7.fhir.r4.model.ConceptMap.ConceptMapGroupComponent) SourceElementComponent(org.hl7.fhir.r4.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