Search in sources :

Example 76 with StructureMap

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

the class StructureMapUtilities method parseRule.

private void parseRule(StructureMap map, List<StructureMapGroupRuleComponent> list, FHIRLexer lexer, boolean newFmt) throws FHIRException {
    StructureMapGroupRuleComponent rule = new StructureMapGroupRuleComponent();
    list.add(rule);
    if (!newFmt) {
        rule.setName(lexer.takeDottedToken());
        lexer.token(":");
        lexer.token("for");
    }
    boolean done = false;
    while (!done) {
        parseSource(rule, lexer);
        done = !lexer.hasToken(",");
        if (!done)
            lexer.next();
    }
    if ((newFmt && lexer.hasToken("->")) || (!newFmt && lexer.hasToken("make"))) {
        lexer.token(newFmt ? "->" : "make");
        done = false;
        while (!done) {
            parseTarget(rule, lexer);
            done = !lexer.hasToken(",");
            if (!done)
                lexer.next();
        }
    }
    if (lexer.hasToken("then")) {
        lexer.token("then");
        if (lexer.hasToken("{")) {
            lexer.token("{");
            if (lexer.hasComment()) {
                rule.setDocumentation(lexer.take().substring(2).trim());
            }
            lexer.skipComments();
            while (!lexer.hasToken("}")) {
                if (lexer.done())
                    throw lexer.error("premature termination expecting '}' in nested group");
                parseRule(map, rule.getRule(), lexer, newFmt);
            }
            lexer.token("}");
        } else {
            done = false;
            while (!done) {
                parseRuleReference(rule, lexer);
                done = !lexer.hasToken(",");
                if (!done)
                    lexer.next();
            }
        }
    } else if (lexer.hasComment()) {
        rule.setDocumentation(lexer.take().substring(2).trim());
    }
    if (isSimpleSyntax(rule)) {
        rule.getSourceFirstRep().setVariable(AUTO_VAR_NAME);
        rule.getTargetFirstRep().setVariable(AUTO_VAR_NAME);
        // with no parameter - e.g. imply what is to be created
        rule.getTargetFirstRep().setTransform(StructureMapTransform.CREATE);
    // no dependencies - imply what is to be done based on types
    }
    if (newFmt) {
        if (lexer.isConstant()) {
            if (lexer.isStringConstant()) {
                rule.setName(lexer.readConstant("ruleName"));
            } else {
                rule.setName(lexer.take());
            }
        } else {
            if (rule.getSource().size() != 1 || !rule.getSourceFirstRep().hasElement())
                throw lexer.error("Complex rules must have an explicit name");
            if (rule.getSourceFirstRep().hasType())
                rule.setName(rule.getSourceFirstRep().getElement() + "-" + rule.getSourceFirstRep().getType());
            else
                rule.setName(rule.getSourceFirstRep().getElement());
        }
        lexer.token(";");
    }
    lexer.skipComments();
}
Also used : StructureMapGroupRuleComponent(org.hl7.fhir.r4.model.StructureMap.StructureMapGroupRuleComponent)

Example 77 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());
    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 {
            if (conceptMapUrl.contains("#")) {
                String[] p = conceptMapUrl.split("\\#");
                StructureMap mapU = worker.fetchResource(StructureMap.class, p[0]);
                for (Resource r : mapU.getContained()) {
                    if (r instanceof ConceptMap && ((ConceptMap) r).getId().equals(p[1])) {
                        cmap = (ConceptMap) r;
                        su = conceptMapUrl;
                    }
                }
            }
            if (cmap == null)
                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.r4.model.Resource) ArrayList(java.util.ArrayList) FHIRException(org.hl7.fhir.exceptions.FHIRException) Base(org.hl7.fhir.r4.model.Base) UriType(org.hl7.fhir.r4.model.UriType) StructureMap(org.hl7.fhir.r4.model.StructureMap) Coding(org.hl7.fhir.r4.model.Coding) TargetElementComponent(org.hl7.fhir.r4.model.ConceptMap.TargetElementComponent) CodeType(org.hl7.fhir.r4.model.CodeType) ConceptMap(org.hl7.fhir.r4.model.ConceptMap) ConceptMapGroupComponent(org.hl7.fhir.r4.model.ConceptMap.ConceptMapGroupComponent) SourceElementComponent(org.hl7.fhir.r4.model.ConceptMap.SourceElementComponent)

Example 78 with StructureMap

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

the class StructureMapUtilities method render.

