简体   繁体   English

Drools规则格式,仅触发一次

[英]Drools Rule format for firing only once

I'm using the drools 6 engine. 我正在使用Drools 6引擎。 Suppose I have an account object that has a collection of sections and each section has a status flag that can be "GOOD" or "BAD". 假设我有一个帐户对象,该对象具有多个部分,每个部分的状态标志都可以是“ GOOD”或“ BAD”。 If I was to write the following: 如果我要编写以下内容:

rule "Check if account has a good subsection"
when
    $account : Account()
    SubSection (status == "GOOD") from $account.getSubSections()
then
    insertLogical(new AccountIsGood($account));
end

I would expect this to simply add the logical rule AccountIsGood($account) if atleast one section was good. 如果至少一节是好的,我希望这会简单地添加逻辑规则AccountIsGood($account) However, it seems this does not simply check for one successful subsection and end the rule, but instead it continues checking all subsections and inserting the logical rule for each valid subsection. 但是,这似乎并不仅仅是检查一个成功的子节并结束规则,而是继续检查所有子节并为每个有效子节插入逻辑规则。 Ex, if an account has four valid subsections, I get four copies of the rule for that account. 例如,如果一个帐户有四个有效的小节,我将获得该帐户规则的四个副本。

So my question is, is there a way to rewrite this rule to get the desired behavior? 所以我的问题是,有没有办法重写此规则以获得所需的行为?

The account class: 帐户类别:

public class Account {
  private List<SubSection> subsections;

  // Getters / Setters/ other code
}

Keep the rules as simple as possible. 保持规则尽可能简单。 While it is feasible to check for the presence of the inserted AccoundIsGood() , it is recommended to write the logic so that only the existence of a single good subsection is tested. 尽管检查插入的AccoundIsGood()的存在是可行的,但建议编写逻辑,以便仅测试单个良好子部分的存在。 Also, you need a from to extract the subsections from the Account object. 同样,您需要一个from来从Account对象中提取子节。

rule testForGood
when
  $account: Account( $sub: subsections )
  exists SubSection( status == "GOOD" ) from $sub
then
  insertLogical( new AccountIsGood($account) );
end

It might be a good idea to keep insertLogical , if there is a chance that the subsection status might change away from "GOOD": the logically inserted fact would be retracted if the accound doesn't have at lease one GOOD section. 如果有可能子节的状态可能会从“ GOOD”变为“ GOOD”,那么最好保持insertLogical :如果插入的词尾至少没有一个GOOD节,则逻辑插入的事实将被撤回。

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

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