ILOG Rules for .NET User Guides > Walkthroughs > Walkthrough: Creating a Ruleflow

This walkthrough presents the following fundamental procedures for creating a ruleflow with ILOG Rule Studio for .NET:

In this walkthrough you create a console application that uses a ruleflow to execute two business rules. The ruleflow also uses a ruleset parameter to keep a count of the number of new account promotions have been made.

Setting up the Business Rule Application

Setting up the application involves creating a solution and defining the classes that model the items of interest in the business domain. You import these classes into the business object model so that you can use them to write rules.

To set up the business rule application
  1. Create a new class library project called ClassLibrary1.
  2. Name the solution Promotions.
  3. Add the following two classes to ClassLibrary1:

The Message class:

using System;
namespace ClassLibrary1{  
  // Class to print a message
  public class Message
  {
    // Prints a message to the console
    public void PrintMessage(string msg)
    {
      Console.WriteLine(msg);
    }
  }
}
Imports System
' Class to print a message
Public Class Message
    ' Prints a message to the console
    Public Sub PrintMessage(ByVal msg As String)
        Console.WriteLine(msg)
    End Sub
End Class

The Customer class:

using System;
namespace ClassLibrary1{
  //A class for holding customer information
  public class Customer
  {
      //Constructor
    public Customer(int age, bool hasAccount)
    {
      this.age = age;
      this.hasAccount = hasAccount;
    }
 
    // The age of the customer
    private int age;
    public int Age 
    {
      get { return age; }
    }
 
    // Test if the customer has an account.
    private bool hasAccount;
    public bool HasAccount
    {
      get { return hasAccount; }
    }
  }
}
Imports System
' A class for holding customer information
Public Class Customer
    Private m_age As Integer
    Private m_hasAccount As Boolean
 
    'Constructor
    Public Sub New(ByVal age As Integer, ByVal hasAccount As Boolean)
        Me.m_age = age
        Me.m_hasAccount = hasAccount
    End Sub
    'The age of the customer
    Public Readonly Property Age() As Integer
        Get
            Return m_age
        End Get
    End Property
    'Test if the customer has an account
    Public Readonly Property HasAccount() As Boolean
        Get
            Return m_hasAccount
        End Get
    End Property
End Class
  1. Import the classes into the business object model, and set the DisplayText property for the classes and their members as follows:
    Business Element 
    Display Text 
    Message 
    message 
        PrintMessage(msg) 
    print {this}: {0} 
    Customer 
    customer 
        get_Age() 
    the age of {this} 
        get_HasAccount() 
    {this} has an account 
  2. To learn about setting up a business object model, see Setting Up a Business Object Model.

Writing the Business Rules

To write the business rules you:

To define the rule project
  1. Add a rule project called BusinessRules1 to the Promotions solution.
  2. Add a reference to the ClassLibrary1 project to the rule project.

In this walkthrough the business rules define promotional messages based on the age of a customer. To use a ruleflow you use ruleset parameters to pass the objects of interest to the rule engine. In this case you need two ruleset parameters: one for the customer object, and another for the message. In this walkthrough, there is also a ruleset parameter that keeps a running count of the number of a particular type of promotion that are made as the rules fire.

To define the ruleset parameters
  1. In the Properties Page for the BusinessRules1 rule project, go to the Ruleset Parameters page of the Common Properties folder.
  2. Click the new ruleset parameter button images/wlkrnt_rproj-addrulesetparam_button.png to add a new ruleset parameter to the list.
  3. Select the ruleset parameter you created, and set its properties as follows:
    Mode 
    In 
    Name 
    customer 
    Type 
    ClassLibrary1.Customer 
  4. Note that you need to explicitly type the value for the Type property.
  5. Similarly, create two more ruleset parameters with the following properties:
    Mode 
    In 
    Name 
    message 
    Type 
    ClassLibrary1.Message 
    Mode 
    InOut 
    Name 
    number of promotions 
    Type 
    System.Int32 
  6. When you have created all three ruleset parameters, click OK.

There are two business rules for this walkthrough. The first defines promotional messages for customers older than 16, and the second defines promotional messages for customers younger than 16.

