Monday, March 20, 2006

Drools on AppFuse

Recently got a chance to adopt rule engine into project to allow abstraction of business rules from the service layer, that way, business rules can be maintained in it's own space. I pick Drools as the underlying rule engine because it is intuitive, simple, and robust JSR94 compliant rule engine, plus it's open source. Below are steps to integrate this powerful rule engine into AppFuse. I use the Spring Modules distribution to make it easier for the integration since all the plumbing is done nicely with Spring.

1. Add required libraries:
    a. springmodules-jsr94-0.2.jar
    b. /jsr94
    c. /drools
    d. /janino

Refer to How to add a Library into AppFuse for information on how to add new library into AppFuse.

2. Drop the Spring ApplicationContext for Drools configuration, applicationContext-rules.xml, into [appfuse]\web\WEB-INF\:

<?xml version="1.0" encoding="UTF-8"?>
<bean id="ruleServiceProvider"
<property name="provider">http://drools.org/</value></property>
<property name="providerClass"><value>org.drools.jsr94.rules.RuleServiceProviderImpl</value>
<bean id="ruleRuntime"
<property name="serviceProvider"><ref local="ruleServiceProvider"/></property>
<bean id="ruleAdministrator"
<ref local="ruleServiceProvider"/></property>
<bean id="ruleSource" class="org.springmodules.jsr94.rulesource.DefaultRuleSource">
<property name="ruleRuntime"><ref local="ruleRuntime"/></property>
<property name="ruleAdministrator"><ref local="ruleAdministrator"/></property>
<property name="source"><value>/WEB-INF/authorizedUsers.drl</value></property>
<property name="bindUri"><value>authorizedUsers</value></property>
<bean id="rulesService" class="org.appfuse.service.impl.RulesServiceDrools">
<property name="ruleSource"><ref local="ruleSource"/></property>

The file, authorizedUsers.drl, is the ruleset, and RulesServiceDrools is the Drools rule implementation we have in this sample setup.

3. The signature for the ruleset, authorizedUsers.drl, in [appfuse]\web\WEB-INF\:

<?xml version="1.0" encoding="UTF-8"?>
name="Get authorized users"
description="Rules to retrieve authorized users"
xs:schemaLocation="http://drools.org/rules rules.xsd
http://drools.org/semantics/java java.xsd">
<rule name="Enabled users">
<parameter identifier="user">
<java:condition> user.isEnabled() != true </java:condition>
<java:consequence> drools.retractObject(user); </java:consequence>
<rule name="Valid credentials">
<parameter identifier="user">
user.isCredentialsExpired() == true || user.getRoles().size() == 0

Two rules are defined in the ruleset: Enabled users and Valid credentials. Both rules check for condition, if met, the user is removed from list. Basically, the two rules mean:

if (!user.isEnabled() && (user.isCredentialsExpired()
|| user.getRoles().size() == 0))

4. The interface for the rules service, RulesService, is simple enough:

public interface RulesService {
public List getAuthorizedUsers(List users);

And the associated implementation, RulesServiceDrools:

public class RulesServiceDrools extends Jsr94RuleSupport implements RulesService {
public final static String ACTIVE_USERS_URI="authorizedUsers";
public List getAuthorizedUsers(List users) {
return executeStateless(ACTIVE_USERS_URI, users);

I couldn't get the Spring Jsr94Template to work properly. Ideally, the output list from Drools should be access via Spring template:

public List getAuthorizedUsers(List users) {
List outputList = getTemplate().executeStateless(ACTIVE_USERS_URI,null,
new StatelessRuleSessionCallback() {
public Object execute(StatelessRuleSession session)
throws InvalidRuleSessionException, RemoteException {
return session.executeRules(users);
return outputList;

5. The Tapestry action code to get the list of users:

public abstract class AuthorizedUsers extends BasePage implements PageRenderListener {
public abstract UserManager getUserManager();
public abstract void setUserManager(UserManager manager);
public abstract RulesService getRulesService();
public abstract void setRulesService(RulesService svr);

public void pageBeginRender(PageEvent event) {
List authorizedUsers = getRulesService().getAuthorizedUsers(getUserManager().getUsers(null));

That's pretty much it to setup Drools in AppFuse and starts ruling away.
Software Culture
  03/01/2004 - 04/01/2004
  04/01/2004 - 05/01/2004
  05/01/2004 - 06/01/2004
  06/01/2004 - 07/01/2004
  08/01/2004 - 09/01/2004
  11/01/2004 - 12/01/2004
  07/01/2005 - 08/01/2005
  09/01/2005 - 10/01/2005
  10/01/2005 - 11/01/2005
  01/01/2006 - 02/01/2006
  03/01/2006 - 04/01/2006
  Smart talk always right?
  Drools on AppFuse
  Braille, braille
  The Dynamic SRC of IMG
  Preferred Locale on AppFuse
  告訴你為什麼程式不 work
  絲綢之路 2000:致命病毒
  AJAX on AppFuse
  Meet Mr. Writely
  健檢, e檢
  Open source ColdFusion
  Rich DHTML client
  cfspring, seriously
  Where are they?
  An Architect's View
  Martin Fowler
  Loud Thinking
  Raible Designs   fullasagoog

Powered by Blogger