Search in sources :

Example 6 with AttributeExpressionLanguageParsingException

use of org.apache.nifi.attribute.expression.language.exception.AttributeExpressionLanguageParsingException in project nifi by apache.

the class ExpressionCompiler method compile.

public CompiledExpression compile(final String expression) {
    try {
        final CharStream input = new ANTLRStringStream(expression);
        final AttributeExpressionLexer lexer = new AttributeExpressionLexer(input);
        final CommonTokenStream lexerTokenStream = new CommonTokenStream(lexer);
        final AttributeExpressionParser parser = new AttributeExpressionParser(lexerTokenStream);
        final Tree ast = (Tree) parser.query().getTree();
        final Tree tree = ast.getChild(0);
        final Evaluator<?> evaluator = buildEvaluator(tree);
        verifyMappingEvaluatorReduced(evaluator);
        final Set<Evaluator<?>> allEvaluators = new HashSet<>(evaluators);
        this.evaluators.clear();
        return new CompiledExpression(expression, evaluator, tree, allEvaluators);
    } catch (final AttributeExpressionLanguageParsingException e) {
        throw e;
    } catch (final Exception e) {
        throw new AttributeExpressionLanguageParsingException(e);
    }
}
Also used : ANTLRStringStream(org.antlr.runtime.ANTLRStringStream) CommonTokenStream(org.antlr.runtime.CommonTokenStream) AttributeExpressionParser(org.apache.nifi.attribute.expression.language.antlr.AttributeExpressionParser) IfElseEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.IfElseEvaluator) OrEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.OrEvaluator) GreaterThanEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.GreaterThanEvaluator) LastIndexOfEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.LastIndexOfEvaluator) UrlEncodeEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.UrlEncodeEvaluator) IteratingEvaluator(org.apache.nifi.attribute.expression.language.evaluation.selection.IteratingEvaluator) SubstringAfterLastEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.SubstringAfterLastEvaluator) ReplaceNullEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.ReplaceNullEvaluator) ToLiteralEvaluator(org.apache.nifi.attribute.expression.language.evaluation.literals.ToLiteralEvaluator) AppendEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.AppendEvaluator) GetStateVariableEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.GetStateVariableEvaluator) MinusEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.MinusEvaluator) ReplaceAllEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.ReplaceAllEvaluator) Base64DecodeEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.Base64DecodeEvaluator) ToStringEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.ToStringEvaluator) MultiAttributeEvaluator(org.apache.nifi.attribute.expression.language.evaluation.selection.MultiAttributeEvaluator) AttributeEvaluator(org.apache.nifi.attribute.expression.language.evaluation.selection.AttributeEvaluator) GreaterThanOrEqualEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.GreaterThanOrEqualEvaluator) AnyAttributeEvaluator(org.apache.nifi.attribute.expression.language.evaluation.selection.AnyAttributeEvaluator) MappingEvaluator(org.apache.nifi.attribute.expression.language.evaluation.selection.MappingEvaluator) AndEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.AndEvaluator) NumberToDateEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.NumberToDateEvaluator) OneUpSequenceEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.OneUpSequenceEvaluator) StringLiteralEvaluator(org.apache.nifi.attribute.expression.language.evaluation.literals.StringLiteralEvaluator) WholeNumberCastEvaluator(org.apache.nifi.attribute.expression.language.evaluation.cast.WholeNumberCastEvaluator) IsEmptyEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.IsEmptyEvaluator) InEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.InEvaluator) LessThanOrEqualEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.LessThanOrEqualEvaluator) UuidEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.UuidEvaluator) IPEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.IPEvaluator) EqualsEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.EqualsEvaluator) CharSequenceTranslatorEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.CharSequenceTranslatorEvaluator) IsNullEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.IsNullEvaluator) NowEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.NowEvaluator) IndexOfEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.IndexOfEvaluator) CountEvaluator(org.apache.nifi.attribute.expression.language.evaluation.reduce.CountEvaluator) ToRadixEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.ToRadixEvaluator) ReplaceFirstEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.ReplaceFirstEvaluator) ToUpperEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.ToUpperEvaluator) ReplaceEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.ReplaceEvaluator) EqualsIgnoreCaseEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.EqualsIgnoreCaseEvaluator) ContainsEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.ContainsEvaluator) PlusEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.PlusEvaluator) FindEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.FindEvaluator) MatchesEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.MatchesEvaluator) Evaluator(org.apache.nifi.attribute.expression.language.evaluation.Evaluator) ModEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.ModEvaluator) Base64EncodeEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.Base64EncodeEvaluator) GetDelimitedFieldEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.GetDelimitedFieldEvaluator) DateEvaluator(org.apache.nifi.attribute.expression.language.evaluation.DateEvaluator) TrimEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.TrimEvaluator) FromRadixEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.FromRadixEvaluator) JsonPathEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.JsonPathEvaluator) BooleanLiteralEvaluator(org.apache.nifi.attribute.expression.language.evaluation.literals.BooleanLiteralEvaluator) DecimalLiteralEvaluator(org.apache.nifi.attribute.expression.language.evaluation.literals.DecimalLiteralEvaluator) LessThanEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.LessThanEvaluator) WholeNumberLiteralEvaluator(org.apache.nifi.attribute.expression.language.evaluation.literals.WholeNumberLiteralEvaluator) StringEvaluator(org.apache.nifi.attribute.expression.language.evaluation.StringEvaluator) LengthEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.LengthEvaluator) ReduceEvaluator(org.apache.nifi.attribute.expression.language.evaluation.reduce.ReduceEvaluator) SubstringAfterEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.SubstringAfterEvaluator) MathEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.MathEvaluator) MultiplyEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.MultiplyEvaluator) AllAttributesEvaluator(org.apache.nifi.attribute.expression.language.evaluation.selection.AllAttributesEvaluator) SubstringEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.SubstringEvaluator) MultiNamedAttributeEvaluator(org.apache.nifi.attribute.expression.language.evaluation.selection.MultiNamedAttributeEvaluator) NotEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.NotEvaluator) SubstringBeforeLastEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.SubstringBeforeLastEvaluator) FormatEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.FormatEvaluator) StartsWithEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.StartsWithEvaluator) PrependEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.PrependEvaluator) EndsWithEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.EndsWithEvaluator) ToLowerEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.ToLowerEvaluator) MultiMatchAttributeEvaluator(org.apache.nifi.attribute.expression.language.evaluation.selection.MultiMatchAttributeEvaluator) BooleanEvaluator(org.apache.nifi.attribute.expression.language.evaluation.BooleanEvaluator) ReplaceEmptyEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.ReplaceEmptyEvaluator) DelineatedAttributeEvaluator(org.apache.nifi.attribute.expression.language.evaluation.selection.DelineatedAttributeEvaluator) NumberCastEvaluator(org.apache.nifi.attribute.expression.language.evaluation.cast.NumberCastEvaluator) HostnameEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.HostnameEvaluator) StringCastEvaluator(org.apache.nifi.attribute.expression.language.evaluation.cast.StringCastEvaluator) BooleanCastEvaluator(org.apache.nifi.attribute.expression.language.evaluation.cast.BooleanCastEvaluator) UrlDecodeEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.UrlDecodeEvaluator) RandomNumberGeneratorEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.RandomNumberGeneratorEvaluator) NotNullEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.NotNullEvaluator) DecimalCastEvaluator(org.apache.nifi.attribute.expression.language.evaluation.cast.DecimalCastEvaluator) JoinEvaluator(org.apache.nifi.attribute.expression.language.evaluation.reduce.JoinEvaluator) SubstringBeforeEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.SubstringBeforeEvaluator) StringToDateEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.StringToDateEvaluator) DateCastEvaluator(org.apache.nifi.attribute.expression.language.evaluation.cast.DateCastEvaluator) DivideEvaluator(org.apache.nifi.attribute.expression.language.evaluation.functions.DivideEvaluator) AttributeExpressionLanguageParsingException(org.apache.nifi.attribute.expression.language.exception.AttributeExpressionLanguageParsingException) CharStream(org.antlr.runtime.CharStream) CompiledExpression(org.apache.nifi.attribute.expression.language.CompiledExpression) AttributeExpressionLanguageException(org.apache.nifi.attribute.expression.language.exception.AttributeExpressionLanguageException) UnknownHostException(java.net.UnknownHostException) AttributeExpressionLanguageParsingException(org.apache.nifi.attribute.expression.language.exception.AttributeExpressionLanguageParsingException) Tree(org.antlr.runtime.tree.Tree) AttributeExpressionLexer(org.apache.nifi.attribute.expression.language.antlr.AttributeExpressionLexer) HashSet(java.util.HashSet)