To write the business rules
  1. In the Intellirule Editor, create the following business rules.
    Note
    When you write these business rules use ruleset parameters and not implicit variables to build the conditions and actions of the rules. In the drop-down list of the Rule Editor, ruleset parameters are marked with this icon images/wlkrnt_rulesetparam_icon.gif
  2. The business rule for customers older than 16 reads as follows:
    If
        all of the following conditions are true:
        - the age of customer is more than 16
        - customer has an account
    Then
        print message: "Thanks for your continued support!";
    Else
        print message: "Check out our great incentives for new account holders!";
     
    The business rule for customers younger than 16 reads as follows:
    If
        all of the following conditions are true:
        - the age of customer is at most 16
        - customer has an account
    Then
        print message: "When your friends join, you get a free skateboard!";
    Else
       print message: "Every new customer gets a free cinema ticket!";
     
  3. Save the solution.

To learn more about writing rules see Writing Rules and Rule Editing Reference.

Creating the Ruleflow

To create the ruleflow you:

To create the ruleflow file
To add a rule task to the ruleflow
  1. In the Ruleflow Editor, open the Ruleflow Editor Toolbox using the link in the center of the blank ruleflow.
  2. The Visual Studio Toolbox appears. It contains the elements you can add to a ruleflow.
  3. Click and drag a Rule Task element into the Ruleflow Editor, then double-click the rule task element.
  4. The Edit Rules dialog box appears.
  5. In the Edit Rules dialog box, double-click the first item in the list of available rules, and then click OK.
  6. All the rules in the rule project appear in the list of selected rules.
  7. In the Properties window for the rule task, set Execution Mode to Sequential and Ordering to Explicit.
To add an action task to the ruleflow
  1. Click and drag an Action Task element into the Ruleflow Editor, then double-click the action task element.
  2. The Edit Actions dialog box appears.
  3. In the Edit Actions dialog box, click <select an action>, then expand Other Actions and double-click set <object> to <value>.
  4. Click <select an object>, then expand Variables and double-click number of promotions.
  5. Click <select value>, then expand System and number, and then double-click <number> + <number>.
  6. Click the left-most <enter a number>, then expand Variables and double-click number of promotions.
  7. Click <enter a number> and type 1.
  8. The action is now:
    Do
        set number of promotions to number of promotions + 1
     
    This increments the value of the ruleset parameter number of promotions.
To create a transition from the rule task to the action task
  1. While pressing SHIFT, drag the pointer from the rule task to the action task.
  2. Alternatively, press CTRL and select the two ruleflow elements, then right-click and select Connect from the shortcut menu.
    A transition from the rule task to the action task appears.
  3. Double-click the transition.
  4. The Edit Transition Conditions dialog box appears.
  5. In the Edit Transition Conditions dialog box, click <select a condition>, then expand Other Conditions and double-click the following condition is not true.
  6. Click <select a condition>, then expand ClassLibrary1 and customer, and then double-click <customer> has an account.
  7. Click <select a customer>, then expand Variables, and then double-click customer.
  8. The transition condition is now:
    If
        the following condition is not true:
            customer has an account
     
  9. Click the transition and in the Properties window set the Display Text property to has account?
To add the start and end points and the else transition to the ruleflow
  1. Click and drag a Start Point and an End Point into the Ruleflow Editor.
  2. Create transitions from:
  3. Right-click in the Ruleflow Editor and click Perform Layout.

The final ruleflow should look something like this:

images/wlkrnt_ruleflow1.png

Defining the Application Logic

To define the application logic you:

To define the application project
  1. Add a Console Application project called ConsoleApplication1 to the Promotions solution.
  2. Add references to the BusinessRules1 and ClassLibrary1 projects, and the .NET components ILOG.Rules and ILOG.Rules.RuleEngine to the ConsoleApplication1 project.
  3. Set the ConsoleApplication1 project to be the startup project.
To implement the application logic
  1. Replace the code created by default for the ConsoleApplication1 project with the following:
