[jboss-jira] [JBoss JIRA] (DROOLS-1422) Make AccumulateFunction simpler and more powerful

Geoffrey De Smet (JIRA) issues at jboss.org
Tue Jan 31 09:07:00 EST 2017


Geoffrey De Smet created DROOLS-1422:
----------------------------------------

             Summary: Make AccumulateFunction simpler and more powerful
                 Key: DROOLS-1422
                 URL: https://issues.jboss.org/browse/DROOLS-1422
             Project: Drools
          Issue Type: Feature Request
          Components: core engine
            Reporter: Geoffrey De Smet
            Assignee: Mario Fusco


* Replace the AccumulateFunction interface with the AccumulateFunction2 interface:
** All core runtime code uses the AccumulateFunction2 interface.
**  Deprecate AccumulateFunction and use a bridge class at DRL compilation time so old implementations still work because they are bridged into the new interface
* Changes of the AccumulateFunction2:
** Methods no longer throw checked exceptions (no "throws Exception"), so drools doesn't have to catch them. This might improve performance.
** Remove "Serializable context". The class itself contains the state.
*** Remove method createContext()
*** Replace method init(Serializeble) with constructor call (no-args normally)
*** Remove parameter Serializable from accumulate(value), reserve(value) and getResult() methods
*** This might improve performance.
** It should Serializable, but doesn't have to Externalizable. Removes methods writeExternal() and readExternal() in the user implementation.
* Interface ReversableAccumulateFunction2 extends AccumulateFunction2
** Only ReversableAccumulateFunction2 has method reverse(value)
** Remove method supportsReverse(): the custom accumulate is reverseable if it also implements this interface
* Opportunities
** construction parameters. For example: fixed average for standard deviation (very useful for OptaPlanner)
** Multi-argument accumulates, for example in DRL: `$total : standardDeviation($groupBy, $weight)`


{code}
public class SumAccumulateFunction
        implements ReversableAccumulateFunction2<Integer, Integer> {

    public int total;

    public SumAccumulateFunction() {
        total = 0;
    }

    public void accumulate(Integer value) {
        total += value;
    }

    public void reverse(Integer value) {
        total -= value;
    }

    public Integer getResult() {
        return total;
    }

}
{code}


{code}
public class AverageAccumulateFunction
        implements ReversableAccumulateFunction2<Integer, Double> {

    public int total;
    public int count;

    public AverageAccumulateFunction() {
        total = 0;
        count = 0;
    }

    public void accumulate(Integer value) {
        total += value;
        count++;
    }

    public void reverse(Integer value) {
        total -= value;
        count--;
    }

    public Double getResult() {
        return (double) total / count;
    }

}
{code}



{code}
public class StdDeviationAccumulateFunction
        implements ReversableAccumulateFunction2<Integer, Double> {

    public final double average;
    public double variance;

    public StdDeviationAccumulateFunction(double average) {
        this.average = average;
        variance = 0;
    }

    public void accumulate(double value) {
        variance += (value - average)²; // TODO
    }

    public void reverse(double value) {
        variance -= (value - average)²; // TODO
    }

    public Double getResult() {
        return Math.sqrt(variance);
    }

}
{code}



--
This message was sent by Atlassian JIRA
(v7.2.3#72005)



More information about the jboss-jira mailing list