modify

The modify keyword modifies objects and updates the agenda.

Purpose

This statement is used in the action part of rules or in functions to modify objects and updates the agenda accordingly.

Context

Functions or rule actions

Syntax

modify [refresh] object {statement1 ... statementn}; 

Description

The statement block after the object argument can modify the state of an object of the working memory. Those statements can be arithmetic expressions or method calls. When an object of the working memory is modified, the agenda is also updated. If the block contains only one statement, the braces ({}) are not required.

Note:

In a modify statement, the statements that modify the object are specified directly in the statement block that follows the object, as shown in the syntax above. This is different from the update statement, where the modifications are specified before the update keyword, independently of the update statement.

If you apply the refresh keyword, the rules that remain true or become true after the modification of the object are reinserted into the agenda. Without the refresh keyword, only the rules that become true as a result of the modification are inserted into the agenda.

After the object has been updated in the working memory, the update can optionally launch a daemon. For this purpose, the class must implement the IlrUpdateDemon interface and must define the updated method (see the update keyword for details).

Example

rule InventoryUpdate { 
   priority = 1,000,000;
   when {
      Sold(item == book; ?ISBN:ISBN);
      ?b:Book(?ISBN == ISBN; currentStock > 0);
   }
   then {
      modify ?b {
         currentStock-=1;
      }
   }
};

The InventoryUpdate rule has a 1,000,000 priority. The first condition matches an object Sold with field item equal to book and instantiates the variable ?ISBN with the value of the field ISBN. The second condition uses the variable ?ISBN to match an object Book and tests that the field current_stock is greater than 0. The condition is true only if the current stock is one or more. When both conditions are fulfilled, the action part can be executed. The object referenced by ?b is updated in the working memory by the modify statement; the field currentStock is decremented by one.

import tmp.*;

rule IncrementClock { 
   when {
      ?t:Time();
   }
   then {
      modify refresh ?t {
         ?t.setSeconds(?t.getSeconds()+1);
      }
   }
};

If the refresh keyword were not used, the rule would be executed only once. The refresh keyword is used to reinsert the rules matching a Time object into the agenda. In the example, the rule is executed forever.