<!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> </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> </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> <FONT
face=Arial><FONT color=#0000ff size=2>$p : Property() from
(em.createNativeQuery("select PROP from
SAMPLEOBJECT").getResultList()))</FONT></FONT></SPAN></DIV>
<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> <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> </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> <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>
<FONT face=Arial color=#0000ff size=2>SampleObject($p memberOf
properties )</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> <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> </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> </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 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> 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></BLOCKQUOTE></BODY></HTML>