retract
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
FlightDisplayExpiredrule removes aFlightobject after the arrival time has passed 30 minutes. The first condition returns aFlightobject in the variable?f. The object fieldstatusmust be equivalent to the static constantARRIVED. The fieldlandingTime + 30must be less than the time returned by the methodcurrentTime(). If the condition istrue, the object?fis removed from the working memory by theretractstatement. - 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
classNameobject is retracted from the working memory.