编写Drools / JBoss规则的LHS的问题我匹配一个事实,然后用这个事实确定是否存在另一个事实

StackOverflow https://stackoverflow.com/questions/415907

  •  03-07-2019
  •  | 
  •  

我正在使用Drools(第一次)表达一些规则,到目前为止它一直很好用。然而,我被赋予了一个新的条件,我无法用规则语言非常清楚地表达。

基本上我需要对玩家账户执行一项操作,如果他们在某个金额之间有未结余额,他们在上周没有付款以及他们没有付款过去4周大于或等于每周扣除。还有一些其他规则,但我已删除它们以简化此问题的规则。这是导致我出现问题的最后一条规则。

rule "The broken rule"
   salience 10
   no-loop
   when
      Player( $playerNumber : playerNumber )
      $a : Account( // balance between £5 and £100 and no arrangement
       playerNumber == $playerNumber &&
         accountBalanceInPence >= 500 &&
         accountBalanceInPence <= 10000
      )
      not ( // no payment in last week
         exists AccountTransaction(
            playerNumber == $playerNumber &&
            transactionDate >= oneWeekAgo &&
            transactionCode == "P" // payment
         )
      )
      /* It's this next bit that is broken */
      not ( // no payment > (weekly cost * 4) paid within last 4 weeks
         $deduction : AccountTransaction( // a recent transaction
            playerNumber == $playerNumber &&
            transactionDate >= fourWeeksAgo &&
            transactionCode == "D" // deduction
         )
         exists AccountTransaction( // the payment
            playerNumber == $playerNumber &&
            transactionDate >= fourWeeksAgo &&
            transactionCode == "P" // payment
            amountInPence >= ($deduction->amountInPence * 4)
         )
   )
   then
      // do some action to the account
end

问题是它只是不起作用,我不断得到org.drools.rule.InvalidRulePackage异常抛出。我只是猜测语法,但似乎无法找到一个显示我正在尝试做的事情的例子。它甚至可能吗?


完整的原始错误消息是:

"unknown:50:3 mismatched token: [@255,1690:1695='exists',<39>,50:3]; expecting type RIGHT_PAREN[54,4]: unknown:54:4 mismatched token: [@284,1840:1852='amountInPence',<7>,54:4]; expecting type RIGHT_PAREN[54,22]: unknown:54:22 Unexpected token '$payment'"

在第一条评论中尝试建议后,错误是:

"[50,3]: unknown:50:3 mismatched token: [@255,1690:1695='exists',<39>,50:3]; expecting type RIGHT_PAREN[54,4]: unknown:54:4 mismatched token: [@284,1840:1852='amountInPence',<7>,54:4]; expecting type RIGHT_PAREN[54,45]: unknown:54:45 mismatched token: [@293,1881:1881='*',<71>,54:45]; expecting type LEFT_PAREN[55,3]: unknown:55:3 mismatched token: [@298,1890:1890=')',<12>,55:3]; expecting type THEN"
有帮助吗?

解决方案

是的,正如你猜测的那样,你需要一个明确的“和”。在“不”内模式将它们连接在一起。

您唯一不需要“和”的时间处于最高水平:

例如

when Foo() Bar()

不需要“和”和

但这与

含义相同
when Foo() and Bar()

所以你的解决方案似乎是正确的。缺乏顶级“和”似乎是大多数规则语言的惯例(回到CLIPS!)

其他提示

经过一些更多的黑客攻击后,不会导致任何运行时错误(虽然我不确定它是否“正确”)。我重写了该条款,将存在于事实周围,并使用了一个中缀并对它们进行分组。

  not ( // no payment > (weekly cost * 4) paid within last 4 weeks
     exists (
        AccountTransaction( // a recent transaction
           playerNumber == $playerNumber &&
           transactionDate >= fourWeeksAgo &&
           transactionCode == "D" // deduction
           $recentDeducation : amountInPence
        ) and
        AccountTransaction( // the payment
           playerNumber == $playerNumber &&
           transactionDate >= fourWeeksAgo &&
           transactionCode == "P" // payment
           amountInPence >= ($recentDeducation * 4)
        )
     )
  )

感谢目前为止提供的所有帮助。

($ deduction-&gt; amountInPence * 4)怎么样?我认为, - &gt; 应该是

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top