Search in sources :

Example 6 with BinaryExpr

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

the class ConstraintParser method compareBigDecimal.

public static Expression compareBigDecimal(BinaryExpr.Operator operator, TypedExpression left, TypedExpression right) {
    left.setExpression(convertExpressionToBigDecimal(left));
    right.setExpression(convertExpressionToBigDecimal(right));
    final MethodCallExpr methodCallExpr = new MethodCallExpr(left.getExpression(), "compareTo");
    methodCallExpr.addArgument(right.getExpression());
    return new BinaryExpr(methodCallExpr, new IntegerLiteralExpr(0), operator);
}
Also used : IntegerLiteralExpr(org.drools.javaparser.ast.expr.IntegerLiteralExpr) BinaryExpr(org.drools.javaparser.ast.expr.BinaryExpr) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr)

Example 7 with BinaryExpr

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

the class ConstraintParser method handleSpecialComparisonCases.

private static Expression handleSpecialComparisonCases(BinaryExpr.Operator operator, TypedExpression left, TypedExpression right) {
    if (isComparisonOperator(operator)) {
        MethodCallExpr compareMethod = null;
        if (left.getType() == String.class && right.getType() == String.class) {
            compareMethod = new MethodCallExpr(null, "org.drools.modelcompiler.util.EvaluationUtil.compareStringsAsNumbers");
        } else if (Comparable.class.isAssignableFrom(left.getType()) && Comparable.class.isAssignableFrom(right.getType())) {
            compareMethod = new MethodCallExpr(null, "org.drools.modelcompiler.util.EvaluationUtil.compare");
        }
        if (compareMethod != null) {
            compareMethod.addArgument(left.getExpression());
            compareMethod.addArgument(right.getExpression());
            compareMethod.addArgument(new StringLiteralExpr(operator.asString()));
            return compareMethod;
        }
    }
    if (isAnyOperandBigDecimal(left, right) && (isComparisonOperator(operator))) {
        return compareBigDecimal(operator, left, right);
    }
    return new BinaryExpr(left.getExpression(), right.getExpression(), operator);
}
Also used : BinaryExpr(org.drools.javaparser.ast.expr.BinaryExpr) StringLiteralExpr(org.drools.javaparser.ast.expr.StringLiteralExpr) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr)

Example 8 with BinaryExpr

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

the class ExpressionTyper method extractPrefixExpressions.

private void extractPrefixExpressions(NullSafeFieldAccessExpr drlxExpr, Expression previous) {
    final BinaryExpr prefixExpression = new BinaryExpr(previous, new NullLiteralExpr(), BinaryExpr.Operator.NOT_EQUALS);
    prefixExpressions.add(prefixExpression);
    final Expression scope = drlxExpr.getScope();
    if (scope != null) {
        final Optional<TypedExpression> typedExpression1 = toTypedExpressionRec(scope);
        typedExpression1.ifPresent(te -> {
            final Expression expression = te.getExpression();
            final BinaryExpr notNullScope = new BinaryExpr(expression, new NullLiteralExpr(), BinaryExpr.Operator.NOT_EQUALS);
            prefixExpressions.add(0, notNullScope);
        });
    }
}
Also used : NullLiteralExpr(org.drools.javaparser.ast.expr.NullLiteralExpr) Expression(org.drools.javaparser.ast.expr.Expression) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) BinaryExpr(org.drools.javaparser.ast.expr.BinaryExpr) HalfBinaryExpr(org.drools.javaparser.ast.drlx.expr.HalfBinaryExpr) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression)

Example 9 with BinaryExpr

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

the class ExpressionTyper method processFirstNode.

