not

The not keyword tests the negation of a rule condition.

Purpose

This statement is used in the condition part of rules to test the negation of a rule condition.

Context

Rule conditions

Syntax

not condition; 

Description

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.

Example

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.