| ILOG Rules for .NET User Guides > Walkthroughs > Walkthrough: Creating a Ruleflow |
Walkthrough: Creating a Ruleflow |
PREVIOUS NEXT |
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 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.
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
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 write the business rules you:
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 add a new ruleset parameter to the list.
Mode |
In |
Name |
customer |
Type |
ClassLibrary1.Customer |
Mode |
In |
Name |
message |
Type |
ClassLibrary1.Message |
Mode |
InOut |
Name |
number of promotions |
Type |
System.Int32 |
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 learn more about writing rules see Writing Rules and Rule Editing Reference.
To create the ruleflow you:
The final ruleflow should look something like this:
To define the application logic you:
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
You can test the business rule application within Visual Studio.
| Copyright © 1987-2008 ILOG S.A. All rights reserved. Legal terms. Documentation homepage. | PREVIOUS NEXT |