<html><head><meta http-equiv="Content-Type" content="text/html charset=windows-1252"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div>Hi folks,</div><div><br></div><div>Can anyone explain how stateless session execution results should work? Working through the docs and tracing the code, it’s getting a bit confusing.</div><div><br></div><div>Firstly, according to the user manual, one should be able to insert a list of facts as a Command, and get an instance of ExecutionResults back, which can then be queried. Here’s the example code:</div><div><a id="d0e3650" style="color: rgb(51, 51, 51); font-family: 'Lucida Grande', Geneva, Verdana, Arial, sans-serif; line-height: 18px; text-align: justify;"><div class="example-contents"><pre xmlns="" xmlns:d="http://docbook.org/ns/docbook" xmlns:rf="java:org.jboss.highlight.XhtmlRendererFactory" class="JAVA" style="font-size: 0.9em; font-family: courrier, monospace; background-color: rgb(245, 245, 245); overflow: auto; padding: 5px 15px 5px 25px; border: 1px solid rgb(204, 204, 204); line-height: 10px;"><span class="java_type" style="color: rgb(0, 0, 0);">StatelessKnowledgeSession</span><span class="java_plain" style="color: rgb(0, 0, 0);">&nbsp;ksession&nbsp;</span><span class="java_operator" style="color: rgb(0, 0, 0);">=</span><span class="java_plain" style="color: rgb(0, 0, 0);">&nbsp;kbase</span><span class="java_separator" style="color: rgb(0, 0, 0);">.</span><span class="java_plain" style="color: rgb(0, 0, 0);">newStatelessKnowledgeSession</span><span class="java_separator" style="color: rgb(0, 0, 0);">();</span>
<br><span class="java_type" style="color: rgb(0, 0, 0);">Command</span><span class="java_plain" style="color: rgb(0, 0, 0);">&nbsp;cmd&nbsp;</span><span class="java_operator" style="color: rgb(0, 0, 0);">=</span><span class="java_plain" style="color: rgb(0, 0, 0);">&nbsp;</span><span class="java_type" style="color: rgb(0, 0, 0);">CommandFactory</span><span class="java_separator" style="color: rgb(0, 0, 0);">.</span><span class="java_plain" style="color: rgb(0, 0, 0);">newInsertElements</span><span class="java_separator" style="color: rgb(0, 0, 0);">(</span><span class="java_plain" style="color: rgb(0, 0, 0);">&nbsp;</span><span class="java_type" style="color: rgb(0, 0, 0);">Arrays</span><span class="java_separator" style="color: rgb(0, 0, 0);">.</span><span class="java_plain" style="color: rgb(0, 0, 0);">asList</span><span class="java_separator" style="color: rgb(0, 0, 0);">(</span><span class="java_plain" style="color: rgb(0, 0, 0);">&nbsp;</span><span class="java_type" style="color: rgb(0, 0, 0);">Object</span><span class="java_separator" style="color: rgb(0, 0, 0);">[]</span><span class="java_plain" style="color: rgb(0, 0, 0);">&nbsp;</span><span class="java_separator" style="color: rgb(0, 0, 0);">{</span><span class="java_plain" style="color: rgb(0, 0, 0);">&nbsp;</span>
<br><span class="java_plain" style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="java_keyword" style="font-weight: bold; color: rgb(127, 27, 85);">new</span><span class="java_plain" style="color: rgb(0, 0, 0);">&nbsp;</span><span class="java_type" style="color: rgb(0, 0, 0);">Cheese</span><span class="java_separator" style="color: rgb(0, 0, 0);">(</span><span class="java_plain" style="color: rgb(0, 0, 0);">&nbsp;</span><span class="java_literal" style="color: rgb(42, 0, 255);">"stilton"</span><span class="java_plain" style="color: rgb(0, 0, 0);">&nbsp;</span><span class="java_separator" style="color: rgb(0, 0, 0);">),</span>
<br><span class="java_plain" style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="java_keyword" style="font-weight: bold; color: rgb(127, 27, 85);">new</span><span class="java_plain" style="color: rgb(0, 0, 0);">&nbsp;</span><span class="java_type" style="color: rgb(0, 0, 0);">Cheese</span><span class="java_separator" style="color: rgb(0, 0, 0);">(</span><span class="java_plain" style="color: rgb(0, 0, 0);">&nbsp;</span><span class="java_literal" style="color: rgb(42, 0, 255);">"brie"</span><span class="java_plain" style="color: rgb(0, 0, 0);">&nbsp;</span><span class="java_separator" style="color: rgb(0, 0, 0);">),</span>
<br><span class="java_plain" style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="java_keyword" style="font-weight: bold; color: rgb(127, 27, 85);">new</span><span class="java_plain" style="color: rgb(0, 0, 0);">&nbsp;</span><span class="java_type" style="color: rgb(0, 0, 0);">Cheese</span><span class="java_separator" style="color: rgb(0, 0, 0);">(</span><span class="java_plain" style="color: rgb(0, 0, 0);">&nbsp;</span><span class="java_literal" style="color: rgb(42, 0, 255);">"cheddar"</span><span class="java_plain" style="color: rgb(0, 0, 0);">&nbsp;</span><span class="java_separator" style="color: rgb(0, 0, 0);">),</span>
<br><span class="java_plain" style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="java_separator" style="color: rgb(0, 0, 0);">});</span>
<br><span class="java_type" style="color: rgb(0, 0, 0);">ExecutionResults</span><span class="java_plain" style="color: rgb(0, 0, 0);">&nbsp;bresults&nbsp;</span><span class="java_operator" style="color: rgb(0, 0, 0);">=</span><span class="java_plain" style="color: rgb(0, 0, 0);">&nbsp;ksession</span><span class="java_separator" style="color: rgb(0, 0, 0);">.</span><span class="java_plain" style="color: rgb(0, 0, 0);">execute</span><span class="java_separator" style="color: rgb(0, 0, 0);">(</span><span class="java_plain" style="color: rgb(0, 0, 0);">&nbsp;cmd&nbsp;</span><span class="java_separator" style="color: rgb(0, 0, 0);">);</span></pre></div></a><div><br></div></div><div>That doesn’t compile, so I fixed it as follows:</div><div><br></div><div><div style="margin: 0px; font-size: 11px; font-family: Monaco;">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<span style="text-decoration: underline">Command</span> cmd = CommandFactory.newInsertElements( Arrays.asList(&nbsp;</div><div style="margin: 0px; font-size: 11px; font-family: Monaco;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #931a68">new</span> Cheese( <span style="color: #3933ff">"stilton"</span> ),</div><div style="margin: 0px; font-size: 11px; font-family: Monaco;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #931a68">new</span> Cheese( <span style="color: #3933ff">"brie"</span> ),</div><div style="margin: 0px; font-size: 11px; font-family: Monaco;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #931a68">new</span> Cheese( <span style="color: #3933ff">"cheddar"</span> )</div><div style="margin: 0px; font-size: 11px; font-family: Monaco;">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; ));</div><div style="margin: 0px; font-size: 11px; font-family: Monaco;">&nbsp; &nbsp; &nbsp; &nbsp; ExecutionResults <span style="text-decoration: underline">bresults</span> = <span style="text-decoration: underline ; color: #0326cc">ksession</span><span style="text-decoration: underline">.execute( cmd )</span>;</div></div><div><br></div><div>This does now compile, but running it throws a ClassCastException:</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>java.lang.ClassCastException: java.util.ArrayList cannot be cast to org.drools.runtime.ExecutionResults</div><div><br></div><div>Taking a look through the Drools code, it would appear that Command has a generic type, which in the case of an InsertElementsCommand is:&nbsp;</div><div><div style="margin: 0px; font-size: 11px; font-family: Monaco;">GenericCommand&lt;Collection&lt;FactHandle&gt;&gt;</div></div><div><br></div><div>In turn, the execute command in StatelessKnowledgeSessionImpl casts its result to that generic type:</div><div><div style="margin: 0px; font-size: 11px; font-family: Monaco;"><span style="color: #931a68"><span class="Apple-tab-span" style="white-space:pre">        </span>public</span> &lt;T&gt; T execute(Command&lt;T&gt; command) {</div></div><div><div style="margin: 0px; font-size: 11px; font-family: Monaco;"><span class="Apple-tab-span" style="white-space:pre">                </span>Object o = ((GenericCommand) command).execute( context );</div></div><div style="margin: 0px; font-size: 11px; font-family: Monaco;"><span class="Apple-tab-span" style="white-space:pre">                </span>...</div><div style="margin: 0px; font-size: 11px; font-family: Monaco;"><div style="margin: 0px;"><span style="color: #931a68"><span class="Apple-tab-span" style="white-space:pre">                </span>return</span> (T) o;</div></div><div><br></div><div>So the ClassCastException is because the execute method returns an ArrayList of FactHandle instead of an ExecutionResults.</div><div><br></div><div>I’m assuming that I must have got the wrong end of the stick somewhere, so what should I be doing to get an ExecutionResults from a stateless session, which I can then query?</div><div><br></div><div>Cheers,</div><div><br></div><div>Steve</div></body></html>