Search in sources :

Example 16 with DrlxExpression

use of org.drools.mvel.parser.ast.expr.DrlxExpression 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());
}
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) Test(org.junit.Test)

Example 17 with DrlxExpression

use of org.drools.mvel.parser.ast.expr.DrlxExpression in project drools by kiegroup.

the class DroolsMvelParserTest method testOOPathExprWithDeclaration.

@Test
public void testOOPathExprWithDeclaration() {
    String expr = "$toy : /wife/children[age > 10]/toys";
    DrlxExpression drlx = parseExpression(parser, expr);
    assertEquals("$toy", drlx.getBind().asString());
    Expression expression = drlx.getExpr();
    assertTrue(expression instanceof OOPathExpr);
    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) DrlxExpression(org.drools.mvel.parser.ast.expr.DrlxExpression) Test(org.junit.Test)

Example 18 with DrlxExpression

use of org.drools.mvel.parser.ast.expr.DrlxExpression in project drools by kiegroup.

the class DroolsMvelParserTest method testMapInitializationEmpty.

@Test
public void testMapInitializationEmpty() {
    String expr = "countItems([])";
    DrlxExpression drlx = parseExpression(parser, expr);
    assertEquals(expr, printNode(drlx));
}
Also used : DrlxExpression(org.drools.mvel.parser.ast.expr.DrlxExpression) Test(org.junit.Test)

Example 19 with DrlxExpression

use of org.drools.mvel.parser.ast.expr.DrlxExpression in project drools by kiegroup.

the class ConstraintPrintVisitor method visit.

@Override
public void visit(OOPathExpr oopathExpr, Void arg) {
    printComment(oopathExpr.getComment(), arg);
    NodeList<OOPathChunk> chunks = oopathExpr.getChunks();
    for (int i = 0; i < chunks.size(); i++) {
        final OOPathChunk chunk = chunks.get(i);
        printer.print(chunk.isSingleValue() ? "." : "/");
        chunk.accept(this, arg);
        printer.print(chunk.getField().toString());
        if (chunk.getInlineCast() != null) {
            printer.print("#");
            chunk.getInlineCast().accept(this, arg);
        }
        List<DrlxExpression> condition = chunk.getConditions();
        final Iterator<DrlxExpression> iterator = condition.iterator();
        if (!condition.isEmpty()) {
            printer.print("[");
            DrlxExpression first = iterator.next();
            first.accept(this, arg);
            while (iterator.hasNext()) {
                printer.print(",");
                iterator.next().accept(this, arg);
            }
            printer.print("]");
        }
    }
}
Also used : OOPathChunk(org.drools.mvel.parser.ast.expr.OOPathChunk) DrlxExpression(org.drools.mvel.parser.ast.expr.DrlxExpression)

Example 20 with DrlxExpression

use of org.drools.mvel.parser.ast.expr.DrlxExpression 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();
}
Also used : Expression(com.github.javaparser.ast.expr.Expression) DrlxExpression(org.drools.mvel.parser.ast.expr.DrlxExpression) HalfBinaryExpr(org.drools.mvel.parser.ast.expr.HalfBinaryExpr) BinaryExpr(com.github.javaparser.ast.expr.BinaryExpr) DrlxExpression(org.drools.mvel.parser.ast.expr.DrlxExpression) SimpleName(com.github.javaparser.ast.expr.SimpleName) NameExpr(com.github.javaparser.ast.expr.NameExpr)

Aggregations

DrlxExpression (org.drools.mvel.parser.ast.expr.DrlxExpression)20 Test (org.junit.Test)15 Expression (com.github.javaparser.ast.expr.Expression)13 DrlxParser.parseExpression (org.drools.mvel.parser.DrlxParser.parseExpression)10 BinaryExpr (com.github.javaparser.ast.expr.BinaryExpr)8 HalfBinaryExpr (org.drools.mvel.parser.ast.expr.HalfBinaryExpr)8 SimpleName (com.github.javaparser.ast.expr.SimpleName)7 OOPathExpr (org.drools.mvel.parser.ast.expr.OOPathExpr)6 EnclosedExpr (com.github.javaparser.ast.expr.EnclosedExpr)3 PrintUtil.printNode (org.drools.mvel.parser.printer.PrintUtil.printNode)3 Node (com.github.javaparser.ast.Node)2 FieldAccessExpr (com.github.javaparser.ast.expr.FieldAccessExpr)2 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)2 NameExpr (com.github.javaparser.ast.expr.NameExpr)2 TypedExpression (org.drools.modelcompiler.builder.generator.TypedExpression)2 DrlNameExpr (org.drools.mvel.parser.ast.expr.DrlNameExpr)2 NodeList (com.github.javaparser.ast.NodeList)1 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)1 AND (com.github.javaparser.ast.expr.BinaryExpr.Operator.AND)1 DIVIDE (com.github.javaparser.ast.expr.BinaryExpr.Operator.DIVIDE)1