<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=us-ascii">
<META content="MSHTML 6.00.2900.3314" name=GENERATOR></HEAD>
<BODY>
<DIV dir=ltr align=left><SPAN class=410312715-07072008><FONT face=Arial 
color=#0000ff size=2>Keyword "From" allows for the retrieval of facts from 
outside the rule engine, whereas "memberOf" works upon fact 
attributes.</FONT></SPAN></DIV>
<DIV dir=ltr align=left><SPAN class=410312715-07072008><FONT face=Arial 
color=#0000ff size=2></FONT></SPAN>&nbsp;</DIV>
<DIV dir=ltr align=left><SPAN class=410312715-07072008><FONT face=Arial 
color=#0000ff size=2>So these would (should!) be other 
alternatives:-</FONT></SPAN></DIV>
<DIV dir=ltr align=left><SPAN class=410312715-07072008><FONT face=Arial 
color=#0000ff size=2></FONT></SPAN>&nbsp;</DIV>
<DIV dir=ltr align=left><SPAN class=410312715-07072008><FONT face=Arial 
color=#0000ff size=2>when</FONT></SPAN></DIV>
<DIV dir=ltr align=left><SPAN class=410312715-07072008>
<DIV dir=ltr align=left><SPAN class=410312715-07072008>&nbsp;&nbsp;&nbsp; <FONT 
face=Arial><FONT color=#0000ff size=2>$p : Property() from 
(em.createNativeQuery("select PROP from 
SAMPLEOBJECT").getResultList()))</FONT></FONT></SPAN></DIV>&nbsp;&nbsp;&nbsp; 
<FONT face=Arial color=#0000ff size=2>SampleObject(properties contains $p 
)</FONT></SPAN></DIV>
<DIV dir=ltr align=left><SPAN class=410312715-07072008><FONT face=Arial 
color=#0000ff size=2>then</FONT></SPAN></DIV>
<DIV dir=ltr align=left><SPAN class=410312715-07072008>&nbsp;&nbsp;&nbsp; <FONT 
face=Arial color=#0000ff size=2>...</FONT></SPAN></DIV>
<DIV dir=ltr align=left><SPAN class=410312715-07072008><FONT face=Arial 
color=#0000ff size=2>end</FONT></SPAN></DIV>
<DIV><FONT face=Arial color=#0000ff size=2></FONT>&nbsp;</DIV><FONT face=Arial 
color=#0000ff size=2></FONT>
<DIV dir=ltr align=left><SPAN class=410312715-07072008><FONT face=Arial 
color=#0000ff size=2>when</FONT></SPAN></DIV>
<DIV dir=ltr align=left><SPAN class=410312715-07072008>
<DIV dir=ltr align=left><SPAN class=410312715-07072008>&nbsp;&nbsp;&nbsp; <FONT 
face=Arial><FONT color=#0000ff size=2>$p : </FONT><FONT color=#0000ff 
size=2>Property() from (em.createNativeQuery("select PROP from 
SAMPLEOBJECT").getResultList()))</FONT></FONT></SPAN></DIV>&nbsp;&nbsp;&nbsp; 
<FONT face=Arial color=#0000ff size=2>SampleObject($p memberOf 
properties&nbsp;)</FONT></SPAN></DIV>
<DIV dir=ltr align=left><SPAN class=410312715-07072008><FONT face=Arial 
color=#0000ff size=2>then</FONT></SPAN></DIV>
<DIV dir=ltr align=left><SPAN class=410312715-07072008>&nbsp;&nbsp;&nbsp; <FONT 
face=Arial color=#0000ff size=2>...</FONT></SPAN></DIV>
<DIV dir=ltr align=left><SPAN class=410312715-07072008><FONT face=Arial 
color=#0000ff size=2>end</FONT></SPAN></DIV>
<DIV><BR><SPAN class=410312715-07072008><FONT face=Arial color=#0000ff 
size=2>With kind regards,</FONT></SPAN></DIV>
<DIV><SPAN class=410312715-07072008><FONT face=Arial color=#0000ff 
size=2></FONT></SPAN>&nbsp;</DIV>
<DIV><SPAN class=410312715-07072008><FONT face=Arial color=#0000ff 
size=2>Mike</FONT></SPAN></DIV>
<BLOCKQUOTE style="MARGIN-RIGHT: 0px">
  <DIV class=OutlookMessageHeader lang=en-us dir=ltr align=left>
  <HR tabIndex=-1>
  <FONT face=Tahoma size=2><B>From:</B> rules-users-bounces@lists.jboss.org 
  [mailto:rules-users-bounces@lists.jboss.org] <B>On Behalf Of </B>David 
  Siefert<BR><B>Sent:</B> 07 July 2008 16:21<BR><B>To:</B> Rules Users 
  List<BR><B>Subject:</B> Re: [rules-users] Calling EntityManager from DRL--DRL 
  Parser attemptsto parse sql query instead of passing as string 
  tocreateNativeQuery method<BR></FONT><BR></DIV>
  <DIV></DIV>
  <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 bgcolor="#ffffff" text="#000000">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("persons");<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("entity",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("presult",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("=============List 
    Results=============");<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("Person: \"" + p.getFirstName() 
    +<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
    &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;"\", " + 
    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; 
    "\", " + 
    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("=============End List 
    Results=====\n\n");&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 "low level" 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( "/Sample.drl" ) );&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 "age"<BR>&nbsp;&nbsp;&nbsp; 
    when<BR>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; $p: Person() from 
    entity.createQuery("from Person p where p.age &lt;= 
    20").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("em", 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 "query-db"</DIV>
      <DIV>&nbsp; when</DIV>
      <DIV>&nbsp;&nbsp;&nbsp; SampleObject(property from 
      (em.createNativeQuery("select PROP from 
      SAMPLEOBJECT").getResultList()))</DIV>
      <DIV>&nbsp; then</DIV>
      <DIV>&nbsp;&nbsp;&nbsp; System.out.println("Got an object!");</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 "unexpected 
      token 'PROP'".&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></BLOCKQUOTE></BODY></HTML>