简体   繁体   中英

Disabling another drools rule from firing

Hi I want to disable some other drools rules from firing when another set of drools rules fire, how would you do that?

Say I have an agenda-group "Daily", which has two sets of drools rules set A has rules "Default-1", "Default-2", "Default-3" set B has rules "Custom-1", "Custom-2", "Custom-3"

when the agenda-group "Daily" gets focused, and the current facts match the Custom pattern, I want "Custom-1" and/or "Custom-2" and/or "Custom-3" to fire only; otherwise, only "Default-1" and/or "Default-2" and/or "Default-3" fire.

Problem is, Default-1/2/3 are always fired. I need a way to disable them in Custom-1/2/3. First, I set the salience level in Custom-1/2/3 to higher than Default-1/2/3. Then I tried to use activation-group. But if I set all of them to the same activation-group, only 1 out of the six rules will fire, thats not what I want.

I am not allowed to change the .java module, which loads all the rules everytime. I can only change the .drl drools rules.

Thank you.

you could try solving your issue with marker objects. suppose you define a Marker class:

public class Marker {
    String uniqueIdentifier;
    //getter and setter, etc
}

(drools allows you to define new classes in *.drl code without resorting to *.java)
then make the custom group run before the default group (salience would work, defining a flow would also work) and "mark" those objects for which a custom rule was triggered by inserting a new Marker fact into memory, something like this:

when
   SomeObject($unique: someIdentifier)
   //normal conditions
then
   insert(new Marker($unique))
   //normal action

and the in the default rules only act on objects for which no custom rule has fired:

when
   SomeObject($unique: someIdentifier)
   not Marker(uniqueIdentifier = $unique)
   //normal conditions
then
   //normal action

also, to prevent this from leaking you may need a 3rd (last) group of rules to clean up:

when
   SomeObject($unique: someIdentifier)
   $marker : Marker(uniqueIdentifier = $unique)
then
   retract($marker)

The Drools support for this kind of behavior exists using Declarative Agenda .

It basically provides this context methods:

void blockMatch(Match match);
void unblockAllMatches(Match match);
void cancelMatch(Match match);

To block other rules from within a rule while the rule blocking the others remains true or they're actually explicitly unblocked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM