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;
}
};
}
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());
}
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;
}
};
}
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;
}
};
}
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();
}
};
}
Aggregations