jBPM and Drools Introduction
Contents
[hide]- 1Copyright Notice
- 2BPMN Introduction
- 3JBPM 6 - 0.0 - jBPM Overview
- 4JBPM 6 - 1.0 - Workbench Overview
- 5JBPM 6 - 1.1 - Creating Simple Process 创建简单流程
- 6JBPM 6 - 1.2 - Process Variables 过程变量
- 7JBPM 6 - 1.3 - InputSets and OutputSets
- 8JBPM 6 - 1.4 - Actors and Groups
- 9JBPM 6 - 1.5 - BAM
- 10JBPM 6 - 2.0.0 - Unit Testing Introduction 单元测试介绍
- 11JBPM 6 - 2.0.1 - Eclipse Overview
- 12JBPM 6 - 2.2 - User Task
- 13JBPM 6 - 2.3 - Debugging Processes
Copyright Notice[edit]
Copyright © 2004-2016 by NobleProg Limited All rights reserved.
This publication is protected by copyright, and permission must be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise.
training@nobleprog.com tel: +44 20 7558 8274
BPMN Introduction[edit]
BPMN Basics[edit]
Flow Objects | Swimlanes |
Connecting Objects | Artifacts (used to provide additional information about the Process) |
Event Basics[edit]
An Event is something that “happens” during the course of a Process.
- Events affect the flow of the Process and usually have a cause or an impact. The term “event” is general enough to cover many things in a Process.
- The start of an Activity, the end of an Activity, the change of state of a document, a Message that arrives, etc., all could be considered Events
Gateways[edit]
Gateways are used to control splitting and merging of sequence flows.
|
Types of Tasks[edit]
JBPM 6 - 0.0 - jBPM Overview[edit]
jBPM General Info[edit]
- jBPM is a BPMS (Business Process Management Suite) written in Java
- jBPM是一个用Java写的业务流程管理系统
- It allows you to design, model, execute, and monitor business processes throughout their life cycle.
- jBPM让你可以在业务流程生命周期里:设计,建模,执行和监视业务流程
- Contains Drools - BRMS
- 包括Drools - BRMS (业务规则管理系统)集成
- jBPM, released by JBOSS (Red Hat boght JBOSS)
- jBPM是由 Red Hat Inc. 发布的(Red Hat 收购了 JBOSS Inc.)
- Open Source and Commercial
- 有两个版本,开源和商业版
BPMN[edit]
BPMN name and standard。[edit]
- In Version 1.x BPMN stands for Business Process Modelling Notation
- BPMN 1.版本是业务流程建模标记法
- Since Version 2.x BPMN stands for Business Process Model and Notation
- BPMN 2.x版本是业务流程模型和标记法
BPMN 2.0 Purpose。[edit]
- Provide a notation that is readily understandable by business and technical people
- 为业务和技术人员提供了一个容易理解的语言
- Create a standardised bridge for the gap between the business process design and process implementation
- 在业务流程的设计和实现之间搭建了一个标准化的桥梁
- To standardise a business process model and notation in the face of many different modelling notations and viewpoints
- 业务流程建模标记法
- To provide a means of communicating process information to other businesses, users, managers and process implementers
- 为其他的业务,用户,经理和过程的实现人提供了一种沟通过程信息的手段
- To exchange BPMN definitions (both domain model and diagram layout) between different tools
- 在不同的工具之间交换BPMN的定义(域模型和框图布局)
Audience of BPMN|BPMN的用户。[edit]
- Business users 业务用户
- Business analysts 业务分析员
- Strategy analyst 策略分析员
- Quality managers 质量管理员
- Technical developers 技术开发人员
- Process designers 流程设计员
- Developers 开发人员
- Integrators 集成人员
- Software, System and Enterprise Architects 软件,系统和企业架构人员
BPMN Conformance|BPMN标准一致性。[edit]
-
Software
based on spec
软件基于BPMN标准
- software tool developed only partially matching compliance/conformance
- 如果软件工具(BPMNS)与部分标准一致,这叫做 基于BPMN标准
-
Modeling Conformance
- 建模标准一致性
- Process modeling 流程建模
- Choreography modelling Choreography建模一致性
-
Execution Conformance 执行标准一致性
- Process Execution 过程的执行
- BPEL Process Execution
-
A tool can comply to one of the above or any subset of them.
- 工具可以符合上面的标准的任何部分
-
BPMN Complete Conformance complies to the all 4 above.
- BPMN完全标准一致符合以上全部四个标准
-
jBPM
is based on BPMN 2.0, but doesn't conform to BPMN standard
- jBPM是基于BPMN2.0标准,但是她不符合BPMN的标准
Sub-models within an end-to-end BPMN model 。[edit]
- Processes (Orchestration)
- Private non-executable (internal) BP
- Private executable (internal) BP <- jBPM
- Public Processes
- Choreographies
- Collaborations (can include Processes and Choreographies
- Conversations
jBPM components[edit]
BPM tools[edit]
JBPM 6 - 1.0 - Workbench Overview[edit]
Starting jBPM console[edit]
cd /opt/jbpm-installer ant start.demo
or
ant start.demo.noeclipse
Go to Firefox and type: http://localhost:8080/jbpm-console
Hello World Demo[edit]
Evaluation Demo 评估的演示[edit]
- An employee performance needs to be evaluated
- 员工绩效评估
- First, the employee gives feedback about their own performance
- 首先员工自我评估
- Later, HR (mary) and PM (john) departments give their opinion about employee department
- 然后人事(marry)和项目经理(john)部门评估
- It does not matter whether HR or PM evaluation is done first
- marry和john的评估顺序无所谓
Lab 1.1.1 Executing Process in Kie[edit]
- localhost:8080/jbpm-console
- login krisv/krisv
- Build Project
- Got to Authoring/Project Autorining
- In Project Explorer select demo / jbpm-playground / Evaluation project
- Click BUSINESS PROCESSES / 'evaluation' process to view the diagram
- Click Open Project Editor / Build & Deploy
- Start an Process Instance
- Process Management / Process Definitions
- Actions / Start
- Employee: krisv (must be this user)
- Reason: evaluation
- Perform Self-Evaluation
- Task List / Actions / Start
- Performance: "I am excellent"
- HR Evaluation
- Log in as mary
- Tasks / Actions / Start
- Click on Magnifying Glass icon / Work tab / Complete
- PM Evaluation
- Log in as john
- Task / Actions / Start
- Click on Magnifying Glass icon / Work tab / Complete
- View Log
- Process Management / Process Instances / Completed
- Actions / Details
Exercise 1[edit]
- Run another instance of the process on your own
JBPM 6 - 1.1 - Creating Simple Process 创建简单流程[edit]
Scenario 场景[edit]
- We are going to recruit people
- 我们要招人
- At first the company is small, so it involves only interviewing people over the phone
- 起初公司规模小,所以只需要电话面试即可
- Later more things to the process will be added
- 以后会有更多的东西加入流程里
- We will use this example over the most of the materials
- 我们将在大部分教材中使用这个例子
Creating Organization Unit, repository, project and package 创建组织单元、库、项目和包[edit]
- Creating Organizational Unit
- Log in as krisv
- Authoring/Administration
- Organization Units/Manage Organization Units/Add
- Name: npou / Owner: NobleProg Ltd
- Creating Repository
- Authoring/Administration
- Repositories/New Repository
- Repository Name: nprep/Organizational Unit: npou
- Creating Project
- Authoring/ Project Authoring
- Switch to npou/nprep
- New Item/Project
- Resource Name: npproj1
- Group ID: nppack
Creating Simple Process[edit]
- Creating simple diagram
- Authoring/Project Authoring
- New Item/Business Process/Name: emp_recruitment1
- Create User Task, set
- Name HT Phone Interview
- Task Name to HT Phone Interview
- Actor: krisv
- Validate the process
- Save, check in with "Added Phone Interview Task" message
Deploying and Executing[edit]
- Deploy the process
- Check whether Instance is running
- Process Management/Process Instances
- Performing a task
- Tasks/Task List
- Actions/Start
- Details / Work / Complete
- Check the Instance
- Process Management / Process Instances / Completed
- View Instance log of the task
Optional Browsing Database for completed instances and tasks[edit]
- Download SQL Workbench
- Login to /home/installuser/jbpm-db (username sa, no password)
- select * from taskevent;
JBPM 6 - 1.2 - Process Variables 过程变量[edit]
Scenario 场景[edit]
- We would like to record the name of the person we are interviewing and the outcome of the interview.
- 我们想记录我们面试的人的名字和面试的结果
Instructions 步骤[edit]
- Copy emp_recruitment1 to emp_recruitment2
Top Right Corner, click Copy - Change the Process Name and ID accordingly
- Create process variable pv_candidate_name (Properties/Variable Definitions)
- Generate forms
- Check in Project Explorer under FORM DEFINITIONS that the forms has been properly generated
- Deploy
- Start new process instance
- Perform a task
- Check in Completed instances (Process Management/Process Instances/Completed/Actions/Details) the value of the variable
Exercise 1.2.1[edit]
- Add pv_candidate_suitable:Boolean variable and generate forms for it
JBPM 6 - 1.3 - InputSets and OutputSets[edit]
InputDataSets Instructions[edit]
- Copy emp_recruitment2 to emp_recruitment3
- Change the Process Name and ID accordingly
- In the Phone Interview task, add DataInputSet ut_candidate_name
- In assignments, map pv_candidate_name to ut_candidate_name
- Generate forms
- Deploy
- Start new process, input Albert as the candidate name
- Perform a task, change the ut_candidate_name field value before complete a task to AlbertModifiedInUserTask
- Check in Completed instances the value of the variable
OutputDataSets Innstructions[edit]
- Create DataOutputSet in Phone Interview task name ut_candidate_suitable:Boolean
- In Assignments, map ut_candidate_suitable to pv_candidate_suitable
- Generate all forms (remove old forms before)
- Edit process form (npproj.emp_recruitment3-taskform) and remove pv_candidate_suitable field
- Save and Deploy
- Execute the process
- When performing the task, tick the "candidate_suitable" field
- After completing the task, go to completed instances and view the process variables and process model.
Exclusive Gateway[edit]
- Modify process as shown on the diagram above
- Add constraint to the appropriate sequence flows:
-
- to Hired end:
return pv_candidate_suitable;
-
- to Not Hired end:
return pv_candidate_suitable == false;
- Run process twice selecting different option in ut_candidate_suitable check-box
- In completed instances check whether the variable got different values
JBPM 6 - 1.4 - Actors and Groups[edit]
You will learn 你将学到[edit]
- How to create a new user
- 如何建立用户
- How to create a new group
- 如何建立组
- How to assign task to a user
- 如何分配任务给一个用户
- How a user can claim and release tasks assign to a group
- 如何一个用户可以领取和释放分配给组的任务
Scenario 场景[edit]
- NobleProg is growing and now more people will perform the tasks.
- 诺波在发展并且更多的人将做这个任务
- We will create np_hr_role with two people: isabel and greg
- 我们将建立一个np_hr_role,其中包含两个人:isabel 和 greg
- We will also create np_manager_role with one person: bernard
- 我们还将建立np_manager_role,其中只有一个人:bernard
- Manager will be responsible for face to face interview
- 经理将负责面对面的面试
Creating users instructions[edit]
- Creating roles
- edit /opt/jbpm-installer/wildfly-8.1.0.Final/standalone/configuration/roles.properties and add following lines (please note that the admin role has been added in order to grant the users permission to Workbench)
bernard=np_manager_role,admin isabel=np_hr_role,admin greg=np_hr_role,admin
- Creating users
- edit /opt/jbpm-installer/wildfly-8.1.0.Final/standalone/configuration/users.properies and add folowing lines
isabel=isabel greg=greg bernard=bernard
Group Assignment[edit]
- Copy emp_recuritment3 to emp_recuritment4
- Change the Process Name and ID accordingly
- Edit Phone Interview task properties
- Remove Actor property
- In Group property type np_hr_role
- Deploy and start the process
Claiming task[edit]
- Log in as greg
- In Tasks you should see unlock padloc icon
- Log in as bernard
- You cannot claim the task because bernard is not np_hr_role
Face to Face interview[edit]
- Add new user task as show in the digram
- Assign the np_manager_role to Face to Face interview user task
- Create pv_f2f_candidate_suitable:Boolean process variable
- Create ut_f2f_candidate_suitable:Boolean task DataOutputSet in Face to Face interview task
- Create appropriate assignments
- Generate task forms
- Create appropriate conditions on the second gateway sequence flows
- Execute and test the process
Lanes[edit]
Sometimes it is a good idea to show different group assignments in lanes
JBPM 6 - 1.5 - BAM[edit]
Summary[edit]
- How to find meaningful statistics related to process variables
Instructions[edit]
- Dahboards
- Business Dashboards
- Login in as krisv/krisv
- Administration -> Data Providers
- Check if jBPM Process Summary exists (go to Instances by process), if not create one
- Create new data provider
- Fill out the query and name as below:
- Name: jBPM Process Summary, Type: SQL Query
- Query:
select processinstanceid, processname, status, start_date, end_date, user_identity, processversion, duration from processinstancelog
Instances by process[edit]
- Change workspace to jBPM Dashboard
Report based on custom variables[edit]
Browse database structure
cd /opt/jbpm-installer ant stop.demo cd /opt/jbpm-installer/db/driver java -jar h2.jar
Change database to jbpm or other location where it is (JDBC URL)
JDBC URL: jdbc:h2:~/jbpm
Find VARIABLEINSTANCELOG Click on it and run
SELECT * FROM VARIABLEINSTANCELOG
Create Data Provider[edit]
- Dashboards / Business Dahsboards
- Workspace: Showcase
- Administration / Data Providers / Create new data provider
- Type: SQL Query
- Name: Process Variables
- Paste the query:
SELECT * FROM VARIABLEINSTANCELOG;
Create Page[edit]
- Page: Click on the "Create new page icon"
- Title: Process Variables Analysis
- Go to Workspace
- Choose Page: Process Variables Analysis
- Create new panel in current page
- Click on Key Performance Indicator
- Drag "Create Panel" to the CENTER_1 side area
- Choose Process Variables data source
- KIP's name: Variables
- Domain X Axis: value
- Rage (Y Axis): variableid
- Change type pie chart
Create Filters[edit]
- Create new panel
- Filter & Drill-down
- Drag it to the left hand side
- Click on the cog and Edit Content
- Tick processid and variableid
The final view:
More Information[edit]
http://docs.jboss.org/jbpm/v6.0/userguide/chap-bam.html
JBPM 6 - 2.0.0 - Unit Testing Introduction 单元测试介绍[edit]
Unit testing 单元测试[edit]
- Unit testing is a software testing method by which individual units of source code are tested to determine if they are fit for use
- 单元测试是一个软件测试方法,人们经过测试代码的独立单元,从而决定是否代码可以投入使用
- Unit tests are short code fragments created by programmers or testers during the development process.
- 单元测试代码是开发过程中程序员或测试人员写的很短的代码片段
- Unit testing provides a sort of living documentation of the system.
- 单元测试相当于提供了一套活着的系统文档
JUnit[edit]
- JUnit is a unit testing framework for the Java programming language.
public class TestFoobar {
@Test
public void testOneThing() {
// Code that tests one thing
}
@Test
public void testAnotherThing() {
// Code that tests another thing
}
}
Assertion[edit]
- An assertion is a true–false statement placed in a program to indicate that the developer thinks 'that the predicate is always true at that place.
- If an assertion evaluates to false at run-time, an assertion failure results, which typically causes execution to abort.
x = 1;
assert (x > 0);
x++;
assert (x > 1);
Appendix[edit]
assert methods used in jBPM[edit]
assertNodeTriggered(processInstance.getId(),"Book Hotel");
assertNodeTriggered(processInstance.getId(), "StartProcess", "Hello", "EndProcess");
// checks whether the given node(s) was/were executed during the process execution
assertProcessInstanceActive(processInstanceId, ksession)
// checks whether the process instance is active
assertProcessInstanceCompleted(processInstanceId, ksession);
// checks whether the process instance has completed successfully
java operators[edit]
Simple Assignment Operator[edit]
= Simple assignment operator
Arithmetic Operators[edit]
+ Additive operator (also used for String concatenation) - Subtraction operator * Multiplication operator / Division operator % Remainder operator
Equality and Relational Operators[edit]
== equal to != not equal to > greater than >= greater than or equal to < less than <= less than or equal to ! logical complement operator; inverts the value of a boolean
Conditional Operators[edit]
&& Conditional-AND || Conditional-OR
JBPM 6 - 2.0.1 - Eclipse Overview[edit]
Sample jBPM Project[edit]
- Start eclipse using ant
$ ant start.eclipse
- Create new jBPM project, include sample project
ProcesTest.java file:
public class ProcessTest extends JbpmJUnitBaseTestCase {
@Test
public void testProcess() {
RuntimeManager manager = createRuntimeManager("sample.bpmn");
RuntimeEngine engine = getRuntimeEngine(null);
KieSession ksession = engine.getKieSession();
ProcessInstance processInstance = ksession.startProcess("com.sample.bpmn.hello");
// check whether the process instance has completed successfully
assertProcessInstanceCompleted(processInstance.getId(), ksession);
assertNodeTriggered(processInstance.getId(), "Hello");
manager.disposeRuntimeEngine(engine);
manager.close();
}
}
Sample.bpmn file:
- Run the project (Menu: Run / Run or Ctrl-F11) and check Console and JUnit windows
More Advanced jBPM Project[edit]
Scenario: A salesman will get a bonus if his sales is over 1000.
BPMN file[edit]
- Create new jBPM project (or open 04.1.2_exercise project)
- Change diagram file name to Bonus.bpmn2
- Add sales variable
- Properties Window/Data Items/Variable List for Process/Add
- Add conditions
- sales>1000
- else - default flow
- Define script tasks
- Congratulation!
System.out.println("You are the best!");
-
- Try harder!
System.out.println("Maybe next time.");
ProcessTest.java file[edit]
- Write Unit Test - add code to ProcessTest class
- Import Man and HashMap (import java.util.HashMap;, import java.util.Map;)
- Run project and check what happens if sales is 20000. Is bonus assigned? Check JUnit Window.
Map<String, Object> params = new HashMap<String, Object>();
params.put("sales", 20000);
ProcessInstance processInstance = ksession.startProcess("com.sample.test", params);
assertNodeTriggered(processInstance.getId(),"Bonus Assigned");
assertProcessInstanceCompleted(processInstance.getId(), ksession);
- The final code
public class ProcessTest extends JbpmJUnitBaseTestCase {
@Test
public void testProcess() throws Exception {
createRuntimeManager("Bonus.bpmn2");
RuntimeEngine engine = getRuntimeEngine(null);
KieSession ksession = engine.getKieSession();
Map<String, Object> params = new HashMap<String, Object>();
params.put("sales", 20000);
ProcessInstance processInstance = ksession.startProcess("com.sample.test", params);
assertNodeTriggered(processInstance.getId(),"Bonus Assigned");
assertProcessInstanceCompleted(processInstance.getId(), ksession);
}
}
- Add another test
Solution[edit]
/**
* Copyright NobleProg Ltd
* Author: Filip Stachecki (filip@nobleprog.pl)
* more info: http://www.nobleprog.co.uk
*/
package com.nobleprog;
import java.util.HashMap;
import java.util.Map;
import org.jbpm.process.instance.impl.demo.SystemOutWorkItemHandler;
import org.jbpm.test.JbpmJUnitBaseTestCase;
import org.junit.Test;
import org.kie.api.runtime.KieSession;
import org.kie.api.runtime.manager.RuntimeEngine;
import org.kie.api.runtime.process.ProcessInstance;
import org.kie.internal.KnowledgeBase;
import org.kie.internal.runtime.StatefulKnowledgeSession;
public class ProcessTest extends JbpmJUnitBaseTestCase {
@Test
public void testProcess() throws Exception {
createRuntimeManager("Bonus.bpmn2");
RuntimeEngine engine = getRuntimeEngine(null);
KieSession ksession = engine.getKieSession();
Map<String, Object> params = new HashMap<String, Object>();
params.put("sales", 20000);
ProcessInstance processInstance = ksession.startProcess("com.sample.test", params);
assertNodeTriggered(processInstance.getId(),"Bonus Assigned");
assertProcessInstanceCompleted(processInstance.getId(), ksession);
}
@Test
public void testProcessNoBonus() throws Exception {
createRuntimeManager("Bonus.bpmn2");
RuntimeEngine engine = getRuntimeEngine(null);
KieSession ksession = engine.getKieSession();
Map<String, Object> params = new HashMap<String, Object>();
params.put("sales", 500);
ProcessInstance processInstance = ksession.startProcess("com.sample.test", params);
assertNodeTriggered(processInstance.getId(),"No Bonus");
assertProcessInstanceCompleted(processInstance.getId(), ksession);
}
}
Exercises[edit]
- 04.1_exclusive_gateway
- 04.1.2_exercise
- 04.2_inclusive_gateway
- 04.2.1_exercise
- Parallel gateway
- 04.3_parallel_exercise
- 05_timer_delay
- 05.1_timer_delay
- 05.1.1_exercise_timer_delay
JBPM 6 - 2.2 - User Task[edit]
public class ProcessTestEmpRec extends JbpmJUnitBaseTestCase {
@Test
public void testProcess() {
RuntimeManager manager = createRuntimeManager("emp_recruitment1.bpmn2");
RuntimeEngine engine = getRuntimeEngine(null);
KieSession ksession = engine.getKieSession();
TaskService taskService = engine.getTaskService();
ProcessInstance processInstance = ksession.startProcess("hrproj.employee_recruitment");
assertProcessInstanceActive(processInstance.getId(), ksession);
// Executing PhoneInterview task
List<TaskSummary> list = taskService.getTasksAssignedAsPotentialOwner("krisv", "en-UK");
TaskSummary task = list.get(0);
System.out.println("Kris is executing task " + task.getName());
taskService.start(task.getId(), "krisv");
taskService.complete(task.getId(), "krisv", null);
assertNodeTriggered(processInstance.getId(), "PhoneInterview");
assertProcessInstanceCompleted(processInstance.getId(), ksession);
manager.disposeRuntimeEngine(engine);
manager.close();
}
public ProcessTestEmpRec() {
super(true, true, "org.jbpm.persistence.jpa.updated");
}
}
Execise[edit]
- Write tests for emp_recuritment4 covering all possible paths
JBPM 6 - 2.3 - Debugging Processes[edit]
Breakpoints[edit]
- current state of your running processes can be inspected and visualized during the execution.
- currently breakpoints cannot be put on the nodes within a process directly
- breakpoints can be put inside any Java code:
- code that is invoking the engine
- code invoked by the engine
- listeners
- rules (than part)
Debug views[edit]
- The process instances view
- shows all running process instances (and their state)
- shows the current state of a process instance
- The audit view
- shows the audit log
- The global data view
- Working memory view (facts)
- Agenda view (activated rules)
The process instances view[edit]
- Open 04_Debuging project
- Windows / Show View / Other / Drools / Process Instances
- Put a breakpoint after ksession.startProcess line
- Press F11 (Debug as JUnit Test)
- Switch to debug perspective
- In Variables window click on ksession
- In Process Instances windows, click on ksession insance
- Look at Process Instance windows to see where the process "hanged"
The process instance view and persistance
- This kind of debuging will not work with pressistance
- Please make sure you will turn persistance off by setting super(true,false)
Audit View[edit]
- Open 04_Deubgging project
- Run ProcessTestAudit.java
- Open the Audit view
- Open mylogfile file