简体   繁体   中英

Drools: Execute a rule only after another was executed

I have two Drools rules in the same drl file like:

rule "If critical"
when 
incident:Incident(state=CRITICAL)
then
incident.getIncidentValve().activateAlarm();
end;

rule "If alarm"
when 
valve:Valve(hasAlarm==true)
then
SMS.send(valve.getId());
end;

My idea is to process, with the first rule, all the incidents that are in critical state. And then with the second rule, if any valve has an alarm, that was set by the "If critical" rule, send an SMS.

The problem is that the very first time I execute the rules the SMS are not sent. This seems to be so because the 'when' of both rules is verified at the same time.

My question is how can I tell Drools that I want to execute first the "If critical" and then, once the 'activateAlarm' methods have been called, execute the rule "If alarm" so the SMS are sent.

I have tried with salience, but that's not what I am looking for because that is only the execution order, after the validation of the 'when' is done, which as I said is done for both rules at the same time.

Also tried with agenda-group and setFocus, but it didn't take me anywhere.

Any idea? Is this possible with Drools?

The problem is that the facts after the 1st rule is executed are not updated. To use the values changed in the 1st rule you have to update the values using the update function.

You can use salience keyword of Drools. Give positive Salience value to the first rule. See documentation here . Try defining your rule like this

rule "If critical"
salience 100 
when 
incident:Incident(state=CRITICAL)
then
incident.getIncidentValve().activateAlarm();
end;

rule "If alarm"
when 
valve:Valve(hasAlarm==true)
then
SMS.send(valve.getId());
end;.

Use extends attribute ex- rule If alarm extends If critical

So If alarm rule will fire only after IF critical rule task completed It means creating complete dependency on previous rule And also you must have to update the fact in first rule by using modify or update if any updated value required in second rule. Hope my answer will help you.let me know if any quires on same.

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