Drools规则格式仅触发一次 [英] Drools Rule format for firing only once

查看:832
本文介绍了Drools规则格式仅触发一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用drools 6引擎。假设我有一个帐户对象,其中包含一系列部分,每个部分都有一个状态标志,可以是GOOD或BAD。如果我要撰写以下内容:

I'm using the drools 6 engine. 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". 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

我希望这只是添加逻辑规则 AccountIsGood($ account)如果至少有一个部分是好的。但是,似乎这不仅仅检查一个成功的子部分并结束规则,而是继续检查所有子部分并为每个有效子部分插入逻辑规则。例如,如果一个帐户有四个有效的小节,我会获得该帐户的四个规则副本。

I would expect this to simply add the logical rule AccountIsGood($account) if atleast one section was good. 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?

帐户类:

public class Account {
  private List<SubSection> subsections;

  // Getters / Setters/ other code
}


推荐答案

保持规则尽可能简单。虽然检查是否存在插入的 AccoundIsGood()是可行的,但建议编写逻辑,以便仅测试是否存在单个良好子部分。此外,您需要中的来从Account对象中提取子部分。

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. Also, you need a from to extract the subsections from the Account object.

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

保持 insertLogical <可能是个好主意/ code>,如果分段状态可能会改变远离GOOD:如果accound没有至少一个GOOD部分,则逻辑插入的事实将被撤回。

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.

这篇关于Drools规则格式仅触发一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