drools窗口不工作

我正在使用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

回答:

尝试这种情况:

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);

}

以上是 drools窗口不工作 的全部内容, 来源链接: utcz.com/qa/262415.html

回到顶部