evaluate

The evaluate keyword specifies tests on objects.

Purpose

This evaluate statement is used in the condition part of a rule to test objects in the working memory.

Context

Rule conditions

Syntax

evaluate (expression); 

Description

Before the evaluate statement, you must write a simple condition that binds a variable to an object or a value. The engine can test any such variable bound to an object or a value. Note that the statements not, exists, and collect are not simple conditions. An evaluate statement is true if all the tests carried out in the expression are true. The expression can be made of multiple tests enclosed in braces ({}).

When the condition part of a rule ends with an evaluate statement, the action part can have an else part, which is executed if the last evaluate statement returns false. If it returns true, the then part is executed.

The evaluate statement differs from the not and exists statements. The not and exists statements apply tests only within the scope of their statement. The evaluate statement is more general in that it specifies tests that must be satisfied by the objects that exist in the working memory.

Example

The following two CloseOrder rules show equivalent ways of testing objects of the working memory. The first rule has two conditions followed by an evaluate statement. In the second rule, the test of the evaluate statement is incorporated in the second condition.

Rule 1
rule CloseOrder {
   when {
      ?s: CustomerShipment(?id:CustomerId; ?sl:shipmentList);
      ?o: CustomerOrder(?id==CustomerId; ?ol:orderList);
      evaluate (?sl.equals(?ol));
   }
   then {
      retract(?s);
      insert Shipped(?o,currentDate());
   }
};
Rule 2
rule CloseOrder {
   when {
      ?s: CustomerShipment(?id:CustomerId; ?sl:shipmentList);
      ?o: CustomerOrder(?id==CustomerId; ?ol:orderList;
                        ?sl.equals(?ol))
   }
   then {
      retract(?s);
      insert Shipped(?o,currentDate()); 
   }
};

For both rules, variable ?s refers to a CustomerShipment object, variable ?id returns the CustomerId field, and variable ?sl returns the shipmentList field. In the second condition, variable ?o refers to a CustomerOrder object where the CustomerId field matches ?id and variable ?ol contains the orderList field. The evaluate statement calls a test, the equals method, which returns true if the two lists contain the same elements. If the conditions are true, the action part is executed. The CustomerShipment object is deleted from the working memory by the retract statement, and a Shipped object is inserted into the working memory by the insert statement.