[rules-users] keeping running stats

Michal Bali michalbali at gmail.com
Wed May 27 17:08:42 EDT 2009


Hi Chris,

You can use 'accumulate' with sliding time window. Have a look here
http://downloads.jboss.com/drools/docs/5.0.1.26597.FINAL/drools-fusion/html/ch02.html#d0e1169

rule "Sound the alarm in case temperature rises above threshold"
when
    TemperatureThreshold( $max : max )
    Number( doubleValue > $max ) from accumulate(
        SensorReading( $temp : temperature ) over window:time( 10m ),
        average( $temp ) )
then
    // sound the alarm
end

The engine will automatically discard any SensorReading older than 10
minutes and keep the calculated average consistent.

Is that what you're looking for?

Michal


2009/5/27 Chris Richmond <crichmond at referentia.com>

>  Hello,
>
>
>
> I have modified the stockTicker fusion example to keep some running stats,
> you can see from the rule snippet below that it injects a stats object based
> on the symbol then matches them as updates come in later.  You can see for
> now I am just updating the running counts and outputting the readings count
> on each stock tick and this works fine.
>
>
>
> What I would like to do however is only have the running averages,stats
> object reflect stock ticks that are still in memory….essentiall only stock
> tick items that have not expired.  As it is now the count just keeps growing
> and growing, I want the count to only reflect the stock ticks within the
> expiration time in the past for stock ticks, but I cannot figure out how to
> make this happen?    Could anyone give me a pointer on how to do this?  How
> to make the stats object only reflect those stock ticks that have not
> expired?  I do not know the strategy for this.
>
>
>
> Thanks,
>
> Chris
>
>
>
> # tells the engine that a StockTick instance will assume the
>
> # role (semantics) of events and that the default retention
>
> # policy will be 2 minutes
>
> *declare* StockTick
>
>     @role( event )
>
>     @expires( 1m )
>
> *end*
>
>
>
> # One can even declare helper facts to make rules easier and
>
> # simpler to write and maintain
>
> *declare* Statistics
>
>     symbol : String @key()
>
>     average : *double*
>
>     readings : *int*
>
>     total : *double*
>
> *end*
>
>
>
> *rule* "Setup statistics"
>
> *when*
>
>    $c : Company( $s : symbol )
>
>    *not*( Statistics( symbol == $s ) )
>
> *then*
>
>    Statistics s = *new* Statistics();
>
>    s.symbol = $s;
>
>    s.readings = s.readings + 1;
>
>    *insert*( s );
>
>
>
> *end*
>
>
>
>
>
>
>
> # a simple rule to show that it is possible to join
>
> # events from an entry-point (stream) with facts
>
> # present in the working memory
>
> *rule* "Update stock stats"
>
>     *agenda-group* "evaluation"
>
>     *lock-on-active*
>
> *when*
>
>
>
>     $cp : Company( $sb : symbol )
>
>     $st : StockTick( symbol == $sb, $pr : price ) *from* entry-point "StockTick
> stream"
>
>
>
>       $stats : Statistics( symbol == $sb  )
>
>
>
> *then*
>
>
>
>       *modify*( $stats ) { readings = readings + 1};
>
>       System.err.println($stats.symbol + "readings: " + $stats.readings);
>
>     // This shows an update on working memory facts with data from joined
> events
>
>     //modify( $cp ) { currentPrice = $pr }
>
>
>
>     // Although events are considered immutable, a common pattern is to
> use a class
>
>     // to represent an event and enrich that event instance with data
> derived from other facts/events.
>
>     // Bellow we "enrich" the event instance with the percentual change in
> the price,
>
>     // based on the previous price
>
>     //modify( $st ) { delta = $cp.delta }
>
>     //modify( $st ) { readings = 5 }
>
>     //System.out.println($st.delta)
>
> *end*
>
> _______________________________________________
> rules-users mailing list
> rules-users at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/rules-users
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/rules-users/attachments/20090527/a27c15ca/attachment.html 


More information about the rules-users mailing list