Workflow Hello World
Workflow is a specific type of Flow that allows long-running business transactions with human interactions. In this tutorial we are going to create a Hello Word Workflow.
Long-running business process
A long running business process or transaction is when the whole end-to-end process might take days and weeks if not months or years to complete. An example is when:
- a person goes to his or her GP and the doctor asks for a blood test.
- Then the doctor gives a patient a blood test exam and sends patient to a lab
- patient goes to a lab and do the blood test
- Lab requires couple of days to run the test and prepare the result to send to the GP
- patient now should get an appointment with GP in couple days and visit the GP
Let's say in this example that's the end of process. As you see in the example, this process takes days to be completed. In in any stage different actors participate in the whole process:
To model this types of process, you can also use multiple set of smaller Flow and manage the connection between them. But to make it simpler, you can use a Workflow.
Let's create a simple Workflow model using the FlexRule Designer:
- Start node
- Receive Task (or Action)
- End node
And also define an input (In) parameter called lab result like below:
When you adding a receive task set the below properties:
- Title: Lab Result
- Display Name: Send lab result
These mean in the Receive Task now the actor (using your application) can query what actions are available. And your application can show the actor at this stage the action (with above properties) are available. And actor and set the lab result parameter and send a Signal to workflow to continue from the step onward.
If you look at the XML model of Workflow you can see the below definition:
<Workflow name="WorkflowWorkflow2" version="126.96.36.199">
<Define name="lab result" direction="In" />
<Transition name="Transition1" to="Task1" />
<End name="End1" />
<Task name="Task1" title="Lab Result" displayName="Send lab result">
<Transition name="Transition4" to="End1" />
A Workflow is not different from any other logic, so you can simply use RuntimeEngine to load and Run workflow. For some specific Workflow functionality the RuntimeEngine has a Workflow property that can be used. Other than that it is same as a normal Flow.
When a generic flow is executed using Run method it will be going from start to end. But in workflow when execution reaches a Task or Timeout, Workflow goes to Waiting state and execution control comes to your application. Then you can decide when to Resume the work flow from where ever it was last time.
So with a workflow you can:
- Run: which creates a new instance of a workflow. That's call a Workflow Instance.
- Resume: which continues a Workflow Instance from the last state. To continue a workflow instance you need to provide its latest context and a Signal
To run a workflow you need to:
- Create an engine using RuntimeEngine
- Call Run method by passing input parameters
byte workflowModel=Encoding.UTF8.GetBytes(wf); var engine = RuntimeEngine.FromXml(workflowModel); // Now we call the run method. var result = engine.Run((object) null);
In the above sample code, we pass null to lab result parameter as this parameter will be used later on, fpr providing the lab results later.
At this stage, workflow will be run and execution will be returned to your code.
For a long running business process, you need to manage context to resume it later on. To access context you can use the below line:
var context = (WorkflowExecutionContext) result.Context;
And if you check the state of Workflow you would notice it is not Complete and it is Waiting.
When a workflow state is on Waiting you can resume the workflow by providing the context.
context.VariableContainer["lab result"] = CreateLabResultObject(); engine.Workflow.Resume(context, new ReceiverSignal("Lab Result"));
In the above sample your application will need to:
- Create and pass lab result (i.e. CreateLabResultObject method). This can be any type.
- Specify a signal by using ReceiverSignal to let workflow instance know what Task to continue with. There might be a situation that multiple of Tasks are available to continue. Signal tell the instance what execution path to take.
And if you check workflow state you would find out it is completed now.