Search in sources :

Example 1 with BinaryExpr

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

the class ASTCompilerVisitor method visit.

@Override
public DirectCompilerResult visit(IfExpressionNode n) {
    DirectCompilerResult condition = n.getCondition().accept(this);
    DirectCompilerResult thenExpr = n.getThenExpression().accept(this);
    DirectCompilerResult elseExpr = n.getElseExpression().accept(this);
    return DirectCompilerResult.of(new ConditionalExpr(new BinaryExpr(Expressions.nativeInstanceOf(Constants.BooleanT, condition.getExpression()), Expressions.reflectiveCastTo(Constants.BooleanT, condition.getExpression()), BinaryExpr.Operator.AND), new EnclosedExpr(thenExpr.getExpression()), new EnclosedExpr(elseExpr.getExpression())), // should find common type between then/else
    thenExpr.resultType).withFD(condition).withFD(thenExpr).withFD(elseExpr);
}
Also used : BinaryExpr(com.github.javaparser.ast.expr.BinaryExpr) ConditionalExpr(com.github.javaparser.ast.expr.ConditionalExpr) EnclosedExpr(com.github.javaparser.ast.expr.EnclosedExpr)

Example 2 with BinaryExpr

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

the class ConstraintParser method handleSpecialComparisonCases.

private SpecialComparisonResult handleSpecialComparisonCases(ExpressionTyper expressionTyper, BinaryExpr.Operator operator, TypedExpression left, TypedExpression right) {
    if (isLogicalOperator(operator)) {
        Expression rewrittenLeft = handleSpecialComparisonCases(expressionTyper, left);
        Expression rewrittenRight = handleSpecialComparisonCases(expressionTyper, right);
        if (rewrittenLeft != null && rewrittenRight != null) {
            return new SpecialComparisonResult(new BinaryExpr(rewrittenLeft, rewrittenRight, operator), left, right);
        }
    }
    boolean comparison = isComparisonOperator(operator);
    if ((isAnyOperandBigDecimal(left, right) || isAnyOperandBigInteger(left, right)) && comparison) {
        return compareBigDecimal(operator, left, right);
    }
    if (comparison) {
        SpecialComparisonCase methodName = specialComparisonFactory(left, right);
        return methodName.createCompareMethod(operator);
    }
    return new SpecialComparisonResult(new BinaryExpr(left.getExpression(), right.getExpression(), operator), left, right);
}
Also used : Expression(com.github.javaparser.ast.expr.Expression) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) DrlxExpression(org.drools.mvel.parser.ast.expr.DrlxExpression) BinaryExpr(com.github.javaparser.ast.expr.BinaryExpr) HalfBinaryExpr(org.drools.mvel.parser.ast.expr.HalfBinaryExpr)

Example 3 with BinaryExpr

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

the class ConstraintParser method combineExpressions.

private Expression combineExpressions(List<Expression> leftPrefixExpresssions, List<Expression> rightPrefixExpresssions, Expression combo) {
    Expression inner = combo;
    if (combo.isEnclosedExpr()) {
        EnclosedExpr enclosedExpr = combo.asEnclosedExpr();
        inner = stripEnclosedExpr(enclosedExpr);
    }
    BinaryExpr binaryExpr;
    if (inner.isBinaryExpr()) {
        binaryExpr = inner.asBinaryExpr();
    } else {
        throw new RuntimeException(combo + " is not nor contains BinaryExpr");
    }
    Expression left = binaryExpr.getLeft();
    for (Expression prefixExpression : leftPrefixExpresssions) {
        left = new BinaryExpr(prefixExpression, left, BinaryExpr.Operator.AND);
    }
    binaryExpr.setLeft(left);
    Expression right = binaryExpr.getRight();
    for (Expression prefixExpression : rightPrefixExpresssions) {
        right = new BinaryExpr(prefixExpression, right, BinaryExpr.Operator.AND);
    }
    binaryExpr.setRight(right);
    return combo;
}
Also used : Expression(com.github.javaparser.ast.expr.Expression) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) DrlxExpression(org.drools.mvel.parser.ast.expr.DrlxExpression) BinaryExpr(com.github.javaparser.ast.expr.BinaryExpr) HalfBinaryExpr(org.drools.mvel.parser.ast.expr.HalfBinaryExpr) DrlxParseUtil.stripEnclosedExpr(org.drools.modelcompiler.builder.generator.DrlxParseUtil.stripEnclosedExpr) EnclosedExpr(com.github.javaparser.ast.expr.EnclosedExpr)

