Partager via


Forward Chaining Control

Forward chaining is a very powerful notion that enables atomic rules to be assembled into rulesets without the definition of, or necessarily even the knowledge of, the dependencies among the rules. However, in some scenarios, the rule writer may want the ability to provide more control over the chaining behavior, specifically the ability to limit the chaining that occurs. This enables the rule modeler to do the following:

  • Limit the repetitive execution of rules, which may give incorrect results.

  • Increase performance.

  • Prevent runaway loops.

Windows Workflow Foundation provides two properties to make this level of control easier:

Both of these values can be set in the RuleSet Editor.

ChainingBehavior Property

The ChainingBehavior property on the RuleSet object can be set to three possible values: Full, UpdateOnly, or None.

  • The Full option is the default and provides the behavior described up to this point.

  • The UpdateOnly option turns off the implicit and attribute-based chaining and prescribes that chaining should occur only for explicit Update statements. This gives you complete control over what rules cause reevaluation. Typically, you would use this to either avoid cyclic dependencies that cause excessive (or even runaway) rule reexecutions or to boost performance by eliminating rule reevaluations that are not required to provide functional completeness of the RuleSet.

  • The final option is None. This option causes the engine to evaluate the rules in strictly linear manner. Each rule would be evaluated only one time and in the order of priority. Rules with a higher priority could affect rules with lower priorities, but the inverse would not be true because no chaining would occur. Therefore, this option would be used with explicit priority assignments unless no dependencies existed among the rules.

ReevaluationBehavior Property

The ReevaluationBehavior property on the Rule object has two possible values: Always, and Never.

  • Always is the default and provides the behavior previously discussed, namely that the rule is always reevaluated based on chaining caused by the actions of other rules.

  • Never, as the name implies, turns off this reevaluation. The rule is evaluated one time, but is not reevaluated if it has previously executed any actions. In other words, if the rule was previously evaluated, and therefore has executed its Then or Else actions, it is not reevaluated. However, the execution of an empty action collection in the Then or Else actions does not indicate that a rule has been executed.

Typically, this property is used to, at the rule level, prevent infinite looping caused by dependencies that the rule has either on its own actions or other rules. For example, the following rule would create its own infinite loop, and reevaluation is not required to fulfill the functional requirements of the rule:

IF this.shippingCharge < 2.5 AND this.orderValue > 100
THEN this.shippingCharge = 0

Alternatively, if the rule is intended to be reevaluated, but only if the OrderValue field is changed, the user can set the chaining behavior on the RuleSet to only chain on explicit Update statements (and then add those Update statements to the relevant rule actions). Of course, the user could have added an additional predicate to this rule that checks that the value of ShippingCost is not already 0. But the chaining controls remove the need for users to define their rules based on the evaluation details instead of their business requirements.

Halt Function

As a final control, a Halt function can be added as a rule action (type “Halt” into the Then or Else action boxes in the editor). This immediately stops RuleSet execution and returns control to the calling code. The usefulness of this function is not necessarily limited to chaining control scenarios, of course. A RuleSet that has a specific functional goal, for example, might use a Halt function to short-circuit execution when the goal has been reached.

See Also

Reference

ChainingBehavior
ReevaluationBehavior
RuleHaltAction
RuleUpdateAction
RuleSet

Concepts

Using RuleSets in Workflows
Rules Evaluation in RuleSets