Do you understand the ConditionedActivityGroup (CAG) ?

There was an interesting question over the ConditiondActivityGroup (CAG) yesterday in the MSDN Workflow Foundation  forum yesterday.(www.windowsworkflow.net).
Here is my test:
 
I’ve tested and commented the last sample (with 3 code Activities, without the until condition, without the delays, with WhenCondition) with maxNumber = 6.
 
Here is the big picture :
 

 
 
 
1° Let’s go
 
step 1.With Visual Studio .Net & the Windows Workflow Extension , create a Sequential Workflow Console application.
step 2.Drag and drop a ConditionedActivityGroup into the designer. 
step 3.In the workflow code, declare a counter + a constant:
 
private int _counter
const int MAXNUMBER = 6;
 
step 4.Drag and drop 3 codeActivities in the CAG.
step 5.In the workflow code, provide the following WhenCondition:
 
      void WhenCondition(object sender, ConditionalEventArgs e)
      {
            CodeActivity ca = sender as CodeActivity;
            _counter++;
            Console.WriteLine("Calling when on {0} with counter at {1} of {2}", ca.Name, _counter, MAXNUMBER);
            e.Result = (_counter < MAXNUMBER);
      }
 
step 6.In the workflow code, provide the following executeCode funnction that will be used by each CodeActivity :
 
       private void CodeActivity_ExecuteCode(object sender, EventArgs e)
        {
            CodeActivity ca = sender as CodeActivity;
            Console.WriteLine("{0} Executing…", ca.Name);
        }
 
step 7.Link the 3 Code Activities ExecuteCode event to the CodeActivity_ExecuteCode function
 
Run the application and you’ll get this :
 

 
 Do you understand the logic behind this ? idon’t worry, you are not alone, here are some explanations.
 
2°Explanations
 
If we don’t specify an UntilCondition in the Cag, the Cag will run until all WhenCondition return false.
 
Console displays are in red , almost each line is commented in [my comment]

Calling when on codeActivity1 with counter at 1 of 6
[my comment: codeActivity1 is not executing ->whencondition evaluated, returns true->will execute]
Calling when on codeActivity2 with counter at 2 of 6
[my comment: codeActivity2 is not executing ->whencondition evaluated, returns true->will execute]
Calling when on codeActivity3 with counter at 3 of 6
[my comment: codeActivity3 is not executing ->whencondition evaluated, returns true->will execute]
codeActivity1 Executing…
codeActivity2 Executing…
codeActivity3 Executing…

Calling when on codeActivity1 with counter at 4 of 6
[my comment: codeActivity1 is not executing ->whencondition evaluated,  returns true->will execute]
Calling when on codeActivity2 with counter at 5 of 6
[my comment: codeActivity2 is not executing ->whencondition evaluated,  returns true->will execute]
Calling when on codeActivity3 with counter at 6 of 6
[my comment: codeActivity3 is not executing ->whencondition evaluated, returns false->activity3 won’t  execute anymore]
codeActivity1 Executing…
codeActivity2 Executing…
Calling when on codeActivity1 with counter at 7 of 6
[my comment: codeActivity1 is not executing ->whencondition evaluated, returns false-> activity1 won’t  execute anymore]
Calling when on codeActivity3 with counter at 8 of 6
[my comment: codeActivity2 is probably still running->whencondition not evaluated
codeActivity3 is not executing->condition evaluated]
Calling when on codeActivity1 with counter at 9 of 6
[my comment: codeActivity1 is not executing->whencondition evaluated, returns false-> activity1 won’t  execute anymore]
Calling when on codeActivity2 with counter at 10 of 6
[my comment: codeActivity2 is not executing->whencondition evaluated, returns false->  activity2 won’t  executed anymore]
Calling when on codeActivity3 with counter at 11 of 6
[my comment: codeActivity3 is not executing->whencondition evaluated, returns false-> activity3 won’t  execute anymore]
[my comment: all activities conditions returned false=> the Cag exit]

3° Using Cag in User Interfaces
 
The best example of Cag usage is the Travel demo application provided by Jurgen Willis (MS, Workflow PM).

 
 
 
 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s