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

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

In this walkthrough you create a console application that uses a decision table to select a message to display in a command window.

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 Property Age() As Integer
        Get
            Return m_age
        End Get
        Set(ByVal Value As Integer)
            m_age = Value
        End Set
    End Property
    'Test if the customer has an account
    Public Property HasAccount() As Boolean
        Get
            Return m_hasAccount
        End Get
        Set(ByVal Value As Boolean)
            m_hasAccount = Value
        End Set
    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:
  2. 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 
    To learn about setting up a business object model, see Setting Up a Business Object Model.

Creating the Decision Table

To create the decision table 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.
  3. Add a Decision Table item called DecisionTable1.dtx to the rule project.
To create the decision table
  1. In Visual Studio, open DecisionTable.dtx.
  2. The Decision Table Editor appears containing an empty decision table with three condition columns and two action columns. By default, action columns have a slightly shaded background.
  3. In the Decision Table Editor, remove the last action column (column E) and the last condition column (column C).
  4. To remove a column, select the column header, and on the shortcut menu, click Remove Columns.
    After removing columns E and C the table has two condition columns (labelled A and B) and one action column (now labelled C).
  5. Double-click on the header of column A.
  6. The Edit Condition Column dialog box appears.
  7. In the Edit Condition Column dialog box, click <select a condition>, then expand ClassLibrary1, and customer, and double-click <customer> has an account.
  8. Click <select a customer>, then expand Variables, double-click the customer, and click OK.
  9. The condition for column A is the customer has an account.
  10. Similarly, set the condition for column B to the age of the customer is between <enter a number> and <enter a number>
  11. To get the is between operator, click equals.
    When you click OK in the Edit Condition Column dialog box, column B is divided into two sub columns: min and max. The min column is for the low end of the range in the condition, and the max column is for the high end of the range.
  12. And finally, set the action for column C to print the message: <enter a string>.

Now the decision table is ready to be populated.

To populate the decision table
  1. In the Decision Table Editor, double-click the first cell in the table (row 1, column A), and, in the drop-down list that appears, click True.
  2. Similarly, set the second cell (row 2, column A) to False.
  3. Double-click the first cell of column B (row 1, min), and type 18, then in the max column for that row, type 35.
  4. Right-click the first cell of column B (row 1, min), and on the shortcut menu click Insert, and then click Insert Row Below.
  5. A new row appears. Notice that the value of column A for this row is assumed to be the same as the row above it. To create an entirely new row--with a new value for column A--you would select a cell in column A and insert the new row from there.
  6. Continue populating column B for the age ranges 36 to 50, and 51 to 65.
  7. Double-click the first cell of the action column (row 1, column C), and type Suggest the First Home Loan promotion.
  8. Similarly, type the following for rows 2, 3 and 4 in column C: Suggest the Mortgage Upgrade promotion., Suggest the Retirement Investment promotion., and Suggest the New Account Promotion.

Now the table is populated, however, you still need to consider the cases for customers that have accounts with ages less than 18 and more than 65. You can also make the table a little easier to read by removing the unwanted cells and adjusting the alignment of the text.

To complete the decision table
  1. Right-click the first cell of column B (row 1, min), and on the shortcut menu click Insert and then click Insert Row Above.
  2. In the Properties window for the cell, set the Operator property to < ...
  3. To display the Properties window for the cell, right-click the cell and on the shortcut menu click Properties.
  4. Set the value of the first cell in column B to 18.
  5. Similarly, create a row below row 4 for the case for customers that have accounts and that are over 65.
  6. For the two new rows, set the corresponding actions to: Suggest the Speed Loan promotion. and Suggest the Investment Consolidation promotion.
  7. Select the empty rows (row 7 onwards), right-click the selected cells, and on the shortcut menu click Remove Rows.
  8. Click the header of column B, and in the Properties window, set the Text Alignment property for the column to Center.

The final decision table should now look something like this:

images/dtable_result.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 message containing the promotion information.
      Message promotion = new Message();
 
      // The customers to be considered.
      Customer jack = new Customer(72, true);
      Customer nandira = new Customer(16, false);
      Customer laurel = new Customer(45, true);
      Customer felix = new Customer(23, true);
 
      // Add objects to the rule engine's working memory.
      engine.Assert(promotion);
      engine.Assert(jack);
      engine.Assert(nandira);
      engine.Assert(laurel);
      engine.Assert(felix);
            
      // Load the ruleset into the rule engine.
      engine.RuleSet = new BusinessRules1(); 
 
      // Execute the ruleset.
      engine.Execute();
 
      //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
        ' Load the ruleset into the rule engine.
        engine.RuleSet = New BusinessRules1
 
        ' The message containing the promotion information.
        Dim promotion As New Message
 
        ' The customers to be considered.
        Dim jack As New Customer(72, True)
        Dim nandira As New Customer(16, False)
        Dim laurel As New Customer(45, True)
        Dim felix As New Customer(23, False)
 
        ' Add objects to the rule engine's working memory.
        engine.Assert(promotion)
        engine.Assert(jack)
        engine.Assert(nandira)
        engine.Assert(laurel)
        engine.Assert(felix)
 
        ' Execute the ruleset.
        engine.Execute()
 
        ' 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 containing the decision table and displays the messages you defined in the action column of the table.
    There is a message for each Customer object in the working memory, and the message is chosen from the table according to the age of the customer and whether or not the customer already has an account.
  4. Check that the messages displayed in the console window are appropriate for the objects in the working memory, and the conditions in the decision table.
  5. Press ENTER to stop the business rule application, and close the console window.