<div dir="ltr">Thanks Wolfgang. <div style>I was trying to solve the whole situation with just 1 rule, but not I&#39;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&#39;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 &#39;Detect last 3&#39;</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&#39;t have an explicit timestamp (drools is providing it) and I&#39;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 &#39;Get EventBs&#39;</div><div style>when</div><div style>   $l: Last3A()</div><div style>   //originally, I&#39;ve used a collect here since I wanted a single activation, but I&#39;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&#39;m calling fireAllRules() after each insertion) I get the following exception:</div>

<div style><br></div><div style><b>Exception executing consequence for rule &quot;Detect last 3&quot; 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&#39;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 &#39;Get EventBs&#39;</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>&quot;Detect last 3&quot;</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&#39;t realize that the object I&#39;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 &#39;from&#39; or any other black box).</div>

<div style>I&#39;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">&lt;<a href="mailto:wolfgang.laun@gmail.com" target="_blank">wolfgang.laun@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

Use &quot;wrapper&quot; or &quot;marker&quot; facts to identify the last three EventA&#39;s<br>
and a rule to &quot;push&quot; EventA&#39;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 &lt;<a href="mailto:esteban.aliverti@gmail.com">esteban.aliverti@gmail.com</a>&gt; wrote:<br>
&gt; Let&#39;s say I have two independent streams of different events: EventA and<br>
&gt; EventB. Which is the best way to get all the occurrences of EventB that<br>
&gt; happened between the last 3 occurrences of EventA?<br>
&gt;<br>
&gt; For example:<br>
&gt;<br>
&gt; from:<br>
&gt; $b1:EventB, $a1:EventA, $b2:EventB, $b3:EventB, $a2:EventA,<br>
&gt; $b4:EventB, $b5:EventB, $a3:EventA, $b6:EventB<br>
&gt;<br>
&gt; I would like to get $b2, $b3, $b4, $b5<br>
&gt;<br>
&gt; Best Regards,<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt; XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX<br>
&gt;<br>
&gt; Esteban Aliverti<br>
&gt; - Blog @ <a href="http://ilesteban.wordpress.com" target="_blank">http://ilesteban.wordpress.com</a><br>
&gt;<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>