Thanks Wolfgang.
I was trying to solve the whole situation with just 1 rule, but not I'm trying to separate the problem in multiple rules. There are some bugs/language-limitations/programmer-skills-limitations that are preventing me to get the expected results though.
Let's say I have a rule identifying my last 3 EventA (ALL my facts are declared as events):
declare Last3A
@role(event)
firstEvent: EventA
lastEvent: EventA
end
rule 'Detect last 3'
when
//some magic using sliding windows
then
//$firstEventA and $lastEventA are the bounds identified in the LHS of the rule.
//EventA class doesn't have an explicit timestamp (drools is providing it) and I'd prefer to leave it that way.
insert (new Last3A($fistEventA, $lastEventA));
end
Then I have my rule trying to get the occurences of EventB that happen during a Last3A
rule 'Get EventBs'
when
$l: Last3A()
//originally, I've used a collect here since I wanted a single activation, but I'll use a single event here just to make syntax cleaner.
EventB(this after $l.firstEvent,this before $l.lastEvent)
then
...
end
If I try the latter rule, after I insert the third EventA (I'm calling fireAllRules() after each insertion) I get the following exception:
Exception executing consequence for rule "Detect last 3" in com.whatever: java.lang.ClassCastException: com.whatever.EventA cannot be cast to java.lang.Number
...
Caused by: java.lang.ClassCastException: com.whatever.EventA cannot be cast to java.lang.Number
at org.drools.base.evaluators.AfterEvaluatorDefinition$AfterEvaluator.evaluateCachedLeft(AfterEvaluatorDefinition.java:341)
I'm assuming Drools is trying to cast the result of $l.firstEvent into a number (probably because it is expecting a timestamp) even though the result is already an Event.
In order to avoid the $l.firstEvent and $l.lastEvent invocations I re-wrote the rule as follows (is ugly, I know):
rule 'Get EventBs'
when
$l: Last3A()
$firstEvent: EventA() from $l.getFirstEvent()
$lastEvent: EventA() from $l.getLastEvent()
EventB(this after $firstEvent,this before $lastEvent)
then
...
end
The exception now is:
Exception executing consequence for rule "Detect last 3" in com.cognitive.nsf.management: java.lang.ClassCastException: org.drools.common.DefaultFactHandle cannot be cast to org.drools.common.EventFactHandle
What is happening now is that Drools doesn't realize that the object I'm using for after and before are actually Events.
So it seems that temporal operators can only be used with events coming from the same rule (and not coming from a 'from' or any other black box).
I'll continue digging on this, but it seems that the only solution will be to use explicit time stamps and then use them instead of the events in my marker fact.
Best Regards,