Search in sources :

Example 16 with BinaryExpr

use of com.github.javaparser.ast.expr.BinaryExpr in project drools by kiegroup.

the class DroolsMvelParserTest method test3BindingOn3Conditions.

@Test
public void test3BindingOn3Conditions() {
    String expr = "$n : name == \"Mario\" && $a : age > 20 && $l : likes != null";
    DrlxExpression drlxExpression = parseExpression(parser, expr);
    Expression bExpr = drlxExpression.getExpr();
    assertTrue(bExpr instanceof BinaryExpr);
    Expression left = ((BinaryExpr) bExpr).getLeft();
    assertTrue(left instanceof BinaryExpr);
    BinaryExpr leftExpr = (BinaryExpr) left;
    DrlxExpression first = (DrlxExpression) leftExpr.getLeft();
    DrlxExpression second = (DrlxExpression) leftExpr.getRight();
    DrlxExpression third = (DrlxExpression) ((BinaryExpr) bExpr).getRight();
    SimpleName bind = first.getBind();
    assertEquals("$n", bind.asString());
    BinaryExpr binaryExpr = ((BinaryExpr) first.getExpr());
    assertEquals("name", toString(binaryExpr.getLeft()));
    assertEquals("\"Mario\"", toString(binaryExpr.getRight()));
    assertEquals(Operator.EQUALS, binaryExpr.getOperator());
    bind = second.getBind();
    assertEquals("$a", bind.asString());
    binaryExpr = ((BinaryExpr) second.getExpr());
    assertEquals("age", toString(binaryExpr.getLeft()));
    assertEquals("20", toString(binaryExpr.getRight()));
    assertEquals(Operator.GREATER, binaryExpr.getOperator());
    bind = third.getBind();
    assertEquals("$l", bind.asString());
    binaryExpr = ((BinaryExpr) third.getExpr());
    assertEquals("likes", toString(binaryExpr.getLeft()));
    assertEquals("null", toString(binaryExpr.getRight()));
    assertEquals(Operator.NOT_EQUALS, binaryExpr.getOperator());
}
Also used : DrlxParser.parseExpression(org.drools.mvel.parser.DrlxParser.parseExpression) Expression(com.github.javaparser.ast.expr.Expression) DrlxExpression(org.drools.mvel.parser.ast.expr.DrlxExpression) DrlxExpression(org.drools.mvel.parser.ast.expr.DrlxExpression) BinaryExpr(com.github.javaparser.ast.expr.BinaryExpr) HalfBinaryExpr(org.drools.mvel.parser.ast.expr.HalfBinaryExpr) SimpleName(com.github.javaparser.ast.expr.SimpleName) Test(org.junit.Test)

Example 17 with BinaryExpr

use of com.github.javaparser.ast.expr.BinaryExpr in project drools by kiegroup.

the class DroolsMvelParserTest method testAndWithImplicitParameterAndParenthesisComplexOnNullSafeField.

@Test
public void testAndWithImplicitParameterAndParenthesisComplexOnNullSafeField() {
    String expr = "value!.length ((> 1 && < 2) || (> 3 && < 4))";
    Expression expression = parseExpression(parser, expr).getExpr();
    BinaryExpr comboExpr = ((BinaryExpr) expression);
    assertEquals(Operator.OR, comboExpr.getOperator());
    BinaryExpr comboExprLeft = ((BinaryExpr) comboExpr.getLeft());
    assertEquals(Operator.AND, comboExprLeft.getOperator());
    BinaryExpr first = (BinaryExpr) comboExprLeft.getLeft();
    assertTrue(first.getLeft() instanceof NullSafeFieldAccessExpr);
    assertEquals("value!.length", toString(first.getLeft()));
    assertEquals("1", toString(first.getRight()));
    assertEquals(Operator.GREATER, first.getOperator());
    HalfBinaryExpr second = (HalfBinaryExpr) comboExprLeft.getRight();
    assertEquals("2", toString(second.getRight()));
    assertEquals(HalfBinaryExpr.Operator.LESS, second.getOperator());
    BinaryExpr comboExprRight = ((BinaryExpr) comboExpr.getRight());
    assertEquals(Operator.AND, comboExprRight.getOperator());
    BinaryExpr third = (BinaryExpr) comboExprRight.getLeft();
    assertEquals("value!.length", toString(third.getLeft()));
    assertEquals("3", toString(third.getRight()));
    assertEquals(Operator.GREATER, third.getOperator());
    HalfBinaryExpr forth = (HalfBinaryExpr) comboExprRight.getRight();
    assertEquals("4", toString(forth.getRight()));
    assertEquals(HalfBinaryExpr.Operator.LESS, forth.getOperator());
}
Also used : HalfBinaryExpr(org.drools.mvel.parser.ast.expr.HalfBinaryExpr) DrlxParser.parseExpression(org.drools.mvel.parser.DrlxParser.parseExpression) Expression(com.github.javaparser.ast.expr.Expression) DrlxExpression(org.drools.mvel.parser.ast.expr.DrlxExpression) BinaryExpr(com.github.javaparser.ast.expr.BinaryExpr) HalfBinaryExpr(org.drools.mvel.parser.ast.expr.HalfBinaryExpr) NullSafeFieldAccessExpr(org.drools.mvel.parser.ast.expr.NullSafeFieldAccessExpr) Test(org.junit.Test)

Example 18 with BinaryExpr

