2016-09-29 14 views
0

Droolsルールの大部分は、類似のwhen部分を持っています。例えば。Droolsの `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 

それが1時間この声明

not ConfirmEvent (
    processMessageId == trn.groupId) 

を定義して、必要ならば何とか再利用することは可能ですか?

答えて

2

二つのアプローチのアイデア:

  1. ルールを使用して、共有するときのステートメントを含む基本ルールを拡張するために、各ルールでキーワード「拡張します」。

  2. ファクトを推論する共有のwhen文(「抽出規則」)を使用してルールを作成します。共有条件が必要なルールの条件でその事実を使用する。このオプションは、それらの条件に対して「概念」(名前付きファクト)を定義し、各ルールに対して1回だけ評価するため、通常は私の推奨アプローチです。 #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

あなたは、plsは、第二の考えを説明し、コード(ルール)のいくつかの平和とそれを示してもらえますか? –

+0

私の答えに追加されました。 – Jeff

+0

例をありがとう! –

1

CEは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と他人のために:trnは別のCEによって導入されているのでしかし、あなたはそれらの組み合わせに基づいて拡張を使用することができます。

関連する問題