Example 4 with BinaryExpr

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

the class ConstraintParser method getEqualityExpression.

private SpecialComparisonResult getEqualityExpression(TypedExpression left, TypedExpression right, BinaryExpr.Operator operator) {
    if ((isAnyOperandBigDecimal(left, right) || isAnyOperandBigInteger(left, right)) && !isAnyOperandNullLiteral(left, right)) {
        return compareBigDecimal(operator, left, right);
    }
    boolean isLeftNumber = isNumber(left);
    boolean isRightNumber = isNumber(right);
    String equalsMethod = isLeftNumber && isRightNumber ? "org.drools.modelcompiler.util.EvaluationUtil.areNumbersNullSafeEquals" : "org.drools.modelcompiler.util.EvaluationUtil.areNullSafeEquals";
    Expression leftExpr = left.uncastExpression();
    Expression rightExpr = right.uncastExpression();
    if (isLeftNumber) {
        if (isString(right)) {
            leftExpr = new BinaryExpr(new StringLiteralExpr(""), leftExpr, PLUS);
        }
    } else if (isRightNumber && isString(left)) {
        rightExpr = new BinaryExpr(new StringLiteralExpr(""), rightExpr, PLUS);
    }
    MethodCallExpr methodCallExpr = new MethodCallExpr(null, equalsMethod);
    // Avoid casts, by using an helper method we leverage autoboxing and equals
    methodCallExpr.addArgument(leftExpr);
    methodCallExpr.addArgument(rightExpr);
    Expression expression = operator == BinaryExpr.Operator.EQUALS ? methodCallExpr : new UnaryExpr(methodCallExpr, UnaryExpr.Operator.LOGICAL_COMPLEMENT);
    return new SpecialComparisonResult(expression, left, right);
}
Also used : Expression(com.github.javaparser.ast.expr.Expression) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) DrlxExpression(org.drools.mvel.parser.ast.expr.DrlxExpression) BinaryExpr(com.github.javaparser.ast.expr.BinaryExpr) HalfBinaryExpr(org.drools.mvel.parser.ast.expr.HalfBinaryExpr) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) UnaryExpr(com.github.javaparser.ast.expr.UnaryExpr) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 5 with BinaryExpr

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

the class ConstraintParser method compileToJavaRecursive.