using System;
using System.IO;
using ILOG.Rules;
using ClassLibrary1;
namespace ConsoleApplication1 
{
  public class RuleExecuter 
  {
    public static void Main() 
    {
      // Create a rule engine in which to execute the ruleset.
      RuleEngine engine = new RuleEngine();
 
      // The promotional message.
      Message promotion = new Message();
 
      // The number of promotions made for new accounts.
      int promo_count = 0;
 
      // The customers to be considered.
      Customer[] customers = new Customer[4];
      customers[0] = new Customer(72, true); //Jack
      customers[1] = new Customer(16, false); //Nandira
      customers[2] = new Customer(45, true); //Laurel
      customers[3] = new Customer(23, false); //Felix
 
      // Load the ruleset into the rule engine.
      engine.RuleSet = new BusinessRules1();
 
      for( int i = 0; i < customers.Length; i++ )
      {
        // set the ruleset parameters
        engine.SetParameterValue("number of promotions", promo_count);
        engine.SetParameterValue("message", promotion);
        engine.SetParameterValue("customer", customers[i]);
 
        // Set the ruleflow to use for executing the ruleset
        engine.Ruleflow = engine.RuleSet.Ruleflows["BusinessRules1", "Ruleflow1"];
 
        // Execute the ruleset.
        engine.Execute();
 
        // Reset the ruleflow before starting again with new ruleset parameters.
        engine.ResetRuleflow();
 
        // Get the value of the running count
        promo_count = (int)engine.GetParameterValues()["number of promotions"];
      }
 
      Console.WriteLine("There were " + promo_count.ToString() + " promotional messages for new accounts.");
 
      //Get the user's input to quit the application.
      Console.WriteLine();
      Console.WriteLine("To exit, press ENTER.");
      Console.In.ReadLine();
    }
  }
}
Imports System
Imports System.IO
Imports ClassLibrary1
Imports ILOG.Rules
Module Module1
 
    Sub Main()
        ' Create a rule engine in which to execute the ruleset.
        Dim engine As New RuleEngine
 
        ' The promotional message
        Dim promotion As New Message
 
        ' The number of promotions made for new accounts
        Dim promo_count As Integer = 0
 
        ' The customers to be considered
        Dim customers As New ArrayList
        customers.Add(New Customer(72, True)) 'Jack
        customers.Add(New Customer(16, False)) 'Nandira
        customers.Add(New Customer(45, True)) 'Laurel
        customers.Add(New Customer(23, False)) 'Felix
 
        ' Load the ruleset into the rule engine.
        engine.RuleSet = New BusinessRules1
 
        ' container for getting the running count
        Dim parameterValues As System.Collections.IDictionary
 
        ' Loop counter
        Dim i As Integer = 0
 
        For i = 0 To customers.Count - 1
            ' Set the ruleset parameters
            engine.SetParameterValue("number of promotions", promo_count)
            engine.SetParameterValue("message", promotion)
            engine.SetParameterValue("customer", customers.Item(i))
 
            ' Set the ruleflow to use for executing the ruleset
            engine.Ruleflow = engine.RuleSet.Ruleflows("BusinessRules1", "Ruleflow1")
 
            ' Execute the ruleset.
            engine.Execute()
            'Reset the ruleflow before starting again with new ruleset parameters.
            engine.ResetRuleflow()
 
            ' Get the value of the running count
            parameterValues = engine.GetParameterValues()
            promo_count = CType(parameterValues("number of promotions"), Integer) Next i
 
        ' Print the number of promotional messages
        Console.WriteLine("There were " + promo_count.ToString() + " promotional messages for new accounts.")
 
        ' Get the user's input to quit the application.
        Console.WriteLine()
        Console.WriteLine("Press ENTER to exit.")
        Console.In.ReadLine()
    End Sub
 
End Module
  1. Save the solution.

Running the Business Rule Application

You can test the business rule application within Visual Studio.

To run the business rule application
  1. In Visual Studio, on the Build menu, click Rebuild Solution.
  2. On the Debug menu, click Start.
  3. The console window opens, and the rule engine executes the ruleset according to the ruleflow and displays the messages you defined in the business rules.
    There is a message for each Customer object passed to the rule engine via the ruleset parameter, customer. There is also a count of the number of times a promotion for a new account was displayed.
  4. Check that the messages displayed in the console window are appropriate for the objects passed to the rule engine, and the conditions in the business rules.
  5. Press ENTER to stop the business rule application, and close the console window.