<div>Thank you Mark! That helps.</div><div><br></div><div>I was just trying to provide some feedback before the final release :)</div>

<br>Best Regards,<br>Michal<br><br><br><div class="gmail_quote">2009/3/19 Mark Proctor <span dir="ltr">&lt;<a href="mailto:mproctor@codehaus.org" target="_blank">mproctor@codehaus.org</a>&gt;</span><br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">





  

<div bgcolor="#ffffff" text="#000000">
Based on your feedbck I have added support for a GetObjects command and
a FireAllRules command. So now you can call GetObjects and have it&#39;s
results assigned to an identifier. FireAllrules can now be manually
called, even for a stateless session. If you manually call fireAllRules
in a stateless session it will no longer be called automatically at the
end.<br>
<br>
Here are two unit tests showing the behaviour. I was going to leave
this till 5.1, but it seems you really need this and the code is safe
enough that it shouldn&#39;t break anything else.<br>
<br>
    public void testGetObjects() throws Exception {<br>
        String str = &quot;&quot;;<br>
        str += &quot;package org.drools \n&quot;;<br>
        str += &quot;import org.drools.Cheese \n&quot;;<br>
        str += &quot;rule rule1 \n&quot;;<br>
        str += &quot;  when \n&quot;;<br>
        str += &quot;    $c : Cheese() \n&quot;;<br>
        str += &quot; \n&quot;;<br>
        str += &quot;  then \n&quot;;<br>
        str += &quot;    $c.setPrice( $c.getPrice() + 5 ); \n&quot;;<br>
        str += &quot;end\n&quot;;        <br>
<br>
        String inXml = &quot;&quot;;<br>
        inXml += &quot;&lt;batch-execution&gt;&quot;;       <br>
        inXml += &quot;  &lt;insert-elements&gt;&quot;;<br>
        inXml += &quot;    &lt;org.drools.Cheese&gt;&quot;;<br>
        inXml += &quot;      &lt;type&gt;stilton&lt;/type&gt;&quot;;<br>
        inXml += &quot;      &lt;price&gt;25&lt;/price&gt;&quot;;<br>
        inXml += &quot;      &lt;oldPrice&gt;0&lt;/oldPrice&gt;&quot;;<br>
        inXml += &quot;    &lt;/org.drools.Cheese&gt;&quot;;<br>
        inXml += &quot;    &lt;org.drools.Cheese&gt;&quot;;<br>
        inXml += &quot;      &lt;type&gt;stilton&lt;/type&gt;&quot;;<br>
        inXml += &quot;      &lt;price&gt;30&lt;/price&gt;&quot;;<br>
        inXml += &quot;      &lt;oldPrice&gt;0&lt;/oldPrice&gt;&quot;;<br>
        inXml += &quot;    &lt;/org.drools.Cheese&gt;&quot;;<br>
        inXml += &quot;  &lt;/insert-elements&gt;&quot;;<br>
        inXml += &quot;  &lt;get-objects out-identifier=&#39;list&#39; /&gt;&quot;;<br>
        inXml += &quot;&lt;/batch-execution&gt;&quot;;<br>
        <br>
        StatelessKnowledgeSession ksession = getSession2(
ResourceFactory.newByteArrayResource( str.getBytes() ) );<br>
        ResultHandlerImpl resultHandler = new
ResultHandlerImpl();        <br>
        getPipeline(ksession).insert( inXml, resultHandler );        <br>
        String outXml = ( String ) resultHandler.getObject();<br>
        <br>
        String expectedXml = &quot;&quot;;<br>
        expectedXml += &quot;&lt;batch-execution-results&gt;\n&quot;;<br>
        expectedXml += &quot;  &lt;result identifier=&#39;list&#39;&gt;\n&quot;;<br>
        expectedXml += &quot;    &lt;list&gt;\n&quot;;<br>
        expectedXml += &quot;      &lt;org.drools.Cheese&gt;\n&quot;;<br>
        expectedXml += &quot;        &lt;type&gt;stilton&lt;/type&gt;\n&quot;;<br>
        expectedXml += &quot;       
&lt;price&gt;35&lt;/price&gt;\n&quot;;        <br>
        expectedXml += &quot;       
&lt;oldPrice&gt;0&lt;/oldPrice&gt;\n&quot;;        <br>
        expectedXml += &quot;      &lt;/org.drools.Cheese&gt;\n&quot;;<br>
        expectedXml += &quot;      &lt;org.drools.Cheese&gt;\n&quot;;<br>
        expectedXml += &quot;       
&lt;type&gt;stilton&lt;/type&gt;\n&quot;;     <br>
        expectedXml += &quot;        &lt;price&gt;30&lt;/price&gt;\n&quot;;<br>
        expectedXml += &quot;       
&lt;oldPrice&gt;0&lt;/oldPrice&gt;\n&quot;;           <br>
        expectedXml += &quot;      &lt;/org.drools.Cheese&gt;\n&quot;;<br>
        expectedXml += &quot;    &lt;/list&gt;\n&quot;;        <br>
        expectedXml += &quot;  &lt;/result&gt;\n&quot;;<br>
        expectedXml += &quot;&lt;/batch-execution-results&gt;\n&quot;;<br>
        <br>
        assertXMLEqual( expectedXml, outXml );<br>
        <br>
        BatchExecutionResults result = ( BatchExecutionResults )
BatchExecutionHelper.newXStreamMarshaller().fromXML( outXml );<br>
        List list = ( List ) result.getValue( &quot;list&quot; );<br>
        Cheese stilton25 = new Cheese( &quot;stilton&quot;, 30);<br>
        Cheese stilton30 = new Cheese( &quot;stilton&quot;, 35);<br>
        <br>
        Set expectedList = new HashSet();<br>
        expectedList.add( stilton25 );<br>
        expectedList.add( stilton30 );<br>
        <br>
        assertEquals( expectedList, new HashSet( list ));              
<br>
    }<br>
