Drools – Guvnor: Spring integration

Using Spring 2.5, Drools-guvnor 5.0

Download Drools guvnor standalone version here jboss-drools-guvnor

Spring configuration:
– bean rulesAgent is for communication between our application and guvnor,
the Drools platform BRMS.
– bean droolsService uses rulesAgent to provide the needed service.

<bean id="rulesAgent" class="com.test.droolsproto.utils.DroolsProtoAgent"></bean>
<bean id="droolsService" class="com.test.droolsproto.serviceImpl.RuleDroolServiceImpl">
<property name="ruleAgent" ref="rulesAgent"></property>
</bean>

Create file “brmsdeployedrules.properties” for the ruleAgent:
In this file the link to use a running instance of guvnor. The package “com.test.droolsproto.rules” is an existing rules package in guvnor. DroolsDemo at the end of package value is the name of the snapshot. A snapshot in guvnor represents an image of rule’s package at specific time.

newInstance=true
url=http://localhost:8080/drools-guvnor/org.drools.guvnor.Guvnor/
package= com.test.droolsproto.rules/DroolsDemo
poll=30
name=droolsconfig

In the web project add drools librairies (drools-api-5.0.1, drools-core-5.0.1) and create DroolsProtoAgent class:

public class DroolsProtoAgent {
   private RuleBase rulebase;
   private StatefulSession session;
   
   private DroolsProtoAgent(){
   }
   
   public RuleBase getRulebase() {
    if (rulebase != null) return rulebase;
    else {
       RuleAgent agent = RuleAgent.newRuleAgent( "/brmsdeployedrules.properties" );
       RuleBase rulebase = agent.getRuleBase();
       return rulebase;
    }
  }
  
  public void setRulebase(RuleBase rulebase) {
    this.rulebase = rulebase;
  }
  
  public void setSession(StatefulSession session) {
    this.session = session;
  }
  
  public StatefulSession getSession() {
    if ( rulebase == null ) {
      rulebase = getRulebase();
    }
    session = rulebase.newStatefulSession();
    return session;
  }
}

Create a drools service:

public class RuleDroolServiceImpl implements RuleDroolsService {

   @Autowired
   private DroolsProtoAgent ruleAgent;

   @Override
   public String applyRule(ValueObject a, ValueObject b){

   try {
     // Load knowledge base session
     StatefulSession ksession = ruleAgent.getSession();
     ksession.startProcess("droolsProcess"); //Start a predefined process in guvnor
     
     // Insert VO as Fact
     ksession.insert(a);
     ksession.insert(b);

     ksession.fireAllRules(); //Apply rules
     return "success or specific guvnor response";
   } catch (Throwable t) {
     t.printStackTrace();
     return "failure";
   }
  }

  public DroolsProtoAgent getRuleAgent() {
   return ruleAgent;
  }

  public void setRuleAgent(DroolsProtoAgent ruleAgent) {
   this.ruleAgent = ruleAgent;
  }
}

Drools integration is complete.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s