【问题标题】:Execution of a single rule for a group in Drools在 Drools 中为组执行单个规则
【发布时间】:2019-04-08 06:06:57
【问题描述】:

我正在研究基于立法的专家系统,我有很多这样的规则:

规则 1: 如果扣押金额大于3000,扣押金额,理由律100

规则2:如果扣押属于家庭类型,扣押金额,理由法200

问题是“Seize”这个动作只能应用一次,但是我需要保存遇到的规则的历史,下面我举个例子

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 中重新应用。

感谢您的建议

【问题讨论】:

    标签: java drools expert-system


    【解决方案1】:

    这里有几个选项。

    1. applyPunishment 更改为幂等。

      您没有显示applyPunishment 的代码,但它可能看起来像

      private boolean alreadySeized = false;
      
      public void applyPunishment() {
          if (alreadySeized) {
              return;
          }
      
          alreadySeized = true;
      

      您也可以基于其他一些已经存在的变量。例如。 if (seizedAmount > 0) return;。但是很难说没有代码它会如何工作。

    2. 您可以将applyPunishment 更改为markForPunishment 之类的东西,它可能看起来像

      private boolean markedForPunishment;
      
      public void markForPunishment() {
          markedForPunishment = true;
      }
      

      然后添加类似

      的规则
      rule "Punish"
      
      when
        $seizure: Seizure(markedForPunishment  == true)
      then
        $seizure.applyPunishment();
      

      使用适当的吸气剂。

      您的其他规则将调用markForPunishment 而不是applyPunishment

    3. 您可以使用ruleflow 将理由与惩罚分开。

    4. 您可以在规则中使用的 then 子句中使用 set a variable

    可能还有其他选择。要做出的重大决定是您想要 MVEL 解决方案还是 Java 解决方案。有几个选项需要同时更改。

    【讨论】:

      猜你喜欢
      • 2022-11-30
      • 2022-09-24
      • 1970-01-01
      • 1970-01-01
      • 2015-06-11
      • 1970-01-01
      • 1970-01-01
      • 2019-06-25
      • 1970-01-01
      相关资源
      最近更新 更多