Search in sources :

Example 16 with StructureMapGroupRuleSourceComponent

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

the class StructureMapUtilities method renderRule.

private static void renderRule(StringBuilder b, StructureMapGroupRuleComponent r, int indent) {
    for (int i = 0; i < indent; i++) b.append(' ');
    boolean canBeAbbreviated = checkisSimple(r);
    boolean first = true;
    for (StructureMapGroupRuleSourceComponent rs : r.getSource()) {
        if (first)
            first = false;
        else
            b.append(", ");
        renderSource(b, rs, canBeAbbreviated);
    }
    if (r.getTarget().size() > 1) {
        b.append(" -> ");
        first = true;
        for (StructureMapGroupRuleTargetComponent rt : r.getTarget()) {
            if (first)
                first = false;
            else
                b.append(", ");
            if (RENDER_MULTIPLE_TARGETS_ONELINE)
                b.append(' ');
            else {
                b.append("\r\n");
                for (int i = 0; i < indent + 4; i++) b.append(' ');
            }
            renderTarget(b, rt, false);
        }
    } else if (r.hasTarget()) {
        b.append(" -> ");
        renderTarget(b, r.getTarget().get(0), canBeAbbreviated);
    }
    if (r.hasRule()) {
        b.append(" then {\r\n");
        renderDoco(b, r.getDocumentation());
        for (StructureMapGroupRuleComponent ir : r.getRule()) {
            renderRule(b, ir, indent + 2);
        }
        for (int i = 0; i < indent; i++) b.append(' ');
        b.append("}");
    } else {
        if (r.hasDependent()) {
            b.append(" then ");
            first = true;
            for (StructureMapGroupRuleDependentComponent rd : r.getDependent()) {
                if (first)
                    first = false;
                else
                    b.append(", ");
                b.append(rd.getName());
                b.append("(");
                boolean ifirst = true;
                for (StringType rdp : rd.getVariable()) {
                    if (ifirst)
                        ifirst = false;
                    else
                        b.append(", ");
                    b.append(rdp.asStringValue());
                }
                b.append(")");
            }
        }
    }
    if (r.hasName()) {
        String n = ntail(r.getName());
        if (!n.startsWith("\""))
            n = "\"" + n + "\"";
        if (!matchesName(n, r.getSource())) {
            b.append(" ");
            b.append(n);
        }
    }
    b.append(";");
    renderDoco(b, r.getDocumentation());
    b.append("\r\n");
}
Also used : StringType(org.hl7.fhir.r4.model.StringType) StructureMapGroupRuleDependentComponent(org.hl7.fhir.r4.model.StructureMap.StructureMapGroupRuleDependentComponent) ContactPoint(org.hl7.fhir.r4.model.ContactPoint) StructureMapGroupRuleSourceComponent(org.hl7.fhir.r4.model.StructureMap.StructureMapGroupRuleSourceComponent) StructureMapGroupRuleTargetComponent(org.hl7.fhir.r4.model.StructureMap.StructureMapGroupRuleTargetComponent) StructureMapGroupRuleComponent(org.hl7.fhir.r4.model.StructureMap.StructureMapGroupRuleComponent)

Example 17 with StructureMapGroupRuleSourceComponent

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

the class StructureMapUtilities method processSource.