public static String render(StructureMap map) {
    StringBuilder b = new StringBuilder();
    b.append("map \"");
    b.append(map.getUrl());
    b.append("\" = \"");
    b.append(Utilities.escapeJson(map.getName()));
    b.append("\"\r\n\r\n");
    renderConceptMaps(b, map);
    renderUses(b, map);
    renderImports(b, map);
    for (StructureMapGroupComponent g : map.getGroup()) renderGroup(b, g);
    return b.toString();
}
Also used : StructureMapGroupComponent(org.hl7.fhir.r4.model.StructureMap.StructureMapGroupComponent) CommaSeparatedStringBuilder(org.hl7.fhir.utilities.CommaSeparatedStringBuilder)

Example 79 with StructureMap

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

the class StructureMapUtilities method resolveGroupReference.

private ResolvedGroup resolveGroupReference(StructureMap map, StructureMapGroupComponent source, String name) throws FHIRException {
    String kn = "ref^" + name;
    if (source.hasUserData(kn))
        return (ResolvedGroup) source.getUserData(kn);
    ResolvedGroup res = new ResolvedGroup();
    res.targetMap = null;
    res.target = null;
    for (StructureMapGroupComponent grp : map.getGroup()) {
        if (grp.getName().equals(name)) {
            if (res.targetMap == null) {
                res.targetMap = map;
                res.target = grp;
            } else
                throw new FHIRException("Multiple possible matches for rule '" + name + "'");
        }
    }
    if (res.targetMap != null) {
        source.setUserData(kn, res);
        return res;
    }
    for (UriType imp : map.getImport()) {
        List<StructureMap> impMapList = findMatchingMaps(imp.getValue());
        if (impMapList.size() == 0)
            throw new FHIRException("Unable to find map(s) for " + imp.getValue());
        for (StructureMap impMap : impMapList) {
            if (!impMap.getUrl().equals(map.getUrl())) {
                for (StructureMapGroupComponent grp : impMap.getGroup()) {
                    if (grp.getName().equals(name)) {
                        if (res.targetMap == null) {
                            res.targetMap = impMap;
                            res.target = grp;
                        } else
                            throw new FHIRException("Multiple possible matches for rule group '" + name + "' in " + res.targetMap.getUrl() + "#" + res.target.getName() + " and " + impMap.getUrl() + "#" + grp.getName());
                    }
                }
            }
        }
    }
    if (res.target == null)
        throw new FHIRException("No matches found for rule '" + name + "'. Reference found in " + map.getUrl());
    source.setUserData(kn, res);
    return res;
}
Also used : StructureMapGroupComponent(org.hl7.fhir.r4.model.StructureMap.StructureMapGroupComponent) StructureMap(org.hl7.fhir.r4.model.StructureMap) FHIRException(org.hl7.fhir.exceptions.FHIRException) UriType(org.hl7.fhir.r4.model.UriType)

Example 80 with StructureMap

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

the class StructureMapUtilities method processTarget.

private void processTarget(String ruleId, TransformContext context, Variables vars, StructureMap map, StructureMapGroupComponent group, StructureMapGroupRuleTargetComponent tgt, String srcVar, boolean atRoot, Variables sharedVars) throws FHIRException {
    Base dest = null;
    if (tgt.hasContext()) {
        dest = vars.get(VariableMode.OUTPUT, tgt.getContext());
        if (dest == null)
            throw new FHIRException("Rule \"" + ruleId + "\": target context not known: " + tgt.getContext());
        if (!tgt.hasElement())
            throw new FHIRException("Rule \"" + ruleId + "\": Not supported yet");
    }
    Base v = null;
    if (tgt.hasTransform()) {
        v = runTransform(ruleId, context, map, group, tgt, vars, dest, tgt.getElement(), srcVar, atRoot);
        if (v != null && dest != null)
            // reset v because some implementations may have to rewrite v when setting the value
            v = dest.setProperty(tgt.getElement().hashCode(), tgt.getElement(), v);
    } else if (dest != null) {
        if (tgt.hasListMode(StructureMapTargetListMode.SHARE)) {
            v = sharedVars.get(VariableMode.SHARED, tgt.getListRuleId());
            if (v == null) {
                v = dest.makeProperty(tgt.getElement().hashCode(), tgt.getElement());
                sharedVars.add(VariableMode.SHARED, tgt.getListRuleId(), v);
            }
        } else {
            v = dest.makeProperty(tgt.getElement().hashCode(), tgt.getElement());
        }
    }
    if (tgt.hasVariable() && v != null)
        vars.add(VariableMode.OUTPUT, tgt.getVariable(), v);
}
Also used : FHIRException(org.hl7.fhir.exceptions.FHIRException) Base(org.hl7.fhir.r4.model.Base)

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