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@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@lists.jboss.org
https://lists.jboss.org/mailman/listinfo/rules-users