<br>
    public void testManualFireAllRules() throws Exception {<br>
        String str = &quot;&quot;;<br>
        str += &quot;package org.drools \n&quot;;<br>
        str += &quot;import org.drools.Cheese \n&quot;;<br>
        str += &quot;global java.util.List list \n&quot;;<br>
        str += &quot;rule rule1 \n&quot;;<br>
        str += &quot;  when \n&quot;;<br>
        str += &quot;    $c : Cheese() \n&quot;;<br>
        str += &quot; \n&quot;;<br>
        str += &quot;  then \n&quot;;<br>
        str += &quot;    $c.setPrice( $c.getPrice() + 5 ); \n&quot;;<br>
        str += &quot;    list.add( $c );&quot;;<br>
        str += &quot;end\n&quot;;        <br>
<br>
        String inXml = &quot;&quot;;<br>
        inXml += &quot;&lt;batch-execution&gt;&quot;;<br>
        inXml += &quot;  &lt;set-global identifier=&#39;list&#39; out=&#39;true&#39;&gt;&quot;;<br>
        inXml += &quot;    &lt;list/&gt;&quot;;<br>
        inXml += &quot;  &lt;/set-global&gt;&quot;;        <br>
        inXml += &quot;  &lt;insert-elements&gt;&quot;;<br>
        inXml += &quot;    &lt;org.drools.Cheese&gt;&quot;;<br>
        inXml += &quot;      &lt;type&gt;stilton&lt;/type&gt;&quot;;<br>
        inXml += &quot;      &lt;price&gt;25&lt;/price&gt;&quot;;<br>
        inXml += &quot;      &lt;oldPrice&gt;0&lt;/oldPrice&gt;&quot;;<br>
        inXml += &quot;    &lt;/org.drools.Cheese&gt;&quot;;<br>
        inXml += &quot;    &lt;org.drools.Cheese&gt;&quot;;<br>
        inXml += &quot;      &lt;type&gt;stilton&lt;/type&gt;&quot;;<br>
        inXml += &quot;      &lt;price&gt;30&lt;/price&gt;&quot;;<br>
        inXml += &quot;      &lt;oldPrice&gt;0&lt;/oldPrice&gt;&quot;;<br>
        inXml += &quot;    &lt;/org.drools.Cheese&gt;&quot;;<br>
        inXml += &quot;  &lt;/insert-elements&gt;&quot;;<br>
        inXml += &quot;  &lt;fire-all-rules /&gt;&quot;;<br>
        inXml += &quot;  &lt;insert out-identifier=&#39;outBrie&#39;&gt;&quot;;<br>
        inXml += &quot;    &lt;org.drools.Cheese&gt;&quot;;<br>
        inXml += &quot;      &lt;type&gt;brie&lt;/type&gt;&quot;;<br>
        inXml += &quot;      &lt;price&gt;10&lt;/price&gt;&quot;;<br>
        inXml += &quot;      &lt;oldPrice&gt;5&lt;/oldPrice&gt;&quot;;<br>
        inXml += &quot;    &lt;/org.drools.Cheese&gt;&quot;;<br>
        inXml += &quot;  &lt;/insert&gt;&quot;;          <br>
        inXml += &quot;&lt;/batch-execution&gt;&quot;;<br>
        <br>
        StatelessKnowledgeSession ksession = getSession2(
ResourceFactory.newByteArrayResource( str.getBytes() ) );<br>
        ResultHandlerImpl resultHandler = new
ResultHandlerImpl();        <br>
        getPipeline(ksession).insert( inXml, resultHandler );        <br>
        String outXml = ( String ) resultHandler.getObject();<br>
        <br>
        String expectedXml = &quot;&quot;;<br>
        expectedXml += &quot;&lt;batch-execution-results&gt;\n&quot;;<br>
        expectedXml += &quot;  &lt;result identifier=&#39;list&#39;&gt;\n&quot;;<br>
        expectedXml += &quot;    &lt;list&gt;\n&quot;;<br>
        expectedXml += &quot;      &lt;org.drools.Cheese&gt;\n&quot;;<br>
        expectedXml += &quot;        &lt;type&gt;stilton&lt;/type&gt;\n&quot;;<br>
        expectedXml += &quot;       
&lt;price&gt;35&lt;/price&gt;\n&quot;;        <br>
        expectedXml += &quot;       
&lt;oldPrice&gt;0&lt;/oldPrice&gt;\n&quot;;        <br>
        expectedXml += &quot;      &lt;/org.drools.Cheese&gt;\n&quot;;<br>
        expectedXml += &quot;      &lt;org.drools.Cheese&gt;\n&quot;;<br>
        expectedXml += &quot;       
&lt;type&gt;stilton&lt;/type&gt;\n&quot;;     <br>
        expectedXml += &quot;        &lt;price&gt;30&lt;/price&gt;\n&quot;;<br>
        expectedXml += &quot;       
&lt;oldPrice&gt;0&lt;/oldPrice&gt;\n&quot;;           <br>
        expectedXml += &quot;      &lt;/org.drools.Cheese&gt;\n&quot;;<br>
        expectedXml += &quot;    &lt;/list&gt;\n&quot;;        <br>
        expectedXml += &quot;  &lt;/result&gt;\n&quot;;<br>
        expectedXml += &quot;  &lt;result identifier=&#39;outBrie&#39;&gt;\n&quot;;<br>
        expectedXml += &quot;    &lt;org.drools.Cheese&gt;\n&quot;;<br>
        expectedXml += &quot;      &lt;type&gt;brie&lt;/type&gt;\n&quot;;<br>
        expectedXml += &quot;      &lt;price&gt;10&lt;/price&gt;\n&quot;;<br>
        expectedXml += &quot;      &lt;oldPrice&gt;5&lt;/oldPrice&gt;\n&quot;;<br>
        expectedXml += &quot;    &lt;/org.drools.Cheese&gt;\n&quot;;<br>
        expectedXml += &quot;  &lt;/result&gt;\n&quot;;          <br>
        expectedXml += &quot;&lt;/batch-execution-results&gt;\n&quot;;<br>
        assertXMLEqual( expectedXml, outXml );<br>
        <br>
        BatchExecutionResults result = ( BatchExecutionResults )
BatchExecutionHelper.newXStreamMarshaller().fromXML( outXml );<br>
        <br>
        // brie should not have been added to the list<br>
        List list = ( List ) result.getValue( &quot;list&quot; );<br>
        Cheese stilton25 = new Cheese( &quot;stilton&quot;, 30);<br>
        Cheese stilton30 = new Cheese( &quot;stilton&quot;, 35);<br>
        <br>
        Set expectedList = new HashSet();<br>
        expectedList.add( stilton25 );<br>
        expectedList.add( stilton30 );<br>
        <br>
        assertEquals( expectedList, new HashSet( list )); <br>
<br>
        // brie should not have changed<br>
        Cheese brie10 = new Cheese( &quot;brie&quot;, 10);<br>
        brie10.setOldPrice( 5 );<br>
        assertEquals( brie10, result.getValue( &quot;outBrie&quot; ) );  <br><div><div></div><div>
    }        <br>
