[rules-users] One question about Database interface
Ojwang Wilson O
wojwang at alcatel-lucent.com
Tue Jul 8 08:05:59 EDT 2008
Hi Senlin,
It will be easy to use JPA. Here is an example.
===============
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
Senlin Liang wrote:
> So, to use a hibernate session, we are storing data in disk in some
> "internal format" used by Drools, and we are not actually
> communicating with some relational database systems. Is this correct?
>
> Thanks
>
> On Mon, Jul 7, 2008 at 10:32 PM, Mark Proctor <mproctor at codehaus.org> wrote:
>
>> Senlin Liang wrote:
>>
>>> Hi all,
>>>
>>> Is there some convinent database interface implemented in Drools
>>> (store and retrieve data from DBMS, such as mysql) ?
>>>
>>> I checked the manual, and found no such information. So I am assuming
>>> that I will have to use JDBC. Is it right?
>>>
>>>
>> you should use Hibernate to access a database. You can then use then pass
>> the hibernate session as a global and use the "from" keyword to execute
>> querries to be filtered through a pattern.
>>
>>> Thanks,
>>> Senlin Liang
>>> _______________________________________________
>>> rules-users mailing list
>>> rules-users at lists.jboss.org
>>> https://lists.jboss.org/mailman/listinfo/rules-users
>>>
>>>
>>>
>> _______________________________________________
>> rules-users mailing list
>> rules-users at lists.jboss.org
>> https://lists.jboss.org/mailman/listinfo/rules-users
>>
>>
>
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/rules-users/attachments/20080708/a013458b/attachment.html
More information about the rules-users
mailing list