David,
Here is what I did to make this work.
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.
public class DroolsTest {
public static final void main(String[] args) {
try {
//load up the rulebase
RuleBase ruleBase = readRule();
WorkingMemory workingMemory = ruleBase.newStatefulSession();
// Start EntityManagerFactory
EntityManagerFactory emf =
Persistence.createEntityManagerFactory("persons");
// Second unit of work
EntityManager newEm = emf.createEntityManager();
EntityTransaction newTx = newEm.getTransaction();
newTx.begin();
workingMemory.setGlobal("entity",newEm);
ArrayList<Person> presult = new ArrayList<Person>();
workingMemory.setGlobal("presult",presult);
workingMemory.fireAllRules();
System.out.println("=============List Results=============");
for ( Person p : presult ){
System.out.println("Person: \"" + p.getFirstName() +
"\", " + p.getLastName() +
"\", " + p.getAge());
}
System.out.println("=============End List
Results=====\n\n");
newTx.commit();
newEm.close();
// Shutting down the application
emf.close();
} catch (Throwable t) {
t.printStackTrace();
}
}
/**
* Please note that this is the "low level" rule assembly API.
*/
private static RuleBase readRule() throws Exception {
//read in the source
Reader source = new InputStreamReader(
DroolsTest.class.getResourceAsStream( "/Sample.drl" ) );
PackageBuilder builder = new PackageBuilder();
builder.addPackageFromDrl( source );
//get the compiled package (which is serializable)
Package pkg = builder.getPackage();
//add the package to a rulebase (deploy the rule package).
RuleBase ruleBase = RuleBaseFactory.newRuleBase();
ruleBase.addPackage( pkg );
return ruleBase;
}
}
==============
Here is the rule sample.drl
package com.sample
import com.sample.DroolsTest.Message;
import com.sample.Person;
import javax.persistence.EntityManager;
global javax.persistence.EntityManager entity;
global java.util.ArrayList presult;
rule "age"
when
$p: Person() from entity.createQuery("from Person p where p.age
<= 20").getResultList();
then
presult.add($p);
end
==========
Wilson
David Siefert wrote:
Hello,
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);
so in my drls, I have:
<code>
global javax.persistence.EntityManager em;
</code>
And then I use this in my LHS of the rule with a from:
<code>
rule "query-db"
when
SampleObject(property from (em.createNativeQuery("select PROP from
SAMPLEOBJECT").getResultList()))
then
System.out.println("Got an object!");
end
</code>
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.
Thanks,
David Siefert
------------------------------------------------------------------------
_______________________________________________
rules-users mailing list
rules-users(a)lists.jboss.org
https://lists.jboss.org/mailman/listinfo/rules-users