Search in sources :

Example 1 with ReduceEvaluator

use of org.apache.nifi.attribute.expression.language.evaluation.reduce.ReduceEvaluator in project nifi by apache.

the class ExpressionCompiler method verifyMappingEvaluatorReduced.

private void verifyMappingEvaluatorReduced(final Evaluator<?> evaluator) {
    final Evaluator<?> rightMostEvaluator;
    if (evaluator instanceof IteratingEvaluator) {
        rightMostEvaluator = ((IteratingEvaluator<?>) evaluator).getLogicEvaluator();
    } else {
        rightMostEvaluator = evaluator;
    }
    Evaluator<?> eval = rightMostEvaluator.getSubjectEvaluator();
    Evaluator<?> lastEval = rightMostEvaluator;
    while (eval != null) {
        if (eval instanceof ReduceEvaluator) {
            throw new AttributeExpressionLanguageParsingException("Expression attempts to call function '" + lastEval.getToken() + "' on the result of '" + eval.getToken() + "'. This is not allowed. Instead, use \"${literal( ${<embedded expression>} ):" + lastEval.getToken() + "(...)}\"");
        }
        lastEval = eval;
        eval = eval.getSubjectEvaluator();
    }
    // if the result type of the evaluator is BOOLEAN, then it will always
    // be reduced when evaluator.
    final ResultType resultType = evaluator.getResultType();
    if (resultType == ResultType.BOOLEAN) {
        return;
    }
    final Evaluator<?> rootEvaluator = getRootSubjectEvaluator(evaluator);
    if (rootEvaluator != null && rootEvaluator instanceof MultiAttributeEvaluator) {
        final MultiAttributeEvaluator multiAttrEval = (MultiAttributeEvaluator) rootEvaluator;
        switch(multiAttrEval.getEvaluationType()) {
            case ALL_ATTRIBUTES:
            case ALL_MATCHING_ATTRIBUTES:
            case ALL_DELINEATED_VALUES:
                {
                    if (!(evaluator instanceof ReduceEvaluator)) {
                        throw new AttributeExpressionLanguageParsingException("Cannot evaluate expression because it attempts to reference multiple attributes but does not use a reducing function");
                    }
                    break;
                }
            default:
                throw new AttributeExpressionLanguageParsingException("Cannot evaluate expression because it attempts to reference multiple attributes but does not use a reducing function");
        }
    }
}
Also used : IteratingEvaluator(org.apache.nifi.attribute.expression.language.evaluation.selection.IteratingEvaluator) ResultType(org.apache.nifi.expression.AttributeExpression.ResultType) MultiAttributeEvaluator(org.apache.nifi.attribute.expression.language.evaluation.selection.MultiAttributeEvaluator) ReduceEvaluator(org.apache.nifi.attribute.expression.language.evaluation.reduce.ReduceEvaluator) AttributeExpressionLanguageParsingException(org.apache.nifi.attribute.expression.language.exception.AttributeExpressionLanguageParsingException)

Example 2 with ReduceEvaluator

use of org.apache.nifi.attribute.expression.language.evaluation.reduce.ReduceEvaluator in project nifi by apache.

the class ExpressionCompiler method buildExpressionEvaluator.