use of com.github.javaparser.ast.expr.BinaryExpr in project drools by kiegroup.

the class DroolsMvelParserTest method testAndWithImplicitParameter2.

@Test
public void testAndWithImplicitParameter2() {
    String expr = "name == \"Mark\" && == \"Mario\" || == \"Luca\"";
    Expression expression = parseExpression(parser, expr).getExpr();
    BinaryExpr comboExpr = ((BinaryExpr) expression);
    assertEquals(Operator.OR, comboExpr.getOperator());
    assertEquals(Operator.AND, ((BinaryExpr) (comboExpr.getLeft())).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());
}
Also used : HalfBinaryExpr(org.drools.mvel.parser.ast.expr.HalfBinaryExpr) DrlxParser.parseExpression(org.drools.mvel.parser.DrlxParser.parseExpression) Expression(com.github.javaparser.ast.expr.Expression) DrlxExpression(org.drools.mvel.parser.ast.expr.DrlxExpression) BinaryExpr(com.github.javaparser.ast.expr.BinaryExpr) HalfBinaryExpr(org.drools.mvel.parser.ast.expr.HalfBinaryExpr) Test(org.junit.Test)

Example 19 with BinaryExpr

use of com.github.javaparser.ast.expr.BinaryExpr in project drools by kiegroup.

the class DroolsMvelParserTest method testEnclosedBindVariable.

@Test
public void testEnclosedBindVariable() {
    String expr = "($n : name == \"Mario\")";
    DrlxExpression drlxExpression = parseExpression(parser, expr);
    Expression enclosedExpr = drlxExpression.getExpr();
    assertTrue(enclosedExpr instanceof EnclosedExpr);
    Expression inner = ((EnclosedExpr) enclosedExpr).getInner();
    assertTrue(inner instanceof DrlxExpression);
    DrlxExpression innerDrlxExpression = (DrlxExpression) inner;
    SimpleName bind = innerDrlxExpression.getBind();
    assertEquals("$n", bind.asString());
    Expression expression = innerDrlxExpression.getExpr();
    BinaryExpr binaryExpr = ((BinaryExpr) expression);
    assertEquals("name", toString(binaryExpr.getLeft()));
    assertEquals("\"Mario\"", toString(binaryExpr.getRight()));
    assertEquals(Operator.EQUALS, binaryExpr.getOperator());
}
Also used : DrlxParser.parseExpression(org.drools.mvel.parser.DrlxParser.parseExpression) Expression(com.github.javaparser.ast.expr.Expression) DrlxExpression(org.drools.mvel.parser.ast.expr.DrlxExpression) DrlxExpression(org.drools.mvel.parser.ast.expr.DrlxExpression) SimpleName(com.github.javaparser.ast.expr.SimpleName) BinaryExpr(com.github.javaparser.ast.expr.BinaryExpr) HalfBinaryExpr(org.drools.mvel.parser.ast.expr.HalfBinaryExpr) EnclosedExpr(com.github.javaparser.ast.expr.EnclosedExpr) Test(org.junit.Test)

Example 20 with BinaryExpr

use of com.github.javaparser.ast.expr.BinaryExpr in project drools by kiegroup.

the class DroolsMvelParserTest method testDotFreeExprWithOr.

@Test
public void testDotFreeExprWithOr() {
    String expr = "this after $a || this after $b";
    Expression expression = parseExpression(parser, expr).getExpr();
    assertTrue(expression instanceof BinaryExpr);
    assertEquals(expr, printNode(expression));
}
Also used : DrlxParser.parseExpression(org.drools.mvel.parser.DrlxParser.parseExpression) Expression(com.github.javaparser.ast.expr.Expression) DrlxExpression(org.drools.mvel.parser.ast.expr.DrlxExpression) BinaryExpr(com.github.javaparser.ast.expr.BinaryExpr) HalfBinaryExpr(org.drools.mvel.parser.ast.expr.HalfBinaryExpr) Test(org.junit.Test)

Aggregations

BinaryExpr (com.github.javaparser.ast.expr.BinaryExpr)43 Expression (com.github.javaparser.ast.expr.Expression)37 HalfBinaryExpr (org.drools.mvel.parser.ast.expr.HalfBinaryExpr)33 DrlxExpression (org.drools.mvel.parser.ast.expr.DrlxExpression)30 DrlxParser.parseExpression (org.drools.mvel.parser.DrlxParser.parseExpression)20 Test (org.junit.Test)20 EnclosedExpr (com.github.javaparser.ast.expr.EnclosedExpr)12 TypedExpression (org.drools.modelcompiler.builder.generator.TypedExpression)10 NameExpr (com.github.javaparser.ast.expr.NameExpr)9 SimpleName (com.github.javaparser.ast.expr.SimpleName)9 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)8 DrlNameExpr (org.drools.mvel.parser.ast.expr.DrlNameExpr)8 FieldAccessExpr (com.github.javaparser.ast.expr.FieldAccessExpr)7 ArrayList (java.util.ArrayList)7 Node (com.github.javaparser.ast.Node)6 UnaryExpr (com.github.javaparser.ast.expr.UnaryExpr)6 NodeList (com.github.javaparser.ast.NodeList)5 LiteralExpr (com.github.javaparser.ast.expr.LiteralExpr)5 NullLiteralExpr (com.github.javaparser.ast.expr.NullLiteralExpr)5 ListCreationLiteralExpression (org.drools.mvel.parser.ast.expr.ListCreationLiteralExpression)5