<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
<title></title>
</head>
<body 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
cite="mid:99011c420807070731v2e92a8d0ne255c475aacf8970@mail.gmail.com"
type="cite">
<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>
<pre wrap="">
<hr size="4" width="90%">
_______________________________________________
rules-users mailing list
<a class="moz-txt-link-abbreviated" href="mailto:rules-users@lists.jboss.org">rules-users@lists.jboss.org</a>
<a class="moz-txt-link-freetext" href="https://lists.jboss.org/mailman/listinfo/rules-users">https://lists.jboss.org/mailman/listinfo/rules-users</a>
</pre>
</blockquote>
</body>
</html>