lesson 4 : ruleflow

Why do we need a Ruleflow ?

When capturing business requirements, most users express the rules by dividing the problem to resolve in steps. As this, it is very convenient to be able to implement it the same way. In the drools/jbpm technology, we are going to use a jbpm process by using rule steps, it is then called a rule flow. But in reality, we can mix jbpm/drools together.

Configure project to become a jbpm project

select the project, right click with the mouse configure/convert to jbpm project

Create your first rule flow

in the src/test/rules create a package lesson4. Right click and create new File and call it ruleflow1.bpmn2 and click the OK button. An error messages appear to say the file is empty but the plugin will create a start event.

put two Rule tasks and one End Event.

Then select each of the Rule set and set the properties as follows :

The workflow should look like that :

Select the workflow in the rear, and in the properties file, change as follows :

Create a new rule file called demo-ruleflow1.drl

Do not forget to add a new entry in the kmodule.xml

Look at the keyword "ruleflow-group". Here the first rule we give it the name "Group1" and the second "Group2". they have the same name as the Rule-flow items we defined in the process definition above. Therefor, the first rule can only be fired when the rule-flow group "Group1" is activated and the same for the second rule and rule-flow group "Group2". Before running a test case, we will add a new callback to know the activities around the jbpm process.

Note that we are only looking for Node of type Rule Step called RuleSetNodeInstance. And the test case looks like this :

Before calling the fireAllRules method, we call a startProcess method with the "RF1" parameter which is the ID we gave to the process above.

And the console display should be like this :

How a rule-group works ?

A rule flow group works like a separate group of rules. Those who are setting the focus when the rule step is called with the same node id as the ruleflow-group. When no more rules can be fired, the process can continue to the next node.

We can go further

starting a ruleflow from a rule

Let us write the following rule :

And here is the following test case :

Rule flow with a condition

It is also possible to execute certain ruleflow-group based on condition that can have the same syntax as a rule constraint.

Create a new package in src/test/rule and calle it lesson4a. Create a new process file that you can call demo-ruleflow2.bpmn2 and a rule file demo-ruleflow2.drl.

The bpmn process should look like this and give it the id "RF3":

The left split should be a "diverge Gateway" and the right one a "converge Gateway". The calculate1 should have a ruleflow called "group1" and the calculate2 "group2".

When clicking on the "diverge gateway", you should select the "OR" type and for the the "converge Gateway" the "XOR".

Now we have to edit each connection Here is for "to Node Calculate1". Do not forget to click the "Imports" button to add the Account class.

"To Node Calculate2".

here is the rule file :

and the test case

if you change the balance to 500, the console should be :

It is more efficient to have two groups of rules like this instead adding for all rules of 'Group1' the constraint on balance > 1000 and balance <= 1000 for "Group2". Indeed, if the level 1000$ changes you have to modify all rules. And furthermore, if the end user gives your the rule : "first case is when balance is less than 1000$", then the good practice is to implement business rules as they are given. And implementing with a ruleflow will help end users to divide their way of expressing more complex rules. We will see int the exercise how it helps.

Last updated

Was this helpful?