| Reference > Rule Languages > ILOG Rule Language > IRL Keywords > when |
when |
PREVIOUS NEXT |
The condition part of a rule.
when {
[collect|evaluate|exists|not|wait] condition1
...
[collect|evaluate|exists|not|wait] condition2
}
The keyword when specifies the condition part of a rule, also known as the left-hand side. It may be composed of one or more conditions upon which a rule can be fired.
A condition can be instantiated with objects in the working memory. Using a class name, instances of that class, or instances of any derived class, may be matched.
A condition may be preceded by a keyword not, exists, or evaluate.
Conditions may be used to:
Car(color == blue) tests if a Car object has a color field equal to the value blue.
Car object in the working memory with a field color, the condition Car(?c:color) instantiates the variable ?c with the color of the car.
?c:Car(color == blue) will return in variable ?c a Car object with the field color equal to blue. If no such object exists in the working memory, the condition will fail.
For each rule, the cardinality of rule instances in the agenda equals the product of the number of matched instances for each condition of the rule. For example, suppose we have two classes, Depart and Destination, and for each, three city instances: Paris, New York and Tokyo.
rule example
{
when {
Depart(?c1:city);
Destination(?c2:city);
}
then {
System.out.println("Possible city pairs are: "
+ ?c1 + ":" + ?c2);
}
}
This rule will generate nine instances of the rule in the agenda, including the three naive results such as "Tokyo:Tokyo".
rule CarColor
{
when {
Car(color == red);
not Car(color == green);
}
then {
System.out.println("There is a red car but no green car.")
}
}
The first condition is true if a Car object exists with the field color equal to red. The second condition tests that no Car object exists with the field color equal to green. If both conditions are true, the action is performed, printing the message "There is a red car but no green car."
rule CarsAvailableAtPriceRange {
when {
Request(?m: mark; ?h: highPrice; ?l: lowPrice );
?car: UsedCar(mark equals ?m ; price < ?h & > ?l)
}
then {
System.out.println("A " + ?car.year + " " + ?car.mark
+ " " + ?car.model + " is available for you.");
}
}
The rule CarsAvailableAtPriceRange searches for a car using information supplied by the user. The rule has two condition statements and one action statement. To execute the action statement, the two condition statements must be fulfilled.
In the first condition, a Request object is matched, binding the three variables ?m, ?h, and ?l with the fields mark, highPrice, and lowPrice, respectively.
In the second condition, a UsedCar object is matched with the following constraints: the field mark equals ?m, and the field price has a value that is between ?h and ?l. If a UsedCar object is found that fulfills these constraints, it is bound to the variable ?car. The action part of the rule consists of printing out any car found, for example, "A 1996 BMW 573i is available for you." Multiple cars may be printed since each UsedCar object fulfilling the constraints generates a separate rule instance (for further details, see the information on rule instances of the agenda).
collect, evaluate, exists, not, wait
| Copyright © 1987-2008 ILOG S.A. All rights reserved. Legal terms. Documentation homepage. | PREVIOUS NEXT |