private DrlxParseResult compileToJavaRecursive(Class<?> patternType, String bindingId, ConstraintExpression constraint, Expression drlxExpr, boolean hasBind, boolean isPositional) {
    boolean isEnclosed = false;
    SimpleName bind = null;
    if (drlxExpr instanceof FullyQualifiedInlineCastExpr) {
        drlxExpr = transformFullyQualifiedInlineCastExpr(context.getTypeResolver(), (FullyQualifiedInlineCastExpr) drlxExpr);
    }
    while (drlxExpr instanceof EnclosedExpr) {
        drlxExpr = ((EnclosedExpr) drlxExpr).getInner();
        isEnclosed = true;
    }
    if (drlxExpr instanceof DrlxExpression) {
        bind = ((DrlxExpression) drlxExpr).getBind();
        drlxExpr = ((DrlxExpression) drlxExpr).getExpr();
    }
    if (drlxExpr instanceof MethodCallExpr && !((MethodCallExpr) drlxExpr).getScope().isPresent() && ((MethodCallExpr) drlxExpr).getNameAsString().equals("eval")) {
        drlxExpr = ((MethodCallExpr) drlxExpr).getArgument(0);
    }
    if (drlxExpr instanceof BinaryExpr) {
        DrlxParseResult result = parseBinaryExpr((BinaryExpr) drlxExpr, patternType, bindingId, constraint, drlxExpr, hasBind, isPositional, isEnclosed);
        if (result instanceof SingleDrlxParseSuccess && bind != null) {
            ((SingleDrlxParseSuccess) result).setExprBinding(bind.asString());
        }
        return result;
    }
    if (drlxExpr instanceof UnaryExpr) {
        return parseUnaryExpr((UnaryExpr) drlxExpr, patternType, bindingId, constraint, drlxExpr, hasBind, isPositional);
    }
    if (drlxExpr instanceof PointFreeExpr) {
        return parsePointFreeExpr((PointFreeExpr) drlxExpr, patternType, bindingId, isPositional);
    }
    if (patternType == null && drlxExpr instanceof MethodCallExpr) {
        MethodCallExpr methodCallExpr = (MethodCallExpr) drlxExpr;
        Optional<MethodDeclaration> functionCall = packageModel.getFunctions().stream().filter(m -> m.getName().equals(methodCallExpr.getName())).findFirst();
        if (functionCall.isPresent()) {
            return parseFunctionInEval(methodCallExpr, patternType, bindingId, isPositional, functionCall);
        }
    }
    if (drlxExpr instanceof FieldAccessExpr) {
        return parseFieldAccessExpr((FieldAccessExpr) drlxExpr, patternType, bindingId);
    }
    String expression = constraint.getExpression();
    if (drlxExpr instanceof DrlNameExpr) {
        return parseNameExpr((DrlNameExpr) drlxExpr, patternType, bindingId, drlxExpr, hasBind, expression);
    }
    if (drlxExpr instanceof OOPathExpr) {
        return parseOOPathExpr((OOPathExpr) drlxExpr, patternType, bindingId, drlxExpr, hasBind, expression);
    }
    if (drlxExpr instanceof LiteralExpr) {
        Class<?> literalExpressionType = getLiteralExpressionType(((LiteralExpr) drlxExpr));
        return new SingleDrlxParseSuccess(patternType, bindingId, drlxExpr, literalExpressionType).setIsPredicate(isBooleanBoxedUnboxed(literalExpressionType));
    }
    if (patternType != null) {
        ExpressionTyperContext expressionTyperContext = new ExpressionTyperContext();
        ExpressionTyper expressionTyper = new ExpressionTyper(context, patternType, bindingId, isPositional, expressionTyperContext);
        TypedExpressionResult leftTypedExpressionResult = expressionTyper.toTypedExpression(drlxExpr);
        Optional<TypedExpression> optLeft = leftTypedExpressionResult.getTypedExpression();
        if (!optLeft.isPresent()) {
            return new DrlxParseFail();
        }
        TypedExpression left = optLeft.get();
        Expression combo = left.getExpression();
        Type exprType = left.getType();
        boolean isPredicate = isBooleanBoxedUnboxed(exprType);
        if (isPredicate) {
            combo = combineExpressions(leftTypedExpressionResult, combo);
        }
        return new SingleDrlxParseSuccess(patternType, bindingId, combo, exprType).setReactOnProperties(expressionTyperContext.getReactOnProperties()).setUsedDeclarations(expressionTyperContext.getUsedDeclarations()).setImplicitCastExpression(expressionTyperContext.getInlineCastExpression()).setNullSafeExpressions(expressionTyperContext.getNullSafeExpressions()).setIsPredicate(isPredicate);
    } else {
        final ExpressionTyperContext expressionTyperContext = new ExpressionTyperContext();
        final ExpressionTyper expressionTyper = new ExpressionTyper(context, null, bindingId, isPositional, expressionTyperContext);
        TypedExpressionResult leftTypedExpressionResult = expressionTyper.toTypedExpression(drlxExpr);
        Optional<TypedExpression> optLeft = leftTypedExpressionResult.getTypedExpression();
        if (!optLeft.isPresent()) {
            return new DrlxParseFail();
        }
        TypedExpression left = optLeft.get();
        return new SingleDrlxParseSuccess(null, bindingId, drlxExpr, left.getType()).setUsedDeclarations(expressionTyperContext.getUsedDeclarations()).setIsPredicate(true);
    }
}
Also used : HalfPointFreeExpr(org.drools.mvel.parser.ast.expr.HalfPointFreeExpr) DeclarationSpec(org.drools.modelcompiler.builder.generator.DeclarationSpec) DrlxParseUtil(org.drools.modelcompiler.builder.generator.DrlxParseUtil) Optional.of(java.util.Optional.of) PLUS(com.github.javaparser.ast.expr.BinaryExpr.Operator.PLUS) LESS(com.github.javaparser.ast.expr.BinaryExpr.Operator.LESS) ThisExpr(com.github.javaparser.ast.expr.ThisExpr) OR(com.github.javaparser.ast.expr.BinaryExpr.Operator.OR) SpecialComparisonCase.specialComparisonFactory(org.drools.modelcompiler.builder.generator.drlxparse.SpecialComparisonCase.specialComparisonFactory) ExpressionTyperContext(org.drools.modelcompiler.builder.generator.expressiontyper.ExpressionTyperContext) BigDecimal(java.math.BigDecimal) NullLiteralExpr(com.github.javaparser.ast.expr.NullLiteralExpr) ParseExpressionErrorResult(org.drools.modelcompiler.builder.errors.ParseExpressionErrorResult) EQUALS(com.github.javaparser.ast.expr.BinaryExpr.Operator.EQUALS) BigDecimalLiteralExpr(org.drools.mvel.parser.ast.expr.BigDecimalLiteralExpr) LiteralExpr(com.github.javaparser.ast.expr.LiteralExpr) CompiledExpressionResult(org.drools.mvelcompiler.CompiledExpressionResult) GREATER_EQUALS(com.github.javaparser.ast.expr.BinaryExpr.Operator.GREATER_EQUALS) NOT_EQUALS(com.github.javaparser.ast.expr.BinaryExpr.Operator.NOT_EQUALS) ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) DateUtils(org.drools.core.util.DateUtils) Arrays.asList(java.util.Arrays.asList) REMAINDER(com.github.javaparser.ast.expr.BinaryExpr.Operator.REMAINDER) BigInteger(java.math.BigInteger) Expression(com.github.javaparser.ast.expr.Expression) FlattenScope.transformFullyQualifiedInlineCastExpr(org.drools.modelcompiler.builder.generator.expressiontyper.FlattenScope.transformFullyQualifiedInlineCastExpr) BinaryExpr(com.github.javaparser.ast.expr.BinaryExpr) OOPathExpr(org.drools.mvel.parser.ast.expr.OOPathExpr) DrlxParseUtil.stripEnclosedExpr(org.drools.modelcompiler.builder.generator.DrlxParseUtil.stripEnclosedExpr) PackageModel(org.drools.modelcompiler.builder.PackageModel) NodeList(com.github.javaparser.ast.NodeList) SimpleName(com.github.javaparser.ast.expr.SimpleName) HalfBinaryExpr(org.drools.mvel.parser.ast.expr.HalfBinaryExpr) ConstraintCompiler(org.drools.mvelcompiler.ConstraintCompiler) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) FullyQualifiedInlineCastExpr(org.drools.mvel.parser.ast.expr.FullyQualifiedInlineCastExpr) MultipleDrlxParseSuccess.createMultipleDrlxParseSuccess(org.drools.modelcompiler.builder.generator.drlxparse.MultipleDrlxParseSuccess.createMultipleDrlxParseSuccess) THIS_PLACEHOLDER(org.drools.modelcompiler.builder.generator.DrlxParseUtil.THIS_PLACEHOLDER) Index(org.drools.model.Index) Collectors(java.util.stream.Collectors) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) EnclosedExpr(com.github.javaparser.ast.expr.EnclosedExpr) List(java.util.List) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) Type(java.lang.reflect.Type) UnaryExpr(com.github.javaparser.ast.expr.UnaryExpr) AND(com.github.javaparser.ast.expr.BinaryExpr.Operator.AND) Optional(java.util.Optional) FieldAccessExpr(com.github.javaparser.ast.expr.FieldAccessExpr) NodeWithArguments(com.github.javaparser.ast.nodeTypes.NodeWithArguments) DrlxExpression(org.drools.mvel.parser.ast.expr.DrlxExpression) MINUS(com.github.javaparser.ast.expr.BinaryExpr.Operator.MINUS) PrintUtil.printNode(org.drools.mvel.parser.printer.PrintUtil.printNode) StringUtils.lcFirstForBean(org.drools.core.util.StringUtils.lcFirstForBean) CastExpr(com.github.javaparser.ast.expr.CastExpr) ArrayList(java.util.ArrayList) PointFreeExpr(org.drools.mvel.parser.ast.expr.PointFreeExpr) NodeWithOptionalScope(com.github.javaparser.ast.nodeTypes.NodeWithOptionalScope) BigIntegerLiteralExpr(org.drools.mvel.parser.ast.expr.BigIntegerLiteralExpr) TypedExpressionResult(org.drools.modelcompiler.builder.generator.expressiontyper.TypedExpressionResult) DrlxParseUtil.getLiteralExpressionType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.getLiteralExpressionType) VariableUsedInBindingError(org.drools.modelcompiler.builder.errors.VariableUsedInBindingError) MULTIPLY(com.github.javaparser.ast.expr.BinaryExpr.Operator.MULTIPLY) DrlxParseUtil.toClassOrInterfaceType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.toClassOrInterfaceType) DrlNameExpr(org.drools.mvel.parser.ast.expr.DrlNameExpr) DrlxParseUtil.createConstraintCompiler(org.drools.modelcompiler.builder.generator.DrlxParseUtil.createConstraintCompiler) NameExpr(com.github.javaparser.ast.expr.NameExpr) LESS_EQUALS(com.github.javaparser.ast.expr.BinaryExpr.Operator.LESS_EQUALS) PrintUtil(org.drools.mvel.parser.printer.PrintUtil) ExpressionTyper(org.drools.modelcompiler.builder.generator.expressiontyper.ExpressionTyper) RuleContext(org.drools.modelcompiler.builder.generator.RuleContext) DIVIDE(com.github.javaparser.ast.expr.BinaryExpr.Operator.DIVIDE) DrlxParseUtil.isBooleanBoxedUnboxed(org.drools.modelcompiler.builder.generator.DrlxParseUtil.isBooleanBoxedUnboxed) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) ModelGenerator(org.drools.modelcompiler.builder.generator.ModelGenerator) Collections(java.util.Collections) GREATER(com.github.javaparser.ast.expr.BinaryExpr.Operator.GREATER) SimpleName(com.github.javaparser.ast.expr.SimpleName) DrlxExpression(org.drools.mvel.parser.ast.expr.DrlxExpression) ExpressionTyperContext(org.drools.modelcompiler.builder.generator.expressiontyper.ExpressionTyperContext) OOPathExpr(org.drools.mvel.parser.ast.expr.OOPathExpr) NullLiteralExpr(com.github.javaparser.ast.expr.NullLiteralExpr) BigDecimalLiteralExpr(org.drools.mvel.parser.ast.expr.BigDecimalLiteralExpr) LiteralExpr(com.github.javaparser.ast.expr.LiteralExpr) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) BigIntegerLiteralExpr(org.drools.mvel.parser.ast.expr.BigIntegerLiteralExpr) FieldAccessExpr(com.github.javaparser.ast.expr.FieldAccessExpr) ExpressionTyper(org.drools.modelcompiler.builder.generator.expressiontyper.ExpressionTyper) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) DrlNameExpr(org.drools.mvel.parser.ast.expr.DrlNameExpr) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) BinaryExpr(com.github.javaparser.ast.expr.BinaryExpr) HalfBinaryExpr(org.drools.mvel.parser.ast.expr.HalfBinaryExpr) HalfPointFreeExpr(org.drools.mvel.parser.ast.expr.HalfPointFreeExpr) PointFreeExpr(org.drools.mvel.parser.ast.expr.PointFreeExpr) UnaryExpr(com.github.javaparser.ast.expr.UnaryExpr) TypedExpressionResult(org.drools.modelcompiler.builder.generator.expressiontyper.TypedExpressionResult) Type(java.lang.reflect.Type) DrlxParseUtil.getLiteralExpressionType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.getLiteralExpressionType) DrlxParseUtil.toClassOrInterfaceType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.toClassOrInterfaceType) Expression(com.github.javaparser.ast.expr.Expression) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) DrlxExpression(org.drools.mvel.parser.ast.expr.DrlxExpression) DrlxParseUtil.stripEnclosedExpr(org.drools.modelcompiler.builder.generator.DrlxParseUtil.stripEnclosedExpr) EnclosedExpr(com.github.javaparser.ast.expr.EnclosedExpr) FlattenScope.transformFullyQualifiedInlineCastExpr(org.drools.modelcompiler.builder.generator.expressiontyper.FlattenScope.transformFullyQualifiedInlineCastExpr) FullyQualifiedInlineCastExpr(org.drools.mvel.parser.ast.expr.FullyQualifiedInlineCastExpr) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

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