Configure jBPM5.4 with Drools

Hello guys as part of jBPM we should even stress a little on drools
Business processes: Represent what the business does.
Business rules: Represent decisions that the business does.

So, although processes and rules are two different thing, there is a clear advantage if your end users are allowed to combine processes and rules. This means for example that:

rules can define which processes to invoke,
rules can specify decisions in that process
rules can augment (or even override) the behaviour specified in the process (for example to handle exceptional cases)
assignment rules can be used to assign actors to (human) tasks
rules can be used to dynamically alter the behaviour of your process
so by delegating the decision to drools we can improve the business process execution.

Step1:
create a sample jBPM process.

bpmnProjectExplorer

HelloWorld.bpmn

now lets integrate drools code to make our process little resilient for that we need to create a drools rule file as below.

newrule

Now lets create two pojo classes as below.

Account.java

/**
 * 
 */
package com.spark.model;

/**
 * @author Sony
 * 
 */
public class Account {

	private long money;
	private String name;

	public Account() {

	}

	public long getMoney() {
		return money;
	}

	public void setMoney(long money) {
		this.money = money;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

}

RiskyAccounts.java

/**
 * 
 */
package com.spark.model;

import java.util.ArrayList;

/**
 * @author Sony
 *
 */
public class RiskyAccounts {

	private ArrayList<Account> accounts = new ArrayList<Account>();
	
	public void addAccount(Account account){
		accounts.add(account);
	}
	
	public void displayRiskyAccounts(){
		for(Account account : accounts){
			System.out.println("AccountName: "+account.getName() +", Money: "+account.getMoney());
		}
	}
}

now its time to code the drools code as follows.simplerule.drl

//created on: 15 Aug, 2013
package com.sample

//list any import classes here.
import com.spark.model.Account;
import com.spark.model.RiskyAccounts;

//declare any global variables here

global RiskyAccounts riskyAccounts;


rule "balanceRequiredInAccount"

    when
        $accountObject : Account(money <= 0)
    then
    	System.out.println("Not enough money in the account !");
   		riskyAccounts.addAccount($accountObject);
		
end

Now its time to trigger our process combined with rules(drools) that we coded as follows.

package com.sample;

import org.drools.KnowledgeBase;
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.builder.ResourceType;
import org.drools.io.ResourceFactory;
import org.drools.runtime.StatefulKnowledgeSession;

import com.spark.model.Account;
import com.spark.model.RiskyAccounts;

/**
 * This is a sample file to launch a process.
 */
public class ProcessMain {

	public static final void main(String[] args) throws Exception {
		// load up the knowledge base
		KnowledgeBase kbase = readKnowledgeBase();
		StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
		
		// start a new process instance
		RiskyAccounts risky = new RiskyAccounts();
		ksession.setGlobal( "riskyAccounts", risky );
		ksession.startProcess("com.sample.bpmn.hello");
		
		Account account = new Account();
		account.setMoney(0);
		account.setName("Current Account");
		
		ksession.insert(account);
		ksession.fireAllRules();
		risky.displayRiskyAccounts();
	}

	private static KnowledgeBase readKnowledgeBase() throws Exception {
		KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
		kbuilder.add(ResourceFactory.newClassPathResource("simplerule.drl"), ResourceType.DRL);
		kbuilder.add(ResourceFactory.newClassPathResource("sample.bpmn"), ResourceType.BPMN2);
		if(kbuilder.hasErrors()){
			throw new RuntimeException(kbuilder.getErrors().toString());
		}
		KnowledgeBase knowledgeBase = kbuilder.newKnowledgeBase();
		knowledgeBase.addKnowledgePackages(kbuilder.getKnowledgePackages());
		return knowledgeBase;
	}
	
}

Happy coding Drools+jBPM 🙂

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