Drools 规则语言:规则并不总是被触发

Drools Rule Language: rule not always fired

我是 Drools Fusion 的新手,我想弄清楚为什么我的规则不是 总是被解雇。我正在使用 Drools 6.3。这些是我要插入的事件:

    private static void initMessageObject() {
    SessionPseudoClock clock = ksession.getSessionClock();

    hellodrools2.AppInfo app = new hellodrools2.AppInfo();
    entryPoint1.insert(app);
    System.out.println(app);

    Steps step1 = new Steps(25, 0);
    entryPoint1.insert(step1);

    clock.advanceTime(30, TimeUnit.MINUTES);

    Steps step2 = new Steps(15, 0);
    entryPoint1.insert(step2);

    clock.advanceTime(15, TimeUnit.MINUTES);

    Steps step3 = new Steps(25, 0);
    entryPoint1.insert(step3);

    try {
        System.err.println("[[ Sleeping ...]]");
        Thread.sleep(5000);
    } catch (final InterruptedException e) {
        e.printStackTrace();
    }
    System.err.println("[[ awake ...]]");

    ksession.halt();
    ksession.dispose();
}

这是我的规则文件:

import hellodrools.Steps
import hellodrools.AppInfo

declare Steps
    @role(event)
end

rule "STEPS RULE"
when
    $totalSteps : Number( doubleValue < 50 ) from accumulate(
        Steps( stepsCount : steps ) over window:time( 1h ) from entry-point
       entryone, sum( stepsCount ) )
then
    System.out.println("STEPS RULE: get moving!");
    System.out.println($totalSteps);
end

这是我的输出:

AppInfo{startTime=Sat Feb 27 21:30:42 CET 2016}
[[ Sleeping ...]]
STEPS RULE: get moving!
0.0
[[ awake ...]]

我希望我的规则会触发 2 次并给出以下输出:

AppInfo{startTime=Sat Feb 27 21:30:42 CET 2016}
[[ Sleeping ...]]
STEPS RULE: get moving!
25.0
STEPS RULE: get moving!
40.0
[[ awake ...]]

我可能忽略了一些事情,但我没有找到太多关于我的信息 问题。有人能解释一下这里到底发生了什么吗? 谢谢。

运行使用 Drools 处理事件有两种很好的方法。

一个是运行线程中的会话,调用fireUntilHalt,同时使用实时时钟。在生产模式下,事件将到达并被插入;为了进行测试,使用一个线程进行模拟 运行 插入一个脚本来插入事件事实并暂停让实时流逝(用秒替换分钟)。

另一个非常适合测试,使用伪时钟和一系列带有时间戳的事件事实(单独或作为属性),执行三元组

clock.advance( ...event.timestamp - clock.getCurrentTime()... );
wmep.insert( event );
ksession.fireAllRules();

为每个事件重复。这应该适用于 5.5 及更高版本,产生您期望的结果。