Search in sources :

Example 11 with Expression

use of org.apache.camel.Expression in project camel by apache.

the class LogicalExpression method createOrExpression.

private Expression createOrExpression(final Expression leftExp, final Expression rightExp) {
    return new Expression() {

        @Override
        public <T> T evaluate(Exchange exchange, Class<T> type) {
            Predicate predicate = ExpressionToPredicateAdapter.toPredicate(leftExp);
            predicate = PredicateBuilder.or(predicate, ExpressionToPredicateAdapter.toPredicate(rightExp));
            boolean answer = predicate.matches(exchange);
            return exchange.getContext().getTypeConverter().convertTo(type, answer);
        }

        @Override
        public String toString() {
            return left + " " + token.getText() + " " + right;
        }
    };
}
Also used : Exchange(org.apache.camel.Exchange) Expression(org.apache.camel.Expression) Predicate(org.apache.camel.Predicate)

Example 12 with Expression

use of org.apache.camel.Expression in project camel by apache.

the class LogicalExpression method createExpression.

@Override
public Expression createExpression(String expression) {
    ObjectHelper.notNull(left, "left node", this);
    ObjectHelper.notNull(right, "right node", this);
    final Expression leftExp = left.createExpression(expression);
    final Expression rightExp = right.createExpression(expression);
    if (operator == LogicalOperatorType.AND) {
        return createAndExpression(leftExp, rightExp);
    } else if (operator == LogicalOperatorType.OR) {
        return createOrExpression(leftExp, rightExp);
    }
    throw new SimpleParserException("Unknown logical operator " + operator, token.getIndex());
}
Also used : SimpleParserException(org.apache.camel.language.simple.types.SimpleParserException) Expression(org.apache.camel.Expression)

Example 13 with Expression

use of org.apache.camel.Expression in project camel by apache.

the class LogicalExpression method createAndExpression.

private Expression createAndExpression(final Expression leftExp, final Expression rightExp) {
    return new Expression() {

        @Override
        public <T> T evaluate(Exchange exchange, Class<T> type) {
            Predicate predicate = ExpressionToPredicateAdapter.toPredicate(leftExp);
            predicate = PredicateBuilder.and(predicate, ExpressionToPredicateAdapter.toPredicate(rightExp));
            boolean answer = predicate.matches(exchange);
            return exchange.getContext().getTypeConverter().convertTo(type, answer);
        }

        @Override
        public String toString() {
            return left + " " + token.getText() + " " + right;
        }
    };
}
Also used : Exchange(org.apache.camel.Exchange) Expression(org.apache.camel.Expression) Predicate(org.apache.camel.Predicate)

Example 14 with Expression

use of org.apache.camel.Expression in project camel by apache.

the class SimpleFunctionStart method doCreateCompositeExpression.

private Expression doCreateCompositeExpression(final String expression) {
    final SimpleToken token = getToken();
    return new Expression() {

        @Override
        public <T> T evaluate(Exchange exchange, Class<T> type) {
            StringBuilder sb = new StringBuilder();
            boolean quoteEmbeddedFunctions = false;
            // we need to concat the block so we have the expression
            for (SimpleNode child : block.getChildren()) {
                // whether a nested function should be lazy evaluated or not
                boolean lazy = true;
                if (child instanceof SimpleFunctionStart) {
                    lazy = ((SimpleFunctionStart) child).lazyEval(child);
                }
                if (child instanceof LiteralNode) {
                    String text = ((LiteralNode) child).getText();
                    sb.append(text);
                    quoteEmbeddedFunctions |= ((LiteralNode) child).quoteEmbeddedNodes();
                // if its quoted literal then embed that as text
                } else if (!lazy || child instanceof SingleQuoteStart || child instanceof DoubleQuoteStart) {
                    try {
                        // pass in null when we evaluate the nested expressions
                        Expression nested = child.createExpression(null);
                        String text = nested.evaluate(exchange, String.class);
                        if (text != null) {
                            if (quoteEmbeddedFunctions && !StringHelper.isQuoted(text)) {
                                sb.append("'").append(text).append("'");
                            } else {
                                sb.append(text);
                            }
                        }
                    } catch (SimpleParserException e) {
                        // must rethrow parser exception as illegal syntax with details about the location
                        throw new SimpleIllegalSyntaxException(expression, e.getIndex(), e.getMessage(), e);
                    }
                // if its an inlined function then embed that function as text so it can be evaluated lazy
                } else if (child instanceof SimpleFunctionStart) {
                    sb.append(child);
                }
            }
            // we have now concat the block as a String which contains the function expression
            // which we then need to evaluate as a function
            String exp = sb.toString();
            SimpleFunctionExpression function = new SimpleFunctionExpression(token);
            function.addText(exp);
            try {
                return function.createExpression(exp).evaluate(exchange, type);
            } catch (SimpleParserException e) {
                // must rethrow parser exception as illegal syntax with details about the location
                throw new SimpleIllegalSyntaxException(expression, e.getIndex(), e.getMessage(), e);
            }
        }

        @Override
        public String toString() {
            return expression;
        }
    };
}
Also used : SimpleParserException(org.apache.camel.language.simple.types.SimpleParserException) SimpleIllegalSyntaxException(org.apache.camel.language.simple.types.SimpleIllegalSyntaxException) Exchange(org.apache.camel.Exchange) Expression(org.apache.camel.Expression) SimpleToken(org.apache.camel.language.simple.types.SimpleToken)

