The following BPMN diagram presents a situation where there is an arbitrary loop with two entry points (one before Task A and one before Task B) and one exit point (after Task C). In situations like that it is not possible to implement this loop with a WhileActivity, because the WhileActivity works only with well-structured loops (i.e. those that have one entry and one exit point).
In many cases, it is possible to implement a loop like that with a StateMachineWorkflow. However, in order to implement this loop inside a SequentialWorkflow, we must exploit the rather obscure EventHandlingScopeActivity.
According to the official documentation, “… an EventHandlingScopeActivity activity executes its main child activity concurrently with an EventHandlersActivity activity. Each EventDrivenActivity within the EventHandlersActivity might execute many times or not at all”.
According to Scott Allen, “… the EventHandlingScope activity is similar to a Listen activity in that it can have multiple branches waiting for events in parallel. We can view these branches by right-clicking the activity and selecting “View Events”. The primary difference between this activity and a Listen activity is that this event continues to listen for all events until its main child activity (the default view) finishes execution. Imagine we are setting up a workflow that will count employee votes over a period of 30 minutes. We could set the main child activity of the EventHandlingScope activity as a Delay activity, with a 30-minute timeout. We can then place event handling activities in the event branches that listen for Yes and No votes. This activity will continue to listen for the Yes and No events until the Delay activity completes in 30 minutes.”
The idea is to setup the appropriate event handlers and raise the events from inside (by emulating the receipt of an external event). A mathematically concrete presentation (by C. Ouyang, M. Dumas, S. Breutel, and A.H.M. ter Hofstede) of this technique can be found here and here.
The main child activity contains two activities: Raises the starting event and waits for the end event. For the example above we need to setup two event handlers. A rule of thumb is that we need one event handler for each activity that has two or more input flows.
The first event handler contains Task A and raises the event for the second event handler.
The second event handler contains the Task B activity and an IfElseActivity, which represents the first gateway of the diagram. Now, the IfElseActivity checks the first condition (a==1) and if true, we raise the event captured by the first event handler. If false, we execute Task C and invoke a second IfElseActivity, which represents the second gateway. This IfElseActivity checks the second condition (b==2) and if true, we raise the event captured by the second event handler, otherwise we raise the event that ends the activity (the event that the main child activity listens to).
A sample project containing the workflow of the above diagram can be downloaded from here.