<br>
<br>
<br>
<br>
<br>
Michal Bali wrote:
<blockquote type="cite">Hi Mark,<br>
  <br>
I want this for a unit test. It will verify that the stateless session
contains only expected objects after execution. I don&#39;t want to write a
new query that will be used only in the unit test. <br>
  <br>
Currently it is possible to iterate over all objects in a
StatefulKnowledgeSession through its getObjects method. However it is
not supported by default on the StatelessKnowledgeSession.<br>
  <br>
I&#39;ve created a custom command for getting the objects. Its execute
method looks like this:<br>
public Iterator&lt; ? &gt; execute(ReteooWorkingMemory session) {<br>
    Iterator&lt;?&gt; objects = session.iterateObjects( filter );<br>
    session.getBatchExecutionResult().getResults().put(identifier,
objects);<br>
    return objects;<br>
}<br>
  <br>
Another thing to note is that StatelessKnowledgeSession.execute(Command
command) method first executes all given commands and then calls
session.fireAllRules. So if you want to insert some objects and run a
query you&#39;ll pass some insert commands and a query command. However the
query will be executed before the fireAllRules call and so it won&#39;t
return anything.
  <div>Alternatively you could pass in a fireAllRules command as well,
which will work fine. The fireAllRules will be executed twice but that
is not a big deal.<br>
Another issue is that the query command makes it difficult to pass in
query arguments that are known only after the rules execution. Because
you have to specify the query before the fireAllRules execution. I
guess you could create your own command and do all logic there but this
is not very nice.
  <div><br>
  </div>
  <div>Just my 2c.<br>
  <br>
