0

I'm new to Drools Fusion and I'm trying to figure out why my rule isn't always fired. I'm using Drools 6.3. These are the events I'm inserting:

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

And this is my rule file:

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

This is my output:

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

I would expect that my rule would fire 2 times and give the following output:

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

I'm probably overlooking some things but I didn't find much information on my problem. Could someone explain what is happening here exactly? Thank you.

Tim
  • 445
  • 5
  • 19

1 Answers1

1

There are two good ways of running event processing with Drools.

One is to run a session in a thread, calling fireUntilHalt, while using a real-time clock. In production mode, events will arrive and be inserted; for testing, simulate with a thread running a script for inserting event facts and pausing to let real time go by (having replaced minutes by seconds).

The other one, well suited for testing, uses a pseudo-clock and a sequence of event facts with their time stamps (separate or as an attribute), executing the triple

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

repeatedly for each event. This should work for versions 5.5 and later, producing the result you expect.

laune
  • 31,114
  • 3
  • 29
  • 42
  • Thank you for answering. However this is still not the behaviour I want. When I do `ksession.fireAllRules();` after every event I get the following output: `STEPS RULE: get moving! 25.0 STEPS RULE: get moving! 40.0 STEPS RULE: get moving! 40.0` I do not understand why I get two times 40.0? – Tim Feb 29 '16 at 13:57
  • 1
    Works for me. - Edit your question ("**Edit:**...") showing the code as you have it now. *Adding the Drools version is essential with all Drools questions.* – laune Feb 29 '16 at 15:37
  • 1
    Works for me, using 6.3.0, as required. – laune Feb 29 '16 at 17:13
  • Yeah now for me to, I made a small change in my rule file and that's why I had a different outcome. Sorry and thank you for your time. – Tim Feb 29 '16 at 17:46