You can do that step once and then reload the compiled rules from the disk, or use guvnor which will compile the rules for you.<div>Cheers<br><br><div class="gmail_quote">On Wed, May 9, 2012 at 12:55 PM, soumya_sd <span dir="ltr">&lt;<a href="mailto:soumya_sd@yahoo.com" target="_blank">soumya_sd@yahoo.com</a>&gt;</span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">I&#39;ve a use case where I need to create a rule dynamically every time the user<br>
sends a request.<br>
My current understanding of Drools is that you need to create a<br>
KnowledgeBuilder and then add the rules<br>
<br>
I&#39;m creating the KnowledgeBase as follows.<br>
<br>
    private static KnowledgeBase readKnowledgeBase() throws Exception {<br>
        long t1 = System.currentTimeMillis();<br>
        KnowledgeBuilder kbuilder =<br>
KnowledgeBuilderFactory.newKnowledgeBuilder();<br>
        long t11 = System.currentTimeMillis();<br>
<br>
<br>
        kbuilder.add(<br>
org.drools.io.ResourceFactory.newByteArrayResource(getRule()),<br>
ResourceType.DRL);<br>
<br>
        long t2 = System.currentTimeMillis();<br>
<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<br>
knowledge.&quot;);<br>
        }<br>
        long t3 = System.currentTimeMillis();<br>
<br>
        KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();<br>
        long t4 = System.currentTimeMillis();<br>
<br>
        kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());<br>
        long t5 = System.currentTimeMillis();<br>
<br>
<br>
        Collection&lt;KnowledgePackage&gt; kpackages =<br>
kbase.getKnowledgePackages();<br>
                for (KnowledgePackage knowledgePackage : kpackages) {<br>
                        System.out.println(&quot;Package -------- &quot; + knowledgePackage.getName());<br>
                        Collection&lt;Rule&gt; rules = knowledgePackage.getRules();<br>
                        for (Rule rule : rules) {<br>
                                System.out.println(&quot;****&quot; + rule.getName());<br>
                        }<br>
                }<br>
        long t6 = System.currentTimeMillis();<br>
<br>
        System.out.println( (t11-t1) + &quot; &quot; + (t2-t11) + &quot; &quot; + ( t3-t2) + &quot; &quot; +<br>
( t4-t3) + &quot; &quot; + ( t5-t4) + &quot; &quot; + ( t6-t5)+ &quot; &quot; );<br>
<br>
        return kbase;<br>
    }<br>
<br>
Based on the timing logs the code take majority (more than 80%) of time in<br>
only these two operations. Is there a way to make it faster ?<br>
<br>
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();<br>
kbuilder.add( org.drools.io.ResourceFactory.newByteArrayResource(getRule()),<br>
ResourceType.DRL);<br>
<br>
thanks.<br>
<br>
<br>
<br>
--<br>
View this message in context: <a href="http://drools.46999.n3.nabble.com/Is-there-a-faster-way-of-doing-this-in-Drools-tp3973888.html" target="_blank">http://drools.46999.n3.nabble.com/Is-there-a-faster-way-of-doing-this-in-Drools-tp3973888.html</a><br>


Sent from the Drools: User forum 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>
</blockquote></div><br><br clear="all"><div><br></div>-- <br> - MyJourney @ <a href="http://salaboy.wordpress.com" target="_blank">http://salaboy.wordpress.com</a><div> - Co-Founder @ <a href="http://www.jugargentina.org" target="_blank">http://www.jugargentina.org</a><br>

 - Co-Founder @ <a href="http://www.jbug.com.ar" target="_blank">http://www.jbug.com.ar</a><br> <br> - Salatino &quot;Salaboy&quot; Mauricio -</div><br>
</div>