<div dir="ltr">Thanks Wolfgang. <div style>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.</div>
<div style><br></div><div style>Let's say I have a rule identifying my last 3 EventA (ALL my facts are declared as events): </div><div style><br></div><div style><br></div><div style><div>declare Last3A</div><div> @role(event)</div>
<div> firstEvent: EventA</div><div> lastEvent: EventA</div><div>end</div></div><div style><br></div><div style>rule 'Detect last 3'</div><div style>when</div><div style> //some magic using sliding windows</div>
<div style>then</div><div style> //$firstEventA and $lastEventA are the bounds identified in the LHS of the rule.</div><div style> //EventA class doesn't have an explicit timestamp (drools is providing it) and I'd prefer to leave it that way. </div>
<div style> insert (new Last3A($fistEventA, $lastEventA)); </div><div style>end</div><div style><br></div><div style><br></div><div style>Then I have my rule trying to get the occurences of EventB that happen during a Last3A</div>
<div style><br></div><div style>rule 'Get EventBs'</div><div style>when</div><div style> $l: Last3A()</div><div style> //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.</div>
<div style> EventB(this after $l.firstEvent,this before $l.lastEvent)</div><div style>then</div><div style> ...</div><div style>end</div><div style><br></div><div style>If I try the latter rule, after I insert the third EventA (I'm calling fireAllRules() after each insertion) I get the following exception:</div>
<div style><br></div><div style><b>Exception executing consequence for rule "Detect last 3" in com.whatever: java.lang.ClassCastException: com.whatever.EventA cannot be cast to java.lang.Number</b><br></div><div style>
<b>...</b></div><div style><b>Caused by: java.lang.ClassCastException: </b><b>com.whatever.EventA</b><b> cannot be cast to java.lang.Number<br></b></div><div style><b>at org.drools.base.evaluators.AfterEvaluatorDefinition$AfterEvaluator.evaluateCachedLeft(AfterEvaluatorDefinition.java:341)<br>
</b></div><div style><br></div><div style>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.</div>
<div style><br></div><div style>In order to avoid the $l.firstEvent and $l.lastEvent invocations I re-wrote the rule as follows (is ugly, I know):</div><div style><br></div><div>rule 'Get EventBs'</div><div>when</div>
<div> $l: Last3A()</div><div> $firstEvent: EventA() from $l.getFirstEvent()</div><div> $lastEvent: EventA() from $l.getLastEvent()</div><div> EventB(this after $firstEvent,this before $lastEvent)</div><div>then</div>
<div> ...</div><div style>end </div><div style><br></div><div style>The exception now is:</div><div style><br></div><div style><b>Exception executing consequence for rule </b><b>"Detect last 3"</b><b> in com.cognitive.nsf.management: java.lang.ClassCastException: org.drools.common.DefaultFactHandle cannot be cast to org.drools.common.EventFactHandle</b><br>
</div><div style><b><br></b></div><div style>What is happening now is that Drools doesn't realize that the object I'm using for after and before are actually Events.</div><div style>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).</div>
<div style>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.</div><div style><br></div><div style>Best Regards,</div>
<div style><br></div><div style><br></div><div style><br></div><div style><br></div></div><div class="gmail_extra"><br clear="all"><div><br>XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX<br><br>Esteban Aliverti<br>- Blog @ <a href="http://ilesteban.wordpress.com" target="_blank">http://ilesteban.wordpress.com</a></div>
<br><br><div class="gmail_quote">On Mon, Jan 7, 2013 at 12:17 PM, Wolfgang Laun <span dir="ltr"><<a href="mailto:wolfgang.laun@gmail.com" target="_blank">wolfgang.laun@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Use "wrapper" or "marker" facts to identify the last three EventA's<br>
and a rule to "push" EventA's through these markers. Then, a simple<br>
rule will give you all EventBs between the extremes of the marker set.<br>
<br>
Take care to get the start-up right ;-)<br>
<br>
-W<br>
<div><div class="h5"><br>
On 07/01/2013, Esteban Aliverti <<a href="mailto:esteban.aliverti@gmail.com">esteban.aliverti@gmail.com</a>> wrote:<br>
> Let's say I have two independent streams of different events: EventA and<br>
> EventB. Which is the best way to get all the occurrences of EventB that<br>
> happened between the last 3 occurrences of EventA?<br>
><br>
> For example:<br>
><br>
> from:<br>
> $b1:EventB, $a1:EventA, $b2:EventB, $b3:EventB, $a2:EventA,<br>
> $b4:EventB, $b5:EventB, $a3:EventA, $b6:EventB<br>
><br>
> I would like to get $b2, $b3, $b4, $b5<br>
><br>
> Best Regards,<br>
><br>
><br>
><br>
><br>
> XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX<br>
><br>
> Esteban Aliverti<br>
> - Blog @ <a href="http://ilesteban.wordpress.com" target="_blank">http://ilesteban.wordpress.com</a><br>
><br>
</div></div>_______________________________________________<br>
rules-users mailing list<br>
<a href="mailto:rules-users@lists.jboss.org">rules-users@lists.jboss.org</a><br>
<a href="https://lists.jboss.org/mailman/listinfo/rules-users" target="_blank">https://lists.jboss.org/mailman/listinfo/rules-users</a><br>
</blockquote></div><br></div>