Archive for March, 2009

In my last post I had written about the Rule Engine that comes embedded with Windows Workflow Foundation and in this post I will show how we can use this with the PolicyActivity.

The System.Workflow.Activities.PolicyActivity class is a specialized workflow activity class that encapsulates a RuleSet.The PolicyActivity.RuleSetReference property maintains reference to a RuleSet stored in a .rules file.During execution time the PolicyActivity retrieves the rules from the .rules file and execute the rules.

The demo that I have prepared implements a very simple rule to calculate discount, based on order value,if order value is > 100 and < 200 then discount is 10% and if > 200 then 20%.


Workflow Foundation Rule Engine – Part I

Posted: March 15, 2009 in WF

In my post on Business Rules and Rule Engine I have explained about Business Rules,Business Processes and what actually a Rule Engine does.In this post I will be discussing about the Rule Engine that comes with Windows Workflow Foundation(WF).

First let us explore the important classes related to Rule Engine and how they provide abstraction for the various components required for rule processing.All the classes are present within the namespace System.Workflow.Activities.Rules.

  • Rule – This class represents a Rule which is nothing but an if-then-else construct.So each Rule is composed of a condition and associated if-then and else actions.Rule class has the properties
    • Condition – This property is of type RuleCondition.This is the base class and actual implementation is provided by the RuleConditionExpression class.This class represents a condition as System.CodeDom.CodeExpression.The list of expression supported by WF is listed in MSDN
    • ElseActions/ThenActions – These are collection of type RuleAction.RuleAction is an abstract base class and actual implementation is provided by the classes:
      • RuleStatementAction – Modifies a property or calls a method.
      • RuleHaltAction – Halts the execution of the RuleEngine and returns the control to the calling code.
      • RuleUpdateAction – This class is used to indicate that a rule is updating a variable on which other rules are dependent.This causes reevaluation of the dependent rules.
  • RuleSet – This is a collection of Rule objects.
  • RuleEngine – This evaluates the RuleSet via it’s Execute method.

Now let’s quickly summarize the basic flow operation for the WF Rules Engine:

  • We have to define the RuleSet and associated Rules using Visual Studio 2005/2008.We can develop our WinForms application as well to create/edit the rules.The basic functionality of RuleEditor is exposed by the class System.Workflow.Activities.Rules.Design.RuleSetDialog.We can invoke this UI from our own application very easily.I will discuss about this in more details along with WF Rules Externalization.
  • The RuleSet editor create the RuleSet objects in memory and finally serialized and stored in some persistence storage.Visual Studio Editor stores them in .rules files.The structure of the .rules files is as shown below.Note how closely it resembles the object model we have discussed above.
  • <RuleDefinitions xmlns=””>
        <RuleSet Name=”SampleRuleSet” ChainingBehavior=”Full” Description=”{p3:Null}” xmlns:p3=””>
            <Rule Name=”SampleRule” ReevaluationBehavior=”Always” Priority=”0″ Description=”{p3:Null}” Active=”True”>
                <RuleExpressionCondition Name=”{p3:Null}”>

  • During execution time these .rules files are read and the RuleSet object is deserialized from xml data.The RuleEngine then executes and evaluates the RuleSets

In my next post I will explain with examples how we can use RuleSet along with PolicyActivity of WF.