private Optional<TypedExpressionCursor> processFirstNode(Expression drlxExpr, List<Node> childNodes, Node firstNode, boolean isInLineCast, Class<?> originalTypeCursor) {
    final Optional<TypedExpressionCursor> result;
    if (firstNode instanceof ThisExpr || (firstNode instanceof NameExpr && firstNode.toString().equals(bindingId))) {
        result = of(thisExpr(drlxExpr, childNodes, isInLineCast, originalTypeCursor));
    } else if (firstNode instanceof NameExpr) {
        result = nameExpr(drlxExpr, (NameExpr) firstNode, isInLineCast, originalTypeCursor);
    } else if (firstNode instanceof FieldAccessExpr && ((FieldAccessExpr) firstNode).getScope() instanceof ThisExpr) {
        result = of(fieldAccessExpr((FieldAccessExpr) firstNode, originalTypeCursor));
    } else if (firstNode instanceof MethodCallExpr) {
        result = of(methodCallExpr((MethodCallExpr) firstNode, originalTypeCursor, new NameExpr("_this")));
    } else if (firstNode instanceof StringLiteralExpr) {
        result = of(stringLiteralExpr((StringLiteralExpr) firstNode));
    } else if (firstNode instanceof EnclosedExpr) {
        result = processFirstNode(drlxExpr, childNodes, ((EnclosedExpr) firstNode).getInner(), isInLineCast, originalTypeCursor);
    } else if (firstNode instanceof CastExpr) {
        result = castExpr((CastExpr) firstNode, drlxExpr, childNodes, isInLineCast, originalTypeCursor);
    } else if (firstNode instanceof ArrayCreationExpr) {
        result = of(arrayCreationExpr(((ArrayCreationExpr) firstNode)));
    } else if (firstNode instanceof BinaryExpr) {
        result = of(binaryExpr((BinaryExpr) firstNode));
    } else {
        result = of(new TypedExpressionCursor((Expression) firstNode, getExpressionType(ruleContext, ruleContext.getTypeResolver(), (Expression) firstNode, context.getUsedDeclarations())));
    }
    result.ifPresent(te -> {
        if (drlxExpr instanceof NullSafeFieldAccessExpr) {
            extractPrefixExpressions((NullSafeFieldAccessExpr) drlxExpr, te.expressionCursor);
        }
    });
    return result.map(te -> {
        if (isInLineCast) {
            Expression exprWithInlineCast = addCastToExpression(te.typeCursor, te.expressionCursor, isInLineCast);
            return new TypedExpressionCursor(exprWithInlineCast, te.typeCursor);
        } else {
            return te;
        }
    });
}
Also used : BinaryExpr(org.drools.javaparser.ast.expr.BinaryExpr) HalfBinaryExpr(org.drools.javaparser.ast.drlx.expr.HalfBinaryExpr) NameExpr(org.drools.javaparser.ast.expr.NameExpr) StringLiteralExpr(org.drools.javaparser.ast.expr.StringLiteralExpr) NullSafeFieldAccessExpr(org.drools.javaparser.ast.drlx.expr.NullSafeFieldAccessExpr) Expression(org.drools.javaparser.ast.expr.Expression) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) CastExpr(org.drools.javaparser.ast.expr.CastExpr) InlineCastExpr(org.drools.javaparser.ast.drlx.expr.InlineCastExpr) NullSafeFieldAccessExpr(org.drools.javaparser.ast.drlx.expr.NullSafeFieldAccessExpr) FieldAccessExpr(org.drools.javaparser.ast.expr.FieldAccessExpr) EnclosedExpr(org.drools.javaparser.ast.expr.EnclosedExpr) ArrayCreationExpr(org.drools.javaparser.ast.expr.ArrayCreationExpr) ThisExpr(org.drools.javaparser.ast.expr.ThisExpr) DrlxParseUtil.nameExprToMethodCallExpr(org.drools.modelcompiler.builder.generator.DrlxParseUtil.nameExprToMethodCallExpr) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr)

Example 10 with BinaryExpr

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

the class AccumulateVisitor method visit.

