<div>Thanks Wilson for the fast reply.</div>
<div> </div>
<div>I did this and almost got it working (mostly due to my newb knowledge of writing rules).  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!  Not sure why that is....</div>
<div> </div>
<div>-David<br><br></div>
<div class="gmail_quote">On Mon, Jul 7, 2008 at 10:45 AM, Wilson O Ojwang <<a href="mailto:wojwang@alcatel-lucent.com">wojwang@alcatel-lucent.com</a>> 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>    public static final void main(String[] args) {<br>        try {<br>            <br>            //load up the rulebase<br>            RuleBase ruleBase = readRule();<br>            WorkingMemory workingMemory = ruleBase.newStatefulSession();<br>
        <br>         // Start EntityManagerFactory<br>            EntityManagerFactory emf =<br>                    Persistence.createEntityManagerFactory("persons");<br><br>            // Second unit of work<br>
            EntityManager newEm = emf.createEntityManager();<br>            EntityTransaction newTx = newEm.getTransaction();<br>            newTx.begin();<br>            <br>            workingMemory.setGlobal("entity",newEm);<br>
            ArrayList<Person> presult = new ArrayList<Person>();<br>            workingMemory.setGlobal("presult",presult);<br>            workingMemory.fireAllRules(); <br>            <br>            System.out.println("=============List Results=============");<br>
            for ( Person p : presult ){<br>                 System.out.println("Person: \"" + p.getFirstName() +<br>                         "\", " + p.getLastName() +            <br>                                     "\", " + p.getAge());<br>
            }<br>            System.out.println("=============End List Results=====\n\n");         <br>            newTx.commit();<br>            newEm.close();<br>         // Shutting down the application<br>            emf.close();<br>
          <br>        } catch (Throwable t) {<br>            t.printStackTrace();<br>        }<br>    }<br>   /**<br>     * Please note that this is the "low level" rule assembly API.<br>     */<br>    private static RuleBase readRule() throws Exception {<br>
        //read in the source<br>        Reader source = new InputStreamReader( DroolsTest.class.getResourceAsStream( "/Sample.drl" ) );      <br>        PackageBuilder builder = new PackageBuilder(); <br>        builder.addPackageFromDrl( source );<br>
<br>        //get the compiled package (which is serializable)<br>        Package pkg = builder.getPackage();<br>        <br>        //add the package to a rulebase (deploy the rule package).<br>        RuleBase ruleBase = RuleBaseFactory.newRuleBase();<br>
        ruleBase.addPackage( pkg );<br>        return ruleBase;<br>    }<br>}<br><br>==============<br>Here is the rule sample.drl<br><br>package com.sample<br> <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 "age"<br>    when<br>        $p: Person() from entity.createQuery("from Person p where p.age <= 20").getResultList();<br>
    then<br>        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> </div>
<div>I am trying to use the EntityManager within a rule.  I pass the EntityManager instance in from my test (currently just a mock of the interface using EasyMock) using setGlobal("em", entitymanager);</div>
<div> </div>
<div>so in my drls, I have:</div>
<div> </div>
<div><code></div>
<div>global javax.persistence.EntityManager em;</div>
<div></code></div>
<div> </div>
<div>And then I use this in my LHS of the rule with a from:</div>
<div> </div>
<div><code></div>
<div>rule "query-db"</div>
<div>  when</div>
<div>    SampleObject(property from (em.createNativeQuery("select PROP from SAMPLEOBJECT").getResultList()))</div>
<div>  then</div>
<div>    System.out.println("Got an object!");</div>
<div>end</div>
<div></code></div>
<div> </div>
<div>However, when I run my JUnit test, I get an error saying "unexpected token 'PROP'".  So it looks like the Drools parser is trying to interpret what is in my string.  Is there a special way to pass a string to the Java operation?  Or can I simply not do this?  How would I be able to get a result set from the db to use in pattern matching?  The rule is dependent upon data from a table to make a decision.</div>
<div> </div>
<div>Thanks,</div>
<div> </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>