Skip to content

Commit

Permalink
[incubator-kie-drools-6180] accumulate min doesn't evaluate correctly…
Browse files Browse the repository at this point in the history
… with more than 18 digits BigDecimal
  • Loading branch information
tkobayas committed Dec 6, 2024
1 parent 1eee5de commit 14db10e
Show file tree
Hide file tree
Showing 7 changed files with 562 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ public static String getFunctionName(Supplier<Class<?>> exprClassSupplier, Strin
functionName = "maxI";
} else if (exprClass == Long.class) {
functionName = "maxL";
} else if (exprClass == BigInteger.class) {
functionName = "maxBI";
} else if (exprClass == BigDecimal.class) {
functionName = "maxBD";
} else if (Number.class.isAssignableFrom( exprClass )) {
functionName = "maxN";
}
Expand All @@ -64,6 +68,10 @@ public static String getFunctionName(Supplier<Class<?>> exprClassSupplier, Strin
functionName = "minI";
} else if (exprClass == Long.class) {
functionName = "minL";
} else if (exprClass == BigInteger.class) {
functionName = "minBI";
} else if (exprClass == BigDecimal.class) {
functionName = "minBD";
} else if (Number.class.isAssignableFrom( exprClass )) {
functionName = "minN";
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/**
* 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.core.base.accumulators;

import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.math.BigDecimal;

/**
* An implementation of an accumulator capable of calculating maximum values
*/
public class BigDecimalMaxAccumulateFunction extends AbstractAccumulateFunction<BigDecimalMaxAccumulateFunction.MaxData> {

public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {

}

public void writeExternal(ObjectOutput out) throws IOException {

}

protected static class MaxData implements Externalizable {
public BigDecimal max = null;

public MaxData() {}

public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
max = (BigDecimal) in.readObject();
}

public void writeExternal(ObjectOutput out) throws IOException {
out.writeObject(max);
}

@Override
public String toString() {
return "max";
}
}

public MaxData createContext() {
return new MaxData();
}

public void init(MaxData data) {
data.max = null;
}

public void accumulate(MaxData data,
Object value) {
if (value != null) {
BigDecimal bdValue = (BigDecimal) value;
data.max = data.max == null || data.max.compareTo(bdValue) < 0 ?
bdValue :
data.max;
}
}

public void reverse(MaxData data,
Object value) {
}

@Override
public boolean tryReverse( MaxData data, Object value ) {
if (value != null) {
return data.max.compareTo((BigDecimal) value) > 0;
}
return true;
}

public Object getResult(MaxData data) {
return data.max;
}

public boolean supportsReverse() {
return false;
}

public Class<?> getResultType() {
return BigDecimal.class;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/**
* 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.core.base.accumulators;

import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.math.BigDecimal;

/**
* An implementation of an accumulator capable of calculating minimum values
*/
public class BigDecimalMinAccumulateFunction extends AbstractAccumulateFunction<BigDecimalMinAccumulateFunction.MinData> {

public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
}

public void writeExternal(ObjectOutput out) throws IOException {
}

protected static class MinData implements Externalizable {
public BigDecimal min = null;

public MinData() {}

public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
min = (BigDecimal) in.readObject();
}

public void writeExternal(ObjectOutput out) throws IOException {
out.writeObject(min);
}

@Override
public String toString() {
return "min";
}
}

public MinData createContext() {
return new MinData();
}

public void init(MinData data) {
data.min = null;
}

public void accumulate(MinData data,
Object value) {
if (value != null) {
BigDecimal bdValue = (BigDecimal) value;
data.min = data.min == null || data.min.compareTo(bdValue) > 0 ?
bdValue :
data.min;
}
}

@Override
public boolean tryReverse( MinData data, Object value ) {
if (value != null) {
return data.min.compareTo((BigDecimal) value) < 0;
}
return true;
}

public void reverse(MinData data,
Object value) {
}

public Object getResult(MinData data) {
return data.min;
}

public boolean supportsReverse() {
return false;
}

public Class<?> getResultType() {
return BigDecimal.class;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/**
* 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.core.base.accumulators;

import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.math.BigInteger;

/**
* An implementation of an accumulator capable of calculating maximum values
*/
public class BigIntegerMaxAccumulateFunction extends AbstractAccumulateFunction<BigIntegerMaxAccumulateFunction.MaxData> {

public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {

}

public void writeExternal(ObjectOutput out) throws IOException {

}

protected static class MaxData implements Externalizable {
public BigInteger max = null;

public MaxData() {}

public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
max = (BigInteger) in.readObject();
}

public void writeExternal(ObjectOutput out) throws IOException {
out.writeObject(max);
}

@Override
public String toString() {
return "max";
}
}

public MaxData createContext() {
return new MaxData();
}

public void init(MaxData data) {
data.max = null;
}

public void accumulate(MaxData data,
Object value) {
if (value != null) {
BigInteger biValue = (BigInteger) value;
data.max = data.max == null || data.max.compareTo(biValue) < 0 ?
biValue :
data.max;
}
}

public void reverse(MaxData data,
Object value) {
}

@Override
public boolean tryReverse( MaxData data, Object value ) {
if (value != null) {
return data.max.compareTo((BigInteger) value) > 0;
}
return true;
}

public Object getResult(MaxData data) {
return data.max;
}

public boolean supportsReverse() {
return false;
}

public Class<?> getResultType() {
return BigInteger.class;
}
}
Loading

0 comments on commit 14db10e

Please sign in to comment.