Search in sources :

Example 21 with StructureMapGroupRuleTargetComponent

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

Example 22 with StructureMapGroupRuleTargetComponent

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

the class StructureMapUtilities method targetToString.

public static String targetToString(StructureMapGroupRuleTargetComponent rt) {
    StringBuilder b = new StringBuilder();
    renderTarget(b, rt, false);
    return b.toString();
}
Also used : CommaSeparatedStringBuilder(org.hl7.fhir.utilities.CommaSeparatedStringBuilder)

Example 23 with StructureMapGroupRuleTargetComponent

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

the class StructureMapUtilities method executeRule.

private void executeRule(String indent, TransformContext context, StructureMap map, Variables vars, StructureMapGroupComponent group, StructureMapGroupRuleComponent rule, boolean atRoot) throws FHIRException {
    log(indent + "rule : " + rule.getName() + "; vars = " + vars.summary());
    Variables srcVars = vars.copy();
    if (rule.getSource().size() != 1)
        throw new FHIRException("Rule \"" + rule.getName() + "\": not handled yet");
    List<Variables> source = processSource(rule.getName(), context, srcVars, rule.getSource().get(0), map.getUrl(), indent);
    if (source != null) {
        for (Variables v : source) {
            for (StructureMapGroupRuleTargetComponent t : rule.getTarget()) {
                processTarget(rule.getName(), context, v, map, group, t, rule.getSource().size() == 1 ? rule.getSourceFirstRep().getVariable() : null, atRoot, vars);
            }
            if (rule.hasRule()) {
                for (StructureMapGroupRuleComponent childrule : rule.getRule()) {
                    executeRule(indent + "  ", context, map, v, group, childrule, false);
                }
            } else if (rule.hasDependent()) {
                for (StructureMapGroupRuleDependentComponent dependent : rule.getDependent()) {
                    executeDependency(indent + "  ", context, map, v, group, dependent);
                }
            } else if (rule.getSource().size() == 1 && rule.getSourceFirstRep().hasVariable() && rule.getTarget().size() == 1 && rule.getTargetFirstRep().hasVariable() && rule.getTargetFirstRep().getTransform() == StructureMapTransform.CREATE && !rule.getTargetFirstRep().hasParameter()) {
                // simple inferred, map by type
                System.out.println(v.summary());
                Base src = v.get(VariableMode.INPUT, rule.getSourceFirstRep().getVariable());
                Base tgt = v.get(VariableMode.OUTPUT, rule.getTargetFirstRep().getVariable());
                String srcType = src.fhirType();
                String tgtType = tgt.fhirType();
                ResolvedGroup defGroup = resolveGroupByTypes(map, rule.getName(), group, srcType, tgtType);
                Variables vdef = new Variables();
                vdef.add(VariableMode.INPUT, defGroup.target.getInput().get(0).getName(), src);
                vdef.add(VariableMode.OUTPUT, defGroup.target.getInput().get(1).getName(), tgt);
                executeGroup(indent + "  ", context, defGroup.targetMap, vdef, defGroup.target, false);
            }
        }
    }
}
Also used : StructureMapGroupRuleDependentComponent(org.hl7.fhir.r4.model.StructureMap.StructureMapGroupRuleDependentComponent) FHIRException(org.hl7.fhir.exceptions.FHIRException) StructureMapGroupRuleTargetComponent(org.hl7.fhir.r4.model.StructureMap.StructureMapGroupRuleTargetComponent) StructureMapGroupRuleComponent(org.hl7.fhir.r4.model.StructureMap.StructureMapGroupRuleComponent) Base(org.hl7.fhir.r4.model.Base)

Example 24 with StructureMapGroupRuleTargetComponent

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

the class StructureMapUtilities method renderTarget.