protected Optional<AccumulateVisitorPatternDSL.NewBinding> visit(RuleContext context, AccumulateDescr.AccumulateFunctionCallDescr function, MethodCallExpr accumulateDSL, PatternDescr basePattern, boolean inputPatternHasConstraints) {
    context.pushExprPointer(accumulateDSL::addArgument);
    final MethodCallExpr functionDSL = new MethodCallExpr(null, "accFunction");
    final String expression = function.getParams()[0];
    final Expression expr = DrlxParseUtil.parseExpression(expression).getExpr();
    final String bindingId = Optional.ofNullable(function.getBind()).orElse(basePattern.getIdentifier());
    Optional<AccumulateVisitorPatternDSL.NewBinding> newBinding = Optional.empty();
    if (expr instanceof BinaryExpr) {
        final DrlxParseResult parseResult = new ConstraintParser(context, packageModel).drlxParse(Object.class, bindingId, expression);
        newBinding = parseResult.acceptWithReturnValue(new ParseResultVisitor<Optional<AccumulateVisitorPatternDSL.NewBinding>>() {

            @Override
            public Optional<AccumulateVisitorPatternDSL.NewBinding> onSuccess(DrlxParseSuccess drlxParseResult) {
                final AccumulateFunction accumulateFunction = AccumulateVisitor.this.getAccumulateFunction(function, drlxParseResult.getExprType());
                final String bindExpressionVariable = context.getExprId(accumulateFunction.getResultType(), drlxParseResult.getLeft().toString());
                drlxParseResult.setExprBinding(bindExpressionVariable);
                context.addDeclarationReplacing(new DeclarationSpec(drlxParseResult.getPatternBinding(), drlxParseResult.getExprType()));
                functionDSL.addArgument(new ClassExpr(toType(accumulateFunction.getClass())));
                final MethodCallExpr newBindingFromBinary = AccumulateVisitor.this.buildBinding(bindExpressionVariable, drlxParseResult.getUsedDeclarations(), drlxParseResult.getExpr());
                context.addDeclarationReplacing(new DeclarationSpec(bindExpressionVariable, drlxParseResult.getExprType()));
                functionDSL.addArgument(new NameExpr(toVar(bindExpressionVariable)));
                return Optional.of(new AccumulateVisitorPatternDSL.NewBinding(Optional.empty(), newBindingFromBinary));
            }

            @Override
            public Optional<AccumulateVisitorPatternDSL.NewBinding> onFail(DrlxParseFail failure) {
                return Optional.empty();
            }
        });
    } else if (expr instanceof MethodCallExpr) {
        final DrlxParseUtil.RemoveRootNodeResult methodCallWithoutRootNode = DrlxParseUtil.removeRootNode(expr);
        final String rootNodeName = getRootNodeName(methodCallWithoutRootNode);
        final TypedExpression typedExpression = parseMethodCallType(context, rootNodeName, methodCallWithoutRootNode.getWithoutRootNode());
        final Class<?> methodCallExprType = typedExpression.getType();
        final AccumulateFunction accumulateFunction = getAccumulateFunction(function, methodCallExprType);
        final Class accumulateFunctionResultType = accumulateFunction.getResultType();
        functionDSL.addArgument(new ClassExpr(toType(accumulateFunction.getClass())));
        // Every expression in an accumulate function gets transformed in a bind expression with a generated id
        // Then the accumulate function will have that binding expression as a source
        final String bindExpressionVariable = context.getExprId(accumulateFunctionResultType, typedExpression.toString());
        Expression withThis = DrlxParseUtil.prepend(DrlxParseUtil._THIS_EXPR, typedExpression.getExpression());
        DrlxParseSuccess result = new DrlxParseSuccess(accumulateFunctionResultType, "", rootNodeName, withThis, accumulateFunctionResultType).setLeft(typedExpression).setExprBinding(bindExpressionVariable);
        final MethodCallExpr binding = expressionBuilder.buildBinding(result);
        newBinding = Optional.of(new AccumulateVisitorPatternDSL.NewBinding(Optional.of(result.getPatternBinding()), binding));
        context.addDeclarationReplacing(new DeclarationSpec(bindExpressionVariable, methodCallExprType));
        functionDSL.addArgument(new NameExpr(toVar(bindExpressionVariable)));
        context.addDeclarationReplacing(new DeclarationSpec(bindingId, accumulateFunctionResultType));
    } else if (expr instanceof NameExpr) {
        final Class<?> declarationClass = context.getDeclarationById(expr.toString()).orElseThrow(RuntimeException::new).getDeclarationClass();
        final String nameExpr = ((NameExpr) expr).getName().asString();
        final AccumulateFunction accumulateFunction = getAccumulateFunction(function, declarationClass);
        functionDSL.addArgument(new ClassExpr(toType(accumulateFunction.getClass())));
        functionDSL.addArgument(new NameExpr(toVar(nameExpr)));
        Class accumulateFunctionResultType = accumulateFunction.getResultType();
        if (accumulateFunctionResultType == Comparable.class && (Comparable.class.isAssignableFrom(declarationClass) || declarationClass.isPrimitive())) {
            accumulateFunctionResultType = declarationClass;
        }
        context.addDeclarationReplacing(new DeclarationSpec(bindingId, accumulateFunctionResultType));
    } else {
        throw new UnsupportedOperationException("Unsupported expression " + expr);
    }
    final MethodCallExpr asDSL = new MethodCallExpr(functionDSL, "as");
    asDSL.addArgument(new NameExpr(toVar(bindingId)));
    accumulateDSL.addArgument(asDSL);
    context.popExprPointer();
    return newBinding;
}
Also used : ConstraintParser(org.drools.modelcompiler.builder.generator.drlxparse.ConstraintParser) BinaryExpr(org.drools.javaparser.ast.expr.BinaryExpr) NameExpr(org.drools.javaparser.ast.expr.NameExpr) DrlxParseFail(org.drools.modelcompiler.builder.generator.drlxparse.DrlxParseFail) DeclarationSpec(org.drools.modelcompiler.builder.generator.DeclarationSpec) Expression(org.drools.javaparser.ast.expr.Expression) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) DrlxParseSuccess(org.drools.modelcompiler.builder.generator.drlxparse.DrlxParseSuccess) ParseResultVisitor(org.drools.modelcompiler.builder.generator.drlxparse.ParseResultVisitor) DrlxParseResult(org.drools.modelcompiler.builder.generator.drlxparse.DrlxParseResult) ClassExpr(org.drools.javaparser.ast.expr.ClassExpr) AccumulateFunction(org.kie.api.runtime.rule.AccumulateFunction) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr)

