This sample will show you how you retrieve some objects by using TopLink and then use Oracle Business Rules to do some selection upon these objects. I am not using neither a file based nor a WebDAV based rule repository for this sample, instead I create the ruleset on the fly.
Suppose you have a table, PERSON, that has the following structure:
PERSON
------
PID NUMBER
NAME VARCHAR2(32)
SEX CHAR(1)
EYECOLOR VARCHAR2(8)
INCOME NUMBER
and this is mapped to a Person object which has corresponding fields and accessors. I will also assume that you have setup a TopLink mapping between the table and the object.
Now, we first need to retrieve the objects from the DB, assuming we are working with a 2-tier here, we do this as follows:
SessionManager sessionManager = SessionManager.getManager();
Session session = sessionManager.getSession("default");
Vector v = session.readAllObjects(Person.class);
This will read all persons in the database and put them into a Vector. Now, we have a vector, v, that holds our persons. The next step is to initiate the RuleSession, as mentioned before, this is done on the fly with one simple rule:
RuleSession rs = new RuleSession();
String rset =
"ruleset main {" +
" import ruleexample1.bo.Person;" +
" rule blueEyeFemaleWithIncomeOver40000 {" +
" if (fact Person p && p.getEye_color().equals(\"Blue\") && p.getSex().equals(\"F\") && p.getIncome()>40000 ) {" +
" println(p.getName() + \" is a female with blue eyes and an income over 400000.\");" +
" }" +
" }" +
"}";
rs.executeRuleset(rset);
this rule will fire for females with blue eyes and has an income over 40000. Once done, we can assign the persons as facts, this is done by:
for (int i=0;i<v.size();i++) {
rs.callFunctionWithArgument( "assert", (Person)v.elementAt(i) );
}
once the facts are in place, we can run this by calling:
rs.callFunction( "run" );
That's it! This will print out a list of objects that matches the conditions set out in the rule set.