use of com.github.javaparser.ast.expr.BinaryExpr in project drools by kiegroup.
the class DroolsMvelParserTest method testBindVariable.
@Test
public void testBindVariable() {
String expr = "$n : name == \"Mark\"";
DrlxExpression drlxExpression = parseExpression(parser, expr);
SimpleName bind = drlxExpression.getBind();
assertEquals("$n", bind.asString());
Expression expression = drlxExpression.getExpr();
BinaryExpr binaryExpr = ((BinaryExpr) expression);
assertEquals("name", toString(binaryExpr.getLeft()));
assertEquals("\"Mark\"", toString(binaryExpr.getRight()));
assertEquals(Operator.EQUALS, binaryExpr.getOperator());
}
use of com.github.javaparser.ast.expr.BinaryExpr in project drools by kiegroup.
the class DroolsMvelParserTest method testOrWithImplicitParameter.
@Test
public void testOrWithImplicitParameter() {
String expr = "name == \"Mark\" || == \"Mario\" || == \"Luca\"";
Expression expression = parseExpression(parser, expr).getExpr();
BinaryExpr comboExpr = ((BinaryExpr) expression);
assertEquals(Operator.OR, comboExpr.getOperator());
BinaryExpr first = ((BinaryExpr) ((BinaryExpr) comboExpr.getLeft()).getLeft());
assertEquals("name", toString(first.getLeft()));
assertEquals("\"Mark\"", toString(first.getRight()));
assertEquals(Operator.EQUALS, first.getOperator());
HalfBinaryExpr second = (HalfBinaryExpr) ((BinaryExpr) comboExpr.getLeft()).getRight();
assertEquals("\"Mario\"", toString(second.getRight()));
assertEquals(HalfBinaryExpr.Operator.EQUALS, second.getOperator());
HalfBinaryExpr third = (HalfBinaryExpr) comboExpr.getRight();
assertEquals("\"Luca\"", toString(third.getRight()));
assertEquals(HalfBinaryExpr.Operator.EQUALS, third.getOperator());
}
use of com.github.javaparser.ast.expr.BinaryExpr in project drools by kiegroup.
the class AccumulateVisitor method parseFirstParameter.
private Optional<NewBinding> parseFirstParameter(PatternDescr basePattern, BaseDescr input, AccumulateDescr.AccumulateFunctionCallDescr function, MethodCallExpr functionDSL, String bindingId) {
final String accumulateFunctionParameterStr = function.getParams()[0];
final Expression accumulateFunctionParameter = DrlxParseUtil.parseExpression(accumulateFunctionParameterStr).getExpr();
if (accumulateFunctionParameter instanceof BinaryExpr) {
return binaryExprParameter(basePattern, function, functionDSL, bindingId, accumulateFunctionParameterStr);
}
if (parameterNeedsConvertionToMethodCallExpr(accumulateFunctionParameter)) {
return methodCallExprParameter(basePattern, input, function, functionDSL, bindingId, accumulateFunctionParameter);
}
if (accumulateFunctionParameter instanceof DrlNameExpr) {
nameExprParameter(basePattern, function, functionDSL, bindingId, accumulateFunctionParameter);
} else if (accumulateFunctionParameter instanceof LiteralExpr) {
literalExprParameter(basePattern, function, functionDSL, bindingId, accumulateFunctionParameter);
} else {
context.addCompilationError(new InvalidExpressionErrorResult("Invalid expression " + accumulateFunctionParameterStr, Optional.of(context.getRuleDescr())));
throw new AccumulateParsingFailedException();
}
return Optional.empty();
}
use of com.github.javaparser.ast.expr.BinaryExpr in project drools by kiegroup.
the class AstUtils method parseBindingAfterAnd.
public static DrlxExpression parseBindingAfterAnd(TokenRange tokenRange, DrlxExpression leftExpr, Expression rightExpr) {
if (leftExpr.getExpr() instanceof BinaryExpr && ((BinaryExpr) leftExpr.getExpr()).getOperator() == BinaryExpr.Operator.AND) {
if (((BinaryExpr) leftExpr.getExpr()).getRight() instanceof NameExpr) {
DrlxExpression newLeft = new DrlxExpression(leftExpr.getBind(), ((BinaryExpr) leftExpr.getExpr()).getLeft());
SimpleName rightName = ((NameExpr) ((BinaryExpr) leftExpr.getExpr()).getRight()).getName();
DrlxExpression newRight = new DrlxExpression(rightName, rightExpr);
return new DrlxExpression(null, new BinaryExpr(tokenRange, newLeft, newRight, BinaryExpr.Operator.AND));
}
if (((BinaryExpr) leftExpr.getExpr()).getRight() instanceof DrlxExpression) {
Expression first = ((BinaryExpr) leftExpr.getExpr()).getLeft();
DrlxExpression innerRight = parseBindingAfterAnd(tokenRange, (DrlxExpression) ((BinaryExpr) leftExpr.getExpr()).getRight(), rightExpr);
Expression second = ((BinaryExpr) innerRight.getExpr()).getLeft();
Expression third = ((BinaryExpr) innerRight.getExpr()).getRight();
return new DrlxExpression(null, new BinaryExpr(tokenRange, new BinaryExpr(tokenRange, first, second, BinaryExpr.Operator.AND), third, BinaryExpr.Operator.AND));
}
}
throw new IllegalStateException();
}
use of com.github.javaparser.ast.expr.BinaryExpr in project drools by kiegroup.
the class AstUtils method transformHalfBinaryArg.
private static Expression transformHalfBinaryArg(TokenRange tokenRange, Expression scope, Expression name, Expression expr, boolean nullSafe) {
if (expr instanceof HalfBinaryExpr) {
Expression left = scope == null ? name : (nullSafe ? new NullSafeFieldAccessExpr(scope, null, name.asNameExpr().getName()) : new FieldAccessExpr(scope, null, name.asNameExpr().getName()));
return new BinaryExpr(tokenRange, left, ((HalfBinaryExpr) expr).getRight(), ((HalfBinaryExpr) expr).getOperator().toBinaryExprOperator());
}
if (expr instanceof EnclosedExpr) {
return transformHalfBinaryArg(tokenRange, scope, name, ((EnclosedExpr) expr).getInner(), nullSafe);
}
if (expr instanceof BinaryExpr) {
BinaryExpr binary = (BinaryExpr) expr;
Expression rewrittenLeft = transformHalfBinaryArg(tokenRange, scope, name, binary.getLeft(), nullSafe);
Expression rewrittenRight = binary.getRight() instanceof HalfBinaryExpr && !(binary.getLeft() instanceof EnclosedExpr) ? binary.getRight() : transformHalfBinaryArg(tokenRange, scope, name, binary.getRight(), nullSafe);
rewrittenRight.setParentNode(rewrittenLeft);
return new BinaryExpr(tokenRange, rewrittenLeft, rewrittenRight, binary.getOperator());
}
throw new IllegalStateException();
}
Aggregations