Human Task in jBPM using Guvnor

Hello all here is my post on jBPM Human Task using Guvnor.
1. After you have installed the complete setup of jBPM 5.4 please run “ant start.demo.noeclipse”.
2. Open browser and create “http://localhost:8080/drools-guvnor/”.
3. In the “KnowledgeBase” please create jBPM process file in the “default package”.

HumanTask

I used two Script Tasks to set the variables and two User Tasks.Script tasks are necessary because a Human Task can have only a single input parameter, “Content”.To pass multiple variables “Content” must be set to a map that contains all the variables.

In the first scipt task code the following content.

map = new java.util.HashMap();
kcontext.setVariable("map",map);
kcontext.setVariable("itemNumber",itemNumber);
kcontext.setVariable("quantity",quantity);
kcontext.setVariable("unitPrice", unitPrice);
kcontext.setVariable("requestedDate",requestedDate);

4. Set ActorId to “krisv”.(this means we are assigning the human task to krisv)
5. Parameter = “Content”, Value = “map”
6. declare the dataOutVariables and assign them to global variables.
This will allow us to use variables inside the Human Task Form.Open the result mapping editor and add a mapping of the returned values.

7.In the next HumanTask assign this to “john” to review and accept the order raised by “krisv” once the order is approved it will be closed and instance is destroyed automatically.

watch the video tutorial on http://youtu.be/Q3MHdtI9Jh0

Happy coding and jBPM 🙂

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 🙂

Configure jBPM with MySQL

Hello Guys, Here is my next post on jBPM, the most flexible option of jBPM is that, it can be configured with major Database vendors of market like Oracle, MS SqlServer, MySql,etc. In this post i want to explain how to download the latest jBPM and configure it with MySQL

Step 1: please download “jbpm-5.4.0.Final-installer-full.zip”
Step 2: Extract it to your preferred locations of your PC.
Step 3: Setup Mysql Database.
Step 4: With MySQL installed, log in as the root user and run the following commands

mysql> CREATE SCHEMA IF NOT EXISTS jbpm5;
mysql> CREATE USER 'jbmp5'@'localhost' IDENTIFIED BY 'jbpm5';
mysql> GRANT ALL PRIVILEGES ON jbpm5.* TO 'jbpm5'@'localhost';

mysql> CREATE SCHEMA IF NOT EXISTS task;
mysql> CREATE USER 'task'@'localhost' IDENTIFIED BY 'task';
mysql> GRANT ALL PRIVILEGES ON task.* TO 'task'@'localhost;

Set up jBPM
Step 5:

ant clean.demo

Second, you will have to change the following files:

a. build.properties
b. db/jbpm-persistence-JPA2.xml
c. db/task-persistence-JPA2.xml
d. standalone.xml
e. task-service/resources/META-INF/persistence.xml
f. lib/jbpm-gwt-console-server-5.4.0.Final-EE6.war/WEB-INF/classes/META-INF/persistence.xml
g. lib/jbpm-human-task-war-5.4.0.Final-EE6.war/WEB-INF/classes/META-INF/persistence.xml

build.properties
change:

# data base related properties
db.driver.jar=${install.home}/db/driver/${db.driver.jar.name}
db.driver.module.dir=${jboss.home}/modules/${db.driver.module.prefix}/main/
# default is H2
db.name=h2
db.driver.jar.name=${db.name}.jar
db.driver.download.url=http://repo1.maven.org/maven2/com/h2database/h2/${H2.version}/h2-${H2.version}.jar
#other options are mysql
#  db.name=mysql
#  db.driver.module.prefix=com/mysql
#  db.driver.jar.name=${db.name}-connector-java.jar
#  db.driver.download.url=https://repository.jboss.org/nexus/service/local/repositories/central/content/mysql/mysql-connector-java/5.1.18/mysql-connector-java-5.1.18.jar
#postresql
#  db.name=postresql
#  db.driver.module.prefix=org/postgresql
#  db.driver.jar.name=${db.name}-jdbc.jar
#  db.driver.download.url=https://repository.jboss.org/nexus/content/repositories/thirdparty-uploads/postgresql/postgresql/9.1-902.jdbc4/postgresql-9.1-902.jdbc4.jar

To:

# data base related properties
db.driver.jar=${install.home}/db/driver/${db.driver.jar.name}
db.driver.module.dir=${jboss.home}/modules/${db.driver.module.prefix}/main/
#  default is H2
#  db.name=h2
#  db.driver.jar.name=${db.name}.jar
#  db.driver.download.url=http://repo1.maven.org/maven2/com/h2database/h2/${H2.version}/h2-${H2.version}.jar
#other options are mysql
  db.name=mysql
  db.driver.module.prefix=com/mysql
  db.driver.jar.name=${db.name}-connector-java.jar
  db.driver.download.url=https://repository.jboss.org/nexus/service/local/repositories/central/content/mysql/mysql-connector-java/5.1.18/mysql-connector-java-5.1.18.jar
#postresql
#  db.name=postgresql
#  db.driver.module.prefix=org/postgresql
#  db.driver.jar.name=${db.name}-jdbc.jar
#  db.driver.download.url=https://repository.jboss.org/nexus/content/repositories/thirdparty-uploads/postgresql/postgresql/9.1-902.jdbc4/postgresql-9.1-902.jdbc4.jar

db/jbpm-persistence-JPA2.xml

change:

<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />

To:

<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />

standalone.xml

Change:

