while
while keyword executes a statement
block.
Purpose
This statement is used in the action part of rules or in functions to execute a statement block numerous times.
Context
Functions or rule actions
Syntax
while (test) [{statement1; ... statementn;}]
Description
The test argument
can be any legal test, as in the Java™ programming
language. The statement block can execute any IRL
statement, arithmetic expressions, and method calls. Empty statement blocks
are valid. If you specify only one statement block,
the braces {} are not mandatory.
Example
rule StockDropWarning {
when {
?c:Client(?a:account_number;?p:percentWarningMark);
ClientStockList(account_number == ?a; ?s:stockList);
}
then {
Enumeration ?enum = ?s.elements();
while (?enum.hasMoreElements()) {
Stock ?x = (Stock)enum.nextElement();
if (((1.-?p)*?x.purchasePrice) > ?x.currentPrice) {
System.out.println("Dear "+ ?c.name + "Your stock "
+ ?x.ticker + " has dropped " + ( (?x.purchasePrice
- ?x.currentPrice) / ?x.purchasePrice ) +
" percent.");
}
}
}
};The rule StockDropWarning checks
the percent change of a client’s stocks and warns if a stock price
is below its purchase price by a certain percentage. The condition Client returns
an account number in variable ?a and a decimal value percentWarningMark in
variable ?p. The second condition, ClientStockList,
returns a stock list in variable ?s corresponding
to the account number equal to ?a. In the action
part of the rule, the while statement iterates on
every stock in the stock list ?s and check the difference
between the purchase price and the current price. If the stock price
decreased in ?p percent below the purchase price,
the println function in the action statement prints
a message.