次の方法で共有


Task 2: Use Forward Chaining of Rules

Download sample

In this task, you insert a PolicyActivity activity into the workflow and create the RuleSet that implements the binary search. When the search has completed, the results are displayed by using a CodeActivity activity.

Note

Although you are encouraged to follow the exercises in a linear manner, it is not required. You can start on this exercise by opening the sample project and proceeding to the steps in the following section.

Add the Policy activity

To add the Policy activity

  1. Switch to the workflow design view (right-click Workflow1.cs or Workflow1.vb in Solution Explorer and choose View Designer).

  2. Drag a Policy activity from the Toolbox and drop it so that it is the last activity in the workflow.

Create the RuleSet

To create the RuleSet

  1. Right-click the Policy activity and choose Properties from the context menu.

  2. Select the RuleSetReference property in the Properties Window and click the ellipsis button that is displayed to the right of the property.

  3. Click New.

Create the Rules

To implement the binary search, three rules are required. The first rule computes the guess by taking the midpoint between the min and max values.

To create the first rule

  1. Click Add Rule.

  2. Enter the following code into the Condition text box.

    this.min <= this.max
    
  3. Enter the following code into the Then Actions text box.

    this.ComputerGuess = this.min + (this.max - this.min) / 2
    this.ComputerTurns = this.ComputerTurns + 1
    this.DisplayComputerGuess()
    

The second and third rules adjust min or max depending on whether the guess is higher or lower than the target number.

To create the second and third rules

  1. Click Add Rule.

  2. Enter the following code into the Condition text box.

    this.ComputerGuess < this.TargetNumber
    
  3. Enter the following code into the Then Actions text box.

    this.min = this.ComputerGuess
    
  4. Click Add Rule.

  5. Enter the following code into the Condition text box.

    this.ComputerGuess > this.TargetNumber
    
  6. Enter the following code into the Then Actions text box.

    this.max = this.ComputerGuess
    

The three rules are named Rule1, Rule2, and Rule3. By default they have a Priority of 0, and Reevaluation of Always. Rules are evaluated in descending order of priority, and if the priority is the same they are evaluated in ascending alphabetical order. In this example, the rules evaluate in the order of Rule1, Rule2, and then Rule3. A RuleSet executes until it has no more rules to evaluate, or until the Halt command is explicitly given.

In this case, Rule1 evaluates and sets the current guess. Rule1 also invokes the DisplayComputerGuess method. Next, Rule2 is evaluated. If the guess is too low, then the min value is updated to the current guess. Rules3 is then evaluated. If the guess is too high, the max value is set to the current guess. If the guess was correct then neither Rule2 nor Rule3 executes, and the RuleSet completes. If the condition for Rule2 or Rule3 evaluates to true and the rule executes, the value of either min or max is updated. Because the condition for Rule1 evaluates min and max, it is now reevaluated because Full Chaining is enabled and the Reevaluation property is set to Always. The next guess is calculated based on the updated values of min and max, and because Rule2 and Rule3 both reference this.ComputerGuess in their conditions, they are both re-evaluated. This continues until the correct number is determined, at which point the RuleSet completes.

Save the RuleSet

To save the RuleSet

  1. Click OK to dismiss the Rule Set Editor dialog box.

  2. Ensure that the newly created RuleSet is selected in the Name list, and click OK.

Display the Status

The final step in the workflow is to report the number of guesses taken by the computer to determine the target number. This is displayed to the user using a CodeActivity activity.

To add the Code activity

  1. Drag a Code activity from the Toolbox and drop it so that it is the last activity in the workflow.

  2. Double-click the Code activity and enter the following code into the generated handler.

    Console.WriteLine("The Computer was able to guess the number in {0} turns.", ComputerTurns);
    

Build and Run the Application

Build and run the application and see how you do against the computer.

To build and run the application

  1. Press Ctrl+F5 to build and run the application.

See Also

Concepts

Using Conditions in Workflows
Forward Chaining of Rules

Other Resources

Completed Rules and Conditions Tutorial
Rules and Conditions

Copyright © 2007 by Microsoft Corporation. All rights reserved.
Last Published: 2010-03-04