<div>Thanks Wilson for the fast reply.</div>
<div>&nbsp;</div>
<div>I did this and almost got it working (mostly due to my newb knowledge of writing rules).&nbsp; However, I tinkered with the rule a bit further, and it turns out i got my original rule post working by simply using memberOf instead of from!&nbsp; Not sure why that is....</div>

<div>&nbsp;</div>
<div>-David<br><br></div>
<div class="gmail_quote">On Mon, Jul 7, 2008 at 10:45 AM, Wilson O Ojwang &lt;<a href="mailto:wojwang@alcatel-lucent.com">wojwang@alcatel-lucent.com</a>&gt; wrote:<br>
<blockquote class="gmail_quote" style="PADDING-LEFT: 1ex; MARGIN: 0px 0px 0px 0.8ex; BORDER-LEFT: #ccc 1px solid">
<div text="#000000" bgcolor="#ffffff">David,<br><br>Here is what I did to make this work.<br><br>This example assume that you have a Person POJO and you want to query all persons with the age lest than or equal to 20.<br>
<br>public class DroolsTest {<br><br>&nbsp;&nbsp;&nbsp; public static final void main(String[] args) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; //load up the rulebase<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; RuleBase ruleBase = readRule();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; WorkingMemory workingMemory = ruleBase.newStatefulSession();<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Start EntityManagerFactory<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; EntityManagerFactory emf =<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Persistence.createEntityManagerFactory(&quot;persons&quot;);<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Second unit of work<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; EntityManager newEm = emf.createEntityManager();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; EntityTransaction newTx = newEm.getTransaction();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; newTx.begin();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; workingMemory.setGlobal(&quot;entity&quot;,newEm);<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ArrayList&lt;Person&gt; presult = new ArrayList&lt;Person&gt;();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; workingMemory.setGlobal(&quot;presult&quot;,presult);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; workingMemory.fireAllRules(); <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(&quot;=============List Results=============&quot;);<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for ( Person p : presult ){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;System.out.println(&quot;Person: \&quot;&quot; + p.getFirstName() +<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&quot;\&quot;, &quot; + p.getLastName() +&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &quot;\&quot;, &quot; + p.getAge());<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(&quot;=============End List Results=====\n\n&quot;);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; newTx.commit();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; newEm.close();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Shutting down the application<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; emf.close();<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } catch (Throwable t) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; t.printStackTrace();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp; /**<br>&nbsp;&nbsp;&nbsp;&nbsp; * Please note that this is the &quot;low level&quot; rule assembly API.<br>&nbsp;&nbsp;&nbsp;&nbsp; */<br>&nbsp;&nbsp;&nbsp; private static RuleBase readRule() throws Exception {<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; //read in the source<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Reader source = new InputStreamReader( DroolsTest.class.getResourceAsStream( &quot;/Sample.drl&quot; ) );&nbsp;&nbsp; &nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; PackageBuilder builder = new PackageBuilder();&nbsp;<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; builder.addPackageFromDrl( source );<br>
<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; //get the compiled package (which is serializable)<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Package pkg = builder.getPackage();<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; //add the package to a rulebase (deploy the rule package).<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; RuleBase ruleBase = RuleBaseFactory.newRuleBase();<br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ruleBase.addPackage( pkg );<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; return ruleBase;<br>&nbsp;&nbsp;&nbsp; }<br>}<br><br>==============<br>Here is the rule sample.drl<br><br>package com.sample<br>&nbsp;<br>import com.sample.DroolsTest.Message;<br>import com.sample.Person;<br>
import javax.persistence.EntityManager;<br>global javax.persistence.EntityManager entity;<br>global java.util.ArrayList presult;<br><br><br>rule &quot;age&quot;<br>&nbsp;&nbsp;&nbsp; when<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; $p: Person() from entity.createQuery(&quot;from Person p where p.age &lt;= 20&quot;).getResultList();<br>
&nbsp;&nbsp;&nbsp; then<br>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; presult.add($p);<br>end<br><br>==========<br><br>Wilson<br><br><br>David Siefert wrote: 
<blockquote type="cite">
<div>
<div></div>
<div class="Wj3C7c">
<div>Hello,</div>
<div>&nbsp;</div>
<div>I am trying to use the EntityManager within a rule.&nbsp; I pass the EntityManager instance in from my test (currently just a mock of the interface using EasyMock) using setGlobal(&quot;em&quot;, entitymanager);</div>
<div>&nbsp;</div>
<div>so in my drls, I have:</div>
<div>&nbsp;</div>
<div>&lt;code&gt;</div>
<div>global javax.persistence.EntityManager em;</div>
<div>&lt;/code&gt;</div>
<div>&nbsp;</div>
<div>And then I use this in my LHS of the rule with a from:</div>
<div>&nbsp;</div>
<div>&lt;code&gt;</div>
<div>rule &quot;query-db&quot;</div>
<div>&nbsp; when</div>
<div>&nbsp;&nbsp;&nbsp; SampleObject(property from (em.createNativeQuery(&quot;select PROP from SAMPLEOBJECT&quot;).getResultList()))</div>
<div>&nbsp; then</div>
<div>&nbsp;&nbsp;&nbsp; System.out.println(&quot;Got an object!&quot;);</div>
<div>end</div>
<div>&lt;/code&gt;</div>
<div>&nbsp;</div>
<div>However, when I run my JUnit test, I get an error saying &quot;unexpected token &#39;PROP&#39;&quot;.&nbsp; So it looks like the Drools parser is trying to interpret what is in my string.&nbsp; Is there a special way to pass a string to the Java operation?&nbsp; Or can I simply not do this?&nbsp; How would I be able to get a result set from the db to use in pattern matching?&nbsp; The rule is dependent upon data from a table to make a decision.</div>

<div>&nbsp;</div>
<div>Thanks,</div>
<div>&nbsp;</div>
<div>David Siefert</div></div></div><pre><hr width="90%" size="4">
_______________________________________________
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></div><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>
<br></blockquote></div><br>