<br>   Hi Tim,<br><br>   There are some concepts that one has to have in mind when playing with temporal reasoning and events. It is clear by your e-mail that we need to do better in documenting all that, as it is complex. I am also trying to improve the engine feedback to help users catch these problems more easily.<br>
<br>* Sliding windows only work and make sense on STREAM mode, although I think Drools 5.0.1 does not generate proper errors for trying to use them in CLOUD mode. I fixed that in 5.1 and now it raises a compile time error. <br>
<br>* Whatever temporal reasoning you are doing in STREAM mode, the session clock is integral part of it and needs to be taken into account. That is because a sliding window always use the current time as the reference date (or anchor if you will). I am not seeing any clock management in your code snippet, so I am assuming you are using the real time clock.<br>
<br>* Having said all that just to layout the ground, your case is clearly a case of using CLOUD mode. In cloud mode, you have all your events in advance and you reason over them. In STREAM mode, you are constantly receiving new events, so the engine has to be prepared to react to them or wait for them when necessary. That is why it also does not make sense to use STREAM mode with stateless sessions.<br>
<br>   So, we understand now that you have all your events in advance (so we will use CLOUD mode), and it does not make sense to use sliding windows. All you want to check is if given a time range you have more than 5 failed responses, so you need an interval fact that contains such date range. The rule would look like this:<br>
<br>declare Payment<br>
        @role( event )<br>
        @timestamp( created )<br>
end<br><br>
rule &quot;Block IP with more than 5 failed payments in the last 24 hours&quot;<br>
when<br>
    $fact : BlockRuleResponse()<br>   $int : Interval() <br>
    $count : Number( intValue &gt; 4 ) from accumulate(<br>
                     Payment(responseCode == 0, this after $int.initDate, this before $int.endDate),<br>
                                   count(1) ) )<br>
then<br>
    System.out.println(&quot;More than 5 failures in 24 hours&quot;);<br>
    System.out.println( $count );<br>
    $fact.setBlocked( true );<br>
end<br><br>    Hope it helps,<br>          Edson<br><br><br><div class="gmail_quote">2009/12/4 Tim S <span dir="ltr">&lt;<a href="mailto:tim_siney@salvationarmy.org">tim_siney@salvationarmy.org</a>&gt;</span><br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<br>
When I run a test case against drools from within eclipse, it will return the<br>
expected results the first time, but fails every time after that. I&#39;d like<br>
to know what&#39;s going on and if my rules are written correctly.<br>
<br>
The object of the rule is to check to see a particular IP address has<br>
submitted more than 5 failed payments in the last 24 hours (indicated by<br>
responseCode == 0). The input to this is a List containing all payments from<br>
the IP address and a helper object to indicate whether further action needs<br>
to be taken by way of a boolean flag.<br>
<br>
declare Payment<br>
        @role( event )<br>
        @timestamp( created )<br>
end<br>
<br>
rule &quot;Block IP with more than 5 failed payments in the last 24 hours&quot;<br>
when<br>
    $fact : BlockRuleResponse()<br>
    $count : Number( intValue &gt; 4 ) from accumulate(<br>
                     Payment(responseCode == 0, $id : id) over window:time(<br>
24h ),<br>
                                count( $id ) )<br>
then<br>
    System.out.println(&quot;More than 5 failures in 24 hours&quot;);<br>
    System.out.println( $count );<br>
    $fact.setBlocked( true );<br>
end<br>
<br>
The code in my test case which runs this is:<br>
<br>
        BlockRuleResponse result = new BlockRuleResponse();<br>
        result.setBlocked(false);<br>
<br>
        // Add all commands to this rule session<br>
        List&lt;Command&gt; cmds = new ArrayList&lt;Command&gt;();<br>
        cmds.add(CommandFactory.newInsert(result));<br>
        cmds.add(CommandFactory.newInsertElements(payments));<br>
<br>
        StatelessKnowledgeSession ksession =<br>
getBase().newStatelessKnowledgeSession();<br>
        ksession.execute(CommandFactory.newBatchExecution(cmds));<br>
        log.debug(result.isBlocked());<br>
<br>
        return result.isBlocked();<br>
<br>
The getBase() method just returns a KnowlegdeBase containing the rule above.<br>
I then create two Lists of payment objects, one with creation dates set to<br>
new Date(), the other with creation dates set more than 24 hours ago. I run<br>
these through the rules engine and the result returned is always true.<br>
<br>
I&#39;ve tried using cloud + stream mode and also using a stateful session and<br>
inserting the donations then firing all rules but none of this seems to<br>
work. Stream mode always returns false, likewise stateful sessions always<br>
return false whereas stateless cloud mode always returns true after the<br>
first run.<br>
<br>
Anyone have any idea what I&#39;m missing?<br>
<font color="#888888"><br>
<br>
--<br>
View this message in context: <a href="http://n3.nabble.com/Rules-returning-unexpected-results-tp68205p68205.html" target="_blank">http://n3.nabble.com/Rules-returning-unexpected-results-tp68205p68205.html</a><br>
Sent from the Drools - User mailing list archive at Nabble.com.<br>
_______________________________________________<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>
</font></blockquote></div><br><br clear="all"><br>-- <br>  Edson Tirelli<br>  JBoss Drools Core Development<br>  JBoss by Red Hat @ <a href="http://www.jboss.com">www.jboss.com</a><br>