Hi Everyone, I am back to the blog!
Today I will try to show a quite new feature of jbpm5, which are On Enter and On Exit node actions. As its name indicates, these are actions which will be executed after or before the execution of a node. This is a quite frequently needed feature, that is not included in BPMN 2.0 specification, so now there is a way to include these actions.
There is an example of it in jBPM 5 github:
Now I want to show something similar, but a more real situation were it was very helpful to use it for me.
In Emergency Services, we needed to make some processing on the result variables of a human task. It was not a trivial assignment that it could done (at least I did not see how) with the assignment expressions of the output mappings. Something like this should be done with the variables:
emergency.addUpdate(vehicle.getId(), new VehicleUpdate(comment, priority));
So we made it with an On Exit Node action. These actions are similar to script nodes, and we can get information about the process and also about the node from the kcontext.
We have a node
<userTask id="_12-2-4" name="Update Situation" >
Which has two data output variables,
<dataOutput id="_12-2-4_commentOutput" name="comment" /> <dataOutput id="_12-2-4_priorityOutput" name="priority" />
And also some input variables,
<dataInput id="_12-2-4_emergencyInput" name="emergency" /> <dataInput id="_12-2-4_vehicleInput" name="vehicle" />
Now, we added a On Exit Node action to make our desired processing,
<extensionElements> <tns:onExit-script scriptFormat="http://www.java.com/java"> Emergency em = (Emergency)((WorkItemNodeInstance)kcontext.getNodeInstance()).getWorkItem().getParameter("emergency"); Vehicle vehicle = (Vehicle)((WorkItemNodeInstance)kcontext.getNodeInstance()).getWorkItem().getParameter("vehicle"); String comment = (String)((WorkItemNodeInstance)kcontext.getNodeInstance()).getWorkItem().getResult("comment"); Integer priority = (Integer)((WorkItemNodeInstance)kcontext.getNodeInstance()).getWorkItem().getResult("priority"); emergency.addUpdate(vehicle.getId(), new VehicleUpdate(comment, priority)); </script> </tns:onExit-script> </extensionElements>
After the human task node is finished, this script will be executed. As you can see, in kcontext we have access to process, with kcontext.getProcessInstance(), process variables with kcontext.getVariable() and also to node instance and the work itam with kcontext.getNodeInstance().
The full bpmn can be found at https://github.com/Salaboy/emergency-service-drools-app/blob/V3/emergency-service-core/src/main/resources/processes/procedures/MultiInjuredEmergencyProcedure.bpmn.
You can also take a look at Emergency Services project source, where you will have lot of Jbpm and Drools examples. Hope this example helps you! Follow us @calcacuervo, @salaboy and @ilesteban.