Example 15 with Expression

use of org.apache.camel.Expression in project camel by apache.

the class UnaryExpression method createDecExpression.

private Expression createDecExpression(final Expression leftExp) {
    return new Expression() {

        @Override
        public <T> T evaluate(Exchange exchange, Class<T> type) {
            Number num = leftExp.evaluate(exchange, Number.class);
            if (num != null) {
                long val = num.longValue();
                val--;
                // convert value back to same type as input as we want to preserve type
                Object left = leftExp.evaluate(exchange, Object.class);
                try {
                    left = exchange.getContext().getTypeConverter().mandatoryConvertTo(left.getClass(), exchange, val);
                } catch (NoTypeConversionAvailableException e) {
                    throw ObjectHelper.wrapRuntimeCamelException(e);
                }
                // and return the result
                return exchange.getContext().getTypeConverter().convertTo(type, left);
            }
            // cannot convert the expression as a number
            Exception cause = new CamelExchangeException("Cannot evaluate " + leftExp + " as a number", exchange);
            throw ObjectHelper.wrapRuntimeCamelException(cause);
        }

        @Override
        public String toString() {
            return left + operator.toString();
        }
    };
}
Also used : Exchange(org.apache.camel.Exchange) CamelExchangeException(org.apache.camel.CamelExchangeException) Expression(org.apache.camel.Expression) NoTypeConversionAvailableException(org.apache.camel.NoTypeConversionAvailableException) NoTypeConversionAvailableException(org.apache.camel.NoTypeConversionAvailableException) SimpleParserException(org.apache.camel.language.simple.types.SimpleParserException) CamelExchangeException(org.apache.camel.CamelExchangeException)

Aggregations

Expression (org.apache.camel.Expression)183 Exchange (org.apache.camel.Exchange)44 Processor (org.apache.camel.Processor)24 Predicate (org.apache.camel.Predicate)22 DefaultExchange (org.apache.camel.impl.DefaultExchange)21 AggregationStrategy (org.apache.camel.processor.aggregate.AggregationStrategy)18 ArrayList (java.util.ArrayList)15 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)15 AggregateProcessor (org.apache.camel.processor.aggregate.AggregateProcessor)15 Language (org.apache.camel.spi.Language)15 BodyInAggregatingStrategy (org.apache.camel.processor.BodyInAggregatingStrategy)14 SendProcessor (org.apache.camel.processor.SendProcessor)14 Test (org.junit.Test)8 SimpleParserException (org.apache.camel.language.simple.types.SimpleParserException)7 File (java.io.File)4 ExecutorService (java.util.concurrent.ExecutorService)4 SimpleIllegalSyntaxException (org.apache.camel.language.simple.types.SimpleIllegalSyntaxException)4 HashMap (java.util.HashMap)3 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)3 CamelExchangeException (org.apache.camel.CamelExchangeException)3