private List<Variables> processSource(String ruleId, TransformContext context, Variables vars, StructureMapGroupRuleSourceComponent src, String pathForErrors, String indent) throws FHIRException {
    List<Base> items;
    if (src.getContext().equals("@search")) {
        ExpressionNode expr = (ExpressionNode) src.getUserData(MAP_SEARCH_EXPRESSION);
        if (expr == null) {
            expr = fpe.parse(src.getElement());
            src.setUserData(MAP_SEARCH_EXPRESSION, expr);
        }
        // string is a holder of nothing to ensure that variables are processed correctly
        String search = fpe.evaluateToString(vars, null, null, new StringType(), expr);
        items = services.performSearch(context.appInfo, search);
    } else {
        items = new ArrayList<Base>();
        Base b = vars.get(VariableMode.INPUT, src.getContext());
        if (b == null)
            throw new FHIRException("Unknown input variable " + src.getContext() + " in " + pathForErrors + " rule " + ruleId + " (vars = " + vars.summary() + ")");
        if (!src.hasElement())
            items.add(b);
        else {
            getChildrenByName(b, src.getElement(), items);
            if (items.size() == 0 && src.hasDefaultValue())
                items.add(src.getDefaultValue());
        }
    }
    if (src.hasType()) {
        List<Base> remove = new ArrayList<Base>();
        for (Base item : items) {
            if (item != null && !isType(item, src.getType())) {
                remove.add(item);
            }
        }
        items.removeAll(remove);
    }
    if (src.hasCondition()) {
        ExpressionNode expr = (ExpressionNode) src.getUserData(MAP_WHERE_EXPRESSION);
        if (expr == null) {
            expr = fpe.parse(src.getCondition());
            // fpe.check(context.appInfo, ??, ??, expr)
            src.setUserData(MAP_WHERE_EXPRESSION, expr);
        }
        List<Base> remove = new ArrayList<Base>();
        for (Base item : items) {
            if (!fpe.evaluateToBoolean(vars, null, null, item, expr)) {
                log(indent + "  condition [" + src.getCondition() + "] for " + item.toString() + " : false");
                remove.add(item);
            } else
                log(indent + "  condition [" + src.getCondition() + "] for " + item.toString() + " : true");
        }
        items.removeAll(remove);
    }
    if (src.hasCheck()) {
        ExpressionNode expr = (ExpressionNode) src.getUserData(MAP_WHERE_CHECK);
        if (expr == null) {
            expr = fpe.parse(src.getCheck());
            // fpe.check(context.appInfo, ??, ??, expr)
            src.setUserData(MAP_WHERE_CHECK, expr);
        }
        List<Base> remove = new ArrayList<Base>();
        for (Base item : items) {
            if (!fpe.evaluateToBoolean(vars, null, null, item, expr))
                throw new FHIRException("Rule \"" + ruleId + "\": Check condition failed");
        }
    }
    if (src.hasLogMessage()) {
        ExpressionNode expr = (ExpressionNode) src.getUserData(MAP_WHERE_LOG);
        if (expr == null) {
            expr = fpe.parse(src.getLogMessage());
            // fpe.check(context.appInfo, ??, ??, expr)
            src.setUserData(MAP_WHERE_LOG, expr);
        }
        CommaSeparatedStringBuilder b = new CommaSeparatedStringBuilder();
        for (Base item : items) b.appendIfNotNull(fpe.evaluateToString(vars, null, null, item, expr));
        if (b.length() > 0)
            services.log(b.toString());
    }
    if (src.hasListMode() && !items.isEmpty()) {
        switch(src.getListMode()) {
            case FIRST:
                Base bt = items.get(0);
                items.clear();
                items.add(bt);
                break;
            case NOTFIRST:
                if (items.size() > 0)
                    items.remove(0);
                break;
            case LAST:
                bt = items.get(items.size() - 1);
                items.clear();
                items.add(bt);
                break;
            case NOTLAST:
                if (items.size() > 0)
                    items.remove(items.size() - 1);
                break;
            case ONLYONE:
                if (items.size() > 1)
                    throw new FHIRException("Rule \"" + ruleId + "\": Check condition failed: the collection has more than one item");
                break;
            case NULL:
        }
    }
    List<Variables> result = new ArrayList<Variables>();
    for (Base r : items) {
        Variables v = vars.copy();
        if (src.hasVariable())
            v.add(VariableMode.INPUT, src.getVariable(), r);
        result.add(v);
    }
    return result;
}
Also used : StringType(org.hl7.fhir.r4.model.StringType) ExpressionNode(org.hl7.fhir.r4.model.ExpressionNode) ArrayList(java.util.ArrayList) CommaSeparatedStringBuilder(org.hl7.fhir.utilities.CommaSeparatedStringBuilder) FHIRException(org.hl7.fhir.exceptions.FHIRException) Base(org.hl7.fhir.r4.model.Base)

Example 18 with StructureMapGroupRuleSourceComponent

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

the class StructureMapUtilities method processSource.

