2009-01-06 72 views
1

我使用Drools(第一次)來表達一些規則,並且它一直在工作迄今爲止。不過,我得到了一個新的條件,我無法用規則語言很清楚地表達出來。編寫Drools/JBoss規則的LHS時出現問題,我匹配一個事實,然後使用該事實來確定是否存在其他事實

基本上我需要對玩家賬戶執行一個操作,如果他們在賬戶之間有一定的餘額,在一定的金額之間,他們還沒有在上週進行支付,他們還沒有進行支付在過去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" 

回答

1

是的,就像你猜測的那樣,你需要在「not」模式中加入一個明確的「and」來將它們連接在一起。

你不需要唯一一次「和」是在頂層:

when Foo() Bar() 

不需要一個「和」

但這隱含地相同

when Foo() and Bar() 

所以你的解決方案似乎是正確的。缺少頂級「和」似乎是大多數規則語言中的約定(回到CLIPS!)

0

什麼($deduction->amountInPence * 4)?我認爲,->應該是.

+0

不幸的是,沒有工作,我會更新與錯誤消息更詳細的問題(他們不不適合發表評論)。 – BenM 2009-01-06 10:59:05

+0

我認爲這是一個編輯錯字 - 是的,「 - >」似乎是錯誤的。這是一個不再需要的舊語法。 – 2009-01-07 02:51:46

1

經過一些更多的黑客攻擊後,不會導致任何運行時錯誤(儘管我不確定它是否「正確」)。我重寫了這個條款,將存在放在事實的兩邊,並使用中綴並對它們進行分組。

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) 
     ) 
    ) 
) 

感謝所有幫助到目前爲止。

+0

你可能不需要「&&」,並可以把每個字段約束用「,」分隔 - 只是一個建議 – 2009-01-07 02:52:42