2015-06-27 12 views
1

Drools版本是6.2.0和我使用流模式當我使用滑動窗口時不能自動刪除事實

我使用@timestamp來告訴引擎使用事件屬性中的時間戳。

問題是工作內存中的事實數量越來越大,即使事實已經過期(10秒),事實也沒有收回。

我嘗試使用僞時鐘,但它也沒有效果。

這是我的DRL:

package test.drools 

import test.drools.LogEntry; 

declare LogEntry 
    @role(event) 
    @timestamp(callDateTime) 
end 

rule "sliding-window-test" 
    when 
     $msgA: LogEntry($sip: sourceIP) 
     Number(intValue > 2) from accumulate (
      $msgB: LogEntry(sourceIP == $sip, this after $msgA) over  window:time(10s); count($msgB)) 
    then 
     System.out.println("rule sliding-window-test action actived!!"); 
     retract($msgA) 
end 

這是我的代碼:

public class LogEntry { 
    private String logcontent = null; 
    private String[] logFieldStrArray = null; 

    private String sourceIP = null; 
    private long callDateTime; 

    public LogEntry(String content) { 
     this.logcontent = content; 
     if (logFieldStrArray == null) { 
      logFieldStrArray = logcontent.split("\\,"); 
     } 

     sourceIP = logFieldStrArray[6]; 

     **callDateTime = System.nanoTime();** 
    } 

    public long getcallDateTime() { 
     return callDateTime; 
    } 

    public String getsourceIP() { 
     return sourceIP; 
    } 
} 

會話配置是正確的,這裏只是展示如何調用時鐘advanceTime。 使用僞時鐘,advanceTime。

public class DroolsSession { 
    private long beginTime = 0, curTime = 0; 

    private statfulKsession; 

    private Object syncobject; 

    public void InsertAndFireAll(Object obj) { 
     synchronized(syncobject) { 
      if (beginTime == 0) { 
       beginTime = ((LogEntry)obj).getcallDateTime(); 
      } else { 
       curTime = ((LogEntry)obj).getcallDateTime(); 
       long l = advanceTime(curTime - beginTime, TimeUnit.NANOSECONDS); 
       beginTime = curTime; 
      } 
      statfulKsession.insert(obj); 
      statfulKsession.fireAllRules(); 
     } 
    } 
} 

順便說一句,我用的是System.nanoTime(),確實Drools支持nanoTime

我期待着您的回答。這是我的榮幸。

+0

我知道@expires()LogEntry事件將工作。但我不知道什麼是「安全」的時間,因爲會有更多的規則,這些新規則可能使用LogEntry,所以Drools引擎應該負責管理工作記憶中事實的生命週期。 – HarryMao

回答

0

什麼規則的條件「滑動窗口測試」說的是: 如果有一個LogEntry事件A(不管是什麼,不管多大年紀)和 如果有兩個以上LogEntry事件小於後面並使用與最近10秒內到達的源IP相同的IP地址:然後回退A.

這不允許自動收回LogEntry事件,因爲它總是有可能在第二個條件稍後完成。