drools窗口不起作用

时间:2017-12-18 10:13:09

标签: java events jboss drools

我正在使用drools(7.5.0.Final版本),我需要帮助计算窗口中的所有事件。运行此代码时,在我的控制台中显示16,而我的预期结果是“6 6 4”。 请帮我解决这个问题。

Java Class

@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 file

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

Runner Class

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

我的结果=&gt; 16

预期结果=&gt; 6 6 4或4 6 6

1 个答案:

答案 0 :(得分: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);
}