private List<Variables> processSource(String ruleId, TransformContext context, Variables vars, StructureMapGroupRuleSourceComponent src, String pathForErrors, String indent) throws FHIRException {
    List<Base> items;
    if (src.getContext().equals("@search")) {
        ExpressionNode expr = (ExpressionNode) src.getUserData(MAP_SEARCH_EXPRESSION);
        if (expr == null) {
            expr = fpe.parse(src.getElement());
            src.setUserData(MAP_SEARCH_EXPRESSION, expr);
        }
        // string is a holder of nothing to ensure that variables are processed correctly
        String search = fpe.evaluateToString(vars, null, null, new StringType(), expr);
        items = services.performSearch(context.getAppInfo(), search);
    } else {
        items = new ArrayList<Base>();
        Base b = vars.get(VariableMode.INPUT, src.getContext());
        if (b == null)
            throw new FHIRException("Unknown input variable " + src.getContext() + " in " + pathForErrors + " rule " + ruleId + " (vars = " + vars.summary() + ")");
        if (!src.hasElement())
            items.add(b);
        else {
            getChildrenByName(b, src.getElement(), items);
            if (items.size() == 0 && src.hasDefaultValue())
                items.add(src.getDefaultValue());
        }
    }
    if (src.hasType()) {
        List<Base> remove = new ArrayList<Base>();
        for (Base item : items) {
            if (item != null && !isType(item, src.getType())) {
                remove.add(item);
            }
        }
        items.removeAll(remove);
    }
    if (src.hasCondition()) {
        ExpressionNode expr = (ExpressionNode) src.getUserData(MAP_WHERE_EXPRESSION);
        if (expr == null) {
            expr = fpe.parse(src.getCondition());
            // fpe.check(context.appInfo, ??, ??, expr)
            src.setUserData(MAP_WHERE_EXPRESSION, expr);
        }
        List<Base> remove = new ArrayList<Base>();
        for (Base item : items) {
            if (!fpe.evaluateToBoolean(vars, null, null, item, expr)) {
                log(indent + "  condition [" + src.getCondition() + "] for " + item.toString() + " : false");
                remove.add(item);
            } else
                log(indent + "  condition [" + src.getCondition() + "] for " + item.toString() + " : true");
        }
        items.removeAll(remove);
    }
    if (src.hasCheck()) {
        ExpressionNode expr = (ExpressionNode) src.getUserData(MAP_WHERE_CHECK);
        if (expr == null) {
            expr = fpe.parse(src.getCheck());
            // fpe.check(context.appInfo, ??, ??, expr)
            src.setUserData(MAP_WHERE_CHECK, expr);
        }
        List<Base> remove = new ArrayList<Base>();
        for (Base item : items) {
            if (!fpe.evaluateToBoolean(vars, null, null, item, expr))
                throw new FHIRException("Rule \"" + ruleId + "\": Check condition failed");
        }
    }
    if (src.hasLogMessage()) {
        ExpressionNode expr = (ExpressionNode) src.getUserData(MAP_WHERE_LOG);
        if (expr == null) {
            expr = fpe.parse(src.getLogMessage());
            // fpe.check(context.appInfo, ??, ??, expr)
            src.setUserData(MAP_WHERE_LOG, expr);
        }
        CommaSeparatedStringBuilder b = new CommaSeparatedStringBuilder();
        for (Base item : items) b.appendIfNotNull(fpe.evaluateToString(vars, null, null, item, expr));
        if (b.length() > 0)
            services.log(b.toString());
    }
    if (src.hasListMode() && !items.isEmpty()) {
        switch(src.getListMode()) {
            case FIRST:
                Base bt = items.get(0);
                items.clear();
                items.add(bt);
                break;
            case NOTFIRST:
                if (items.size() > 0)
                    items.remove(0);
                break;
            case LAST:
                bt = items.get(items.size() - 1);
                items.clear();
                items.add(bt);
                break;
            case NOTLAST:
                if (items.size() > 0)
                    items.remove(items.size() - 1);
                break;
            case ONLYONE:
                if (items.size() > 1)
                    throw new FHIRException("Rule \"" + ruleId + "\": Check condition failed: the collection has more than one item");
                break;
            case NULL:
        }
    }
    List<Variables> result = new ArrayList<Variables>();
    for (Base r : items) {
        Variables v = vars.copy();
        if (src.hasVariable())
            v.add(VariableMode.INPUT, src.getVariable(), r);
        result.add(v);
    }
    return result;
}
Also used : CommaSeparatedStringBuilder(org.hl7.fhir.utilities.CommaSeparatedStringBuilder) FHIRException(org.hl7.fhir.exceptions.FHIRException)

Example 19 with StructureMapGroupRuleSourceComponent

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

the class StructureMapUtilities method sourceToString.

public static String sourceToString(StructureMapGroupRuleSourceComponent r) {
    StringBuilder b = new StringBuilder();
    renderSource(b, r, false);
    return b.toString();
}
Also used : CommaSeparatedStringBuilder(org.hl7.fhir.utilities.CommaSeparatedStringBuilder)

