2016-09-29 51 views
0

我有大量的Drools規則與類似的when部分。例如。重複使用部分Drools` when when陳述

rule "Rule 1" 
    when 
     trn: TransactionEvent() 

     // some `trn` related statements 

     not ConfirmEvent (
      processMessageId == trn.groupId) 
    then 
     // some actions 
end 

rule "Rule 2" 
    when 
     trn: TransactionEvent() 

     // some other `trn` related statements 

     not ConfirmEvent (
      processMessageId == trn.groupId) 
    then 
     // some other actions 
end 

是否有可能定義一個時間這個說法

not ConfirmEvent (
    processMessageId == trn.groupId) 

,並根據需要以某種方式重用?

回答

2

兩種方法的想法:

  1. 使用規則「擴展」與每個規則關鍵字擴展一個基規則包含共享時語句。

  2. 使用推斷事實的共享時語句(「提取規則」)創建規則。在需要共享條件的規則的條件時使用該事實。這個選項通常是我的首選方法,因爲它爲這些條件定義了一個「概念」(一個命名的事實),並且對每個規則只評估一次。

    #2規則例如:

    rule "Transaction situation exists" 
        when 
         trn: TransactionEvent() 
    
         // some `trn` related statements 
    
         $optionalData : // bind as wanted 
    
         not ConfirmEvent (
          processMessageId == trn.groupId) 
        then 
         InferredFact $inferredFact = new InferredFact($optionalData); 
         insertLogical($inferredFact); 
    end 
    
    rule "Rule 1" 
        when 
         InferredFact() 
         AdditionalCondition() 
        then 
         // some actions 
    end 
    
    rule "Rule 2" 
        when 
         InferredFact() 
         OtherCondition() 
        then 
         // some other actions 
    end 
    
+0

你能,請解釋第二個想法,並與代碼(規則)一些和平說明呢? –

+0

添加到我的答案。 – Jeff

+0

謝謝你的例子! –

1

很明顯,not CE本身無效,因爲它指的是一些trn。但由於trn被另一個CE介紹,您可以使用擴展了基於它們的組合:

rule "Commont TE + not CE" 
when 
    trn: TransactionEvent() 
    not ConfirmEvent (processMessageId == trn.groupId) 
then 
end 

rule "Rule 1" extends "Commont TE + not CE" 
when 
    // some `trn` related statements 
then 
    //... 
end 

等,爲Rule 2等。