2011/7/23 Chris Richmond <span dir="ltr">&lt;<a href="mailto:crichmond@referentia.com">crichmond@referentia.com</a>&gt;</span><br><div class="gmail_quote"><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<u></u>

  
    
  
  <div bgcolor="#ffffff" text="#000000">
    Wolfgang,<br>
    <br>
    Thanks very much for that explanation, it cleared up many questions.<br>
    <br>
    &quot;...If the order of your concurrently arriving events is essential,
    you may have to insert an ordinal as a property...&quot;<br>
    <br></div></blockquote><div><br>Is &quot;sequence number&quot; a better term for this additional property? Usually it is not considered necessary, either because (in reality) events never arrive concurrently (according to the clock providing the timestamps!) or because it does not matter. It&#39;s not an elegant solution: you&#39;ll need additional constraints to achieve firing in order, and you may have to retract or mark processed events to be able to write <a href="http://s.th">s.th</a>.<br>
<br>Event( $ord: ordinal, /* processed == false */ ... )<br>not Event( ordinal &lt; $ord, /* processed == false */ )<br><br>An entirely different approach would be to use a custom Agenda strategy, processing activiations FIFO. But that would be rather hefty.<br>
<br>There&#39;s also the issue of running the engine in a thread of its own (fireUntilHalt) as opposed to alternate between single insertions and fireAllRules(). Obviously the latter would guarantee processing in order of arrival.<br>
<br>All of the aforementioned have their pros and cons - much depends on circumstances defined by your application.<br>-W<br><br> </div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div bgcolor="#ffffff" text="#000000">
    Can you expand on this idea or in an general practice for handling
    real time streams of data that need to be processed in order they
    were injected?<br>
    <br>
    Thanks,<br><font color="#888888">
    Chris</font><div><div></div><div class="h5"><br>
    <br>
    <br>
    <br>
    <br>
    On 7/21/2011 7:54 PM, Wolfgang Laun wrote:
    <blockquote type="cite">The insertion of DataReading events happens
      &quot;instantaneously&quot;, without Drools regaining &quot;consciousness&quot; in
      between. So, when the Engine wakes up, it finds 20 activations in
      its agenda, with the same salience. Tie breaking is defined to use
      LIFO.<br>
      <br>
      Running the Engine in real time, these 20 events are indeed
      concurrently from the Engine&#39;s point of view, and any order of
      firings is justified; all the more so because your rule contains
      nothing to prefer a firing of an older event.<br>
      <br>
      If the order of your concurrently arriving events is essential,
      you may have to insert an ordinal as a property.<br>
      <br>
      Cheers<br>
      -W<br>
      <br>
      <div class="gmail_quote">On 22 July 2011 11:07, Chris Richmond <span dir="ltr">&lt;<a href="mailto:crichmond@referentia.com" target="_blank">crichmond@referentia.com</a>&gt;</span>
        wrote:<br>
        <blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">I am running a simple test like so:<br>
          <br>
          package com.sample;<br>
          <br>
          import org.drools.KnowledgeBase;<br>
          import org.drools.KnowledgeBaseFactory;<br>
          import org.drools.builder.KnowledgeBuilder;<br>
          import org.drools.builder.KnowledgeBuilderError;<br>
          import org.drools.builder.KnowledgeBuilderErrors;<br>
          import org.drools.builder.KnowledgeBuilderFactory;<br>
          import org.drools.builder.ResourceType;<br>
          import org.drools.io.ResourceFactory;<br>
          import org.drools.logger.KnowledgeRuntimeLogger;<br>
          import org.drools.logger.KnowledgeRuntimeLoggerFactory;<br>
          import org.drools.runtime.KnowledgeSessionConfiguration;<br>
          import org.drools.runtime.StatefulKnowledgeSession;<br>
          import org.drools.runtime.conf.ClockTypeOption;<br>
          import org.drools.runtime.rule.WorkingMemoryEntryPoint;<br>
          <br>
          import com.sample.DroolsTest.Message;<br>
          <br>
          public class FusionMain {<br>
          <br>
            public static final void main(String[] args) {<br>
          <br>
          <br>
          <br>
          <br>
              try {<br>
          <br>
                KnowledgeSessionConfiguration config =<br>
          KnowledgeBaseFactory.newKnowledgeSessionConfiguration();<br>
                config.setOption( ClockTypeOption.get(&quot;realtime&quot;) );<br>
                KnowledgeBase kbase;<br>
                kbase = readKnowledgeBase();<br>
                final StatefulKnowledgeSession ksession =<br>
          kbase.newStatefulKnowledgeSession();<br>
          <br>
                WorkingMemoryEntryPoint myStream =<br>
          ksession.getWorkingMemoryEntryPoint(&quot;My Stream&quot;);<br>
          <br>
                Thread t = new Thread(new Runnable(){<br>
          <br>
                  @Override<br>
                  public void run() {<br>
                    ksession.fireUntilHalt();<br>
          <br>
                  }<br>
          <br>
                });<br>
          <br>
                t.start();<br>
          <br>
                for (float x = 0.0f; x &lt; 20.0f; x++){<br>
                  DataReading dr = new DataReading(&quot;Reading &quot; + x, x);<br>
                  myStream.insert(dr);<br>
                }<br>
          <br>
          <br>
              } catch (Exception e) {<br>
                // TODO Auto-generated catch block<br>
                e.printStackTrace();<br>
              }<br>
          <br>
            }<br>
          <br>
            private static KnowledgeBase readKnowledgeBase() throws
          Exception {<br>
              KnowledgeBuilder kbuilder =<br>
          KnowledgeBuilderFactory.newKnowledgeBuilder();<br>
             
          kbuilder.add(ResourceFactory.newClassPathResource(&quot;Sample.drl&quot;),<br>
          ResourceType.DRL);<br>
              KnowledgeBuilderErrors errors = kbuilder.getErrors();<br>
              if (errors.size() &gt; 0) {<br>
                for (KnowledgeBuilderError error: errors) {<br>
                  System.err.println(error);<br>
                }<br>
                throw new IllegalArgumentException(&quot;Could not parse
          knowledge.&quot;);<br>
              }<br>
              KnowledgeBase kbase =
          KnowledgeBaseFactory.newKnowledgeBase();<br>
             
          kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());<br>
              return kbase;<br>
            }<br>
          <br>
          <br>
          }<br>
          <br>
          With the following rule in the sample.drl file:<br>
          <br>
          package com.sample<br>
          import com.sample.DroolsTest.Message;<br>
          import java.util.Date;<br>
          <br>
          declare DataReading<br>
              @role( event )<br>
          <br>
          end<br>
          <br>
          rule &quot;MyGuidedRule&quot;<br>
              dialect &quot;mvel&quot;<br>
              when<br>
                  $dr: DataReading( reading &gt; &quot;10.0&quot; ) from
          entry-point &quot;My Stream&quot;<br>
              then<br>
                  System.err.println(&quot;Reading: &quot; +  $<a href="http://dr.name" target="_blank">dr.name</a>
          + &quot; &gt; 10.0 &quot; +<br>
          System.currentTimeMillis());<br>
          end<br>
          <br>
          know I am running fireUntilHalt on a seperate thread as you
          can see, but<br>
          events injected fire rules out of order...in fact almost the
          exact<br>
          opposite order in which they were inserted to the stream every
          time:<br>
          <br>
          Reading: Reading 19.0 &gt; 10.0 1311325346490<br>
          Reading: Reading 18.0 &gt; 10.0 1311325346491<br>
          Reading: Reading 17.0 &gt; 10.0 1311325346491<br>
          Reading: Reading 16.0 &gt; 10.0 1311325346491<br>
          Reading: Reading 15.0 &gt; 10.0 1311325346491<br>
          Reading: Reading 14.0 &gt; 10.0 1311325346491<br>
          Reading: Reading 13.0 &gt; 10.0 1311325346491<br>
          Reading: Reading 12.0 &gt; 10.0 1311325346491<br>
          Reading: Reading 11.0 &gt; 10.0 1311325346491<br>
          <br>
          I want to eventually perform temporal reasoning, but how can I
          expect<br>
          that to work if events are not evaluated in the order they
          were inserted.<br>
          <br>
          Perhaps I am setting up my session/kb improperly?<br>
          <br>
          Thanks,<br>
          Chris<br>
          _______________________________________________<br>
          rules-users mailing list<br>
          <a href="mailto:rules-users@lists.jboss.org" target="_blank">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>
      <pre><fieldset></fieldset>
_______________________________________________
rules-users mailing list
<a href="mailto:rules-users@lists.jboss.org" target="_blank">rules-users@lists.jboss.org</a>
<a href="https://lists.jboss.org/mailman/listinfo/rules-users" target="_blank">https://lists.jboss.org/mailman/listinfo/rules-users</a>
</pre>
    </blockquote>
    <br>
  </div></div></div>

<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>
<br></blockquote></div><br>