PLUGTREE JBPM5 TUTORIAL #1 : MODELLING YOUR PROCESS
This posts aims to teach the basics of modelling your processes into JBPM5. You’ll learn how to use the builtin Eclipse editor to create simple processes, with branching, joining, external variables and code constraints. For more information about the high level capabilities of jBPM5 check out this other post.
Before we start
The environment needed for this post example must be set up correctly. For instructions on how to do it read this post before. Then we need the demo contents installed into your local machine. You can learn more about it in this other post.
Inspecting our work environment
First open up Eclipse by executing the eclipse executable file from the jbpm-installer/eclipse folder. Next we must create a jBPM project. Right click the Navigator panel, click New, then Other…
Choose jBPM project. Another dialog will popup asking for a name, put whatever you want here. The next dialog will give you the option to create two sample files automatically: one containing a BPMN sample process and the other a class to start the engine and fire up that process. Leave them both checked.
Click Next and then Eclipse will ask you to choose a jBPM runtime. Change the folder for the one you have installed, pick the /lib folder. Here resides a complete runtime environment.
After you click Finish , the jBPM Eclipse plugin will generate a project with the necessary libraries and two sample files to execute and test it. Open the src/main/resources folder. A sample.bpmn file will appear. Double click it.
The jBPM editor will appear on the right side of the IDE.
Exploring the BPMN Editor
First we have one panel on the left divided in three: the first section is the general tools panel, with almost self explanatory icons. The Sequence Flow icon connects nodes as you might expect.
The Service Tasks are Custom Workitems icons. These are domain specific tasks which can be configured to give end users a more expressive and natural modelling environment. For more information about workitems, take a look at this.
The Component panel supports the following nodes:
This of course is not a complete list of the BPMN2 capabilities but a limited toolset for the Editor. If you want a full fledged BPMN2 editor. Check out Oryx. For more information about the BPMN2 standard, read this.
To properly edit new process you need to open the standard Eclipse Properties view. You can do that by going to Window, Show View, Other….. And select Properties from the list. A new tab will show with the process model properties.
Understanding the built in example process
The example process included with jBPM5 is extremely simple, a screenshot below:
The green dot on the screen is the Start node. Every process must have one and only one Start node. The red dot is one End Event, there must be at least one in every process.
The yellow square with the “Hello” text is a Script Task. Tasks of this kind are helpful to execute a Java or MVEL code snippet. You can edit the action to be executed in the Properties panel view.
A text editor will pop up where you can type the code and tell jBPM in what type of language you want this action to be interpreted. Also you can add the needed imports and global classes. For more information about these topics take a look at this Drools Documentation section. This action just prints out text into the console, but you can add whatever you want here.
Also, the process itself has properties as seen in the below screenshot:
The Connection layout is just the way the editor connects the nodes. It is useful to try the different options, but in the end they are a matter of taste and visual clutter. We can leave that alone for this example.
We have a Name and ID attributes. Every element in the model has 2 mandatory attributes: a Name and an ID. The Name is just a text description while the ID is a unique numerical one.
The package is a namespace for the process to avoid name conflicts. The version attribute is helpful for managing the process lifecycle as process evolve over time and you can have different versions. JBPM offers a few ways to migrate old versions into new versions.
Also, from the properties view of the model we can define the process variables. Process variables define the process data. This data can be used to make decisions during the execution of the process, transform it and use it anyway you like. Variables can be primitive types or any Java object. You can input data to the process in different ways and we’ll cover one approach to variable handling in this example.
Finally another configurable item from the Properties view are the process swinlanes. Swimlanes are a BPMN2 concept to define the different roles in the process. This is necessary for User Tasks as they need to be assigned to a role or an specific user.
Now that we know the editor’s toolbox, we’ll model a very basic process.
Modelling a basic process
Our example process will be a very simple and automated. By automated we mean that there’s no human intervention. A future post will cover tasks that need human interaction.
Our example is a Customer classification process: depending on the yearly income of our customer, we categorise him/her as “Regular” or “VIP”. A graphical representation of the process is depicted below:
The process is included in the following file: customerSelection.bpmn. As the process starts a Diverging Gateway splits the flow in two: It goes to the left VIP node if the customer yearly income is greater than 999999 and to the left if its otherwise. Finally we join the two branches and end the process.
First, we send two external variables to the process: customerType and income. The first one will be written inside the process and the income is what will help it decide. We have to declare this two variables in the properties panel:
CustomerType is a String:
while income is a Float:
and we have to start the process with an additional argument:
Map<String, Object> params = new HashMap<String, Object>();
This argument is a Map containing the declared variables.
Getting back to our process, the diverging gateway is a XOR one (because we want to go to just one of the outgoing nodes and not all) and has the following two constraints:
Both are really simple but serve to illustrate their usage. The VIP constraint evaluates to true if the income variable is greater than 999999:
While the regular constraint evaluates to true if the variable is less than that amount:
The two constraints are MVEL expressions and their type is code (because we want to evaluate a code snippet and not a Drools rule snippet). As you can see you can use the external variables by their name directly into the code.
In this case the split will evaluate the two constraints and will send the control flow to the one that evaluates to true.
After the split there are two Script tasks. As you might notice the use of Script Task is restricted to very simple logic, if you need more complex logic you can map that logic as rules or use the Service tasks (also known as Custom Workitems).
In these case the two Script tasks only serve the purpose of setting the customerType variable to a “VIP” or a “Regular”. This is done through the action attribute:
The action editor looks like this:
In this case the dialect is also MVEL for simplicity. The kcontext is a global object accessible to all Action nodes and must be used to change a variable value.
Running the example
To execute the example process just right click the ProcessTest class inside the src/main/java and click Run as Java application. Before this be sure to modify the necessary lines to create the parameters map and feed the process engine with the appropiate data as instructed above.
After reading this post you will be able to create very simple processes using splits, joins and script tasks. You can add constraints for simple decisions inside the branches, use external data via variables and execute simple logic inside the process.
The next post will extend this example to involve human interaction and more complex logic using rules. Stay tuned!
JBPM5 professional services
Plugtree LLC offers enterprise services for jBPM 5 and Drools 5: custom training suited to your company’s needs, a variety of consulting topics from architecture reviews, best practices and performance tuning, and an all encompassing production support with SLAs according to your needs. We’ve helped dozens of clients to adopt the Drools and jBPM technologies across a variety of industries and you can be one of them too. Contact us: email@example.com