<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="&#39;courier new&#39;, monospace">FireAllRulesCommand farc = (FireAllRulesCommand) CommandFactory.newFireAllRules(maxRulesToFire); </font></div><div><font face="&#39;courier new&#39;, monospace">farc.setOutIdentifier(&quot;num-rules-fired&quot;); </font></div>
<div><font face="&#39;courier new&#39;, monospace">commands.add(farc); // commands is List&lt;Command&lt;?&gt;&gt; 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="&#39;courier new&#39;, 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&#39;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&#39;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:&#39;courier new&#39;,monospace">public &lt;T&gt; T execute(Command&lt;T&gt; command) {</div>
<div style="font-family:&#39;courier new&#39;,monospace">    StatefulKnowledgeSession ksession = newWorkingMemory();</div><div style="font-family:&#39;courier new&#39;,monospace">    ExecutionResults results = new ExecutionResultImpl();</div>
<div style="font-family:&#39;courier new&#39;,monospace"><br></div><div style="font-family:&#39;courier new&#39;,monospace">    KnowledgeCommandContext context = new KnowledgeCommandContext( new ContextImpl( &quot;ksession&quot;,</div>
<div style="font-family:&#39;courier new&#39;,monospace">                                                                                    null ),</div><div style="font-family:&#39;courier new&#39;,monospace">                                                                   null,</div>
<div style="font-family:&#39;courier new&#39;,monospace">                                                                   null,</div><div style="font-family:&#39;courier new&#39;,monospace">                                                                   ksession,</div>
<div style="font-family:&#39;courier new&#39;,monospace">                                                                   results );</div><div style="font-family:&#39;courier new&#39;,monospace"><br></div><div style="font-family:&#39;courier new&#39;,monospace">
    try {</div><div style="font-family:&#39;courier new&#39;,monospace">        ((StatefulKnowledgeSessionImpl) ksession).session.startBatchExecution( results );</div><div style="font-family:&#39;courier new&#39;,monospace">
<br></div><div><font face="arial, helvetica, sans-serif">If this is indeed a bug and not something silly I&#39;m missing, I&#39;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><div><font face="arial, helvetica, sans-serif">Mike</font></div>