Search in sources :

Example 1 with BooleanFieldValue

use of org.activityinfo.model.type.primitive.BooleanFieldValue in project activityinfo by bedatadriven.

the class XPathBuilder method appendTo.

private void appendTo(FormulaNode formulaNode, StringBuilder xpath) {
    if (formulaNode instanceof ConstantNode) {
        ConstantNode constantNode = (ConstantNode) formulaNode;
        FieldValue value = constantNode.getValue();
        if (value instanceof BooleanFieldValue) {
            BooleanFieldValue booleanFieldValue = (BooleanFieldValue) value;
            xpath.append(booleanFieldValue.asBoolean() ? TRUE : FALSE);
        } else if (value instanceof EnumValue) {
            String xpathExpr = resolveSymbol(formulaNode);
            xpath.append(xpathExpr);
        } else {
            xpath.append(constantNode.asExpression());
        }
    } else if (formulaNode instanceof FunctionCallNode) {
        FunctionCallNode functionCallNode = (FunctionCallNode) formulaNode;
        List<FormulaNode> arguments = functionCallNode.getArguments();
        FormulaFunction function = functionCallNode.getFunction();
        switch(function.getId()) {
            case "&&":
                appendBinaryInfixTo("and", arguments, xpath);
                break;
            case "==":
                appendBinaryInfixTo("=", arguments, xpath);
                break;
            case "||":
                appendBinaryInfixTo("or", arguments, xpath);
                break;
            case "containsAny":
            case "containsAll":
            case "notContainsAny":
            case "notContainsAll":
                appendFunction(function.getId(), arguments, xpath);
                break;
            case "!=":
            case ">":
            case ">=":
            case "<":
            case "<=":
                appendBinaryInfixTo(function.getId(), arguments, xpath);
                break;
            case "!":
                appendUnaryFunction("not", arguments.get(0), xpath);
                break;
            default:
                throw new XPathBuilderException("Unsupported function " + function.getId());
        }
    } else if (formulaNode instanceof GroupNode) {
        GroupNode groupNode = (GroupNode) formulaNode;
        FormulaNode expr = groupNode.getExpr();
        xpath.append("(");
        appendTo(expr, xpath);
        xpath.append(")");
    } else if (formulaNode instanceof SymbolNode) {
        SymbolNode symbolNode = (SymbolNode) formulaNode;
        String xpathExpr = resolveSymbol(symbolNode);
        xpath.append(xpathExpr);
    } else if (formulaNode instanceof CompoundExpr) {
        CompoundExpr compoundExpr = (CompoundExpr) formulaNode;
        List<FormulaNode> arguments = new ArrayList<>();
        arguments.add(compoundExpr.getValue());
        arguments.add(compoundExpr.getField());
        appendBinaryInfixTo("=", arguments, xpath);
    } else {
        throw new XPathBuilderException("Unknown expr node " + formulaNode);
    }
}
Also used : FormulaFunction(org.activityinfo.model.formula.functions.FormulaFunction) EnumValue(org.activityinfo.model.type.enumerated.EnumValue) ArrayList(java.util.ArrayList) BooleanFieldValue(org.activityinfo.model.type.primitive.BooleanFieldValue) ArrayList(java.util.ArrayList) List(java.util.List) FieldValue(org.activityinfo.model.type.FieldValue) BooleanFieldValue(org.activityinfo.model.type.primitive.BooleanFieldValue)

Example 2 with BooleanFieldValue

use of org.activityinfo.model.type.primitive.BooleanFieldValue in project activityinfo by bedatadriven.

the class IfFunction method apply.

@Override
public FieldValue apply(List<FieldValue> arguments) {
    checkArity(arguments, 3);
    BooleanFieldValue condition = toBoolean(arguments.get(0));
    FieldValue ifTrue = arguments.get(1);
    FieldValue ifFalse = arguments.get(2);
    if (condition == BooleanFieldValue.TRUE) {
        return ifTrue;
    } else {
        return ifFalse;
    }
}
Also used : FieldValue(org.activityinfo.model.type.FieldValue) BooleanFieldValue(org.activityinfo.model.type.primitive.BooleanFieldValue) BooleanFieldValue(org.activityinfo.model.type.primitive.BooleanFieldValue)

Aggregations

FieldValue (org.activityinfo.model.type.FieldValue)2 BooleanFieldValue (org.activityinfo.model.type.primitive.BooleanFieldValue)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 FormulaFunction (org.activityinfo.model.formula.functions.FormulaFunction)1 EnumValue (org.activityinfo.model.type.enumerated.EnumValue)1