Constraints restrict what you see through a window; they do not modify or<br>qualify the window. Thus, basically you see the last three CPUReportEvents,<br>and new activations are possible for each new event of this type. Other<br>
patterns (such as ComEntity) create (in your case: two) alpha network nodes,<br>with which the renewed triplet is joined; now the restriction in the CPUReportEvent<br>pattern reduces what goes into the collect and, ultimately, into the List.<br>
<br>While this (hopefully) explains why things happen the way you&#39;ve observed,<br>it won&#39;t help you in fixing your problem. I would add a field<br>List&lt;CPUReportEvent&gt; reports to ComEntity and write these<br>
two rules:<br><br>rule &quot;register event&quot;<br>when<br>    $e: CPUReportEvent( $comEntity: comEntity )<br>    $c: ComEntity( this == $comEntity, $reports: reports )<br>then<br>    $reports.add( $e );<br>    if( $reports.size() &gt; 3 ) $reports.remove(0);<br>
    update( $c );<br>    retract( $e ); # optional, depends on what else needs to be done with $e<br>end<br><br>rule &quot;compute average&quot;<br>when<br>    ComEntity( $reports: reports )<br>    Number( $avg: doubleValue ) from accumulate( CPUReportEvent($a: amount)  from $reports, average($a) )<br>
then<br>   ... $avg...<br>end<br><br>-W<br><br><div class="gmail_quote">On 8 December 2010 15:04, AberAber <span dir="ltr">&lt;<a href="mailto:maryellenbench@hotmail.com">maryellenbench@hotmail.com</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><br>
I have been working with sliding windows, and they do not appear to work<br>
properly in DROOLS 5.1.1 or I am misunderstanding how they work.  My goal is<br>
to have the last X number of samples averaged or summed.  I&#39;ve tried two<br>
mechanism, the accumulate/from, and the collect, and they both behave<br>
similarly wrong (or different than I expect), but this was much easier to<br>
follow so I&#39;ve posted it for help.<br>
<br>
The way I have the code structured, I receive values for two nodes, 1 and 2<br>
every second or two, which each contain random values.  After each value<br>
received I pass the CPUReportEvent and fire the rules.  The results are<br>
bizarre.  Sometimes it resets back to 1 count, sometimes it keeps the value<br>
to the previous one.  It seems to also fire both rules when really only one<br>
should&#39;ve been triggered?  Can someone explain the results and how to adjust<br>
it to work for my specs? Thanks!<br>
<br>
I can&#39;t see into the accumulate function, so here&#39;s the collect function,<br>
but they perform identically.<br>
<br>
<br>
package com.sample<br>
<br>
import com.sample.DroolsTest.Message;<br>
import com.sample.DroolsInput;<br>
import com.sample.CPUReportEvent;<br>
import com.ComEntity;<br>
import java.util.Iterator<br>
import java.util.ArrayList<br>
<br>
<br>
declare CPUReportEvent<br>
  // declare a fact type as an event, default is &#39;fact&#39;<br>
  @role( event )<br>
end<br>
<br>
rule &quot;collect with length window&quot;<br>
no-loop true<br>
when<br>
    $comEntity : ComEntity()<br>
    $list : ACPrrayList ()  from collect(<br>
              CPUReportEvent(comEntity == $comEntity)<br>
                   over window:length(3) )<br>
then<br>
    System.out.print(&quot;Entity &quot; + $comEntity.getName() + &quot; fired of count &quot; +<br>
$list.size() + &quot; value&quot;);<br>
<br>
     Double sum = 0.0;<br>
     Double count = 0.0;<br>
     for(Iterator it = $list.iterator();it.hasNext();) {<br>
            CPUReportEvent report = (CPUReportEvent) it.next();<br>
            System.out.print(&quot; &quot; + report.getAmount());<br>
            sum = sum + report.getAmount();<br>
            count = count + 1.0;<br>
     }<br>
<br>
    if (count &gt; 0.0)<br>
    {<br>
        Double avg = (sum / count);<br>
        System.out.print(&quot; - Sum &quot; + sum + &quot; avg &quot; + avg + &quot; count &quot; +<br>
count);<br>
    }<br>
<br>
    System.out.println();<br>
