Search in sources :

Example 16 with StructureMap

use of org.hl7.fhir.r4.model.StructureMap in project org.hl7.fhir.core by hapifhir.

the class StructureMapUtilities method parseGroup.

private void parseGroup(StructureMap result, FHIRLexer lexer) throws FHIRException {
    lexer.token("group");
    StructureMapGroupComponent group = result.addGroup();
    group.setName(lexer.take());
    if (lexer.hasToken("extends")) {
        lexer.next();
        group.setExtends(lexer.take());
    }
    lexer.skipComments();
    while (lexer.hasToken("input")) parseInput(group, lexer);
    while (!lexer.hasToken("endgroup")) {
        if (lexer.done())
            throw lexer.error("premature termination expecting 'endgroup'");
        parseRule(group.getRule(), lexer);
    }
    lexer.next();
    lexer.skipComments();
}
Also used : StructureMapGroupComponent(org.hl7.fhir.dstu2016may.model.StructureMap.StructureMapGroupComponent)

Example 17 with StructureMap

use of org.hl7.fhir.r4.model.StructureMap 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 18 with StructureMap

use of org.hl7.fhir.r4.model.StructureMap 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 19 with StructureMap

use of org.hl7.fhir.r4.model.StructureMap in project org.hl7.fhir.core by hapifhir.

the class StructureMapUtilities method renderImports.

private void renderImports(StringBuilder b, StructureMap map) {
    for (UriType s : map.getImport()) {
        b.append("imports \"");
        b.append(s.getValue());
        b.append("\"\r\n");
    }
    if (map.hasImport())
        b.append("\r\n");
}
Also used : UriType(org.hl7.fhir.dstu2016may.model.UriType)

Example 20 with StructureMap

use of org.hl7.fhir.r4.model.StructureMap in project org.hl7.fhir.core by hapifhir.

the class StructureMapUtilities method parseUses.

private void parseUses(StructureMap result, FHIRLexer lexer) throws FHIRException {
    lexer.token("uses");
    StructureMapStructureComponent st = result.addStructure();
    st.setUrl(lexer.readConstant("url"));
    lexer.token("as");
    st.setMode(StructureMapModelMode.fromCode(lexer.take()));
    lexer.skipToken(";");
    if (lexer.hasComment()) {
        st.setDocumentation(lexer.take().substring(2).trim());
    }
    lexer.skipComments();
}
Also used : StructureMapStructureComponent(org.hl7.fhir.dstu2016may.model.StructureMap.StructureMapStructureComponent)

Aggregations

FHIRException (org.hl7.fhir.exceptions.FHIRException)69 DefinitionException (org.hl7.fhir.exceptions.DefinitionException)23 FHIRFormatError (org.hl7.fhir.exceptions.FHIRFormatError)17 XhtmlNode (org.hl7.fhir.utilities.xhtml.XhtmlNode)16 StructureMap (org.hl7.fhir.r4b.model.StructureMap)13 StructureMap (org.hl7.fhir.r5.model.StructureMap)13 IOException (java.io.IOException)11 ArrayList (java.util.ArrayList)11 StructureMap (org.hl7.fhir.r4.model.StructureMap)11 CommaSeparatedStringBuilder (org.hl7.fhir.utilities.CommaSeparatedStringBuilder)11 Complex (org.hl7.fhir.dstu2016may.formats.RdfGenerator.Complex)10 Complex (org.hl7.fhir.dstu3.utils.formats.Turtle.Complex)9 Complex (org.hl7.fhir.r4.utils.formats.Turtle.Complex)9 StructureMap (org.hl7.fhir.dstu3.model.StructureMap)8 File (java.io.File)7 UriType (org.hl7.fhir.r4.model.UriType)7 Test (org.junit.jupiter.api.Test)7 Base (org.hl7.fhir.dstu3.model.Base)6 TextFile (org.hl7.fhir.utilities.TextFile)6 FileOutputStream (java.io.FileOutputStream)5