Search in sources :

Example 91 with Predicate

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

the class SimplePredicateParser method createPredicates.

/**
     * Creates the {@link Predicate}s from the AST nodes.
     *
     * @return the created {@link Predicate}s, is never <tt>null</tt>.
     */
private List<Predicate> createPredicates() {
    List<Predicate> answer = new ArrayList<Predicate>();
    for (SimpleNode node : nodes) {
        Expression exp = node.createExpression(expression);
        if (exp != null) {
            Predicate predicate = ExpressionToPredicateAdapter.toPredicate(exp);
            answer.add(predicate);
        }
    }
    return answer;
}
Also used : Expression(org.apache.camel.Expression) NullExpression(org.apache.camel.language.simple.ast.NullExpression) LogicalExpression(org.apache.camel.language.simple.ast.LogicalExpression) LiteralExpression(org.apache.camel.language.simple.ast.LiteralExpression) BinaryExpression(org.apache.camel.language.simple.ast.BinaryExpression) UnaryExpression(org.apache.camel.language.simple.ast.UnaryExpression) ArrayList(java.util.ArrayList) Predicate(org.apache.camel.Predicate) SimpleNode(org.apache.camel.language.simple.ast.SimpleNode)

Example 92 with Predicate

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

the class BinaryExpression method createRangeExpression.

private Expression createRangeExpression(final String expression, final Expression leftExp, final Expression rightExp) {
    return new Expression() {

        @Override
        public <T> T evaluate(Exchange exchange, Class<T> type) {
            Predicate predicate;
            String range = rightExp.evaluate(exchange, String.class);
            Matcher matcher = RANGE_PATTERN.matcher(range);
            if (matcher.matches()) {
                // wrap as constant expression for the from and to values
                Expression from = ExpressionBuilder.constantExpression(matcher.group(1));
                Expression to = ExpressionBuilder.constantExpression(matcher.group(3));
                // build a compound predicate for the range
                predicate = PredicateBuilder.isGreaterThanOrEqualTo(leftExp, from);
                predicate = PredicateBuilder.and(predicate, PredicateBuilder.isLessThanOrEqualTo(leftExp, to));
            } else {
                throw new SimpleIllegalSyntaxException(expression, right.getToken().getIndex(), operator + " operator is not valid. Valid syntax:'from..to' (where from and to are numbers).");
            }
            if (operator == BinaryOperatorType.NOT_RANGE) {
                predicate = PredicateBuilder.not(predicate);
            }
            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) Matcher(java.util.regex.Matcher) SimpleIllegalSyntaxException(org.apache.camel.language.simple.types.SimpleIllegalSyntaxException) Predicate(org.apache.camel.Predicate)

Example 93 with Predicate

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

the class BinaryExpression method createIsExpression.

private Expression createIsExpression(final String expression, final Expression leftExp, final Expression rightExp) {
    return new Expression() {

        @Override
        public <T> T evaluate(Exchange exchange, Class<T> type) {
            Predicate predicate;
            String name = rightExp.evaluate(exchange, String.class);
            if (name == null || "null".equals(name)) {
                throw new SimpleIllegalSyntaxException(expression, right.getToken().getIndex(), operator + " operator cannot accept null. A class type must be provided.");
            }
            Class<?> rightType = exchange.getContext().getClassResolver().resolveClass(name);
            if (rightType == null) {
                throw new SimpleIllegalSyntaxException(expression, right.getToken().getIndex(), operator + " operator cannot find class with name: " + name);
            }
            predicate = PredicateBuilder.isInstanceOf(leftExp, rightType);
            if (operator == BinaryOperatorType.NOT_IS) {
                predicate = PredicateBuilder.not(predicate);
            }
            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) SimpleIllegalSyntaxException(org.apache.camel.language.simple.types.SimpleIllegalSyntaxException) Predicate(org.apache.camel.Predicate)

Example 94 with Predicate

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

the class SimpleLanguage method createPredicate.

public Predicate createPredicate(String expression) {
    ObjectHelper.notNull(expression, "expression");
    expression = loadResource(expression);
    // support old simple language syntax
    @SuppressWarnings("deprecation") Predicate answer = SimpleBackwardsCompatibleParser.parsePredicate(expression, allowEscape);
    if (answer == null) {
        // use the new parser
        SimplePredicateParser parser = new SimplePredicateParser(expression, allowEscape);
        answer = parser.parsePredicate();
    }
    return answer;
}
Also used : Predicate(org.apache.camel.Predicate)

Example 95 with Predicate

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

the class FilterBeforeSplitTest method createRouteBuilder.

@Override
protected RouteBuilder createRouteBuilder() throws Exception {
    return new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            Predicate goodWord = body().contains("World");
            from("direct:start").to("mock:before").filter(goodWord).to("mock:good").end().split(body().tokenize(" "), new MyAggregationStrategy()).to("mock:split").end().to("mock:result");
        }
    };
}
Also used : RouteBuilder(org.apache.camel.builder.RouteBuilder) Predicate(org.apache.camel.Predicate)

Aggregations

Predicate (org.apache.camel.Predicate)124 Exchange (org.apache.camel.Exchange)70 Test (org.junit.Test)40 HashMap (java.util.HashMap)25 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)23 Expression (org.apache.camel.Expression)22 Processor (org.apache.camel.Processor)19 DefaultExchange (org.apache.camel.impl.DefaultExchange)10 RouteBuilder (org.apache.camel.builder.RouteBuilder)9 File (java.io.File)8 Matchers.containsString (org.hamcrest.Matchers.containsString)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 Map (java.util.Map)7 Ignore (org.junit.Ignore)7 IOException (java.io.IOException)6 ArrayList (java.util.ArrayList)6 AggregateProcessor (org.apache.camel.processor.aggregate.AggregateProcessor)6 AggregationStrategy (org.apache.camel.processor.aggregate.AggregationStrategy)6 PDDocument (org.apache.pdfbox.pdmodel.PDDocument)6 BodyInAggregatingStrategy (org.apache.camel.processor.BodyInAggregatingStrategy)5