1

我正在使用 Drool Fusion 来执行窗口规则。以下是我的测试课:

public class Test {
    int count;

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public Test(int count) {
        this.count = count;
    }
}

当我尝试执行以下规则时,我得到了意想不到的结果:

declare Test
@role(event)
@timestamp(time)
@expires(10m)
end


rule "Sliding window test1"
agenda-group "g0"
when
    $number: Number() from accumulate(Test($t:count <10) over window:time(10s) , average($t))
then
   System.out.println("Test Rule 1 " + $number );
end

rule "Sliding window test2"
agenda-group "g0"
when
    $number: Number() from accumulate(Test($t:count >10 ) over window:time(10s) , average($t))
then
   System.out.println("Test Rule 2 " + $number );
end

响应:在窗口规则结束后,即10 seconds两个规则都被触发。

我的实施者是:

public static void main(String[] args) throws InterruptedException {
        KieServices kieServices = KieServices.Factory.get();
        KieContainer kieContainer = kieServices.getKieClasspathContainer();
        KieSession kieSession = kieContainer.newKieSession("window-session");
        SessionPseudoClock clock = kieSession.getSessionClock();
        long curr = System.currentTimeMillis();
        while(true) {
            long time = System.currentTimeMillis();
            for(int i=0;i<20;i++) {
                Test test = new Test(i);
                kieSession.insert(test);
                kieSession.getAgenda().getAgendaGroup("g0").setFocus();
                clock.advanceTime(time - clock.getCurrentTime(), TimeUnit.MILLISECONDS);
                kieSession.fireAllRules();
                System.out.println(System.currentTimeMillis() - curr);
                Thread.sleep(10); //historical event test
            }
        }
    }

我得到了意想不到的输出,即 10 秒后两个规则都在 1 秒后触发,它重复相同。这种行为仅在我使用Pseudo clock. 请帮助

4

0 回答 0