2013-09-30 31 views
1

我正在嘗試爲某個事件編寫一個規則,該事件需要檢查某些事件是否發生在某個時間窗口之後。目前的代碼如下所示(它工作正常):如何在drools-fusion中編寫動態臨時規則

rule "Detect BPM reseed not starting when requested from Mart" 
     when 
      $martDailyRefreshRequestedEvent: MessageSentEvent(
       $correlationId: correlationId, 
       $when: timestamp, 
       messageTypeName == "MartDailyRefreshCompletedEvent") 
        from entry-point "mart" 
      not (MessageHandleStartedEvent(
        this after[0ms, 30s] $martDailyRefreshRequestedEvent, 
        correlationId == $correlationId, 
        messageTypeName == "MartDailyRefreshCompletedEvent") 
          from entry-point "bpm") 
     then 
      notifier.notify("BPM not responding to MartDailyRefreshCompletedEvent quick enough", 
       String.format(
        "At **%s** Mart sent out a **MartDailyRefreshCompletedEvent**.\n\n**BPM** was supposed to react to it within **30 seconds**.", 
        $when)); 
    end 

此刻的30s實際上是硬編碼。我讀到,如果你想參數化規則,你需要使用其他事實來聲明KB,但我無法弄清楚如何處理時態規則。

所以:如何在該規則中配置30s以便我可以更改應用程序外部的值?像這樣:MessageHandleStartedEvent(this after [ $duration ] ...

回答

0

您可以使用模板爲了從Drools外部提供硬編碼的30。

template dynamicTimer 
rule "Detect BPM reseed not starting when requested from Mart" 
    when 
     $martDailyRefreshRequestedEvent: MessageSentEvent(
      $correlationId: correlationId, 
      $when: timestamp, 
      messageTypeName == "MartDailyRefreshCompletedEvent") 
       from entry-point "mart" 
     not (MessageHandleStartedEvent(
       this after[0ms, @{timeout}s] $martDailyRefreshRequestedEvent, 
       correlationId == $correlationId, 
       messageTypeName == "MartDailyRefreshCompletedEvent") 
         from entry-point "bpm") 
    then 
     notifier.notify("BPM not responding to MartDailyRefreshCompletedEvent quick enough", 
      String.format(
       "At **%s** Mart sent out a **MartDailyRefreshCompletedEvent**.\n\n**BPM** was supposed to react to it within **@{timeout} seconds**.", 
       $when)); 
end 
end template 

然後,你只需要提供30作爲模板參數:

ObjectDataCompiler converter = new ObjectDataCompiler(); 
InputStream templateStream = getClass().getResourceAsStream(resource.getFilePath()); 
Collection<Map<String, String>> paramMaps = new ArrayList<>(); 
Map<String,String> param = new HashMap<>(); 
param.put("timeout", "30"); 
paramMaps.add(param); 
String drl = converter.compile(paramMaps, templateStream); 
Reader rdr = new StringReader(drl); 
kbuilder.add(ResourceFactory.newReaderResource(rdr), ResourceType.DRL);