Search in sources :

Example 86 with StructureMap

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

the class StructureMapUtilities method getTargetType.

public StructureDefinition getTargetType(StructureMap map) throws FHIRException {
    boolean found = false;
    StructureDefinition res = null;
    for (StructureMapStructureComponent uses : map.getStructure()) {
        if (uses.getMode() == StructureMapModelMode.TARGET) {
            if (found)
                throw new FHIRException("Multiple targets found in map " + map.getUrl());
            found = true;
            res = worker.fetchResource(StructureDefinition.class, uses.getUrl());
            if (res == null)
                throw new FHIRException("Unable to find " + uses.getUrl() + " referenced from map " + map.getUrl());
        }
    }
    if (res == null)
        throw new FHIRException("No targets found in map " + map.getUrl());
    return res;
}
Also used : StructureDefinition(org.hl7.fhir.r4.model.StructureDefinition) StructureMapStructureComponent(org.hl7.fhir.r4.model.StructureMap.StructureMapStructureComponent) FHIRException(org.hl7.fhir.exceptions.FHIRException)

Example 87 with StructureMap

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

the class StructureMapUtilities method executeDependency.

private void executeDependency(String indent, TransformContext context, StructureMap map, Variables vin, StructureMapGroupComponent group, StructureMapGroupRuleDependentComponent dependent) throws FHIRException {
    ResolvedGroup rg = resolveGroupReference(map, group, dependent.getName());
    if (rg.target.getInput().size() != dependent.getVariable().size()) {
        throw new FHIRException("Rule '" + dependent.getName() + "' has " + Integer.toString(rg.target.getInput().size()) + " but the invocation has " + Integer.toString(dependent.getVariable().size()) + " variables");
    }
    Variables v = new Variables();
    for (int i = 0; i < rg.target.getInput().size(); i++) {
        StructureMapGroupInputComponent input = rg.target.getInput().get(i);
        StringType rdp = dependent.getVariable().get(i);
        String var = rdp.asStringValue();
        VariableMode mode = input.getMode() == StructureMapInputMode.SOURCE ? VariableMode.INPUT : VariableMode.OUTPUT;
        Base vv = vin.get(mode, var);
        if (// * once source, always source. but target can be treated as source at user convenient
        vv == null && mode == VariableMode.INPUT)
            vv = vin.get(VariableMode.OUTPUT, var);
        if (vv == null)
            throw new FHIRException("Rule '" + dependent.getName() + "' " + mode.toString() + " variable '" + input.getName() + "' named as '" + var + "' has no value (vars = " + vin.summary() + ")");
        v.add(mode, input.getName(), vv);
    }
    executeGroup(indent + "  ", context, rg.targetMap, v, rg.target, false);
}
Also used : StringType(org.hl7.fhir.r4.model.StringType) StructureMapGroupInputComponent(org.hl7.fhir.r4.model.StructureMap.StructureMapGroupInputComponent) FHIRException(org.hl7.fhir.exceptions.FHIRException) ContactPoint(org.hl7.fhir.r4.model.ContactPoint) Base(org.hl7.fhir.r4.model.Base)

Example 88 with StructureMap

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

the class StructureMapUtilities method findMatchingMaps.

private List<StructureMap> findMatchingMaps(String value) {
    List<StructureMap> res = new ArrayList<StructureMap>();
    if (value.contains("*")) {
        for (StructureMap sm : worker.listTransforms()) {
            if (urlMatches(value, sm.getUrl())) {
                res.add(sm);
            }
        }
    } else {
        StructureMap sm = worker.getTransform(value);
        if (sm != null)
            res.add(sm);
    }
    Set<String> check = new HashSet<String>();
    for (StructureMap sm : res) {
        if (check.contains(sm.getUrl()))
            throw new Error("duplicate");
        else
            check.add(sm.getUrl());
    }
    return res;
}
Also used : StructureMap(org.hl7.fhir.r4.model.StructureMap) ArrayList(java.util.ArrayList) FHIRFormatError(org.hl7.fhir.exceptions.FHIRFormatError) HashSet(java.util.HashSet)

Example 89 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)

Example 90 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 : StructureMap(org.hl7.fhir.r5.model.StructureMap) FHIRException(org.hl7.fhir.exceptions.FHIRException)

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