Example 20 with StructureMapGroupRuleSourceComponent

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

the class StructureMapUtilities method renderRule.

private void renderRule(StringBuilder b, StructureMapGroupRuleComponent r, int indent) throws FHIRException {
    for (int i = 0; i < indent; i++) b.append(' ');
    b.append(r.getName());
    b.append(": for ");
    boolean first = true;
    for (StructureMapGroupRuleSourceComponent rs : r.getSource()) {
        if (first)
            first = false;
        else
            b.append(", ");
        renderSource(b, rs);
    }
    if (r.getTarget().size() > 1) {
        b.append(" make ");
        first = true;
        for (StructureMapGroupRuleTargetComponent rt : r.getTarget()) {
            if (first)
                first = false;
            else
                b.append(", ");
            b.append("\r\n");
            for (int i = 0; i < indent + 4; i++) b.append(' ');
            renderTarget(b, rt);
        }
    } else if (r.hasTarget()) {
        b.append(" make ");
        renderTarget(b, r.getTarget().get(0));
    }
    if (r.hasRule()) {
        b.append(" then {");
        renderDoco(b, r.getDocumentation());
        for (int i = 0; i < indent; i++) b.append(' ');
        b.append("}\r\n");
    } else {
        if (r.hasDependent()) {
            first = true;
            for (StructureMapGroupRuleDependentComponent rd : r.getDependent()) {
                if (first)
                    first = false;
                else
                    b.append(", ");
                b.append(rd.getName());
                b.append("(");
                boolean ifirst = true;
                for (StringType rdp : rd.getVariable()) {
                    if (ifirst)
                        ifirst = false;
                    else
                        b.append(", ");
                    b.append(rd.getVariable());
                }
            }
        }
        renderDoco(b, r.getDocumentation());
        b.append("\r\n");
    }
}
Also used : StringType(org.hl7.fhir.dstu2016may.model.StringType) StructureMapGroupRuleDependentComponent(org.hl7.fhir.dstu2016may.model.StructureMap.StructureMapGroupRuleDependentComponent) StructureMapGroupRuleSourceComponent(org.hl7.fhir.dstu2016may.model.StructureMap.StructureMapGroupRuleSourceComponent) StructureMapGroupRuleTargetComponent(org.hl7.fhir.dstu2016may.model.StructureMap.StructureMapGroupRuleTargetComponent)

Aggregations

FHIRException (org.hl7.fhir.exceptions.FHIRException)9 CommaSeparatedStringBuilder (org.hl7.fhir.utilities.CommaSeparatedStringBuilder)7 FHIRFormatError (org.hl7.fhir.exceptions.FHIRFormatError)4 ArrayList (java.util.ArrayList)3 StringType (org.hl7.fhir.dstu3.model.StringType)3 StringType (org.hl7.fhir.r4.model.StringType)3 ExpressionNode (org.hl7.fhir.dstu2016may.model.ExpressionNode)2 StructureMapGroupRuleSourceComponent (org.hl7.fhir.dstu2016may.model.StructureMap.StructureMapGroupRuleSourceComponent)2 ExpressionNode (org.hl7.fhir.dstu3.model.ExpressionNode)2 StructureMapGroupRuleSourceComponent (org.hl7.fhir.dstu3.model.StructureMap.StructureMapGroupRuleSourceComponent)2 ExpressionNode (org.hl7.fhir.r4.model.ExpressionNode)2 StructureMapGroupRuleSourceComponent (org.hl7.fhir.r4.model.StructureMap.StructureMapGroupRuleSourceComponent)2 IOException (java.io.IOException)1 Base (org.hl7.fhir.dstu2016may.model.Base)1 StringType (org.hl7.fhir.dstu2016may.model.StringType)1 StructureMapGroupRuleDependentComponent (org.hl7.fhir.dstu2016may.model.StructureMap.StructureMapGroupRuleDependentComponent)1 StructureMapGroupRuleTargetComponent (org.hl7.fhir.dstu2016may.model.StructureMap.StructureMapGroupRuleTargetComponent)1 FHIRLexerException (org.hl7.fhir.dstu2016may.utils.FHIRLexer.FHIRLexerException)1 Property (org.hl7.fhir.dstu3.elementmodel.Property)1 Base (org.hl7.fhir.dstu3.model.Base)1