Share via


Where do Rules fit in a Workflow?

A workflow has a bunch of activities. Some activities like the If-Else activity or the While activity have a confition. This condition is really part of a business process "rule". Eg. if the condition models a check for OrderQuantity being over a certain amount, it is part of a business process that makes decisions based on how many items a customer is ordering.

These conditions can be written in one of two ways - in code, as a code condition; or declaratively, as a rule condition. In the case of a code condition, when the condition needs to be evaluated that piece of code is called and it's return value - true or false determines if the condition is true or false. In case of a rule condition, when the condition needs to be evaluated, the rule (condition expression) is evaluated. A rule condition is specified using a Rule Condition Editor.

Rule conditions are stored as XML in a .rules file. When running inside a WF, you can use the WorkflowChanges class to change rules on the fly.

Try this now - create a Sequential Workflow Project, drop an IfElse activity, and in the Properties Window, choose a Declarative Rule Condition. You should see a + show up, expand it and give a name to the Condition. You will notice a .rules file in your project. This is the XML file I am referring to. At this point, you will see that the file has one RuleExpressionCondition whose expression is null since we haven't set anything yet. Click the ellipses next to Expression. This will bring up the Rule Condition Editor, just type in true to create an expression that always evaluates to true. Now the .rules file has an expression - and it is the CodeDom representation of the expression you had entered.

Thus, simple activities like If-Else or While represent one way in which Workflows use Rules. There is also the CAG activity, which is a composite activity containing other activities, each having a When condition. This is another place you can use Rules within a Workflow. You might be thinking, and rightly so, that actual business processes have not one but an intricate mesh of conditions, where one condition might depend on what another condition evaluates to. For such complex group of rules, we have the Policy activity. The Policy activity uses the WF Rules Engine.

Using a policy activity, you are dealing with a rule set instead of a rule. A rule set is a collection of one or more rules, where each rule has the form of If condition - Then Action - Else Action. The Rules Engine then looks at all these rules, determine which rules in this rule set are true and execute those rules. We'll explore the Policy Activity and the WF Rules Engine in future posts.

In summary, Rules are exposed in conditions of various out of box Windows Workflow Foundation activities. These conditions drive the execution of the workflow. These conditions are what make business processes make progress from one step to another. Rules rule.