Workflow Hello World

From FlexRule Wiki
(Redirected from Workflow Hello Word)
Jump to: navigation, search

Introduction

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:

  1. a person goes to his or her GP and the doctor asks for a blood test.
  2. Then the doctor gives a patient a blood test exam and sends patient to a lab
  3. patient goes to a lab and do the blood test
  4. Lab requires couple of days to run the test and prepare the result to send to the GP
  5. 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:

  • Patient
  • GP
  • Lab

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.

First Workflow

Let's create a simple Workflow model using the FlexRule Designer:

  1. Start node
  2. Receive Task (or Action)
  3. End node

Hello word workflow.png

Parameters

And also define an input (In) parameter called lab result like below:

Hello word workflow parameter.png

Receive Task

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.

Result Workflow

If you look at the XML model of Workflow you can see the below definition:

  1. <Workflow name="WorkflowWorkflow2" version="1.0.0.0">
  2.   <Declaration>
  3.     <Define name="lab result" direction="In" />
  4.   </Declaration>
  5.   <Nodes>
  6.     <Start name="Start1">
  7.       <Transition name="Transition1" to="Task1" />
  8.     </Start>
  9.     <End name="End1" />
  10.     <Task name="Task1" title="Lab Result" displayName="Send lab result">
  11.       <Transition name="Transition4" to="End1" />
  12.     </Task>
  13.   </Nodes>
  14. </Workflow>

Running Workflow

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:

  1. Run: which creates a new instance of a workflow. That's call a Workflow Instance.
  2. 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

Run

To run a workflow you need to:

  1. Create an engine using RuntimeEngine
  2. 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.

Assert.AreEqual(FlowState.Waiting, context.State);

Resume

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:

  1. Create and pass lab result (i.e. CreateLabResultObject method). This can be any type.
  2. 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.

Assert.AreEqual(FlowState.Completed, context.State);