Home > Articles > Programming

  • Print
  • + Share This
This chapter is from the book

Rule Capabilities

WF has two types of rules, and it stores rules in two different formats. The first type of rule, a conditional rule, is bound to a control flow activity and is used to determine how the control flow activity processes. For example, which branch of an IfElse activity should execute? The second type of rule in WF, a RuleSet, executes a collection of rules and adds prioritization, reexecution, and other capabilities to the collection of rules.

The next sections look at conditional rules first in both formats and then at RuleSets.

Conditional Rules

The While and IfElse activities covered in the "Standard Modeling Activities" section are both governed by conditional rules. The While activity's Condition property holds a rule that determines whether the While activity should continue iterating. Common conditions in a While activity are counter < 3 or IsValid = true. In the first example, the While iterates until the counter value is 3 (or more). In the second example, it iterates while IsValid is true. The While activity's Condition property is evaluated at each iteration to determine whether it should continue iteration. The Condition property can be set as a Declarative Rule Condition or as a Code Condition. Declarative Rule Conditions can be created using the Rule Condition Editor and are stored in an XML format in a .rules file. Figure 1.15 shows the Declarative Rule Condition being created in the Rule Condition Editor.

Figure 1.15

Figure 1.15 Rule Condition Editor.

Figure 1.16 shows the While activity Condition property set to Declarative Rule Condition and pointed to the rule created (named CounterDeclarative) in Figure 1.15. The CounterDeclarative Declarative Rule Condition is now bound to the While activity Condition property, which ensures iteration will stop when counter is 3 or more.

Figure 1.16

Figure 1.16 While activity condition property.

Declarative Rule Conditions are stored in an XML format in a .rules file. The file is extremely verbose and hard to read. The advantage of creating rules declaratively is that they can be changed at runtime without recompilation, stored in a database, and more general tooling support exists for them.

The alternative is to set the While activity Condition property to Code Condition. It then requests a method name. You would insert counter<3 in the method and if the value is less than 3, the method would return true, otherwise it would return false. The method replaces the verbose XML file as a storage medium. Code Conditions do not receive the same level of tooling support as Declarative Rule Conditions in WF.

RuleSets

A RuleSet permits a collection of rules to be created that each has a corresponding action. The rules in the RuleSet can be prioritized and configured to reevaluate if a dependent item changes. Reevaluation can be explicitly set on an individual rule or defined overall by setting the RuleSet Chaining property (or both).

When the RuleSet Chaining property is set to Full, rules that have a dependent value changed downstream are automatically reevaluated. Think of a pricing scenario where a discount is applicable if the customer reaches a year-to-date-sales threshold. This discount rule can be reevaluated every time the year-to-date-sales change. The neat part is that WF can do much of this detection automatically via clever CODEDOM programming (a .NET serialization capability) in contrast to forcing the developer to attribute the dependencies.

Figure 1.17 shows a RuleSet with three rules. It is set to Full Chaining, so it will automatically reevaluate. The selected DiscountPercent rule checks if the OrderAmount is larger than the DiscountThreshold. If so, it applies a 5% discount. The other two rules in the RuleSet have their own conditions and actions. Notice that the other two rules (YearlySales and TotalOrderAmount) contain information likely related and relevant to calculating a discount. RuleSet rules also have an optional Else action that executes if the rule condition evaluates to false. A RuleSet is a collection of related rules that solve a common problem such as pricing.

Figure 1.17

Figure 1.17 RuleSet dialog.

In WF, RuleSets are always stored in .rules files. WF does not add any tools management, central rules storage, or other common functionality found in a commercial rules engine product. However, some add-on products provide some of these capabilities. One of the add-ons stores RuleSets in a SQL database and retrieves them at runtime. Another does some analysis on the rules (Hour 12, "Working with the WF RuleSet").

RuleSets can be added to a workflow via the Policy activity, which has a RuleSetReference property that binds a RuleSet to a Policy activity. The Policy activity then executes the RuleSet from the workflow.

See Hour 2 for more details on Declarative Rule Conditions and Code Conditions. See Hour 12 for more details on RuleSets.

  • + Share This
  • 🔖 Save To Your Account