我正在开发一个基于立法的专家系统,我有很多这样的规则:
规则 1: 如果扣押金额大于3000,则扣押金额,理由法100
规则2:如果扣押属于家庭类型,扣押金额,正当理由法200
问题是“捕获”操作只能应用一次,但我需要保存满足规则的历史记录,我在下面给出一个示例
rule "law 100"
when
$seizure: Seizure(amount>3000)
then
$seizure.getRules().add("Justification: law 100 of the civil that says bla bla");
$seizure.applyPunishment();
rule "law 200"
when
$seizure: Seizure(type == TYPES.Family)
then
$seizure.getRules().add("Justification: law 200 of the family code that says bla bla");
$seizure.applyPunishment();
正如我上面所示,我需要保存描述规则“$seizure.getRules().add("Justification: law of the Civil Code");”的“then”部分。我还需要如果“$seizure.applyPunishment();”已在规则 1 中应用,不会在规则 2 中重新应用。
谢谢建议
最佳答案
这里有多种选择。
将
applyPunishment
更改为幂等。您没有显示
applyPunishment
的代码,但它可能看起来像private boolean alreadySeized = false; public void applyPunishment() { if (alreadySeized) { return; } alreadySeized = true;
您还可以将其基于已经存在的其他一些变量。例如。
if (seizedAmount > 0) return;
。但很难说如果没有代码,它是如何工作的。您可以将
applyPunishment
更改为markForPunishment
之类的内容,如下所示private boolean markedForPunishment; public void markForPunishment() { markedForPunishment = true; }
然后添加一条规则,例如
rule "Punish" when $seizure: Seizure(markedForPunishment == true) then $seizure.applyPunishment();
带有适当的 setter/getter 。
您的其他规则将调用
markForPunishment
而不是applyPunishment
。您可以使用 ruleflow将正当理由与惩罚分开。
- 你可以set a variable在您的规则中使用的 then 子句中。
可能还有其他选择。需要做出的重大决定是您想要 MVEL 解决方案还是 Java 解决方案。有几个选项需要同时更改两者。
关于java - 在 Drools 中为组执行单个规则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53144943/