Skip to content

Commit

Permalink
Merge branch 'apache:main' into incubator-kie-issues#1748
Browse files Browse the repository at this point in the history
  • Loading branch information
ChinchuAjith authored Mar 7, 2025
2 parents 84a703e + 866a77a commit cc0d641
Show file tree
Hide file tree
Showing 20 changed files with 1,612 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import java.io.Serializable;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Arrays;
Expand Down Expand Up @@ -265,6 +266,7 @@ private static boolean shouldCoerceBToString(TypedExpression a, TypedExpression
b.getType() != String.class &&
b.getType() != Object.class &&
b.getType() != BigDecimal.class &&
b.getType() != BigInteger.class &&
!(Map.class.isAssignableFrom(b.getRawClass())) &&
!(b.getExpression() instanceof NullLiteralExpr) &&
b.getType() != Serializable.class;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
Expand Down Expand Up @@ -92,7 +93,7 @@
import org.drools.mvel.parser.printer.PrintUtil;
import org.drools.mvelcompiler.CompiledExpressionResult;
import org.drools.mvelcompiler.ConstraintCompiler;
import org.drools.mvelcompiler.util.BigDecimalArgumentCoercion;
import org.drools.mvelcompiler.util.BigNumberArgumentCoercion;
import org.drools.util.ClassUtils;
import org.drools.util.MethodUtils;
import org.drools.util.Pair;
Expand Down Expand Up @@ -827,17 +828,23 @@ public static Expression convertArithmeticBinaryToMethodCall(BinaryExpr binaryEx
}

/*
* BigDecimal arithmetic operations should be converted to method calls. We may also apply this to BigInteger.
* BigDecimal/BigInteger arithmetic operations should be converted to method calls.
*/
public static boolean shouldConvertArithmeticBinaryToMethodCall(BinaryExpr.Operator operator, java.lang.reflect.Type leftType, java.lang.reflect.Type rightType) {
return isArithmeticOperator(operator) && (leftType.equals(BigDecimal.class) || rightType.equals(BigDecimal.class));
return isArithmeticOperator(operator) && (leftType.equals(BigDecimal.class) || rightType.equals(BigDecimal.class) || leftType.equals(BigInteger.class) || rightType.equals(BigInteger.class));
}

/*
* After arithmetic to method call conversion, BigDecimal should take precedence regardless of left or right. We may also apply this to BigInteger.
* After arithmetic to method call conversion, BigDecimal/BigInteger should take precedence regardless of left or right.
*/
public static java.lang.reflect.Type getBinaryTypeAfterConversion(java.lang.reflect.Type leftType, java.lang.reflect.Type rightType) {
return (leftType.equals(BigDecimal.class) || rightType.equals(BigDecimal.class)) ? BigDecimal.class : leftType;
if (leftType.equals(BigDecimal.class) || rightType.equals(BigDecimal.class)) {
return BigDecimal.class;
} else if (leftType.equals(BigInteger.class) || rightType.equals(BigInteger.class)) {
return BigInteger.class;
} else {
return leftType;
}
}

private java.lang.reflect.Type getBinaryType(TypedExpression leftTypedExpression, TypedExpression rightTypedExpression, Operator operator) {
Expand Down Expand Up @@ -906,7 +913,7 @@ private TypedExpressionCursor parseMethodCallExpr(MethodCallExpr methodCallExpr,
RuleContext.FunctionType typedDeclaredFunction = functionType.get();
methodCallExpr.setScope(null);

promoteBigDecimalParameters(methodCallExpr, argsType, typedDeclaredFunction.getArgumentsType().toArray(new Class[0]));
promoteBigNumberParameters(methodCallExpr, argsType, typedDeclaredFunction.getArgumentsType().toArray(new Class[0]));

return new TypedExpressionCursor(methodCallExpr, typedDeclaredFunction.getReturnType());
}
Expand All @@ -917,7 +924,7 @@ private TypedExpressionCursor parseMethodCallExpr(MethodCallExpr methodCallExpr,
}

Class<?>[] actualArgumentTypes = m.getParameterTypes();
promoteBigDecimalParameters(methodCallExpr, argsType, actualArgumentTypes);
promoteBigNumberParameters(methodCallExpr, argsType, actualArgumentTypes);

if (methodName.equals("get") && List.class.isAssignableFrom(rawClassCursor) && originalTypeCursor instanceof ParameterizedType) {
return new TypedExpressionCursor(methodCallExpr, ((ParameterizedType) originalTypeCursor).getActualTypeArguments()[0]);
Expand All @@ -926,7 +933,7 @@ private TypedExpressionCursor parseMethodCallExpr(MethodCallExpr methodCallExpr,
return new TypedExpressionCursor(methodCallExpr, actualTypeFromGenerics(originalTypeCursor, m.getGenericReturnType(), rawClassCursor));
}

private void promoteBigDecimalParameters(MethodCallExpr methodCallExpr, Class[] argsType, Class<?>[] actualArgumentTypes) {
private void promoteBigNumberParameters(MethodCallExpr methodCallExpr, Class[] argsType, Class<?>[] actualArgumentTypes) {
if (actualArgumentTypes.length == argsType.length && actualArgumentTypes.length == methodCallExpr.getArguments().size()) {
for (int i = 0; i < argsType.length; i++) {
Class<?> argumentType = argsType[i];
Expand All @@ -938,7 +945,7 @@ private void promoteBigDecimalParameters(MethodCallExpr methodCallExpr, Class[]
// unbind the original argumentExpression first, otherwise setArgument() will remove the argumentExpression from coercedExpression.childrenNodes
// It will result in failing to find DrlNameExpr in AST at DrlsParseUtil.transformDrlNameExprToNameExpr()
methodCallExpr.replace(argumentExpression, new NameExpr("placeholder"));
Expression coercedExpression = new BigDecimalArgumentCoercion().coercedArgument(argumentType, actualArgumentType, argumentExpression);
Expression coercedExpression = new BigNumberArgumentCoercion().coercedArgument(argumentType, actualArgumentType, argumentExpression);
methodCallExpr.setArgument(i, coercedExpression);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.drools.model.codegen.execmodel.bigintegertest;

import java.math.BigInteger;

public class BIFact {

private BigInteger value1;
private BigInteger value2;

public BigInteger getValue1() {
return value1;
}

public void setValue1(BigInteger value1) {
this.value1 = value1;
}

public BigInteger getValue2() {
return value2;
}

public void setValue2(BigInteger value2) {
this.value2 = value2;
}
}
Loading

0 comments on commit cc0d641

Please sign in to comment.