Skip to main content

Pre-execution

Workflow Engine has a special mode that simulates process execution. It is called Pre-Execution. The process simulation is required to track the future path of process execution and leave some artifacts in your system. For example, a real user sent a document for approval, and they may want to know what the approval stages are and who will approve the document. This is exactly what the demo showcases. Before a user sends a vacation request for approval, the Document's Transition History table is filled in the document. It contains the information about what the stages are and who will approve the document. In the abovementioned example, the Pre-execution mode is called every time document status is changed to Idled.

The Pre-execution mode is enabled with one of the following methods:

WorkflowInit.Runtime.PreExecute(processId, "StartActivityName");
WorkflowInit.Runtime.PreExecuteFromCurrentActivity(processId);
WorkflowInit.Runtime.PreExecuteFromInitialActivity(processId);

All three methods differ only by the Activity which Workflow Engine starts the execution simulation with. Let’s see how emulation occurs:

  • The physical state of a process does not change during simulation. In other words, the values of CurrentState, CurrentActivity and process parameters won’t change. Simulation does not affect the process.
  • Simulation starts with the Activity that is specified when the PreExecute method is called.
  • Only transitions with Classifier=Direct are taken into account during simulation.
  • During simulation, the conditions are calculated (if there are any), and only one transition with the Auto or Command trigger is selected from the Activity. Further, the next Activity is executed and a new transition is selected.

Simulation ends when:

  • There are no transitions that can be executed. In other words, there are no transitions with Classifier=Direct, Trigger=Auto or Command, for which the conditions specified in Condition are true.
  • There is more than one transition that can be executed.

During simulation, a process is executed almost in the same way as it would be executed for real, aside from the following points:

  • When an Activity is executed, Actions are taken not from the Implementation section, but from the PreExecution Implementation section.

  • In case Workflow Engine finds a transition triggered by a command, it calculates the Restrictions of this transition and fills the processInstance.IdentityIds process parameter. In case the transition is a command one, the processInstance.CommandName process parameter is also filled.

  • The calculation of conditions does not differ from the one during regular process execution. However, during simulation, the Result on pre-execution property of the Conditions with the Action type is taken into account. In case this property’s value is set to True or False, the Action won’t be executed, and the specified value will be used.

  • Besides, during simulation, the processInstance.IsPreExecution process parameter is set to true.

One of the ways to start simulation during the change of a process state is to launch it from the ProcessStatusChanged event handler:

runtime.ProcessStatusChanged += (sender, args) =>
{
if (args.NewStatus == ProcessStatus.Idled)
{
runtime.PreExecuteFromCurrentActivity(args.ProcessId);
}
};