It's interesting to compare
Thread.sleep(1000*60*1);
with
for(int i=0; i<60; i++ ){
kSession.fireAllRules();
Thread.sleep(1005);
}
where the spurious erroneous firing of "slidingTimeCount" does not fire.
What (I think) happens, is that one additional activation of
"slidingTimeCount" is created while the count is still > 0, but it
never fires. Then, the accumulate/window reduces the accumulated
collection but the constraint intValue > 0 is not tested.
Due to the deplorable lack of documentation I can't say that you
shouldn't run sessions in STREAM mode with repeated fireAllRules() and
random sleeps to let the time pass. In addition to calling
fireAllRules() repeatedly, it is also preferable to run the session
using fireUntilHalt(), inserting from parallel threads. Again, no
light is shed on this by even a shred of documentation...
Cheers
-W
On 06/09/2013, Alexander Wolf <mail(a)alexander-wolf.net> wrote:
Drools Version 5.5.0 FINAL
Here's the code for session setup:
KnowledgeBuilder kbuilder =
KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newClassPathResource("myrule.drl"),
ResourceType.DRL);
// Check the builder for errors
if (kbuilder.hasErrors()) {
System.out.println(kbuilder.getErrors().toString());
throw new RuntimeException("Unable to compile
\"myrule.drl\".");
}
KnowledgeBase kbase = kbuilder.newKnowledgeBase();
//configure knowledge base
KnowledgeBaseConfiguration baseConfig =
KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
baseConfig.setOption( EventProcessingOption.STREAM );
//init session clock
KnowledgeSessionConfiguration sessionConfig =
KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
sessionConfig.setOption( ClockTypeOption.get("realtime") );
//init stateful knowledge session
StatefulKnowledgeSession ksession =
kbase.newStatefulKnowledgeSession(sessionConfig, null);
And here's how I inserted the events for testing purpose:
//get Knowledge Base
StatefulKnowledgeSession ksession = initKnowledgeSession();
//entry point for sensor events
WorkingMemoryEntryPoint sensorEventStream =
ksession.getWorkingMemoryEntryPoint( "SensorEventStream" );
Room room = new Room(24);
ksession.insert(room);
System.out.println("1. fireAllRules()");
ksession.fireAllRules();
//insert events
for(int i=2;i<8;i++){
SensorEvent event = new SensorEvent("inc" + (i-1));
sensorEventStream.insert( event );
System.out.println(i + ". fireAllRules()");
ksession.fireAllRules();
Thread.sleep(1005);
}
//let thread sleep for another 1m to see if dereffered rules fire (timers,
(not) after rules)
Thread.sleep(1000*60*1);
ksession.dispose();
On 05.09.2013, at 12:02, Wolfgang Laun <wolfgang.laun(a)gmail.com> wrote:
> Hard to believe, but who knows.
>
> Drools version and session setup, please.
>
> -W
>
>
> On 05/09/2013, Alexander Wolf <mail(a)alexander-wolf.net> wrote:
>> Hey guys,
>>
>> I wrote a rule to count the SensorEvents in my KnowledgeSession that
>> have
>> been inserted via entry-point SensorEventStream over the last 3 seconds
>> -
>> but I only want the event to fire, if there were any events in the last
>> 3
>> seconds at all.
>>
>> Problem: if there were no recent events, i still get the output:
>>
>>> Events in last 3 seconds: 0
>>
>> I don't really get why?! This is my rule code:
>>
>>
>> rule "slidingTimeCount"
>> when
>> $n: Number (intValue > 0) from accumulate ( $e: SensorEvent() over
>> window:time(3s) from entry-point SensorEventStream, count($e))
>> then
>> System.out.println("Events in last 3 seconds: " + $n);
>> end
>>
> _______________________________________________
> rules-users mailing list
> rules-users(a)lists.jboss.org
>
https://lists.jboss.org/mailman/listinfo/rules-users