Example 7 with AttributeExpressionLanguageParsingException

use of org.apache.nifi.attribute.expression.language.exception.AttributeExpressionLanguageParsingException 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

AttributeExpressionLanguageParsingException (org.apache.nifi.attribute.expression.language.exception.AttributeExpressionLanguageParsingException)7 ReduceEvaluator (org.apache.nifi.attribute.expression.language.evaluation.reduce.ReduceEvaluator)4 MultiAttributeEvaluator (org.apache.nifi.attribute.expression.language.evaluation.selection.MultiAttributeEvaluator)4 Tree (org.antlr.runtime.tree.Tree)3 BooleanEvaluator (org.apache.nifi.attribute.expression.language.evaluation.BooleanEvaluator)3 UnknownHostException (java.net.UnknownHostException)2 ArrayList (java.util.ArrayList)2 ANTLRStringStream (org.antlr.runtime.ANTLRStringStream)2 CharStream (org.antlr.runtime.CharStream)2 CommonTokenStream (org.antlr.runtime.CommonTokenStream)2 AttributeExpressionLexer (org.apache.nifi.attribute.expression.language.antlr.AttributeExpressionLexer)2 AttributeExpressionParser (org.apache.nifi.attribute.expression.language.antlr.AttributeExpressionParser)2 ExpressionCompiler (org.apache.nifi.attribute.expression.language.compile.ExpressionCompiler)2 DateEvaluator (org.apache.nifi.attribute.expression.language.evaluation.DateEvaluator)2 Evaluator (org.apache.nifi.attribute.expression.language.evaluation.Evaluator)2 StringEvaluator (org.apache.nifi.attribute.expression.language.evaluation.StringEvaluator)2 BooleanCastEvaluator (org.apache.nifi.attribute.expression.language.evaluation.cast.BooleanCastEvaluator)2 DateCastEvaluator (org.apache.nifi.attribute.expression.language.evaluation.cast.DateCastEvaluator)2 DecimalCastEvaluator (org.apache.nifi.attribute.expression.language.evaluation.cast.DecimalCastEvaluator)2 NumberCastEvaluator (org.apache.nifi.attribute.expression.language.evaluation.cast.NumberCastEvaluator)2