Drools规则格式仅触发一次

编程入门 行业动态 更新时间:2024-10-20 16:25:16
本文介绍了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 ,如果分段状态可能会改变远离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规则格式仅触发一次

本文发布于:2023-11-27 12:22:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1638054.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:规则   格式   Drools

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!