Uploaded image for project: 'JBRULES'
  1. JBRULES
  2. JBRULES-3466

accumulate with an "and" condition results in NPE on reverse

This issue belongs to an archived project. You can view it, but you can't modify it. Learn more

XMLWordPrintable

      I have a rule with this element in the when clause:

              $beforePrediction : Number() from accumulate(
                   JobSlot(job != NULL_JOB, $j : job, $service == service, $ordinal > ordinal) and $p : Prediction(job == $j, service == $service),
                   sum($p.getMillisMid())
              )
      

      When I execute this I have cases where there are no matching Prediction instances, so the sum is zero. Later on retract, I get this:

      Caused by: java.lang.NullPointerException: null
      	at org.drools.base.accumulators.SumAccumulateFunction.reverse(SumAccumulateFunction.java:83)
      	at org.drools.base.accumulators.JavaAccumulatorFunctionExecutor.reverse(JavaAccumulatorFunctionExecutor.java:130)
      	at org.drools.rule.Accumulate.reverse(Accumulate.java:208)
      

      The relevant code is this:

          public void accumulate(Serializable context,
                                 Object value) {
              SumData data = (SumData) context;
              data.total += ((Number) value).doubleValue();
          }
      

      and this:

          public void reverse(Object workingMemoryContext,
                              Object context,
                              Tuple leftTuple,
                              InternalFactHandle handle,
                              Declaration[] declarations,
                              Declaration[] innerDeclarations,
                              WorkingMemory workingMemory) throws Exception {
      
              final Object value = ((JavaAccumulatorFunctionContext) context).reverseSupport.remove( Integer.valueOf( handle.getId() ) );
              this.function.reverse( ((JavaAccumulatorFunctionContext) context).context,
                                     value );
          }
      

      A breakpoint reveals that handle is a Prediction instance. Somehow that handle was not added to reverseSupport, apparently.

            etirelli@redhat.com Edson Tirelli
            chrisdolan_jira Chris Dolan (Inactive)
            Archiver:
            rhn-support-ceverson Clark Everson

              Created:
              Updated:
              Resolved:
              Archived: