Task 2: Add the User Ranking to the Application
In this task, you add a PolicyActivity activity and configure a RuleSet to implement the user ranking portion of the application.
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 Required Workflow Member Variables
In the Workflow1
class, add a member variable to hold the computed ranking.
To add the ranking workflow member
Open
Workflow1
in code view (right-click Workflow1.cs or Workflow1.vb in Solution Explorer and choose View Code).Add the following member declaration to the
Workflow1
class.public string Ranking;
Add the Policy Activity
A PolicyActivity activity is used to execute the rules contained in a RuleSet. In this tutorial, these rules are used to determine the player's ranking.
To add the Policy activity
Switch to the workflow design view (right-click Workflow1.cs or Workflow1.vb in Solution Explorer and choose View Designer).
Drag a Policy activity from the Toolbox and drop it so that it is the last activity in the workflow.
Create the RuleSet
Once the PolicyActivity activity is added, the RuleSet is created.
To configure the RuleSet
Right-click the Policy activity and choose Properties from the context menu.
Select the RuleSetReference property in the Properties window and click the ellipsis button that is displayed to the right of the property.
Click New.
The dialog that is displayed is the Rule Set Editor. This dialog is used to create and edit the rules in a RuleSet. Three rules are required to implement the ranking algorithm for this application.
To add the rules
Click Add Rule.
Enter the following expression into the Condition text box.
this.Turns <= 10
Enter the following expression into the Then Actions text box.
this.Ranking = "Expert"
Click Add Rule.
Enter the following expression into the Condition box.
this.Turns > 10 && this.Turns <= 20
Enter the following expression into the Then Actions text box.
this.Ranking = "OK"
Click Add Rule.
Enter the following expression into the Condition text box.
this.Turns > 20
Enter the following expression into the Then Actions text box.
this.Ranking = "Needs Work"
When the RuleSet executes the value of the workflow's Ranking
member is set to the proper value based on the number of turns taken by the user to determine the random number.
To save the rules
Click OK to close the Rule Set Editor dialog box.
Ensure that the newly-created RuleSet is selected in the Name list, and click OK.
Display the Ranking
Once the ranking is calculated, it is displayed to the user using a CodeActivity activity.
To display the ranking
Drag a Code activity from the Toolbox and drop it so that it is the last activity in the workflow.
Double-click the Code activity and enter the following code into the generated handler.
Console.WriteLine("Your ranking: {0}", Ranking);
Build and Run the Application
Build and run the application and see how you rank.
To build and run the application
- Press Ctrl+F5 to build and run the application.
In Exercise 2: Use Rules Chaining with the PolicyActivity Activity you add a second PolicyActivity activity and use rules to implement a binary search algorithm to automate the process of determining the random number.
See Also
Concepts
Using the PolicyActivity Activity
Using RuleSets in Workflows
Copyright © 2007 by Microsoft Corporation. All rights reserved.
Last Published: 2010-03-04