The not keyword tests the negation of a rule condition.
This statement is used in the condition part of rules to test the negation of a rule condition.
Rule conditions
not condition;
You use the not statement in the condition part of rules, to a simple condition, an in statement, or a from statement. The not statement returns true if the specified condition is false. A not condition returns true for a simple condition if the working memory does not contain any object that can match the condition. A not statement returns true with an in or from statement when the in or from statement returns false. Use not for the contrary of both exists class or just class.
A not condition cannot be bound to an external variable. The not condition returns true when no object in the working memory matches the condition. Hence it is invalid to bind a false condition to a variable.
Any variable bound within the not condition is local to the condition. The remainder of the rule cannot contain any reference to it.
You can use the special variable ?this within the not statement to designate the current working memory object being tested.
rule GuestPromotion {
when {
?g:Guest(?i:id);
not GroupId(this.contains(?i));
}
then {
?g.promotions();
}
}
The GuestPromotion rule verifies that a guest is not part of a group, in order to provide a promotion. The first condition uses the variable ?g to reference a Guest object and the variable ?i to return the field value id. The not statement is used to test whether this id is contained in the object GroupId. The method contains is applied to the variable ?this. The then part launches the method promotions().
rule LargestPercentStockChange {
when {
?s: Stock(?c:currentValue; ?l:lastClosing; ?p:(?c-?l)/?l);
not Stock(?c1:currentValue; ?l1:lastClosing; ?p < (?c1-?l1)/?l1);
}
then {
System.out.println(?s.ticker + "has the largest change of: "+ ?p +"%");
}
};
The purpose of the LargestPercentStockChange rule is to print out the stock with the largest percent change.
The first condition, in the line starting with ?s, specifies a stock, and the rule variables named ?c and ?l are bound to the current stock value and the last closing value, respectively. The stock percent change is calculated and saved as ?p.
In the second condition, the line contains the not keyword. As in the previous condition, a percent change is calculated with rule variables ?c1 and ?l1. The condition then compares the result with the previous calculated change ?p. The not keyword causes the condition to fail for any percentage change except the largest.