<br>
end<br>
<br>
<br>
RESULTS:<br>
Node1 : 7.0<br>
Entity Node2 fired of count 0 value<br>
Entity Node1 fired of count 1 value 7.0 - Sum 7.0 avg 7.0 count 1.0<br>
Node2 : 94.0<br>
Entity Node2 fired of count 1 value 94.0 - Sum 94.0 avg 94.0 count 1.0<br>
Node1 : 43.0<br>
Entity Node1 fired of count 2 value 7.0 43.0 - Sum 50.0 avg 25.0 count 2.0<br>
Node2 : 85.0<br>
Entity Node2 fired of count 2 value 94.0 85.0 - Sum 179.0 avg 89.5 count 2.0<br>
Entity Node1 fired of count 1 value 43.0 - Sum 43.0 avg 43.0 count 1.0<br>
Node1 : 88.0<br>
Entity Node2 fired of count 1 value 85.0 - Sum 85.0 avg 85.0 count 1.0<br>
Entity Node1 fired of count 2 value 43.0 88.0 - Sum 131.0 avg 65.5 count 2.0<br>
Node2 : 47.0<br>
Entity Node2 fired of count 2 value 85.0 47.0 - Sum 132.0 avg 66.0 count 2.0<br>
Entity Node1 fired of count 1 value 88.0 - Sum 88.0 avg 88.0 count 1.0<br>
Node1 : 39.0<br>
Entity Node2 fired of count 1 value 47.0 - Sum 47.0 avg 47.0 count 1.0<br>
Entity Node1 fired of count 2 value 88.0 39.0 - Sum 127.0 avg 63.5 count 2.0<br>
Node1 : 0.0<br>
Entity Node1 fired of count 2 value 39.0 0.0 - Sum 39.0 avg 19.5 count 2.0<br>
<br>
<br>
<br>
When I set the window to 4, the results are similar but never get past a<br>
count of 2.<br>
<br>
<br>
Node2 : 87.0<br>
Entity Node2 fired of count 1 value 87.0 - Sum 87.0 avg 87.0 count 1.0<br>
Entity Node1 fired of count 0 value<br>
Node1 : 39.0<br>
Entity Node1 fired of count 1 value 39.0 - Sum 39.0 avg 39.0 count 1.0<br>
Node2 : 11.0<br>
Entity Node2 fired of count 2 value 87.0 11.0 - Sum 98.0 avg 49.0 count 2.0<br>
Node1 : 48.0<br>
Entity Node1 fired of count 2 value 39.0 48.0 - Sum 87.0 avg 43.5 count 2.0<br>
Node2 : 33.0<br>
Entity Node2 fired of count 2 value 11.0 33.0 - Sum 44.0 avg 22.0 count 2.0<br>
Node1 : 69.0<br>
Entity Node1 fired of count 2 value 48.0 69.0 - Sum 117.0 avg 58.5 count 2.0<br>
Node2 : 63.0<br>
Entity Node2 fired of count 2 value 33.0 63.0 - Sum 96.0 avg 48.0 count 2.0<br>
Node1 : 82.0<br>
Entity Node1 fired of count 2 value 69.0 82.0 - Sum 151.0 avg 75.5 count 2.0<br>
Node2 : 94.0<br>
Entity Node2 fired of count 2 value 63.0 94.0 - Sum 157.0 avg 78.5 count 2.0<br>
Node1 : 65.0<br>
Entity Node1 fired of count 2 value 82.0 65.0 - Sum 147.0 avg 73.5 count 2.0<br>
<br>
<br>
And here&#39;s sliding window length of 5:<br>
Node1 : 34.0<br>
Entity Node2 fired of count 0 value<br>
Entity Node1 fired of count 1 value 34.0 - Sum 34.0 avg 34.0 count 1.0<br>
Node1 : 3.0<br>
Entity Node1 fired of count 2 value 34.0 3.0 - Sum 37.0 avg 18.5 count 2.0<br>
Node2 : 92.0<br>
Entity Node2 fired of count 1 value 92.0 - Sum 92.0 avg 92.0 count 1.0<br>
Node1 : 61.0<br>
Entity Node1 fired of count 3 value 34.0 3.0 61.0 - Sum 98.0 avg<br>
32.666666666666664 count 3.0<br>
Node1 : 65.0<br>
Entity Node1 fired of count 4 value 34.0 3.0 61.0 65.0 - Sum 163.0 avg 40.75<br>
count 4.0<br>
Node2 : 81.0<br>
Entity Node2 fired of count 2 value 92.0 81.0 - Sum 173.0 avg 86.5 count 2.0<br>
Entity Node1 fired of count 3 value 3.0 61.0 65.0 - Sum 129.0 avg 43.0 count<br>
3.0<br>
Node1 : 29.0<br>
Entity Node1 fired of count 3 value 61.0 65.0 29.0 - Sum 155.0 avg<br>
51.666666666666664 count 3.0<br>
Node2 : 28.0<br>
Entity Node2 fired of count 2 value 81.0 28.0 - Sum 109.0 avg 54.5 count 2.0<br>
Node2 : 46.0<br>
Entity Node2 fired of count 3 value 81.0 28.0 46.0 - Sum 155.0 avg<br>
51.666666666666664 count 3.0<br>
Entity Node1 fired of count 2 value 65.0 29.0 - Sum 94.0 avg 47.0 count 2.0<br>
Node2 : 55.0<br>
Entity Node2 fired of count 4 value 81.0 28.0 46.0 55.0 - Sum 210.0 avg 52.5<br>
count 4.0<br>
Entity Node1 fired of count 1 value 29.0 - Sum 29.0 avg 29.0 count 1.0<br>
<font color="#888888">--<br>
View this message in context: <a href="http://drools-java-rules-engine.46999.n3.nabble.com/DROOLS-problem-with-sliding-windows-tp2039892p2039892.html" target="_blank">http://drools-java-rules-engine.46999.n3.nabble.com/DROOLS-problem-with-sliding-windows-tp2039892p2039892.html</a><br>

Sent from the Drools - User mailing list archive at Nabble.com.<br>
_______________________________________________<br>
rules-users mailing list<br>
<a href="mailto:rules-users@lists.jboss.org">rules-users@lists.jboss.org</a><br>
<a href="https://lists.jboss.org/mailman/listinfo/rules-users" target="_blank">https://lists.jboss.org/mailman/listinfo/rules-users</a><br>
</font></blockquote></div><br>