retract
The retract keyword removes an object from the working memory.
Purpose
This keyword is used in the action part of rules or in functions to remove an object from the working memory.
Context
Functions or rule actions
Syntax
retract object;
Description
The object can be defined by a variable or by an expression that denotes an object associated with the current rule engine or the engine that you specify (context in the code sample below).
After the object has been retracted from the working memory, the removal can optionally execute a daemon. To execute a daemon, the object class must implement the interface IlrRetractDemon and define the method retracted.
Example
The first example shows a basic retract statement while the second example shows the implementation of a daemon after the object is removed.
- Example 1
-
rule FlightDisplayExpired { when { ?f: Flight( status==ARRIVED; landingTime + 30 < currentTime() ); } then { retract ?f; } };The FlightDisplayExpired rule removes a Flight object after the arrival time has passed 30 minutes. The first condition returns a Flight object in the variable ?f. The object field status must be equivalent to the static constant ARRIVED. The field landingTime + 30 must be less than the time returned by the method currentTime(). If the condition is true, the object ?f is removed from the working memory by the retract statement.
- Example 2
-
public void retracted(IlrContext context) { System.out.println("Retracted a className object from memory with fieldName: " + fieldName); }This example prints a message each time a className object is retracted from the working memory.