ruleset

The ruleset keyword declares a ruleset.

Purpose

This keyword is used to declare a ruleset.

Context

At the top level of rulesets

Syntax

ruleset rulesetName
{
   [property propertyName = value;] 
   [instances (className) = value|{value1,...,valuen};] 
   [hasher (typeName variableName) = value;] 
   [in typeName variableName [= value];] 
   [inout typeName variableName [= value];] 
   [out typeName variableName [= value];] 
};

Description

It is not mandatory to declare rulesets. If you choose to do so, you must specify at least the ruleset name.

In addition to the ruleset name, a ruleset declaration can also contain the following elements:

  • property

    You can define properties on the ruleset.

  • instances

    You can specify class instances to declare on which instances for a specified class the rule engine works. If you do not specify class instances, the engine looks for objects of this class in the working memory.

    There are two ways to define class instances:

    • Give a value whose type implements java.util.Collection. Such a collection contains the objects of the specified class on which the rule engine works.

    • Explicitly specify the instances used by the engine. Values are given to compute these instances. The value type is derived from the specified class.

  • hasher

    You can define a hashing expression to optimize rule execution.

  • in, out, inout

    You can define ruleset parameters as a simple way to exchange data between the application and the ruleset. Ruleset parameters are accessible from a rule, function, or task definition in the ruleset.

Example

ruleset Connect4
{
  // Provides the grid, the player and the adversary
  in Grid grid;
  out SavedGame saved;

  // The latest move
  int turn;
  Position move;

  // Who's the winner, what is the connect4, and any other reason
  // to end the game.
  int winner = Constants.None;
  Connect4 connect4;
  boolean ending = false;
  String message;

  // Where to find the position objects.
  instances(Position) = ?grid.getAllPositions();
};

This example illustrates a ruleset declaration. The ruleset name is Connect4. Ruleset parameters are declared. One of them, named grid, is an input parameter. Its initial value is set by a call to IlrContext.IlrContext#setParameters(). The parameter saved is an output parameter. Its value can be obtained from the application with the different API calls listed above. The ruleset variables turn, move, winner, connect4, ending, and message are local.

Instances are defined for the class Position. Each object of type Position is bound in a rule condition part. The engine does not look for those objects in the working memory but in the specified list obtained with ?grid.getAllPositions().