Ah sorry: the output:
1. fireAllRules()
2. fireAllRules()
Events in last 3 seconds: 1
3. fireAllRules()
Events in last 3 seconds: 2
4. fireAllRules()
Events in last 3 seconds: 3
5. fireAllRules()
Events in last 3 seconds: 3
6. fireAllRules()
Events in last 3 seconds: 3
7. fireAllRules()
Events in last 3 seconds: 3
20sec after room was modified
Events in last 3 seconds: 0
-- there is another rule in drl file that fires 20s after the room was inserted:
rule "timerRuleAfterAllEvents"
timer ( int: 20s )
when
$room : Room( )
then
System.out.println("20sec after room was modified");
end
Somehow this rule triggers the "slidingTimeCount" rule activation again.
On 06.09.2013, at 10:39, 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