Enterprise Informatization
LECTURE
Piotr Zabawa, PhD. Eng.
IBM/Rational Certified Consultant
e-mail: [email protected]
Lecture 2
Business Process Modeling
with the aid of BPMN
in Jboss Drools environment
Piotr Zabawa, PhD. Eng. Institute of Computer Science Department of Physics, Mathematics and Computer Science
Literature
Software Engineering Section
This lecture (Lecture 2) was created on the basis of the following books:
Bali M., Drools JBoss Rules 5.0. Developers Guide, Packt Publishing 2009.
Browne P., JBoss Drools Business Rules, Packt Publishing 2009.
BPMN2 diagram elements
• Only the elements of BPMN2 available in JBossDrools 5.1 will be presented here in order to make Drools learning curve more steep. • Next part of the lecture is dedicated to configuration of Drools in
Eclipse. Simple projects creation is also explained.
• The more advanced example – a banking case-study is also elaborated to illustrate all practical problems specific for working with Drools as well as their solutions.
Piotr Zabawa, PhD. Eng. Institute of Computer Science Department of Physics, Mathematics and Computer Science
Start/End
Software Engineering Section
Start – the only process or sub-process starting point. No entry point, exactly one exit point.
End – one of possibly many process or sub-process
Gateways
Piotr Zabawa, PhD. Eng. Institute of Computer Science Department of Physics, Mathematics and Computer Science
Software Engineering Section
Diverge (Split) – the place of process splitting into many paths (flows, branches).
AND – flow into all branches at the same time OR – flow into all branches recognized as true XOR – only one branch must be chosen
Converge (Join) – place of joining many branches into one
AND – continuation after finishing all branches XOR – continuation after finishing the first branch
Tasks
Software Engineering Section
Rule – covers the group of business rules for exetution during this task
Script – a code block for execution during this task
User Task – task that requires manual business process handling by a business worker
Subprocess – the process element used for
Service Tasks
Piotr Zabawa, PhD. Eng. Institute of Computer Science Department of Physics, Mathematics and Computer Science
Software Engineering Section
Log - built into Drools file logging service
Email – built into Drools e-mail notification service
Events
Software Engineering Section
Timer – event triggered by timer
Error – event triggered by an error
Sub-Process
Piotr Zabawa, PhD. Eng. Institute of Computer Science Department of Physics, Mathematics and Computer Science
Software Engineering Section
Sub-Process – used for modeling subprocesses embeded in a
process. Subprocess has access to all its parent process data but not vice-versa. Like a process it must have one starting point and may have many end points.
Multiple Instances
Software Engineering Section
Multiple Instances – used to mark multiple execution of a subprocess placed in this container.
Advanced model sample
Piotr Zabawa, PhD. Eng. Institute of Computer Science Department of Physics, Mathematics and Computer Science
Advanced model sample
Advanced model sample
Piotr Zabawa, PhD. Eng. Institute of Computer Science Department of Physics, Mathematics and Computer Science
Disadvantages of traditional approach
Disadvantages of traditional approach:if (customer.getLevel() == Level.Gold){ // coś dla Gold
}
else if (customer.getLevel==Level.Silver){ if (customer.getAccounts()==null){
// coś dla Silver, który nnie ma rachunku??? }
else{
for(Account account : customer.getAccounts()) if (account.getBalance<0){
// coś dla Silver mającego konto z debetem }
else if (account.getBalance>=0 and account.getBalance()<=1000){} else if (account.getBalance>1000 and account.getBalance()<=100000){} else if (account.getBalance>100000 and account.getBalance()<=500000){} // …
} }
How to handle business logic changes in such a structural code?
Solution of the problem
It is better to decompose the structural problem into a set of independent conditions:
if Customer(level==Level.Silver) and no Account
if Customer(level==Level.Silver) and has Account debit
if Customer(level==Level.Silver) and has Account less then 1000 //…
Piotr Zabawa, PhD. Eng. Institute of Computer Science Department of Physics, Mathematics and Computer Science
Motivation for business rules
• Advantages • Disadvantages
• When not to apply
Business rules - advantages
• Easy for understanding by business analyst and new developer
• Easy to focus on the problem’s essence – what not how • Ease of introduction, deletion of rules
• Ease of change introduction to the rules • High algorithm efficiency
• Ease of transformation of business analysts requirements into rules
Piotr Zabawa, PhD. Eng. Institute of Computer Science Department of Physics, Mathematics and Computer Science
Business rules - advantages
• Ease of unified mechanism usage while adding orthogonal business responsibilities (compare to Aspect Oriented
Programming) – for example audit, reporting, event logging
• Ease of reuse (the same rule in many processes)
• Independence of sequential processing of rules (in contrast to imperative languages like C++/Java/C#)
• Ease of rules exectution from processes and/or execution of processes from rules (Drools)
Business rules - advantages
• Independent lifecycles. Rules and processes are extracted from object-oriented software into separate frequently changing artifacts. Independent change management processes.
• Ease of embedding Drools in applications
Piotr Zabawa, PhD. Eng. Institute of Computer Science Department of Physics, Mathematics and Computer Science
Business rules - disadvantages
• Mental problems among the programmers – different way of thinking in case of declarative languages
• Difficulties in finding and elimination of errors in rules/processes
• Diffilulties in full debugging of rules without deep knowledge of business rules engine’s detailed way of working
Business rules - advantages
• High memory resources requirement
• Theoretical danger of recursion (especially in case of modeling to-be) in case of changing common data by at
least two rules. In such case we do not know what is wrong – process design or rule implementation.
Piotr Zabawa, PhD. Eng. Institute of Computer Science Department of Physics, Mathematics and Computer Science
Business rules
– when not?
• If less then 20 rules
• If business logic changes rarely
• If business rules are simple and/or are working on one object ony
• If in pseudo-code the nesting level of conditional instructions is less then 3
• If software is created temporarely and will not evolve • If there are not enough resources to handle business rules
Drools
Piotr Zabawa, PhD. Eng. Institute of Computer Science Department of Physics, Mathematics and Computer Science
Software Engineering Section
This software is BLiP (Business Logic integration Platform).
We will be using only three of them:
• Drools Expert – silnik reguł biznesowych
• Drools Flow – silnik procesów biznesowych
Drools IDE installation
The installation steps of the whole JBossTools IDE
containing Drools plugins as well as Java EE WTP platform are desribed on:
www.pk.edu.pl/~pzabawa/en/business
Drools configuration is described in the following document:
Konfiguracja środowiska developerskiego Drools
Konfiguracja środowiska
developerskiego Drools
Opisana w pliku:
JBossDrools_01_RuntimeSetting.pdf
Piotr Zabawa, PhD. Eng. Institute of Computer Science Department of Physics, Mathematics and Computer Science
Drools_HelloWorld samples
That are the most simplified samples jst to start with
Drools designed to show some useful mechanisms available in Drools engines. Some of them are related to different
problems of greetings. They could constitute a base for
making experiments with development environment as well as modeling business processes.
Futher these introductory mechanisms will be extendet into more complex examples referring to banking systems.
Drools_HelloWorld samples
quick overview and start• HelloWorld01_ProcessOnly – created with the aid of wizard; contains a process with an action and runner class • HelloWorld02_RuleOnly – created with the aid of
wizard; contains rules and runner class
• HelloWorld03_ProcessAndRule – created manualy on the basis of previous two samples; contains both process and rules as well as changed runner class
• HelloWorld04_ProcessAndDSLRule – previous sample adapted to business analysts expectations via application of DSL (Domain-Specific Language)
Piotr Zabawa, PhD. Eng. Institute of Computer Science Department of Physics, Mathematics and Computer Science
Drools_HelloWorld samples
events and statistical analysis
• HelloWorld05_RulesAndEvents – simple programm showing how to write an event-driven greeting
• HelloWorld06_StatefullSession – more complex sample showing how to make statistical analysis of facts located in knowledge base with the aid of Drools query
• HelloWorld07_DroolsTypeDeclaration – quite complex sample which contains statistical analysis of business
events (no time frames)
• HelloWorld08_DRL2JavaPropagation – previous
sample enriched by access to native Drools class with the aid of Java reflection
Drools_HelloWorld samples
events in time axe (CEP)• HelloWorld09_? –
uzupełnić!
• HelloWorld10_? –
uzupełnić!
Piotr Zabawa, PhD. Eng. Institute of Computer Science Department of Physics, Mathematics and Computer Science
Przykłady Drools_HelloWorld
architektura• HelloWorld11_BusinessRulesEngine – przykład
pokazujący jak w sposób nie naiwny korzystać w aplikacji z silnika reguł biznesowych ukrytego za interfejsem
uzupełnić!
• HelloWorld12_BusinessProcessEngine – przykład ilustrujący możliwość ukrycia przed aplikacją procesu biznesowego za interfejsem
uzupełnić!
New project creation instructions
The way of creation of a new project containing a process is described in the file:
JBossDrools_02_ProjectCreationWizard.pdf
Piotr Zabawa, PhD. Eng. Institute of Computer Science Department of Physics, Mathematics and Computer Science
Drools_HelloWorld samples
The way of execution of a sample containing a process is described in the following file:
JBossDrools_03_ProjectWorkflowExecution.pdf
Business rules engine concept
Sample – simple business rule:
package droolsbook; rule „basic rule”
when // condition(s) albo LHS Account ( balanace < 100 ) then // consequence albo RHS
System.out.println(„Account balance is less then 100”); end
Piotr Zabawa, PhD. Eng. Institute of Computer Science Department of Physics, Mathematics and Computer Science
Business rules engine concept
Sample cont. – fact as a Java (POJO) bean:
package droolsbook; public class Account{
private long balance;
public long getBalance(){ return balance; } public void setBalance(long balance){
this.balance = balance; }
@Override
public boolean equals(final Object other){//…}
@Override
public int hashCode(){//…}
@Override
public String toString(){//…} }
Business rules engine concept
Description based on the previous sample. The client class performs the following steps: • Creates knowledge base object
• Places resources in this object (business rules in this case) • Creates session object (statefull or stateless)
• Creates an object of Account class
• Places Account class object in session object • Runs rules
Builder design pattern is used for Drools objects creation. More detailed information can be found in
Drools_HelloWorld samples.
Piotr Zabawa, PhD. Eng. Institute of Computer Science Department of Physics, Mathematics and Computer Science
Business rules engine concept
Description of Drools engine reation:
Drools tries to fit each fact (Accoun here) placed in
knowledge base session to each condition of each rule located in knowledge base in such a way that these facts fulfill these rules. This process is called pattern matching. If a particular fact fulfills all conditions of a particular rule, then the
consequence of this rule is executed for this fact. This way the functionality of a fact is moved from application business logic to the business rule. We can say that we controll objects functionality from oustide.
Business rules engine concept
Description of Drools engine reation cont.:
If there is more facts and rule conditions of a particular rule are related to more then one fact, then business rules engine fits fact sets in such a way that it finds all
combinations of facts that fulfill the rule conditions as a
whole facts set. Then the consequence of this rule is executed for each set of facts that fullfilled the rule condition.
Piotr Zabawa, PhD. Eng. Institute of Computer Science Department of Physics, Mathematics and Computer Science
Business rules engine concept
Rules consequences execution order:
The order of rule consequences execution for particluar fats set is not
determined – the rule engine makes such a decision for us (compare to remarks related to engine disadvantages). There are however some
mechanisms in Drools that haelp to influence in some extent to the execution order:
• Agenda
– Rule priorities (salience) – Rules grouping
• (activation-group) or (agenda-group)
• Ruleflow (ruleflow-group)
Business rules language
• A rule may contain many conditions
Account ( balance == 200) Customer (name == „John”)
• A rule can declare variables (declaration only – the engine substitutes data)
$account : Account ( $type : type )
• A rule can use declared variable (for each Account class object we find all Customer class objects with the acount field being previously found Account class object)
$account : Account()
Customer ( account == $account )
Piotr Zabawa, PhD. Eng. Institute of Computer Science Department of Physics, Mathematics and Computer Science
Business rules language
• Native Java types and other – String – Regular expression – Date – Boolean – Enum • Comments – Single line # or // – /* block */
• Packages and import
Business rules language
• Global variables (declaration, passing to session, consumption in rule)
• Dialects
– Java – default
– mvel – useful due to shortened notation of rules
Piotr Zabawa, PhD. Eng. Institute of Computer Science Department of Physics, Mathematics and Computer Science
Business rules language
• Operators in rule conditions – And
– Or – Not – Exists – Eval
– return value restriction (fact method call surrounded by elipses)
– Inline eval
– Nested accessors
Business rules language
• Operators in rules conditions cont. – This – Queues actions • Contains • Not contains • Member of • Not member of • from
Piotr Zabawa, PhD. Eng. Institute of Computer Science Department of Physics, Mathematics and Computer Science
Business rules language
• Methods in rule consequences
– Modify (modifies a fact in a session) – Insert (places a anew fact in a session) – Retract (removes a fact from a session)
Business rules language
• Rule attributes – salience – no-loop – dialect
Piotr Zabawa, PhD. Eng. Institute of Computer Science Department of Physics, Mathematics and Computer Science