Skip to content

Deflow algorithm

Gurmit Teotia edited this page Oct 30, 2017 · 27 revisions

Scheduling of activities, timer or any other child element is powered by Dependency Flow (Deflow) algorithm. Deflow algorithm simplify the writing of complex workflow involving parallel execution branches and let you schedule elements when multiple parallel activities are finished executing.

Deflow algorithm work as described below:

  1. When a scheduling item is completed then select all of its children to schedule next
  2. When scheduling an item (activity or timer), ensure that all of its parent branch are inactive
  3. When jumping down the executing branch, then try to schedule the first joint item as described in step 2
  4. When an item can not be scheduled because scheduling condition is evaluated to false then try to schedule the first joint item as described in step 2

Above implementation of Deflow algorithm will work very well for many complex scenario, however you can customise any of above step in the workflow.

Lets us look at the following example to understand how Deflow algorithm works. Assume we have a workflow to book the holidays and it schedule the activities as shown below:

Workflow image

In Guflow you can write above workflow as below:

[WorkflowDescription("1.0")]
public class BookHolidaysWorkflow : Workflow
{
  public BookHolidaysWorkflow()
  {
    ScheduleActivity<BookFlight>();
    ScheduleActivity<BookFlightFood>().After<BookFlight>();
	
    ScheduleActivity<BookHotel>()
    ScheduleActivity<AddDinner>().After<BookHotel>();
	 
    ScheduleActivity<ChargeCustomer>().After<BookFlightFood>().After<AddDinner>();
		
    ScheduleActivity<SendEmail>().After<ChargeCustomer>();
  }
}

In above workflow BookFlight and BookHotel are the startup activities which will executes in parallel when workflow will start and after their completion their children will be scheduled. ChargeCustomer activity will wait until both branches- 1) BookFlight, BookFlighFood and 2) BookHotel, AddDinner are completed and these branches can complete in any order. SendEmail activity will be scheduled after ChargeCustomer and once it is completed, workflow will be completed too.

Let us modify the above workflow with scheduling conditions:

[WorkflowDescription("1.0")]
public class BookHolidaysWorkflow : Workflow
{
  public BookHolidaysWorkflow()
  {
	ScheduleActivity<BookFlight>().When(a=>Input.BookFlight)
	  WithInput(a=>Input.BookingId);
	ScheduleActivity<BookFlightFood>().After<BookFlight>().When(a=>Input.AddFlightFood)
	.WithInput(a=>Input.FlightFoodChoice);
	
	ScheduleActivity<BookHotel>().When(a=>Input.BookHotel);
	ScheduleActivity<AddDinner>().After<BookHotel>().When(a=>Input.AddDinner)
	.WithInput(a=>Input.DinnerChoice)
	
	ScheduleActivity<ChargeCustomer>().After<BookFlightFood>().After<AddDinner>();
		
	ScheduleActivity<SendEmail>().After<ChargeCustomer>();
	}
}

In above example either of branch - 1) BookFlight, BookFlightFood and 2) BookHotel, AddDinner may not be scheduled because of scheduling conditions. When either of active branch reach "ChargeCustomer", workflow will detect that another branch is not active and it will not wait for other branch activities to complete and instead will schedule the ChargeCustomer activity.

Clone this wiki locally