rule
rule keyword declares a rule.
Purpose
This keyword is used to declare a rule.
Context
At the top level of rulesets
Syntax
rule ruleName {[priority = value;]
[property propertyName = value;]
when {condition1 ... conditionn [evaluate (expression)]}
then {[action1 ... actionm]}
[else {[action1 ... actionp]}]
};
Description
A rule in IRL is composed of three parts:
A header part, which defines the name of the rule, its priority, and properties.
The condition part, which begins with the keyword
when, also referred to by its side of the rule.The action part, which begins with the keyword
then, also referred by its side of the rule.
When the condition part ends with an evaluate statement,
the action part can have an else part, executed if
the evaluate statement returns false.
If it returns true, the then part
is executed.
If a formal comment (/**...*/)
precedes the rule definition, the comment is saved so that it can
be retrieved later using the API.
Example
rule VideoCallBilling {
when {
?c:Customer(?p:phoneNo);
?v:collect (new videoCallCollection())
Usage(phoneNo == ?p; type == videoCall)
where (size() > 0);
}
then {
float ?t = 0.;
Enumeration ?enum = ?v.elements();
while (?enum.hasMoreElements()) {
Usage ?x = (Usage)enum.nextElement();
?t += ?x.charge();
}
System.out.println("Dear "+ ?c.name + "Your bill for
Video conference calls is : "+ ?t);
}
};The VideoCallBilling rule
collects appropriate Usage objects, sums the charge,
and prints the result with the client’s name. The first condition
returns the Customer object in variable ?c,
with the field phoneNo stored in variable ?p.
The second condition is a collect statement. This
statement returns a videoCallCollection object in
variable ?v. This collection object contains Usage objects
with the field phoneNo equal to the variable ?p, phoneNo from
the previous condition, and field type equal to videoCall.
The where part of the collect statement
verifies that one or more Usage objects have been
collected. The then part of the rule declares two
variables ?t, initialized to 0,
and variable ?enum, set to the elements of the collection
using the default collection method elements(). Using
the while statement, the elements of the collection
are enumerated and the Usage objects are accessed
and referenced using the variable ?x. Variable ?t and
the method charge()are used to sum the charge for
each Usage object. The println statement
prints the client’s name and the total charge.
rule CheckTemperature {
priority = 1,000,000 + 1;
when {
Sensor(type==Temperature; value>150);
}
then {
insert Alarm();
}
};The CheckTemperature rule has
a priority of 1,000,001. If an object Sensor with
a field type equal to Temperature has
a value greater than 150, the condition part is true
and the action part is executed. An object Alarm is
inserted into the working memory using the command insert.