Drools + Activity | 1 Drools + Activiti Integration. By GulamNadim Mansuri
Drools + Activity | 1
Drools + Activiti
Integration.
By GulamNadim Mansuri
Drools + Activity | 2
What is Drools?
Drools is a business rule management system (BRMS). It is
known as a Production Rule System, which enhancing
implementation of Rete Algorithm.
Activiti :
Activiti is a light-weight workflow and Business Process
Management (BPM) Platform. Activiti is open source based on
java. In many Requirements we need some constraint to define
on Process. This Constraints is nothing but the rule which we
have to define on Process.
Define rules on Business Process Management we need drools,
but Activiti doesn't have native integration of drools as JBPM.
So Manually we have to integrate Drools with Activiti. Step by
step guide of whole process is described below with proper
explanation of each.
Drools + Activity | 3
Setups Requirements:
For this entire process you require some setups files.
1> JAVA (jdk recommended 1.7): You have to install java (jdk
1.7) and set java_home path.
2> Activity Designer: You need activity-designer-
5.12.0.zip plugins for eclipse. So you can create
Activiti Project in Eclispe.
You can download Activity Designer for Eclipse from URL:
activiti.org/designer/archived/activiti-
designer-5.12.0.zip
Drools + Activity | 4
Step 1:
First we add plugin into the Eclipse, So for that follow below
steps.
Open Eclipse click on Help
Install new Software
click on Add and copy the path of downloaded activiti-
designer-5.12.0.zip file.
Enter “Activiti” in Name field.
Drools + Activity | 5
You will see above screen where Activiti BPMN Designer
will be loaded. [If it show pending then you have wait till it
will not show this option]
You have to select this loaded Acitiviti Designer, so click on
check box and then Next.
It will load all the file related to this Designer, so wait till it
not complete then click on next, Accept agreement and
click on finish it will add plugin in your eclipse.
Now, You are able to create project of Activiti, you will
have Activiti Project option for project creation.
Step 2:
To run Activiti Project you need some jars related to activiti and
drools.
There is a two way to add jars
1. Add them in build path
2. Add using maven
We will use maven. You have to declare necessary dependency
jars in pom.xml so it will download jars and related to that jar
as well.
Install Maven we have to follow below steps.
Drools + Activity | 6
In Eclipse go to Help
click on Eclipse Market Place
Search for Maven.
I am using juno eclipse so I have install Maven Integration
for Eclipse.
Each time when you install software it will ask you for the
restart of eclipse choose yes.
It will restart eclipse and you have activiti and maven both
for your project.
Drools + Activity | 7
Step 3:
Eclipse Ready so now, lets create project of activiti and use
activiti.
First create project for that
Go to File->New->Other->Activiti Project->Enter Name-
>Finish.
It will create project for you and your project have above
hierarchy.
Drools + Activity | 8
We will create project on loan system. Which will validate
customer income if its satisfy condition then loan is
approved otherwise rejected.
For that we will create Loan Pojo, Rule file, BPMN Process
Diagram, Service class, and main class.
Step 4:
First we will create Pojo class, so for that go to
src/main/java inside this create one package com.Beans.
In this package create Pojo class. Right click on package →
new and select class. Give Name “LoanApplicant”. Copy
below code inside created class.
LoanApplicant.java
package com.Beans;
import java.io.Serializable;
public class LoanApplicant implements Serializab le {
private static final long serialVersionUID = 1L;
private long income;
Drools + Activity | 9
private String name;
private long loanAmount;
private boolean checkCred itOk;
public long getIncome() {
return income;
}
public void setIncome(long income) {
this .income = income;
}
public String getName() {
return name;
}
public void setName(String name) {
this .name = name;
}
public long getLoanAmount() {
return loanAmount;
}
public void setLoanAmount(long loanAmount) {
this .loanAmount = loanAmount;
}
public boolean is CheckCreditOk() {
return checkCreditOk;
}
public void setCheckCreditOk(boolean checkCreditOk) {
this .checkCreditOk = checkCred itOk;
Drools + Activity | 10
}
@Override
public String toString() {
return " LoanApplicant [income=" + income + ", name=" + name
+ ", loanAmount=" + loanAmount
+ ", checkCreditOk=" + checkCreditOk +
"]";
}
}
Step 5:
We will create Diagram for the project.
For that go to src/main/resources, there is one more
package “diagrams”. You have to create Process file inside
“diagrams” folder.
Right click on diagrams-> New-> Other-> Activiti ->Activiti
Diagram. Give name “ BusinessRuleLoanProcess.bpmn” .
This file have following Diagram.
Drools + Activity | 11
To Create this Diagram you have to BPMN notation given
in the right hand side.
First Select Start Event from Event, then we have Service
Task which is given in task, then Business Rule Task same
in task. This Business Rule Task is connected with Gateway
which is Exclusive Gateway. It Have to path one is connect
with Human Task and one is connect with End or
Terminate Event.
Start Event->Script Task->Bussiness rule Task->Exclusive
Gateway->Usertask->End Event.
Drools + Activity | 12
Step 6:
Create Rule file.
For that Go to diagrams-> new-> Other-> drools-> rule resource.
Give name “LoanRequestRules.drl”.
Copy below rules in file.
import com.Beans.LoanApplicant;
rule "CreditCheckRule"
when
$la :LoanApplicant(income > (2 * loanAmount))
then
$la.setCheckCreditOk(true);
System.out.println(" You are eligible loan Approved "+ $la.getName());
end
This File have Rule name as “CreditcheckRule”. This rule
check income with required loan amount. If income is
greater then double asking loan amount then it will allow
otherise it remain false.
Ex. If your salary is 10,000 and you request for 2,000 loan
amount then 2,000 * 2= 4,000 which is less then 10,000 so
it will give you loan, but you asking for 6,000 loan then
10,000 is not greater then 12,000 so it will not give you
loan.
Drools + Activity | 13
Step 7:
We will create one Service class which is used in Service
Task notation.
Inside com.Beans create one class “LoanService.java” and
copy below code.
package com.Beans;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.JavaDelegate;
public class LoanService implements JavaDelegate {
@Override
public void execute(DelegateExecution arg0) throws Exception {
// TODO Auto-generated method stub
LoanApplicant loan=new LoanApplicant();
loan.setName((String) arg0.getVariable("name"));
loan.setLoanAmount((Long) arg0.getVariable("amount"));
loan.setIncome((Long) arg0.getVariable("salary"));
arg0.setVariable("loanapplicant", loan);
Drools + Activity | 14
}
}
Here we have implement JavaDelegate, When you want to call class from
service task or you want to add custom business logic.
This class override execute methode, which have parameter this parameter
contain values of Process Variables. You have used variables on process
Diagrams then you will fetch it using this Object.
Create one LoanApplicant Object. This Object contain values of Process
variables.
At end we have pass this object to “loanapplicant” alias.
Step 8:
We have create Diagrams and classes which is required for
Notation which we have used in Diagrams. Now, we will
set properties of Notation.
For that click on start event at below you will see
properties window, click on it and go to form option.
If you don't have property window then click on window,
go to show view click on other and search property, select
it and click on ok.
Drools + Activity | 15
We have to create 3 variables here as you see in image.
To create Variable click on New it will open One Dialogue
box. Enter ID, name and type.
Note: You have to use small alphabet for type.
Click on OK.
Name has string data type, amount has long and salary has
long data type.
Drools + Activity | 16
We have set 3 variables now, this 3 variable available for whole
process diagrams.
name is contain name of Applicant,
amount is contain loan amount,
salary contain salary of Applicant.
We have set start event properties, now we have to set
Service Task properties.
For that click on Service Task, go to properties there is
attribute call “Service class”.
Click on this attribute and select the “LoanService.java”
class which we have created.
Drools + Activity | 17
Go to Business Rule Task, go to properties.
Inside Property you have Main config option click on it.
Here you have to set 2 property Rule Name and Input
Variables.
Rule Name: Write name of the Rule, we have given in rule
file “CreditCheckRule”.
Input Variables: ${loanapplicant}
Drools + Activity | 18
Go to Exclusive condition, to put condtion click on
sequence flow and set property as shown below.
Drools + Activity | 19
In property click on Main config option and write following
line.
${loanapplicant.checkCreditOk==true}
Here loanapplicant is same object which we have used in
execute method.
Set Condition for other sequence flow.
Drools + Activity | 20
Write following line:
${loanapplicant.checkCreditOk==false}
Step 9:
Its time to add necessary dependency for Activiti and
Drools.
You dont have to create pom.xml for that when you create
project pom.xml is created by default.
Open it and Add below depedency in the file.
<dependency>
Drools + Activity | 21
<groupId>org.drools</groupId>
<artifactId>knowledge-api</artifactId>
<version>5.4.0.Final</version>
</dependency>
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-compiler</artifactId>
<version>5.4.0.Final</version>
</dependency>
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-core</artifactId>
<version>5.4.0.Final</version>
</dependency>
<dependency>
<groupId>org.mvel</groupId>
<artifactId>mvel2</artifactId>
<version>2.1.0.drools16</version>
</dependency>
Drools + Activity | 22
Step 10:
Create Deployment artifact.
For that right click on project-> create deployment artifact.
This will cerate two file, which have same name as your process file, but
extension is different.
First file have “.bar” extension same as .war file and other have “.jar”
extension.
These files created inside deployment folder of your project.
Step 11:
Create Junit test so that we can execute this application.
To create Junit right click on
“BusinessRuleLoanProcess.bpmn” file->Activiti->generate
junit test.
This will create classes inside src/test/java and one file
inside src/test/resource.
Inside src/test/resources you have file activiti.cfg.xml,
Open it and copy property below the existing property
tag.
Drools + Activity | 23
<property name=" jobExecutorActivate" value="true" />
<property name="customPostDeployers">
<list>
<bean class="org.activiti.engine.impl.rules.RulesDeployer" />
</list>
</property>
RulesDeployer is used for deploy and execute the rule on
Activiti engine.
Inside src/test/java Junit test class is generated, so open it
and copy below code.
package org.activiti.designer.test;
import static org.junit.Assert.*;
import java.util.HashMap;
import java.util.Map;
import java.util.zip.ZipInputStream;
import java.io.FileInputStream;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.test.ActivitiRule;
import org.junit.Rule;
import org.junit.Test;
public class ProcessTestMyProcess {
Drools + Activity | 24
private String filename = "/home/attune2/Alternative Drools Eclipse/workspace/Drools-
Demo/deployment/BusinessRuleLoanProcess.bar";
@Rule
public ActivitiRule activitiRule = new ActivitiRule();
@Test
public void startProcess() throws Exception {
RepositoryService repositoryService = activitiRule.getRepositoryService();
ZipInputStream inputStream = new ZipInputStream(new
FileInputStream(filename));
String deploymentID =
repositoryService.createDeployment().name("BusinessRuleLoanProcess.bar").addZipInputStrea
m(inputStream).deploy().getId();
RuntimeService runtimeService = activitiRule.getRuntimeService();
Map<String, Object> variableMap = new HashMap<String, Object>();
variableMap.put("name", "Nadim");
variableMap.put("amount", 2400L);
variableMap.put("salary", 10000L);
ProcessInstance processInstance =
runtimeService.startProcessInstanceByKey("myProcess", variableMap);
assertNotNull(processInstance.getId());
System.out.println("id " + processInstance.getId() + " "
+ processInstance.getProcessDefinitionId());
}
Drools + Activity | 25
}
In this class you have to change “filename”, this is the path
of your bar file.
In your Project you have created bar file so copy its
path.
Step 12:
Its time to run Application.
Go to->your ProcessTest.java->Run as->Junit Test.
You will find above output.