JBRULES-3421 created and pull request submitted with fix and tests.<div><br></div><div><br><div class="gmail_quote">On Fri, Mar 2, 2012 at 1:57 PM, Mike Melton <span dir="ltr"><<a href="mailto:mike.melton@gmail.com">mike.melton@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div>Drools 5.3.1.Final.</div><div><br></div>I have a use case where I am executing a BatchExecutionCommand against a stateless session. One of the sub-commands is a FireAllRulesCommand, from which I would like to retrieve the number of rules actually fired. Since there is no factory or constructor method to configure both a maximum rules to fire and an out identifier, I have to use code like this to set up my FireAllRulesCommand:<div>
<br></div><div><font face="'courier new', monospace">FireAllRulesCommand farc = (FireAllRulesCommand) CommandFactory.newFireAllRules(maxRulesToFire); </font></div><div><font face="'courier new', monospace">farc.setOutIdentifier("num-rules-fired"); </font></div>
<div><font face="'courier new', monospace">commands.add(farc); // commands is List<Command<?>> which I later use to construct a BatchExecutionCommand</font></div><div><br></div><div>At runtime, when I execute the BatchExecutionCommand, I get an NPE at FireAllRulesCommand:110. After some investigating, I determined that the value returned from <font face="'courier new', monospace">(ExecutionResultImpl) ((KnowledgeCommandContext) context).getExecutionResults()</font> is null, which causes the NPE.</div>
<div><br></div><div>As far as I can tell, the cause of this breaks down like so:</div><div>- StatelessKnowledgeSession.execute() fires with the batch command as input.</div><div>- It constructs a KnowledgeCommandContext, setting the ExecutionResults parameter of the constructor to null (line 254).</div>
<div>- KnowledgeCommandContext.executionResults is a private member with no setter so there is no way to give it a value after it's been instantiated.</div><div>- The FireAllRulesCommand fires as part of the batch, and when it tries to access the execution results from the context, the NPE is thrown.</div>
<div><br></div><div>StatelessKnowledgeSession.execute() later gets the execution results from the session itself, via the ExecutionResultsImpl instance which is constructed and passed to the startBatchExecution() method on line 262. I haven't had time to try this myself, but if we constructed the ExecutionResultsImpl earlier and passed it to both the KnowledgeCommandContext constructor and the startBatchExecution() method, would the problem be alleviated?</div>
<div><br></div><div>In other words, something like this (StatelessKnowledgeSessionImpl.java:251):</div><div><br></div><div><div style="font-family:'courier new',monospace">public <T> T execute(Command<T> command) {</div>
<div style="font-family:'courier new',monospace"> StatefulKnowledgeSession ksession = newWorkingMemory();</div><div style="font-family:'courier new',monospace"> ExecutionResults results = new ExecutionResultImpl();</div>
<div style="font-family:'courier new',monospace"><br></div><div style="font-family:'courier new',monospace"> KnowledgeCommandContext context = new KnowledgeCommandContext( new ContextImpl( "ksession",</div>
<div style="font-family:'courier new',monospace"> null ),</div><div style="font-family:'courier new',monospace"> null,</div>
<div style="font-family:'courier new',monospace"> null,</div><div style="font-family:'courier new',monospace"> ksession,</div>
<div style="font-family:'courier new',monospace"> results );</div><div style="font-family:'courier new',monospace"><br></div><div style="font-family:'courier new',monospace">
try {</div><div style="font-family:'courier new',monospace"> ((StatefulKnowledgeSessionImpl) ksession).session.startBatchExecution( results );</div><div style="font-family:'courier new',monospace">
<br></div><div><font face="arial, helvetica, sans-serif">If this is indeed a bug and not something silly I'm missing, I'll be happy to submit a JIRA and even a pull request with the fix.</font></div></div><div><font face="arial, helvetica, sans-serif"><br>
</font></div><div><font face="arial, helvetica, sans-serif">Thanks</font></div><span class="HOEnZb"><font color="#888888"><div><font face="arial, helvetica, sans-serif">Mike</font></div>
</font></span></blockquote></div><br></div>