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 World 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. A good 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 the patient to a lab
  3. The patient goes to a lab and does the blood test
  4. The lab requires a couple of days to run the test and prepare the results to send to the GP
  5. The patient now should get an appointment with the GP in a couple of days and visit the GP

Let's say in this example that's the end of the process. As you can see, this process takes days to be completed. At any stage different actors can participate in the whole process:

  • Patient
  • GP
  • Lab

To model this type of process, you can also use multiple sets of smaller Flow and manage the connection between them. In order 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

Also define an input (In) parameter called lab result, as shown below:

Hello word workflow parameter.png

Receive Task

When you add a receive task, set the properties below:

  • Title: Lab Result
  • Display Name: Send lab result

These mean that in the Receive Task the actor (using your application) can query what actions are available. At this stage, your application can show the actor the actions (with the above properties) that are available. The actor can set the lab result parameter and send a Signal to the workflow to continue from this step onward.

Result Workflow

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

  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 no 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 finish. In workflow, when execution reaches a Task or Timeout, Workflow goes into a Waiting state and execution control comes to your application. Then you can decide when to Resume the work flow from wherever it was last time.

With a workflow you can:

  1. Run: which creates a new instance of a workflow. That's called 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 the 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 the lab result parameter, as this parameter will be used later on for providing the lab results.

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 in order to resume it later. To access context you can use the line below:

var context = (WorkflowExecutionContext) result.Context;

If you check the state of Workflow you would notice it is not Complete and instead 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 example your application will need to:

  1. Create and pass lab result (i.e., CreateLabResultObject method). This can be of any type.
  2. Specify a signal by using ReceiverSignal to let the workflow instance know with what Task to continue. There might be a situation where multiple Tasks are available to continue. Signal will tell the instance what execution path to take.

If you check workflow state you will find that it is completed now.

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