@SuppressWarnings({ "rawtypes", "unchecked" })
private Evaluator<?> buildExpressionEvaluator(final Tree tree) {
    if (tree.getChildCount() == 0) {
        throw new AttributeExpressionLanguageParsingException("EXPRESSION tree node has no children");
    }
    final Evaluator<?> evaluator;
    if (tree.getChildCount() == 1) {
        evaluator = buildEvaluator(tree.getChild(0));
    } else {
        // we can chain together functions in the form of:
        // ${x:trim():substring(1,2):trim()}
        // in this case, the subject of the right-most function is the function to its left; its
        // subject is the function to its left (the first trim()), and its subject is the value of
        // the 'x' attribute. We accomplish this logic by iterating over all of the children of the
        // tree from the right-most child going left-ward.
        evaluator = buildFunctionExpressionEvaluator(tree, 0);
    }
    Evaluator<?> chosenEvaluator = evaluator;
    final Evaluator<?> rootEvaluator = getRootSubjectEvaluator(evaluator);
    if (rootEvaluator != null) {
        if (rootEvaluator instanceof MultiAttributeEvaluator) {
            final MultiAttributeEvaluator multiAttrEval = (MultiAttributeEvaluator) rootEvaluator;
            switch(multiAttrEval.getEvaluationType()) {
                case ANY_ATTRIBUTE:
                case ANY_MATCHING_ATTRIBUTE:
                case ANY_DELINEATED_VALUE:
                    chosenEvaluator = new AnyAttributeEvaluator((BooleanEvaluator) evaluator, multiAttrEval);
                    break;
                case ALL_ATTRIBUTES:
                case ALL_MATCHING_ATTRIBUTES:
                case ALL_DELINEATED_VALUES:
                    {
                        final ResultType resultType = evaluator.getResultType();
                        if (resultType == ResultType.BOOLEAN) {
                            chosenEvaluator = new AllAttributesEvaluator((BooleanEvaluator) evaluator, multiAttrEval);
                        } else if (evaluator instanceof ReduceEvaluator) {
                            chosenEvaluator = new MappingEvaluator((ReduceEvaluator) evaluator, multiAttrEval);
                        } else {
                            throw new AttributeExpressionLanguageException("Cannot evaluate Expression because it attempts to reference multiple attributes but does not use a reducing function");
                        }
                        break;
                    }
            }
            evaluators.add(chosenEvaluator);
            switch(multiAttrEval.getEvaluationType()) {
                case ANY_ATTRIBUTE:
                    chosenEvaluator.setToken("anyAttribute");
                    break;
                case ANY_MATCHING_ATTRIBUTE:
                    chosenEvaluator.setToken("anyMatchingAttribute");
                    break;
                case ANY_DELINEATED_VALUE:
                    chosenEvaluator.setToken("anyDelineatedValue");
                    break;
                case ALL_ATTRIBUTES:
                    chosenEvaluator.setToken("allAttributes");
                    break;
                case ALL_MATCHING_ATTRIBUTES:
                    chosenEvaluator.setToken("allMatchingAttributes");
                    break;
                case ALL_DELINEATED_VALUES:
                    chosenEvaluator.setToken("allDelineatedValues");
                    break;
            }
        }
    }
    return chosenEvaluator;
}
Also used : AttributeExpressionLanguageException(org.apache.nifi.attribute.expression.language.exception.AttributeExpressionLanguageException) AnyAttributeEvaluator(org.apache.nifi.attribute.expression.language.evaluation.selection.AnyAttributeEvaluator) BooleanEvaluator(org.apache.nifi.attribute.expression.language.evaluation.BooleanEvaluator) AllAttributesEvaluator(org.apache.nifi.attribute.expression.language.evaluation.selection.AllAttributesEvaluator) MultiAttributeEvaluator(org.apache.nifi.attribute.expression.language.evaluation.selection.MultiAttributeEvaluator) ResultType(org.apache.nifi.expression.AttributeExpression.ResultType) ReduceEvaluator(org.apache.nifi.attribute.expression.language.evaluation.reduce.ReduceEvaluator) AttributeExpressionLanguageParsingException(org.apache.nifi.attribute.expression.language.exception.AttributeExpressionLanguageParsingException) MappingEvaluator(org.apache.nifi.attribute.expression.language.evaluation.selection.MappingEvaluator)

Aggregations

ReduceEvaluator (org.apache.nifi.attribute.expression.language.evaluation.reduce.ReduceEvaluator)2 MultiAttributeEvaluator (org.apache.nifi.attribute.expression.language.evaluation.selection.MultiAttributeEvaluator)2 AttributeExpressionLanguageParsingException (org.apache.nifi.attribute.expression.language.exception.AttributeExpressionLanguageParsingException)2 ResultType (org.apache.nifi.expression.AttributeExpression.ResultType)2 BooleanEvaluator (org.apache.nifi.attribute.expression.language.evaluation.BooleanEvaluator)1 AllAttributesEvaluator (org.apache.nifi.attribute.expression.language.evaluation.selection.AllAttributesEvaluator)1 AnyAttributeEvaluator (org.apache.nifi.attribute.expression.language.evaluation.selection.AnyAttributeEvaluator)1 IteratingEvaluator (org.apache.nifi.attribute.expression.language.evaluation.selection.IteratingEvaluator)1 MappingEvaluator (org.apache.nifi.attribute.expression.language.evaluation.selection.MappingEvaluator)1 AttributeExpressionLanguageException (org.apache.nifi.attribute.expression.language.exception.AttributeExpressionLanguageException)1