Rule Condition Editor in Workflow


Introduction: A rule condition is a condition that can be evaluated to a Boolean value. It features validation and cloning facilities, as well as a dependency management mechanism. A rule action represents an action to be executed. A rule set is a collection of rules that can be executed and validated as a group. Rule or a Rule condition is involved as a property in an activity or a workflow, the design-time editors for the rule or rule condition use these dialogs.

Step 1 : Open Visual Studio2010.

  • Go to File->New->Project.
  • Select Sequential Workflow Console Application.
fau8.gif

When we click in OK button then following [Design] window opens.

r1`.gif

Step 2 : Drag activity from Toolbox and define activity name.

  • Drag CodeActivity from Toolbox.
r2.gif


Step 3 : Now we double-click in CodeActivity and write the below code.

Code :

using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;

namespace my_workflow
{
    public sealed partial class Workflow1 : SequentialWorkflowActivity
    {
        public Dictionary<int, string> cities = new Dictionary<int, string>();
        public string FromCity;
        public string ToCity;
        public Workflow1()
        {
            InitializeComponent();
            cities.Add(1, "allahabad");
            cities.Add(2, "london");
            cities.Add(3, "india");
            cities.Add(4, "noida");
            cities.Add(5, "delhi");
            cities.Add(6, "banglore");
            cities.Add(7, "istanbul");
            cities.Add(8, "japan");
        }
        private void codeActivity1_ExecuteCode(object sender, EventArgs e)
        {
            Console.WriteLine("Enter Passenger Name : ");
            string passengerName = Console.ReadLine();
            Console.WriteLine("Enter Departure City :");
            this.FromCity = Console.ReadLine();
            Console.WriteLine("Enter Destination City :");
            this.ToCity = Console.ReadLine();
 }

The first activity of our workflow is complete. Now as per scenario specified above, we will 'check' the user's entered city names in our dictionary collection to ascertain do we have flights available as per user's requirement, and on the basis of this 'condition check', we will either display 'flight confirmed or not.

Step 4 : Drag If ElseActivity from Toolbox for the condition because after the condition check display the message in user console.

r3.gif

Step 5 : Now define Rule Condition Editor for each branch.

  • Select If Else condition and right-click.
  • Select properties define Condition-Name and Expression.
  • When we click Condition-Name then open Rule Condition Editor open.
  • Click New option and define condition.
r4.gif

Condition :

this.cities.ContainsValue(this.FromCity.ToLower()) == True && this.cities.ContainsValue(this.ToCity.ToLower()) == True

Step 6 : Define same as IfElseActivity2 give condition in Rule Condition Editor.

Condition : 

!this.cities.ContainsValue(this.FromCity.ToLower()) == True && !this.cities.ContainsValue(this.ToCity.ToLower()) == True

Step 7 : Now again drag CodeActivity from Toolbox and double-click and write the below code.

r5.gif

Code :

private void codeActivity2_ExecuteCode(object sender, EventArgs e)
{
      Console.WriteLine("Flight Booking Confirmed!");
      Console.ReadKey();
}
private void codeActivity3_ExecuteCode(object sender, EventArgs e)
{
      Console.WriteLine("Sorry, No Such Flight Exists!");
      Console.ReadKey();
}

Step 8 : Now go to Program.cs file and write the below code.

Code :

using
System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Workflow.Runtime;
using System.Workflow.Runtime.Hosting;
namespace my_workflow
{
    class Program
    {
        static void Main(string[] args)
        {
            using (WorkflowRuntime workflowRuntime = new WorkflowRuntime())
            {
                AutoResetEvent waitHandle = new AutoResetEvent(false);
                workflowRuntime.WorkflowCompleted += delegate(object sender, WorkflowCompletedEventArgs e) { waitHandle.Set(); };
                workflowRuntime.WorkflowTerminated += delegate(object sender, WorkflowTerminatedEventArgs e)
                {
                    Console.WriteLine(e.Exception.Message);
                    waitHandle.Set();
                };
                WorkflowInstance instance = workflowRuntime.CreateWorkflow(typeof(my_workflow.Workflow1));
                instance.Start();
                waitHandle.WaitOne();
            }
        }
    }
}

Step 9 : Now press F5 and run the application.

r1.gif

Up Next
    Ebook Download
    View all
    Learn
    View all