<div>Hi, I&#39;v been out of this list for a while (like 2 years) but always reading, so maybe what I&#39;m going</div>
<div>to add could not be relevant... please correct me if I&#39;m worng.</div>
<div> </div>
<div>The thing is, that Krishna is doing a little job extra on the RHS, because the action is not just</div>
<div>&quot;System.out.println&quot;, it has an insert of a new fact,</div>
<div> </div>
<div>               InventoryItem fact0 = new InventoryItem();<br>               fact0.setCategoryCode( &quot;ONPROMO&quot; );<br>               insert(fact0 );</div>
<div> </div>
<div>So that could make the difference here... maybe check this InventoryItem constructor could help.</div>
<div> </div>
<div>The other thing I would do is to put some kind of Map as a global variable in the engine and put this</div>
<div>relation you are trying to make:</div>
<div> </div>
<div>     inventoryItemCode == &quot;8903210392090&quot;  =&gt; InventoryItem with categoryCode=&quot;ONPROMO&quot;<br></div>
<div>so you could have a Map with inventoryItemCode as key and categoryCode as value,</div>
<div> and finally when you finish the execution of the rule&#39;s engine you can create new InventoryItems from</div>
<div>the resulting Map.</div>
<div> </div>
<div> </div>
<div>Regards.<br><br></div>
<div class="gmail_quote">On Sun, Feb 7, 2010 at 12:04 PM, Wolfgang Laun <span dir="ltr">&lt;<a href="mailto:wolfgang.laun@gmail.com">wolfgang.laun@gmail.com</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="PADDING-LEFT: 1ex; MARGIN: 0px 0px 0px 0.8ex; BORDER-LEFT: #ccc 1px solid">There are several techniques available for quickly locating an object based<br>on a key, as was done by the simple rule in the original posting. They vary<br>
w.r.t. setup time, access time and memory requirement. Here are the<br>times (in seconds, on a 1 year old laptop) for 20000 random keys of length<br>13, made up from digits.<br><br>array: create 20000 elements    0.066<br>
array: locate 20000 elements    0.024<br>map: create 20000 elements    0.020<br>map: locate 20000 elements    0.024<br>trie: create 20000 elements    0.028<br>trie: locate 20000 elements    0.025<br><br>&#39;array&#39; uses Arrays.sort and Arrays.binarySearch. &#39;map&#39; uses<br>
a HashMap, and &#39;trie&#39; is a slapdash implementation of this<br>well-known data structure. Increasing the number of entries will,<br>eventually, show the superiority of HashMap, but its memory<br>requirement is certainly higher than that of an array.<br>
<br>I cannot claim that any of these solutions would be applicable for<br>the original problem where the consequence of the presented<br>rule inserts another fact, which might lead to more rules firing, etc.<br>But such techniques might be considered up front, reducing<br>
rule-based processing to those &quot;second generation&quot; facts.<br><br>Using literal data in a (large) number of similar statements is always<br>an indication that an alternative approach should be investigated<br>where multiplicity is put into data instead of code.<br>
<br>-W<br><br>2010/2/6 Edson Tirelli &lt;<a href="mailto:ed.tirelli@gmail.com">ed.tirelli@gmail.com</a>&gt;:<br>
<div>
<div></div>
<div class="h5">&gt;     Hi All,<br>&gt;<br>&gt; &quot;There is nothing in that which would gain from Drools&#39; Rete algorithm with<br>&gt; its superior &quot;many-patterns-many-object&quot; matching capabilities.&quot;<br>
&gt;<br>&gt; I know Wolfgang knows all this, but I think we need to clarify this<br>&gt; statement for the general public of the list. Assuming all the rules are<br>&gt; indeed like he described (very simple, with a single alpha constraint), then<br>
&gt; there are efficient and simple ways to implement it without a rules engine,<br>&gt; but that is not by using &quot;if&quot;. At least he would have to use a hashing<br>&gt; technique, or the performance would be roughly linear on number of rules (<br>
&gt; O(r) ) when in Drools, that does use hashing techniques for alpha<br>&gt; constraints, it is constant ( O(1) ). Also, it depends on the non-functional<br>&gt; requirements he has, like lifecycle management.<br>&gt;<br>
&gt;      In any case, I decided to take the bate and implement a quick example<br>&gt; for this. I changed the HelloWorld example in Drools to generate 20k rules<br>&gt; in the same format you have:<br>&gt;<br>&gt; rule &quot;rule X&quot;<br>
&gt;     dialect &quot;mvel&quot;<br>&gt; when<br>&gt;     Message( message == &quot;X&quot; )<br>&gt; then<br>&gt;     System.out.println( X );<br>&gt; end<br>&gt;<br>&gt;     Replacing X by the number of the rule. Then I parse, compile and build a<br>
&gt; kbase for these rules and fire the rules for a single object. This is<br>&gt; **NOT** a benchmark, so I am just giving you a rough idea of the hardware...<br>&gt; I am running it on my laptop, with other stuff on. My laptop is a 2-years<br>
&gt; old lenovo, dual core, 2Gb memory. I am running with -Xmx1024M.<br>&gt;<br>&gt;     Results I got (in ms):<br>&gt;<br>&gt; 1. Generating Package   ...done. 163 ms<br>&gt; 2. Dumping source       ...done. 280897 ms<br>
&gt; 3. Parsing and compiling...done. 40557 ms<br>&gt; 4. Creating kbase       ...done. 79512 ms<br>&gt; 5. Firing rules         ...done. 29 ms<br>&gt;<br>&gt;     Please note that steps 1 and 2 are the rules generation. You don&#39;t have<br>
&gt; that, since your source code is ready for processing, right? Step 3 is<br>&gt; executed by the KnowledgeBuilder and step 4 is the<br>&gt; kbase.addKnowledgePackages().<br>&gt;<br>&gt;     Again, this is a very simple case showing that it is possible to parse,<br>
&gt; compile and load 20k rules quite easily in a drools kbase. Also remember<br>&gt; that kbases are designed to be shared, so you should incur that cost once<br>&gt; and then create sessions (that are really light to create) reusing the<br>
&gt; kbase.<br>&gt;<br>&gt;     Finally, there are obviously room to improve, since the compilation and<br>&gt; kbase building is single thread today. We want to move to multi-thread<br>&gt; compilation in the future. That should improve things a bit.<br>
&gt;<br>&gt;    The source code I used is bellow.<br>&gt;<br>&gt;    Cheers,<br>&gt;        Edson<br>&gt;<br>&gt;<br>&gt; public class DroolsTest {<br>&gt;<br>&gt;     private static final int NUMBER_OF_RULES = 20000;<br>
&gt;<br>&gt;     public static final void main(String[] args) {<br>&gt;         try {<br>&gt;             // load up the knowledge base<br>&gt;             KnowledgeBase kbase = readKnowledgeBase();<br>&gt;             StatefulKnowledgeSession ksession =<br>
&gt; kbase.newStatefulKnowledgeSession();<br>&gt;             //KnowledgeRuntimeLogger logger =<br>&gt; KnowledgeRuntimeLoggerFactory.newFileLogger(ksession, &quot;test&quot;);<br>&gt;             // go !<br>&gt;             long ts = System.currentTimeMillis();<br>
&gt;             Message message = new Message();<br>&gt;             message.setMessage(&quot;5&quot;);<br>&gt;             message.setStatus(Message.HELLO);<br>&gt;             ksession.insert(message);<br>&gt;             ksession.fireAllRules();<br>
&gt;             System.out.print(&quot;5. Firing rules         ...&quot;);<br>&gt;             System.out.println(&quot;done.<br>&gt; &quot;+(System.currentTimeMillis()-ts)+&quot;ms&quot;);<br>&gt;             //logger.close();<br>
&gt;         } catch (Throwable t) {<br>&gt;             t.printStackTrace();<br>&gt;         }<br>&gt;     }<br>&gt;<br>&gt;     private static KnowledgeBase readKnowledgeBase() throws Exception {<br>&gt;         KnowledgeBuilder kbuilder =<br>
&gt; KnowledgeBuilderFactory.newKnowledgeBuilder();<br>&gt;<br>&gt;         System.out.print(&quot;1. Generating Package   ...&quot;);<br>&gt;         long ts = System.currentTimeMillis();<br>&gt;         PackageDescr pkg = generatePackageDescr();<br>
&gt;         System.out.println(&quot;done. &quot;+(System.currentTimeMillis()-ts)+&quot;ms&quot;);<br>&gt;<br>&gt;         System.out.print(&quot;2. Dumping source       ...&quot;);<br>&gt;         ts = System.currentTimeMillis();<br>
&gt;         String source = new DrlDumper(  ).dump( pkg );<br>&gt;         //System.out.println(source);<br>&gt;         System.out.println(&quot;done. &quot;+(System.currentTimeMillis()-ts)+&quot;ms&quot;);<br>&gt;<br>&gt;         System.out.print(&quot;3. Parsing and compiling...&quot;);<br>
&gt;         ts = System.currentTimeMillis();<br>&gt;         kbuilder.add(ResourceFactory.newReaderResource( new StringReader(<br>&gt; source ) ), ResourceType.DRL);<br>&gt;         KnowledgeBuilderErrors errors = kbuilder.getErrors();<br>
&gt;         if (errors.size() &gt; 0) {<br>&gt;             for (KnowledgeBuilderError error: errors) {<br>&gt;                 System.err.println(error);<br>&gt;             }<br>&gt;             throw new IllegalArgumentException(&quot;Could not parse<br>
&gt; knowledge.&quot;);<br>&gt;         }<br>&gt;         System.out.println(&quot;done. &quot;+(System.currentTimeMillis()-ts)+&quot;ms&quot;);<br>&gt;<br>&gt;         System.out.print(&quot;4. Creating kbase       ...&quot;);<br>
&gt;         ts = System.currentTimeMillis();<br>&gt;         KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();<br>&gt;         kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());<br>&gt;         System.out.println(&quot;done. &quot;+(System.currentTimeMillis()-ts)+&quot;ms&quot;);<br>
&gt;         return kbase;<br>&gt;     }<br>&gt;<br>&gt;     private static PackageDescr generatePackageDescr() {<br>&gt;         PackageDescr result = new PackageDescr(&quot;org.drools.test&quot;);<br>&gt;         result.addImport( new ImportDescr(&quot;com.sample.DroolsTest.Message&quot;)<br>
&gt; );<br>&gt;         for( int i = 1; i &lt;= NUMBER_OF_RULES; i++ ) {<br>&gt;             RuleDescr rule = new RuleDescr(&quot;rule &quot;+i);<br>&gt;             AttributeDescr dialect = new AttributeDescr(&quot;dialect&quot;, &quot;mvel&quot;);<br>
&gt;             AndDescr lhs = new AndDescr();<br>&gt;             PatternDescr pat = new PatternDescr(&quot;Message&quot;);<br>&gt;             FieldConstraintDescr constr = new<br>&gt; FieldConstraintDescr(&quot;message&quot;);<br>
&gt;             LiteralRestrictionDescr restr = new<br>&gt; LiteralRestrictionDescr(&quot;==&quot;,String.valueOf( i ));<br>&gt;             constr.addRestriction( restr );<br>&gt;             pat.addConstraint( constr );<br>
&gt;             lhs.addDescr( pat );<br>&gt;             rule.addAttribute( dialect );<br>&gt;             rule.setLhs( lhs );<br>&gt;             rule.setConsequence( &quot;System.out.println(&quot;+i+&quot;);\n&quot; );<br>
&gt;             result.addRule( rule );<br>&gt;         }<br>&gt;         return result;<br>&gt;     }<br>&gt;<br>&gt;     public static class Message {<br>&gt;<br>&gt;         public static final int HELLO = 0;<br>&gt;         public static final int GOODBYE = 1;<br>
&gt;<br>&gt;         private String message;<br>&gt;<br>&gt;         private int status;<br>&gt;<br>&gt;         public String getMessage() {<br>&gt;             return this.message;<br>&gt;         }<br>&gt;<br>&gt;         public void setMessage(String message) {<br>
&gt;             this.message = message;<br>&gt;         }<br>&gt;<br>&gt;         public int getStatus() {<br>&gt;             return this.status;<br>&gt;         }<br>&gt;<br>&gt;         public void setStatus(int status) {<br>
&gt;             this.status = status;<br>&gt;         }<br>&gt;<br>&gt;     }<br>&gt;<br>&gt; }<br>&gt;<br>&gt;<br>&gt;<br>&gt; 2010/2/6 Wolfgang Laun &lt;<a href="mailto:wolfgang.laun@gmail.com">wolfgang.laun@gmail.com</a>&gt;<br>
&gt;&gt;<br>&gt;&gt; Have you tried increasing the heap size for the JVM?<br>&gt;&gt;<br>&gt;&gt; &quot;20K rules [of the same] simple form&quot; sounds as if you are<br>&gt;&gt; trying to use a hammer where a screwdriver would be appropriate.<br>
&gt;&gt; If your description is correct, these 20K rules would be nothing<br>&gt;&gt; but 20K if statements, in disguised form. There is nothing<br>&gt;&gt; in that which would gain from Drools&#39; Rete algorithm with<br>
&gt;&gt; its superior &quot;many-patterns-many-object&quot; matching capabilities.<br>&gt;&gt;<br>&gt;&gt; -W<br>&gt;&gt;<br>&gt;&gt; On Sat, Feb 6, 2010 at 12:42 PM, kpowerinfinity<br>&gt;&gt; &lt;<a href="mailto:kpowerinfinity@gmail.com">kpowerinfinity@gmail.com</a>&gt; wrote:<br>
&gt;&gt; &gt; Hello,<br>&gt;&gt; &gt;<br>&gt;&gt; &gt; We are trying to run a DRL file that has more than 20K rules, all of<br>&gt;&gt; &gt; which have the simple form:<br>&gt;&gt; &gt;<br>&gt;&gt; &gt;<br>&gt;&gt; &gt;<br>
&gt;&gt; &gt; rule &quot;testpeoplenumber&quot;<br>&gt;&gt; &gt;        salience 100<br>&gt;&gt; &gt;        dialect &quot;mvel&quot;<br>&gt;&gt; &gt;        when<br>&gt;&gt; &gt;                LineItem( inventoryItemCode == &quot;8903210392090&quot;)<br>
&gt;&gt; &gt; then<br>&gt;&gt; &gt;                InventoryItem fact0 = new InventoryItem();<br>&gt;&gt; &gt;                fact0.setCategoryCode( &quot;ONPROMO&quot; );<br>&gt;&gt; &gt;                insert(fact0 );<br>
&gt;&gt; &gt; end<br>&gt;&gt; &gt;<br>&gt;&gt; &gt; However, the rule file is not getting parsed by the builder, and<br>&gt;&gt; &gt; either takes a very long time (well over half an hour), or gives a<br>&gt;&gt; &gt; Heap Overflow exception. We tried with a smaller file (about 1K rules,<br>
&gt;&gt; &gt; and the situation is the same).<br>&gt;&gt; &gt;<br>&gt;&gt; &gt; The parsing code is:<br>&gt;&gt; &gt; org.drools.KnowledgeBaseConfiguration kbc =<br>&gt;&gt; &gt; org.drools.KnowledgeBaseFactory.newKnowledgeBaseConfiguration(null,<br>
&gt;&gt; &gt; cl);<br>&gt;&gt; &gt;            org.drools.KnowledgeBase kbase =<br>&gt;&gt; &gt; org.drools.KnowledgeBaseFactory.newKnowledgeBase(kbc);<br>&gt;&gt; &gt;                org.drools.builder.KnowledgeBuilderConfiguration<br>
&gt;&gt; &gt; knowledgeBuilderConfig =<br>&gt;&gt; &gt;<br>&gt;&gt; &gt; org.drools.builder.KnowledgeBuilderFactory.newKnowledgeBuilderConfiguration(null,<br>&gt;&gt; &gt; cl);<br>&gt;&gt; &gt;            org.drools.builder.KnowledgeBuilder builder =<br>
&gt;&gt; &gt; org.drools.builder.KnowledgeBuilderFactory.newKnowledgeBuilder(kbase,<br>&gt;&gt; &gt; knowledgeBuilderConfig);<br>&gt;&gt; &gt;            org.drools.builder.impl.KnowledgeBuilderImpl builderImpl =<br>&gt;&gt; &gt; builder as org.drools.builder.impl.KnowledgeBuilderImpl;<br>
&gt;&gt; &gt;<br>&gt;&gt; &gt;<br>&gt;&gt; &gt;  builder.add(org.drools.io.ResourceFactory.newFileResource(filename),<br>&gt;&gt; &gt;                    org.drools.builder.ResourceType.DRL);<br>&gt;&gt; &gt;<br>&gt;&gt; &gt; We are using Drools v5. We&#39;ve tried both with the java jar as well as<br>
&gt;&gt; &gt; in .NET (using IKVM to compile it).<br>&gt;&gt; &gt;<br>&gt;&gt; &gt; Any help in understanding why this is happening and how it can be<br>&gt;&gt; &gt; avoided will be appreciated.<br>&gt;&gt; &gt;<br>&gt;&gt; &gt; Warm Regards<br>
&gt;&gt; &gt; Krishna.<br>&gt;&gt; &gt;<br>&gt;&gt; &gt; --<br>&gt;&gt; &gt; <a href="http://kpowerinfinity.wordpress.com/" target="_blank">http://kpowerinfinity.wordpress.com</a><br>&gt;&gt; &gt; <a href="http://www.linkedin.com/in/kpowerinfinity" target="_blank">http://www.linkedin.com/in/kpowerinfinity</a><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; 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;<br>&gt;<br>&gt;<br>&gt; --<br>&gt;  Edson Tirelli<br>&gt;  JBoss Drools Core Development<br>&gt;  JBoss by Red Hat @ <a href="http://www.jboss.com/" target="_blank">www.jboss.com</a><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>&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>-----------------------------<br>Felipe Piccolini<br><a href="mailto:felipe.piccolini@gmail.com">felipe.piccolini@gmail.com</a><br>