Search in sources :

Example 11 with BinaryExpr

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

the class ConstraintUtil method processExpression.

private static void processExpression(Expression expr) {
    if (expr instanceof MethodCallExpr) {
        MethodCallExpr mcExpr = (MethodCallExpr) expr;
        if (canInverse(mcExpr)) {
            inverseMethodCallExpr(mcExpr);
        }
    } else if (expr instanceof BinaryExpr) {
        BinaryExpr bExpr = (BinaryExpr) expr;
        if (bExpr.getOperator() == BinaryExpr.Operator.AND || bExpr.getOperator() == BinaryExpr.Operator.OR) {
            Expression left = bExpr.getLeft();
            processExpression(left);
            Expression right = bExpr.getRight();
            processExpression(right);
        }
    } else if (expr instanceof UnaryExpr) {
        Expression expression = ((UnaryExpr) expr).getExpression();
        processExpression(expression);
    } else if (expr instanceof EnclosedExpr) {
        Expression inner = ((EnclosedExpr) expr).getInner();
        processExpression(inner);
    }
}
Also used : Expression(com.github.javaparser.ast.expr.Expression) BinaryExpr(com.github.javaparser.ast.expr.BinaryExpr) EnclosedExpr(com.github.javaparser.ast.expr.EnclosedExpr) DrlxParseUtil.stripEnclosedExpr(org.drools.modelcompiler.builder.generator.DrlxParseUtil.stripEnclosedExpr) UnaryExpr(com.github.javaparser.ast.expr.UnaryExpr) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 12 with BinaryExpr

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

the class DroolsMvelParserTest method testOOPathExprWithBackReference.

@Test
public void testOOPathExprWithBackReference() {
    String expr = "$toy : /wife/children/toys[name.length == ../../name.length]";
    DrlxExpression drlx = parseExpression(parser, expr);
    assertEquals("$toy", drlx.getBind().asString());
    Expression expression = drlx.getExpr();
    assertTrue(expression instanceof OOPathExpr);
    final OOPathChunk secondChunk = ((OOPathExpr) expression).getChunks().get(2);
    final BinaryExpr secondChunkFirstCondition = (BinaryExpr) secondChunk.getConditions().get(0).getExpr();
    final DrlNameExpr rightName = (DrlNameExpr) ((FieldAccessExpr) secondChunkFirstCondition.getRight()).getScope();
    assertEquals(2, rightName.getBackReferencesCount());
    assertEquals(expr, printNode(drlx));
}
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) OOPathExpr(org.drools.mvel.parser.ast.expr.OOPathExpr) OOPathChunk(org.drools.mvel.parser.ast.expr.OOPathChunk) DrlNameExpr(org.drools.mvel.parser.ast.expr.DrlNameExpr) 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 13 with BinaryExpr

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

the class DroolsMvelParserTest method testAndWithImplicitParameterAndParenthesisMixedLeft.

@Test
public void testAndWithImplicitParameterAndParenthesisMixedLeft() {
    String expr = "value ((> 1 && < 2) || > 3)";
    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();
    assertEquals("value", 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 third = ((BinaryExpr) comboExpr.getRight());
    assertEquals("value", toString(third.getLeft()));
    assertEquals("3", toString(third.getRight()));
    assertEquals(Operator.GREATER, 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 14 with BinaryExpr

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

the class DroolsMvelParserTest method testAndWithImplicitParameterAndParenthesisComplex.

@Test
public void testAndWithImplicitParameterAndParenthesisComplex() {
    String expr = "value ((> 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();
    assertEquals("value", 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", 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) Test(org.junit.Test)

Example 15 with BinaryExpr

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

the class DroolsMvelParserTest method testBindingOnRight.

@Test
public void testBindingOnRight() {
    String expr = "$n : name == \"Mario\" && $a : age > 20";
    DrlxExpression drlxExpression = parseExpression(parser, expr);
    Expression bExpr = drlxExpression.getExpr();
    assertTrue(bExpr instanceof BinaryExpr);
    Node left = ((BinaryExpr) bExpr).getLeft();
    assertTrue(left instanceof DrlxExpression);
    DrlxExpression leftExpr = (DrlxExpression) left;
    SimpleName leftBind = leftExpr.getBind();
    assertEquals("$n", leftBind.asString());
    Expression expression = leftExpr.getExpr();
    BinaryExpr binaryExpr = ((BinaryExpr) expression);
    assertEquals("name", toString(binaryExpr.getLeft()));
    assertEquals("\"Mario\"", toString(binaryExpr.getRight()));
    assertEquals(Operator.EQUALS, binaryExpr.getOperator());
    Node right = ((BinaryExpr) bExpr).getRight();
    assertTrue(right instanceof DrlxExpression);
    DrlxExpression rightExpr = (DrlxExpression) right;
    SimpleName rightBind = rightExpr.getBind();
    assertEquals("$a", rightBind.asString());
    BinaryExpr binaryExpr2 = ((BinaryExpr) rightExpr.getExpr());
    assertEquals("age", toString(binaryExpr2.getLeft()));
    assertEquals("20", toString(binaryExpr2.getRight()));
    assertEquals(Operator.GREATER, binaryExpr2.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) PrintUtil.printNode(org.drools.mvel.parser.printer.PrintUtil.printNode) Node(com.github.javaparser.ast.Node) SimpleName(com.github.javaparser.ast.expr.SimpleName) 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