private static void renderTarget(StringBuilder b, StructureMapGroupRuleTargetComponent rt, boolean abbreviate) {
    if (rt.hasContext()) {
        if (rt.getContextType() == StructureMapContextType.TYPE)
            b.append("@");
        b.append(rt.getContext());
        if (rt.hasElement()) {
            b.append('.');
            b.append(rt.getElement());
        }
    }
    if (!abbreviate && rt.hasTransform()) {
        if (rt.hasContext())
            b.append(" = ");
        if (rt.getTransform() == StructureMapTransform.COPY && rt.getParameter().size() == 1) {
            renderTransformParam(b, rt.getParameter().get(0));
        } else if (rt.getTransform() == StructureMapTransform.EVALUATE && rt.getParameter().size() == 1) {
            b.append("(");
            b.append("\"" + ((StringType) rt.getParameter().get(0).getValue()).asStringValue() + "\"");
            b.append(")");
        } else if (rt.getTransform() == StructureMapTransform.EVALUATE && rt.getParameter().size() == 2) {
            b.append(rt.getTransform().toCode());
            b.append("(");
            b.append(((IdType) rt.getParameter().get(0).getValue()).asStringValue());
            b.append("\"" + ((StringType) rt.getParameter().get(1).getValue()).asStringValue() + "\"");
            b.append(")");
        } else {
            b.append(rt.getTransform().toCode());
            b.append("(");
            boolean first = true;
            for (StructureMapGroupRuleTargetParameterComponent rtp : rt.getParameter()) {
                if (first)
                    first = false;
                else
                    b.append(", ");
                renderTransformParam(b, rtp);
            }
            b.append(")");
        }
    }
    if (!abbreviate && rt.hasVariable()) {
        b.append(" as ");
        b.append(rt.getVariable());
    }
    for (Enumeration<StructureMapTargetListMode> lm : rt.getListMode()) {
        b.append(" ");
        b.append(lm.getValue().toCode());
        if (lm.getValue() == StructureMapTargetListMode.SHARE) {
            b.append(" ");
            b.append(rt.getListRuleId());
        }
    }
}
Also used : StructureMapTargetListMode(org.hl7.fhir.r4.model.StructureMap.StructureMapTargetListMode) StringType(org.hl7.fhir.r4.model.StringType) StructureMapGroupRuleTargetParameterComponent(org.hl7.fhir.r4.model.StructureMap.StructureMapGroupRuleTargetParameterComponent)

Example 25 with StructureMapGroupRuleTargetComponent

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

Aggregations

FHIRException (org.hl7.fhir.exceptions.FHIRException)35 FHIRFormatError (org.hl7.fhir.exceptions.FHIRFormatError)12 DefinitionException (org.hl7.fhir.exceptions.DefinitionException)10 CommaSeparatedStringBuilder (org.hl7.fhir.utilities.CommaSeparatedStringBuilder)7 IOException (java.io.IOException)6 StringType (org.hl7.fhir.dstu3.model.StringType)6 StringType (org.hl7.fhir.r4.model.StringType)6 IdType (org.hl7.fhir.dstu3.model.IdType)4 StructureMapGroupRuleTargetComponent (org.hl7.fhir.dstu3.model.StructureMap.StructureMapGroupRuleTargetComponent)4 ProfiledType (org.hl7.fhir.dstu3.model.TypeDetails.ProfiledType)4 PathEngineException (org.hl7.fhir.exceptions.PathEngineException)4 StructureMapGroupRuleTargetComponent (org.hl7.fhir.r4.model.StructureMap.StructureMapGroupRuleTargetComponent)4 NodeType (org.hl7.fhir.utilities.xhtml.NodeType)4 StringType (org.hl7.fhir.dstu2016may.model.StringType)3 StructureMapGroupRuleTargetComponent (org.hl7.fhir.dstu2016may.model.StructureMap.StructureMapGroupRuleTargetComponent)3 Base (org.hl7.fhir.dstu3.model.Base)3 BooleanType (org.hl7.fhir.dstu3.model.BooleanType)3 UriType (org.hl7.fhir.dstu3.model.UriType)3 Base (org.hl7.fhir.r4.model.Base)3 IdType (org.hl7.fhir.r4.model.IdType)3