<datasource jta="true" jndi-name="java:jboss/datasources/jbpmDS" pool-name="H2DS" enabled="true" use-java-context="true" use-ccm="true">
  <connection-url>jdbc:h2:~/jbpm</connection-url>
  <driver>h2</driver>
  <pool>
    <min-pool-size>1</min-pool-size>
    <max-pool-size>4</max-pool-size>
    <prefill>false</prefill>
    <use-strict-min>false</use-strict-min>
    <flush-strategy>FailingConnectionOnly</flush-strategy>
  </pool>
  <security>
    <user-name>sa</user-name>
  </security>
  <validation>
    <check-valid-connection-sql>SELECT 1</check-valid-connection-sql>
    <validate-on-match>false</validate-on-match>
    <background-validation>false</background-validation>
  </validation>
</datasource>
<drivers>
  <driver name="h2" module="com.h2database.h2">
    <xa-datasource-class>org.h2.jdbcx.JdbcDataSource</xa-datasource-class>
  </driver>
</drivers>

To:

<datasource jta="true" jndi-name="java:jboss/datasources/jbpmDS" pool-name="H2DS" enabled="true" use-java-context="true" use-ccm="true">
  <connection-url>jdbc:mysql://localhost:3306/jbpm5</connection-url>
  <driver>mysql</driver>
  <pool>
    <min-pool-size>1</min-pool-size>
    <max-pool-size>4</max-pool-size>
    <prefill>false</prefill>
    <use-strict-min>false</use-strict-min>
    <flush-strategy>FailingConnectionOnly</flush-strategy>
  </pool>
  <security>
    <user-name>jbpm5</user-name>
    <password>jbpm5</password>
  </security>
  <validation>
    <check-valid-connection-sql>SELECT 1</check-valid-connection-sql>
    <validate-on-match>false</validate-on-match>
    <background-validation>false</background-validation>
  </validation>
</datasource>
<drivers>
  <driver name="mysql" module="com.mysql">
    <xa-datasource-class>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</xa-datasource-class>
  </driver>
</drivers>

<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />

db/task-persistence-JPA2.xml

Change:

<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>

To:

<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>

task-service/resources/META-INF/persistence.xml

Change:

<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
<property name="hibernate.connection.driver_class" value="org.h2.Driver"/>
<property name="hibernate.connection.url" value="jdbc:h2:tcp://localhost/runtime/task" />
<property name="hibernate.connection.username" value="sa"/>
<property name="hibernate.connection.password" value=""/>

To:

<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="hibernate.connection.driver_class" value="org.jdbc.Driver"/>
<property name="hibernate.connection.url" value="jdbc:mysql:://localhost:3306/task" />
<property name="hibernate.connection.username" value="task"/>
<property name="hibernate.connection.password" value="task"/>

Now its time to change the persistence.xml in lib/jbpm-gwt-console-server-5.4.0.Final-EE6.war and lib/jbpm-human-task-war-5.4.0.Final-EE6.war. to do this install 7-zip software open the war file using 7Zip and change the persistence.xml save and close it as below.

lib/jbpm-human-task-war-5.4.0.Final-EE6.war/WEB-INF/classes/META-INF/persistence.xml
Change:

<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" />

To:

<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />

lib/jbpm-gwt-console-server-5.4.0.Final-EE6.war/WEB-INF/classes/META-INF/persistence.xml
Change:

<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" />

To:

<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />

Install jBPM
Run:

ant install.demo 

To start the jBPM environment you can run:

ant start.demo.db

or

ant start.demo.db.noeclipse

after you execute all the steps above and run the application, open the database and you should find your database with the tables as shown below.
jbpm5_schema

Happy Coding, Happy jBPM 🙂

jBPM Loops Concept

Hello all, As told i will be posting jBPM tutorials from now, here is my next tutorial on jBPM loops. prior to this tutorial please download jBPM 5.x version and install the demo environment after that open the eclipse jBPM editor and code the process as shown below. just by drag and drop the BPM components.

jbpm-looping

once the process is ready please click on the empty space of the editor and edit the properties as below.

process_init_var

now click the “first log node” as shown below and open the property Palette edit the action Values as shown below.
the “kcontext” is the jBPM KnowledgeContext available inorder to access the process variables.
Edit_log1

in the process shown there are two gateways the first one is converging gateway and the second one is diverging gateway. i will post a seperate article on gateways later.click on the first gateway and assign XOR as value for the type value. now click the second log node and in the property pallete edit the properties and put the simple logic as shown below.

Edit_log2

Now click on the second gateway since it is the converging gateway, click the Condition browse button and add a condition as shown below.

ifCondition

now lets give the else logic as shown below.else

once done lets prompt the process that it is done by editing the last log node.
now its time for a test run of the process

package com.spark.jbpm.sample.loops;

import java.util.HashMap;
import java.util.Map;

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.logger.KnowledgeRuntimeLogger;
import org.drools.logger.KnowledgeRuntimeLoggerFactory;
import org.drools.runtime.StatefulKnowledgeSession;

/**
 * @author Sony
 *
 */
public class LoopingExample {
	
	public static final void main(String[] args) {
		try {
			// load up the knowledge base
			KnowledgeBase kbase = readKnowledgeBase();
			StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
			KnowledgeRuntimeLogger logger = KnowledgeRuntimeLoggerFactory.newThreadedFileLogger(ksession, "test", 1000);
			// start a new process instance
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("count", 5);
			ksession.startProcess("com.sample.looping", params);
			logger.close();
		} catch (Throwable t) {
			t.printStackTrace();
		}
	}

	private static KnowledgeBase readKnowledgeBase() throws Exception {
		KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
		kbuilder.add(ResourceFactory.newClassPathResource("looping/Looping.bpmn"), ResourceType.BPMN2);
		return kbuilder.newKnowledgeBase();
	}

}

and finally u will see the O/P of your first jBPM Process.

Happy jBPM 🙂