Search in sources :

Example 1 with MappingEvaluator

use of org.apache.nifi.attribute.expression.language.evaluation.selection.MappingEvaluator in project nifi by apache.

the class StandardPreparedQuery method getVariableImpact.

@Override
public VariableImpact getVariableImpact() {
    final VariableImpact existing = this.variableImpact;
    if (existing != null) {
        return existing;
    }
    final Set<String> variables = new HashSet<>();
    for (final Expression expression : expressions) {
        if (!(expression instanceof CompiledExpression)) {
            continue;
        }
        final CompiledExpression compiled = (CompiledExpression) expression;
        for (final Evaluator<?> evaluator : compiled.getAllEvaluators()) {
            if (evaluator instanceof AttributeEvaluator) {
                final AttributeEvaluator attributeEval = (AttributeEvaluator) evaluator;
                final Evaluator<String> nameEval = attributeEval.getNameEvaluator();
                if (nameEval instanceof StringLiteralEvaluator) {
                    final String referencedVar = nameEval.evaluate(Collections.emptyMap()).getValue();
                    variables.add(referencedVar);
                }
            } else if (evaluator instanceof AllAttributesEvaluator) {
                final AllAttributesEvaluator allAttrsEval = (AllAttributesEvaluator) evaluator;
                final MultiAttributeEvaluator iteratingEval = allAttrsEval.getVariableIteratingEvaluator();
                if (iteratingEval instanceof MultiNamedAttributeEvaluator) {
                    variables.addAll(((MultiNamedAttributeEvaluator) iteratingEval).getAttributeNames());
                } else if (iteratingEval instanceof MultiMatchAttributeEvaluator) {
                    return VariableImpact.ALWAYS_IMPACTED;
                }
            } else if (evaluator instanceof AnyAttributeEvaluator) {
                final AnyAttributeEvaluator allAttrsEval = (AnyAttributeEvaluator) evaluator;
                final MultiAttributeEvaluator iteratingEval = allAttrsEval.getVariableIteratingEvaluator();
                if (iteratingEval instanceof MultiNamedAttributeEvaluator) {
                    variables.addAll(((MultiNamedAttributeEvaluator) iteratingEval).getAttributeNames());
                } else if (iteratingEval instanceof MultiMatchAttributeEvaluator) {
                    return VariableImpact.ALWAYS_IMPACTED;
                }
            } else if (evaluator instanceof MappingEvaluator) {
                final MappingEvaluator<?> allAttrsEval = (MappingEvaluator<?>) evaluator;
                final MultiAttributeEvaluator iteratingEval = allAttrsEval.getVariableIteratingEvaluator();
                if (iteratingEval instanceof MultiNamedAttributeEvaluator) {
                    variables.addAll(((MultiNamedAttributeEvaluator) iteratingEval).getAttributeNames());
                }
            }
        }
    }
    final VariableImpact impact = new NamedVariableImpact(variables);
    this.variableImpact = impact;
    return impact;
}
Also used : MultiNamedAttributeEvaluator(org.apache.nifi.attribute.expression.language.evaluation.selection.MultiNamedAttributeEvaluator) MultiAttributeEvaluator(org.apache.nifi.attribute.expression.language.evaluation.selection.MultiAttributeEvaluator) MultiMatchAttributeEvaluator(org.apache.nifi.attribute.expression.language.evaluation.selection.MultiMatchAttributeEvaluator) AnyAttributeEvaluator(org.apache.nifi.attribute.expression.language.evaluation.selection.AnyAttributeEvaluator) MultiMatchAttributeEvaluator(org.apache.nifi.attribute.expression.language.evaluation.selection.MultiMatchAttributeEvaluator) MultiNamedAttributeEvaluator(org.apache.nifi.attribute.expression.language.evaluation.selection.MultiNamedAttributeEvaluator) MultiAttributeEvaluator(org.apache.nifi.attribute.expression.language.evaluation.selection.MultiAttributeEvaluator) AttributeEvaluator(org.apache.nifi.attribute.expression.language.evaluation.selection.AttributeEvaluator) MappingEvaluator(org.apache.nifi.attribute.expression.language.evaluation.selection.MappingEvaluator) StringLiteralEvaluator(org.apache.nifi.attribute.expression.language.evaluation.literals.StringLiteralEvaluator) AllAttributesEvaluator(org.apache.nifi.attribute.expression.language.evaluation.selection.AllAttributesEvaluator) AnyAttributeEvaluator(org.apache.nifi.attribute.expression.language.evaluation.selection.AnyAttributeEvaluator) HashSet(java.util.HashSet)

Example 2 with MappingEvaluator

use of org.apache.nifi.attribute.expression.language.evaluation.selection.MappingEvaluator 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

AllAttributesEvaluator (org.apache.nifi.attribute.expression.language.evaluation.selection.AllAttributesEvaluator)2 AnyAttributeEvaluator (org.apache.nifi.attribute.expression.language.evaluation.selection.AnyAttributeEvaluator)2 MappingEvaluator (org.apache.nifi.attribute.expression.language.evaluation.selection.MappingEvaluator)2 MultiAttributeEvaluator (org.apache.nifi.attribute.expression.language.evaluation.selection.MultiAttributeEvaluator)2 HashSet (java.util.HashSet)1 BooleanEvaluator (org.apache.nifi.attribute.expression.language.evaluation.BooleanEvaluator)1 StringLiteralEvaluator (org.apache.nifi.attribute.expression.language.evaluation.literals.StringLiteralEvaluator)1 ReduceEvaluator (org.apache.nifi.attribute.expression.language.evaluation.reduce.ReduceEvaluator)1 AttributeEvaluator (org.apache.nifi.attribute.expression.language.evaluation.selection.AttributeEvaluator)1 MultiMatchAttributeEvaluator (org.apache.nifi.attribute.expression.language.evaluation.selection.MultiMatchAttributeEvaluator)1 MultiNamedAttributeEvaluator (org.apache.nifi.attribute.expression.language.evaluation.selection.MultiNamedAttributeEvaluator)1 AttributeExpressionLanguageException (org.apache.nifi.attribute.expression.language.exception.AttributeExpressionLanguageException)1 AttributeExpressionLanguageParsingException (org.apache.nifi.attribute.expression.language.exception.AttributeExpressionLanguageParsingException)1 ResultType (org.apache.nifi.expression.AttributeExpression.ResultType)1