共用方式為


Rules Evaluation in RuleSets

Rules technology is exposed in two key ways in Windows Workflow Foundation:

  • As conditions on activities.

  • As a forward-chaining ruleset in a PolicyActivity activity.

Forward chaining is discussed later in this section, but it refers to the ability for the actions of one rule to cause other, dependent rules to be reevaluated.

The primary reason for a developer to use a rule condition instead of code condition is that rule conditions become part of the model and can be dynamically updated at runtime on executing workflow instances. A secondary advantage of rule conditions is that as part of the model, more sophisticated tools could be built on top of the model to provide additional authoring experiences, dependency management, cross-condition analysis, and so on.

The PolicyActivity activity encapsulates the definition and execution of a RuleSet. A RuleSet is a collection of rules with a set of execution semantics. Rules are, in turn, If-Then-Else expressions that operate on the workflow members.

Rules Evaluation

Each rule in a RuleSet has a priority value with a default of "0". The rules in a RuleSet can be considered a sorted collection, ordered by the priority values. The Windows Workflow Foundation rules evaluator evaluates rules individually and executes the rule’s actions based on the results of the rule’s condition evaluation.

The evaluation mechanism can be conceptually described as:

  1. Start with the list of active rules.

  2. Find the highest priority rule.

  3. Evaluate the rule and execute its Then/Else actions as appropriate.

  4. If the actions of a rule update a field or property that is used by the condition of one or more previous rules in the list (ones with a higher priority), reevaluate those previous rules.

    Note

    Only those rules that have a specific dependency are reevaluated.

  5. Continue the process until all rules in the RuleSet have been evaluated (or a Halt is executed).

In the following conceptual example, assume the following ruleset, where "A", "B", and so on represent data in the workflow.

Rule 4 (Priority = 4)
IF A = 15
THEN B = 5

Rule 3 (Priority = 3)
IF C = 5
THEN B = 10

Rule 2 (Priority 2)
IF D = 2
THEN A = 15

Rule 1 (Priority 1)
IF B = 5 
THEN E = 7

Assume that you have the following input data:

  • A =0

  • B = 0

  • C = 5

  • D = 2

  • E = 0

The evaluation would continue as follows:

  1. Rule 4 is evaluated; it evaluates to false, and because it has no Else actions, no actions are executed.

  2. Rule 3 evaluates to true and its action is executed, setting B = 10. Rule 4 does not depend on the value of B; therefore the evaluation proceeds to Rule2.

  3. Rule 2 evaluates to true and its action is executed, setting A = 15.

  4. Rules 3 and 2 are not reevaluated since their conditions do not rely on the value of A. However, because Rule 4 uses the value of A in its conditions, it is reevaluated. It evaluates to true and its action is executed, setting B = 5. Rules 4, 3 and 2 do not depend on the value of B, so the evaluation proceeds to Rule 1.

  5. Rule 1 evaluates to true and its action is executed, setting E = 7.

The resulting data set is now the following:

  • A = 15

  • B = 5

  • C = 5

  • D = 2

  • E = 7

See Also

Reference

RuleSet
PolicyActivity
RuleSet

Concepts

Workflow Changes to Rule Conditions
Using RuleSets in Workflows