<br>   Tom,<br><br>   The way to improve performance is to break the combinatorial problem by using one or both of the following:<br><br>* Write patterns against different classes: instead of writing the rule as A() and A(), use different classes to represent each component and write A() and B().<br>
<br>* Write alpha constraints (constraints against constant values) besides the beta constraints (join). Example:<br><br>$a1: A( type == &quot;source&quot; )<br>$a2: A( type == &quot;destination&quot;, someAttr &gt; $a1.someAttr )<br>
<br>    Both of the above tricks help to break the worst case scenario ( O(n^p), where n is the number of facts and p is the number of patterns).<br><br>    Edson<br><br><div class="gmail_quote">2010/1/21 Tom Carchrae <span dir="ltr">&lt;<a href="mailto:tom@carchrae.net">tom@carchrae.net</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;">Please don&#39;t confuse my message with bashing Drools for not being as<br>
fast as Java.  While I am not very familiar with Rete, I&#39;m aware of<br>
what goes on behind the scenes; I&#39;ve been programming declarative<br>
optimization systems for many years.  My query relates to how to<br>
perform such a comparison with reasonable efficiency in Drools.<br>
<font color="#888888"><br>
Tom<br>
</font><div><div></div><div class="h5"><br>
<br>
On Thu, Jan 21, 2010 at 7:16 PM, Greg Barton &lt;<a href="mailto:greg_barton@yahoo.com">greg_barton@yahoo.com</a>&gt; wrote:<br>
&gt; pit-&gt;out<br>
&gt;<br>
&gt; That falls into a rare class: two letter typos that pass a spell checker. :)<br>
&gt;<br>
&gt; --- On Thu, 1/21/10, Greg Barton &lt;<a href="mailto:greg_barton@yahoo.com">greg_barton@yahoo.com</a>&gt; wrote:<br>
&gt;<br>
&gt;&gt; From: Greg Barton &lt;<a href="mailto:greg_barton@yahoo.com">greg_barton@yahoo.com</a>&gt;<br>
&gt;&gt; Subject: Re: [rules-users] drools performance with &lt;<br>
&gt;&gt; To: &quot;Rules Users List&quot; &lt;<a href="mailto:rules-users@lists.jboss.org">rules-users@lists.jboss.org</a>&gt;<br>
&gt;&gt; Date: Thursday, January 21, 2010, 9:04 PM<br>
&gt;&gt; This is not at all surprising.<br>
&gt;&gt; To set up and run the rules there&#39;s quite a bit of code<br>
&gt;&gt; running behind the scenes besides just the comparison test.<br>
&gt;&gt; (For instance, there&#39;s object creation just to track the<br>
&gt;&gt; fact that two objects matched the rule, and in this case<br>
&gt;&gt; those objects are created for every possible combination of<br>
&gt;&gt; TestObject instances before the first rule ever fires, then<br>
&gt;&gt; added to lists, and so on...  So in the case of 1000<br>
&gt;&gt; TestObject instances, there&#39;s 1,000,000 object creations and<br>
&gt;&gt; list additions just for that.)<br>
&gt;&gt;<br>
&gt;&gt; So, as has been said many times on this list, and probably<br>
&gt;&gt; in a few blogs, for any given algorithm a hard coded java<br>
&gt;&gt; version will invariably be faster than rules.  What<br>
&gt;&gt; rules gives you is a way to express solutions to complex<br>
&gt;&gt; problems in a flexible and comprehensible way.  It<br>
&gt;&gt; shines when the problems are so complex that coding them<br>
&gt;&gt; procedurally is pit of the question.<br>
&gt;&gt;<br>
&gt;&gt; The same relationship is there between C and java.<br>
&gt;&gt; Java gives you managed, portable code, but for that you give<br>
&gt;&gt; up the raw speed of C.<br>
&gt;&gt;<br>
&gt;&gt; --- On Thu, 1/21/10, Tom Carchrae &lt;<a href="mailto:carchrae@gmail.com">carchrae@gmail.com</a>&gt;<br>
&gt;&gt; wrote:<br>
&gt;&gt;<br>
&gt;&gt; &gt; From: Tom Carchrae &lt;<a href="mailto:carchrae@gmail.com">carchrae@gmail.com</a>&gt;<br>
&gt;&gt; &gt; Subject: [rules-users] drools performance with &lt;<br>
&gt;&gt; &gt; To: <a href="mailto:rules-users@lists.jboss.org">rules-users@lists.jboss.org</a><br>
&gt;&gt; &gt; Date: Thursday, January 21, 2010, 6:40 PM<br>
&gt;&gt; &gt; Hello,<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; I&#39;m new to Drools and am trying it out.  I&#39;ve found<br>
&gt;&gt; it<br>
&gt;&gt; &gt; easy enough so<br>
&gt;&gt; &gt; far, but quickly ran into a performance bottleneck.<br>
&gt;&gt; &gt; (perhaps an all<br>
&gt;&gt; &gt; too familiar story! :) ).  So, I made a simple<br>
&gt;&gt; example<br>
&gt;&gt; &gt; to try and<br>
&gt;&gt; &gt; track down the behavior.  Here it is.<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; I wanted to create a rule that uses an &lt;<br>
&gt;&gt; comparison<br>
&gt;&gt; &gt; between pairs of<br>
&gt;&gt; &gt; objects.  The following are the performance results:<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; 10 objects.  Java comparison took 0 ms<br>
&gt;&gt; &gt; 10 objects.  Rule firing took 2516 ms<br>
&gt;&gt; &gt; 100 objects.  Java comparison took 0 ms<br>
&gt;&gt; &gt; 100 objects.  Rule firing took 1984 ms<br>
&gt;&gt; &gt; 1000 objects.  Java comparison took 47 ms<br>
&gt;&gt; &gt; 1000 objects.  Rule firing took 132500 ms<br>
&gt;&gt; &gt; 10000 objects.  Java comparison took 4641 ms<br>
&gt;&gt; &gt; 10000 objects. Rule firing took .... (please wait...)<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; Here is the rule<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; rule &quot;A &lt; B&quot;<br>
&gt;&gt; &gt;     when<br>
&gt;&gt; &gt;         a : TestObject(<br>
&gt;&gt; &gt; )<br>
&gt;&gt; &gt;         b : TestObject(<br>
&gt;&gt; &gt; valueDouble &lt; a.valueDouble )<br>
&gt;&gt; &gt;     then<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; b.setValueint(b.getValueint()+1);<br>
&gt;&gt; &gt; end<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; Now, I do understand that this (worst-case) is a<br>
&gt;&gt; comparison<br>
&gt;&gt; &gt; of N x N<br>
&gt;&gt; &gt; objects... so, 1000 = 1000000 comparisons.  However,<br>
&gt;&gt; &gt; ~100ms per<br>
&gt;&gt; &gt; comparison seems relatively expensive for such a<br>
&gt;&gt; simple<br>
&gt;&gt; &gt; check.  I am<br>
&gt;&gt; &gt; using StatefulKnowledgeSession (I tried Stateless but<br>
&gt;&gt; this<br>
&gt;&gt; &gt; gave only a<br>
&gt;&gt; &gt; mild performance improvement - and I would also like<br>
&gt;&gt; to use<br>
&gt;&gt; &gt; insert in<br>
&gt;&gt; &gt; some of my rules).<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; How would one speed up such a query?  I know part of<br>
&gt;&gt; &gt; the problem is<br>
&gt;&gt; &gt; the &lt; comparison.  The results with == are much<br>
&gt;&gt; &gt; better (presumably, it<br>
&gt;&gt; &gt; can do lots of pruning of the space)<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; Model creation 0<br>
&gt;&gt; &gt; 10 objects.  Rule firing took 2829 ms<br>
&gt;&gt; &gt; 100 objects.  Rule firing took 93 ms<br>
&gt;&gt; &gt; 1000 objects.  Rule firing took 657 ms<br>
&gt;&gt; &gt; 10000 objects.  Rule firing took 3375 ms<br>
&gt;&gt; &gt; 100000 objects.  Rule firing took 33266 ms<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; I have thought about clever tricks that I could do to<br>
&gt;&gt; try<br>
&gt;&gt; &gt; and speed<br>
&gt;&gt; &gt; things up, like sorting into groups, etc.  But it<br>
&gt;&gt; all<br>
&gt;&gt; &gt; seems relatively<br>
&gt;&gt; &gt; convoluted.  Am I correct in coming to the<br>
&gt;&gt; conclusion<br>
&gt;&gt; &gt; that a &lt;<br>
&gt;&gt; &gt; comparison is not the kind of thing that you should do<br>
&gt;&gt; in<br>
&gt;&gt; &gt; Drools?<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; How would you go about this?  I would prefer to keep<br>
&gt;&gt; &gt; all the logic in<br>
&gt;&gt; &gt; the DRL files.  I&#39;m guessing you do a Java based<br>
&gt;&gt; &gt; comparison and use a<br>
&gt;&gt; &gt; fire-once rule to invoke this code.<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; Thanks in advance for any insight,<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; Tom<br>
&gt;&gt; &gt; _______________________________________________<br>
&gt;&gt; &gt; rules-users mailing list<br>
&gt;&gt; &gt; <a href="mailto:rules-users@lists.jboss.org">rules-users@lists.jboss.org</a><br>
&gt;&gt; &gt; <a href="https://lists.jboss.org/mailman/listinfo/rules-users" target="_blank">https://lists.jboss.org/mailman/listinfo/rules-users</a><br>
&gt;&gt; &gt;<br>
&gt;&gt;<br>
&gt;&gt;<br>
&gt;&gt;<br>
&gt;&gt;<br>
&gt;&gt; _______________________________________________<br>
&gt;&gt; rules-users mailing list<br>
&gt;&gt; <a href="mailto:rules-users@lists.jboss.org">rules-users@lists.jboss.org</a><br>
&gt;&gt; <a href="https://lists.jboss.org/mailman/listinfo/rules-users" target="_blank">https://lists.jboss.org/mailman/listinfo/rules-users</a><br>
&gt;&gt;<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt;<br>
&gt; _______________________________________________<br>
&gt; rules-users mailing list<br>
&gt; <a href="mailto:rules-users@lists.jboss.org">rules-users@lists.jboss.org</a><br>
&gt; <a href="https://lists.jboss.org/mailman/listinfo/rules-users" target="_blank">https://lists.jboss.org/mailman/listinfo/rules-users</a><br>
&gt;<br>
<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>
</div></div></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>