ILOG Rules for .NET User Guides > Walkthroughs > Walkthrough: Hello World

This walkthrough presents the following fundamental procedures for creating a business rule application with ILOG Rules for .NET.

In this walkthrough, you create a console application that uses a very simple business rule to display a message in a command window.

Creating a New Solution

You develop business rule applications for the .NET environment within a Visual Studio solution.

To create a new solution

Setting up the Business Object Model

The business object model defines the business domain: the things of interest to the business, and a vocabulary for writing rules about them. The vocabulary of the business rules is implemented as annotations to the types in the business object model.

To set up the business object model you:

To define the class library project
  1. Add a class library project called ClassLibrary1 to the solution.
  2. Add the following class to the project:
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
  1. Save the solution.
To import the class library into the Business Object Model View
  1. Build the solution.
  2. In the Business Object Model View, use the Import Project Wizard to import the ClassLibrary1 project.
  3. In the Business Object Model View, click ClassLibrary1 and use the Business Object Model Wizard to import the classes of ClassLibrary1 into the business object model.
To annotate the business object model
  1. In the Business Object Model View, select PrintMessage method of the Message class.
  2. In the Properties Window, set the DisplayText property of PrintMessage to print {this}: {0}.
  3. In this property, {this} is a placeholder for the Message object on which the PrintMessage method is called, and {0} is a placeholder for the argument of the method.

Defining the Business Logic

The business logic of an application is the part that implements the policies of the business. For example, the following is a policy that could be part of the business logic of an application that manages company's relationship with its customers: clients in the Gold service program are upgraded to the Platinum service program after they have renewed their annual subscription five times.

In ILOG Rules for .NET you implement the business logic of an application using business rules. To define the business logic for this business rule application you:

To define the rule project
  1. Add a rule project called BusinessRules1 to the HelloWorld solution.
  2. Add a reference to the ClassLibrary1 project to the rule project.
  3. Add a new Business Rule item called BusinessRules1.blx to the rule project.
To write the business rule
  1. In Visual Studio, open BusinessRules1.blx.
  2. In the rule called BusinessRules1.BusinessRules1, press CTL + SPACEBAR.
  3. On the Intellirule menu, double-click then then press CTL + SPACEBAR.
  4. On the Intellirule menu, double-click print<a message>then press CTL + SPACEBAR.
  5. On the Intellirule menu, double-click the message then press CTL + SPACEBAR.
  6. On the Intellirule menu, double-click <a string> and press CTL + SPACEBAR.
  7. After the colon (:) type "Hello World!";
  8. Make sure you include the quotation marks and the semi-colon.
  9. Save the solution.

The completed rule reads as follows:

 Then print the Message: "Hello world!";

Note
This business rule has no conditions and, therefore, always executes. Typically, however, business rules have conditions that must be met for the rules to be executed.

Defining the Application Logic

The application logic of an application is the part that performs tasks such as database management, and handling interactions with users. In a business rule application, the application logic includes the part of the application that embeds the rule engine, which executes business rules. Using a rule engine enables you to manage the business logic with processes external to the application.

To define the application logic you:

To define the application project
  1. In the ConsoleApplication1 project, add references to the BusinessRules1 and ClassLibrary1 projects, and the .NET components ILOG.Rules and ILOG.Rules.RuleEngine.
  2. 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();
 
      // Create the message object which is the subject of the 
      // rules in the ruleset.
      Message hello = new Message();
 
      // Load the ruleset into the rule engine.
      // The ruleset is generated when you build the rule project.
      engine.RuleSet = new BusinessRules1(); 
 
      // Add the object to the rule engine's working memory.
      engine.Assert(hello);
 
      // Execute the ruleset.
      // The rule engine evaluates the conditions in the ruleset 
      // using the objects in the working memory. The rule 
      // engine initiates the actions of the rules whose 
      // conditions are true. 
      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.
        ' The ruleset is generated when you build the rule project.
        engine.RuleSet = New BusinessRules1
 
        ' Create the message object which is the subject of the 
        ' rules in the ruleset.
        Dim hello As New Message
 
        ' Add the object to the rule engine's working memory.
        engine.Assert(hello)
 
        ' Execute the ruleset.
        ' The rule engine evaluates the conditions in the ruleset 
        ' using the objects in the working memory. The rule 
        ' engine initiates the actions of the rules whose 
        ' conditions are true. 
        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.