Search in sources :

Example 36 with BinaryExpr

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());
}
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 37 with BinaryExpr

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());
}
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 38 with BinaryExpr

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();
}
Also used : InvalidExpressionErrorResult(org.drools.modelcompiler.builder.errors.InvalidExpressionErrorResult) Expression(com.github.javaparser.ast.expr.Expression) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) DrlNameExpr(org.drools.mvel.parser.ast.expr.DrlNameExpr) BinaryExpr(com.github.javaparser.ast.expr.BinaryExpr) LiteralExpr(com.github.javaparser.ast.expr.LiteralExpr)

Example 39 with BinaryExpr

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

Example 40 with BinaryExpr

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();
}
Also used : HalfBinaryExpr(org.drools.mvel.parser.ast.expr.HalfBinaryExpr) 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) NullSafeFieldAccessExpr(org.drools.mvel.parser.ast.expr.NullSafeFieldAccessExpr) FieldAccessExpr(com.github.javaparser.ast.expr.FieldAccessExpr) EnclosedExpr(com.github.javaparser.ast.expr.EnclosedExpr) NullSafeFieldAccessExpr(org.drools.mvel.parser.ast.expr.NullSafeFieldAccessExpr)

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