2017-12-18 155 views
0

我正在使用drools(7.5.0.Final版本),我需要幫助計算窗口中的所有事件。在我的控制檯中運行此代碼時顯示16,而我的預期結果是「6 6 4」。 請幫我解決這個問題。drools窗口不工作

的Java類

@Role(Role.Type.EVENT) 
@Timestamp("timestamp") 
public class Event { 

    private int id; 
    private int type; 
    private Date timestamp; 


    public Event() { 

    } 

    public Event(int id, int type, Date timestamp) { 
     this.id = id; 
     this.type = type; 
     this.timestamp = timestamp; 
    } 

    public int getId() { 
     return id; 
    } 

    public void setId(int id) { 
     this.id = id; 
    } 

    public int getType() { 
     return type; 
    } 

    public void setType(int type) { 
     this.type = type; 
    } 

    public Date getTimestamp() { 
     return timestamp; 
    } 

    public void setTimestamp(Date timestamp) { 
     this.timestamp = timestamp; 
    } 

    @Override 
    public String toString() { 
     return "Event{" + 
       "id=" + id + 
       ", type=" + type + 
       ", timestamp=" + timestamp + 
       '}'; 
    } 
} 

DRL文件

rule "R1" 
when 
    total: Number() from accumulate(
     e: Event(type == 2010908) over window:time(100ms), count(e)) 
then 
    System.out.println(total); 
end 

日誌文件

2017/12/17 05:00:00.000 2010908 1 
2017/12/17 05:00:00.010 2010908 2 
2017/12/17 05:00:00.020 2010908 3 
2017/12/17 05:00:00.030 1 1 
2017/12/17 05:00:00.040 2010908 4 
2017/12/17 05:00:00.050 2010908 5 
2017/12/17 05:00:00.060 2100469 1 
2017/12/17 05:00:00.070 2010908 6 
2017/12/17 05:00:00.080 2010908 7 
2017/12/17 05:00:00.090 2010908 8 
2017/12/17 05:00:00.100 2101411 1 
2017/12/17 05:00:00.110 2101417 1 
2017/12/17 05:00:00.120 1 2 
2017/12/17 05:00:00.130 2010908 9 
2017/12/17 05:00:00.140 2010908 10 
2017/12/17 05:00:00.150 2012997 1 
2017/12/17 05:00:00.160 21 1 
2017/12/17 05:00:00.170 2010908 11 
2017/12/17 05:00:00.180 2010908 12 
2017/12/17 05:00:00.190 2010908 13 
2017/12/17 05:00:00.200 1 3 
2017/12/17 05:00:00.210 1 4 
2017/12/17 05:00:00.220 2010908 14 
2017/12/17 05:00:00.230 2010908 15 
2017/12/17 05:00:00.240 2010908 16 

亞軍類

public class Runner { 
    public static void main(String[] args) throws Exception { 
     KieServices ks = KieServices.Factory.get(); 
     KieContainer kc = ks.getKieClasspathContainer(); 

     KieSession kieSession = kc.newKieSession("TKS"); 
     List<Event> events = LogReader.events(); 


     for (Event event : events) { 
      kieSession.insert(event); 
     } 

     kieSession.fireAllRules(); 
     kieSession.dispose(); 
    } 
} 

我的結果=> 16

預期結果=> 6 6 4或4 6 6

回答

0

嘗試這種情況:

for (Event event : events) { 
    kieSession.insert(event); 
    kieSession.fireAllRules(); 
} 

但是對於一個測試,即接近實時的,使用僞並像這樣運行你的測試:

// thread 1 
kieSession.fireUntilHalt(); 

// thread 2 
for (Event event : events) { 
    advance session pseudo clock to event.timestamp 
    kieSession.insert(event); 
}