简体   繁体   English

流口水:仅在执行另一个规则后执行规则

[英]Drools: Execute a rule only after another was executed

I have two Drools rules in the same drl file like: 我在同一个drl文件中有两个Drools规则,例如:

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. 然后,使用第二条规则,如果有任何阀门发出警报(由“如果紧急”规则设置),则发送SMS。

The problem is that the very first time I execute the rules the SMS are not sent. 问题是,我第一次执行规则时,不会发送SMS。 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. 我的问题是如何告诉Drools我要先执行“如果紧急”,然后在调用“ activateAlarm”方法后执行规则“如果警报”,以便发送SMS。

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. 还尝试了议程组和setFocus,但并没有带我去任何地方。

Any idea? 任何想法? Is this possible with Drools? 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. 您可以使用Drools的显着性关键字。 Give positive Salience value to the first rule. 将正的Salience值赋予第一条规则。 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. 因此, 如果仅在IF关键规则任务完成后才触发警报规则,这意味着完全依赖以前的规则,并且还必须通过使用Modify或update来更新第一个规则中的事实,如果第二个规则中需要任何更新的值。 Hope my answer will help you.let me know if any quires on same. 希望我的回答对您有所帮助。如有任何疑问,请告知我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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