[rules-users] DROOLS problem with sliding windows

AberAber maryellenbench at hotmail.com
Wed Dec 8 09:04:38 EST 2010


I have been working with sliding windows, and they do not appear to work
properly in DROOLS 5.1.1 or I am misunderstanding how they work.  My goal is
to have the last X number of samples averaged or summed.  I've tried two
mechanism, the accumulate/from, and the collect, and they both behave
similarly wrong (or different than I expect), but this was much easier to
follow so I've posted it for help.

The way I have the code structured, I receive values for two nodes, 1 and 2
every second or two, which each contain random values.  After each value
received I pass the CPUReportEvent and fire the rules.  The results are
bizarre.  Sometimes it resets back to 1 count, sometimes it keeps the value
to the previous one.  It seems to also fire both rules when really only one
should've been triggered?  Can someone explain the results and how to adjust
it to work for my specs? Thanks!

I can't see into the accumulate function, so here's the collect function,
but they perform identically.


package com.sample
 
import com.sample.DroolsTest.Message;
import com.sample.DroolsInput;
import com.sample.CPUReportEvent;
import com.ComEntity;
import java.util.Iterator
import java.util.ArrayList


declare CPUReportEvent
  // declare a fact type as an event, default is 'fact'
  @role( event )
end

rule "collect with length window"
no-loop true
when
    $comEntity : ComEntity()
    $list : ArrayList ()  from collect(
              CPUReportEvent(comEntity == $comEntity) 
                   over window:length(3) )
then
    System.out.print("Entity " + $comEntity.getName() + " fired of count " +
$list.size() + " value");
    
     Double sum = 0.0;
     Double count = 0.0;
     for(Iterator it = $list.iterator();it.hasNext();) {
            CPUReportEvent report = (CPUReportEvent) it.next();
            System.out.print(" " + report.getAmount());
            sum = sum + report.getAmount();
            count = count + 1.0;
     }
    
    if (count > 0.0)
    {
        Double avg = (sum / count);
        System.out.print(" - Sum " + sum + " avg " + avg + " count " +
count);
    }
    
    System.out.println();
    
end 


RESULTS:
Node1 : 7.0
Entity Node2 fired of count 0 value
Entity Node1 fired of count 1 value 7.0 - Sum 7.0 avg 7.0 count 1.0
Node2 : 94.0
Entity Node2 fired of count 1 value 94.0 - Sum 94.0 avg 94.0 count 1.0
Node1 : 43.0
Entity Node1 fired of count 2 value 7.0 43.0 - Sum 50.0 avg 25.0 count 2.0
Node2 : 85.0
Entity Node2 fired of count 2 value 94.0 85.0 - Sum 179.0 avg 89.5 count 2.0
Entity Node1 fired of count 1 value 43.0 - Sum 43.0 avg 43.0 count 1.0
Node1 : 88.0
Entity Node2 fired of count 1 value 85.0 - Sum 85.0 avg 85.0 count 1.0
Entity Node1 fired of count 2 value 43.0 88.0 - Sum 131.0 avg 65.5 count 2.0
Node2 : 47.0
Entity Node2 fired of count 2 value 85.0 47.0 - Sum 132.0 avg 66.0 count 2.0
Entity Node1 fired of count 1 value 88.0 - Sum 88.0 avg 88.0 count 1.0
Node1 : 39.0
Entity Node2 fired of count 1 value 47.0 - Sum 47.0 avg 47.0 count 1.0
Entity Node1 fired of count 2 value 88.0 39.0 - Sum 127.0 avg 63.5 count 2.0
Node1 : 0.0
Entity Node1 fired of count 2 value 39.0 0.0 - Sum 39.0 avg 19.5 count 2.0



When I set the window to 4, the results are similar but never get past a
count of 2.


Node2 : 87.0
Entity Node2 fired of count 1 value 87.0 - Sum 87.0 avg 87.0 count 1.0
Entity Node1 fired of count 0 value
Node1 : 39.0
Entity Node1 fired of count 1 value 39.0 - Sum 39.0 avg 39.0 count 1.0
Node2 : 11.0
Entity Node2 fired of count 2 value 87.0 11.0 - Sum 98.0 avg 49.0 count 2.0
Node1 : 48.0
Entity Node1 fired of count 2 value 39.0 48.0 - Sum 87.0 avg 43.5 count 2.0
Node2 : 33.0
Entity Node2 fired of count 2 value 11.0 33.0 - Sum 44.0 avg 22.0 count 2.0
Node1 : 69.0
Entity Node1 fired of count 2 value 48.0 69.0 - Sum 117.0 avg 58.5 count 2.0
Node2 : 63.0
Entity Node2 fired of count 2 value 33.0 63.0 - Sum 96.0 avg 48.0 count 2.0
Node1 : 82.0
Entity Node1 fired of count 2 value 69.0 82.0 - Sum 151.0 avg 75.5 count 2.0
Node2 : 94.0
Entity Node2 fired of count 2 value 63.0 94.0 - Sum 157.0 avg 78.5 count 2.0
Node1 : 65.0
Entity Node1 fired of count 2 value 82.0 65.0 - Sum 147.0 avg 73.5 count 2.0


And here's sliding window length of 5:
Node1 : 34.0
Entity Node2 fired of count 0 value
Entity Node1 fired of count 1 value 34.0 - Sum 34.0 avg 34.0 count 1.0
Node1 : 3.0
Entity Node1 fired of count 2 value 34.0 3.0 - Sum 37.0 avg 18.5 count 2.0
Node2 : 92.0
Entity Node2 fired of count 1 value 92.0 - Sum 92.0 avg 92.0 count 1.0
Node1 : 61.0
Entity Node1 fired of count 3 value 34.0 3.0 61.0 - Sum 98.0 avg
32.666666666666664 count 3.0
Node1 : 65.0
Entity Node1 fired of count 4 value 34.0 3.0 61.0 65.0 - Sum 163.0 avg 40.75
count 4.0
Node2 : 81.0
Entity Node2 fired of count 2 value 92.0 81.0 - Sum 173.0 avg 86.5 count 2.0
Entity Node1 fired of count 3 value 3.0 61.0 65.0 - Sum 129.0 avg 43.0 count
3.0
Node1 : 29.0
Entity Node1 fired of count 3 value 61.0 65.0 29.0 - Sum 155.0 avg
51.666666666666664 count 3.0
Node2 : 28.0
Entity Node2 fired of count 2 value 81.0 28.0 - Sum 109.0 avg 54.5 count 2.0
Node2 : 46.0
Entity Node2 fired of count 3 value 81.0 28.0 46.0 - Sum 155.0 avg
51.666666666666664 count 3.0
Entity Node1 fired of count 2 value 65.0 29.0 - Sum 94.0 avg 47.0 count 2.0
Node2 : 55.0
Entity Node2 fired of count 4 value 81.0 28.0 46.0 55.0 - Sum 210.0 avg 52.5
count 4.0
Entity Node1 fired of count 1 value 29.0 - Sum 29.0 avg 29.0 count 1.0
-- 
View this message in context: http://drools-java-rules-engine.46999.n3.nabble.com/DROOLS-problem-with-sliding-windows-tp2039892p2039892.html
Sent from the Drools - User mailing list archive at Nabble.com.



More information about the rules-users mailing list