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.

create a sample jBPM process.



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


Now lets create two pojo classes as below.

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) { = money;

	public String getName() {
		return name;

	public void setName(String name) { = name;


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){
	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"

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

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.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 );
		Account account = new Account();
		account.setName("Current Account");

	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);
			throw new RuntimeException(kbuilder.getErrors().toString());
		KnowledgeBase knowledgeBase = kbuilder.newKnowledgeBase();
		return knowledgeBase;

Happy coding Drools+jBPM 🙂


Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s