[rules-users] Another solution for: Detecting more than 3 occurrences within 1 hour

Tina Vießmann tviessmann at stud.hs-bremen.de
Thu Aug 5 16:31:10 EDT 2010


Thank you for this approach, Wolfgang. That sounds great.  :)
I just have some further question about your solution.

#1
I'm thinking about an approach without the need modifying things outside 
the drl file. Is something like that doable?
Because I have to create the watcher class? (I don't expect it to be, 
but why not ask. ;))

#2
> (What and count are somewhat redundant, but this avoids clumsy patterns.)
Am I right that what and count have to be defined as global variables 
and initialized using setGlobal() (from a part of the java application)?

#3
> rule addEvent
> when
>  $watcher : Watcher( $eventA : what, $set : valueSet )
>  $eventB : Value( this after[0ms,1h] $eventA &&
>   //                                   this != $eventA &&      ### set 
> includes Watcher.what
>                                    
>  eval(valueExceededLimit($eventB.getAlarms()) && ! $set.contains( this 
> ) ) )
I'm sorry, could you explain to me the part of $eventB in sentences, 
please? I've got confused by the comments... :(


Thank you very much! :)
Tina



> Basic idea: associate a Watcher with each event.
>
> class Watcher {
>    Value what;
>    int count = 1;
>    Set<Value> valueSet = new HashSet<Value>();
>    Watcher( Value first ){
>       valueSet.add( what = first );
>    }
>    //...
> }
>
> And now the rules:
>
> rule attachWatcher
> when
>   $event : Value( eval(parameterValueExceededLimit($eventA.getAlarms())) )
>   not( Watcher( what == $event ) )
> then
>   insert( new Watcher( $event ) );
> end
>
> rule addEvent
> when
>  $watcher : Watcher( $eventA : what, $set : valueSet )
>  $eventB : Value( this after[0ms,1h] $eventA &&
>   //                                   this != $eventA &&      ### set 
> includes Watcher.what
>                                    
>  eval(valueExceededLimit($eventB.getAlarms()) && ! $set.contains( this 
> ) ) )
> then
>   modify( $watcher ){
>       setValueList( $watcher.getValueSet().add( $eventB ),
>       setCount( $watcher.getCount() + 1 )
>   }
> end
>
> rule testLimit
> when
>    $watcher : Watcher( count > Limit )
> then
>   // raise hell,
>   // probably: get rid of all in $watcher.set, and $watcher
> end
>
> (What and count are somewhat redundant, but this avoids clumsy patterns.)
>
> Watcher should be declared as Event, with @expires, so they'll 
> disappear with the (primary) Event each one is watching.
>
> Cheers
> -W
>
>
> 2010/8/5 Tina Vießmann <tviessmann at stud.hs-bremen.de 
> <mailto:tviessmann at stud.hs-bremen.de>>
>
>     Hi,
>
>     I'm working on thinking in Drools rules. Right now I'm trying to
>     solve this:
>       The rule shall fire if _a special event occurs more than 3 times
>     within 1 hour_.
>
>     My _first thought of a solution_ was to count the count the
>     detected events using a counter. But the counter has to be a
>     global variable, hasn't it? And global variables are not to be
>     used to frequently, aren't they?
>     And global variables must always be initialized from outside the
>     rules file, don't they?
>
>     Because of these thoughts I've looked for a _different solution
>     without global variables_. I came up with:
>
>         function boolean valueExceededLimit(Set<Alarms> alarmSet) {
>            //....
>         }
>
>         rule "more than 3 occurs within 1 hour"
>
>             when
>                 // event #1
>                 $eventA : Value(
>         eval(parameterValueExceededLimit($eventA.getAlarms())) )
>                 // event #2
>                 $eventB : Value( this after[0ms,1h] $eventA &&
>                                             this != $eventA &&
>                                           
>          eval(valueExceededLimit($eventB.getAlarms())) )
>                 // event #3
>                 $eventC : Value( this after[0ms,1h] $eventA &&
>                                             this != $eventA &&
>                                             this != $eventB &&
>                                           
>          eval(valueExceededLimit($eventC.getAlarms())) )
>                 // event #4  ->  4 > 3
>                 $eventD : Value( this after[0ms,1h] $eventA &&
>                                             this != $eventA &&
>                                             this != $eventB &&
>                                             this != $eventC &&
>                                           
>          eval(valueExceededLimit($eventD.getAlarms())) )
>
>             then
>                 // ... do something ...
>
>         end
>
>
>     More than 3 is kind of a doable task. But I think of this solution
>     as heavy in case its needed to detect a larger number of events. I
>     would be thankful for other approaches to the problem.
>
>
>     Thanks :)
>     Tina
>
>     _______________________________________________
>     rules-users mailing list
>     rules-users at lists.jboss.org <mailto:rules-users at lists.jboss.org>
>     https://lists.jboss.org/mailman/listinfo/rules-users
>
>
>
> _______________________________________________
> 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/20100805/5e3a865a/attachment.html 


More information about the rules-users mailing list