Best Regards,<br>
Michal<br>
  <br>
  <br>
  <div class="gmail_quote">2009/3/17 Mark Proctor <span dir="ltr">&lt;<a href="mailto:mproctor@codehaus.org" target="_blank">mproctor@codehaus.org</a>&gt;</span><br>
  <blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
    <div bgcolor="#ffffff" text="#000000">
    <div>Michal Bali wrote:
    <blockquote type="cite">Hi,<br>
      <br>
I am trying this on trunk. There is currently no way how to iterate
objects in a StatelessKnowledgeSession.<br>
      <br>
I&#39;ve tried to use the GetObjectsCommand, however when it is executes
the result is lost in the ether :). The collection of objects is not
handed back to the user.<br>
      <br>
I guess it should probably be added to the BatchExecutionResults? It
will then be possible to access this collection of objects.<br>
    </blockquote>
    </div>
You have ot specify what it is you want as an out, either as an &#39;out&#39;
on an inserted fact or global or a query. <br>
    <div>
    <blockquote type="cite"><br>
Please let me know if there is a different way how to iterate over
objects in a StatelessKnowledgeSession. I&#39;d like to use an ObjectFilter
as well.<br>
    </blockquote>
    </div>
Extend the Query interface and use that to filter the iterator on a
returned query.<br>
    <blockquote type="cite"><br>
Best Regards,<br>
Michal<br>
      <pre><hr size="4" width="90%">_______________________________________________
rules-users mailing list
<a href="mailto:rules-users@lists.jboss.org" target="_blank">rules-users@lists.jboss.org</a>
<a href="https://lists.jboss.org/mailman/listinfo/rules-users" target="_blank">https://lists.jboss.org/mailman/listinfo/rules-users</a>
  </pre>
    </blockquote>
    <br>
    </div>
    <br>
_______________________________________________<br>
rules-users mailing list<br>
    <a href="mailto:rules-users@lists.jboss.org" target="_blank">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>
    <br>
  </blockquote>
  </div>
  <br>
  </div>
  </div>
  <pre><hr size="4" width="90%">_______________________________________________
rules-users mailing list
<a href="mailto:rules-users@lists.jboss.org" target="_blank">rules-users@lists.jboss.org</a>
<a href="https://lists.jboss.org/mailman/listinfo/rules-users" target="_blank">https://lists.jboss.org/mailman/listinfo/rules-users</a>
  </pre>
</blockquote>
<br>
</div></div></div>

<br>_______________________________________________<br>
rules-users mailing list<br>
<a href="mailto:rules-users@lists.jboss.org" target="_blank">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>
<br></blockquote></div><br>