Aggregations

BinaryExpr (org.drools.javaparser.ast.expr.BinaryExpr)10 Expression (org.drools.javaparser.ast.expr.Expression)7 MethodCallExpr (org.drools.javaparser.ast.expr.MethodCallExpr)7 HalfBinaryExpr (org.drools.javaparser.ast.drlx.expr.HalfBinaryExpr)5 NameExpr (org.drools.javaparser.ast.expr.NameExpr)5 StringLiteralExpr (org.drools.javaparser.ast.expr.StringLiteralExpr)5 TypedExpression (org.drools.modelcompiler.builder.generator.TypedExpression)5 EnclosedExpr (org.drools.javaparser.ast.expr.EnclosedExpr)4 FieldAccessExpr (org.drools.javaparser.ast.expr.FieldAccessExpr)4 UnaryExpr (org.drools.javaparser.ast.expr.UnaryExpr)4 Optional (java.util.Optional)3 DrlxExpression (org.drools.javaparser.ast.drlx.expr.DrlxExpression)3 CastExpr (org.drools.javaparser.ast.expr.CastExpr)3 IntegerLiteralExpr (org.drools.javaparser.ast.expr.IntegerLiteralExpr)3 LiteralExpr (org.drools.javaparser.ast.expr.LiteralExpr)3 ThisExpr (org.drools.javaparser.ast.expr.ThisExpr)3 DeclarationSpec (org.drools.modelcompiler.builder.generator.DeclarationSpec)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Node (org.drools.javaparser.ast.Node)2