Removing references to an entity
Before an agent deletes its bound entity, all the references to this entity from other agents must be removed.
- By subscribing to events that notify them of the deletion of the entity, see Emission of notification events.
- By subscribing to the same deletion event. The agent bound to the entity must process the deletion event with a delay, see Subscription from all agents to the deletion event.
Emission of notification events
The following pattern illustrates the deletion of a flight entity. The flight entity has a departure airport and an arrival airport.
An event is sent from the external world to remove this flight. A Flight agent listens to the Remove Flight Event and processes the event in the following rule:
when a Remove Flight Event occurs
then
emit a new Removing Flight Event where the airport is the departure airport of 'the flight';
emit a new Removing Flight Event where the airport is the arrival airport of 'the flight';
The Airport agents listen to Removing Flight Event, and process the event in the following rule:
when a Removing Flight Event occurs
if
the departing flights of 'the airport' contain the flight of this Removing Flight Event
then
remove the flight of this Removing Flight Event from the departing flights of 'the airport';
emit a new Flight Removed Event where the flight is the flight of this Removing Flight Event;
when a Removing Flight Event occurs
if
the arrival flights of 'the airport' contain the flight of this Removing Flight Event
then
remove the flight of this Removing Flight Event from the arrival flights of 'the airport'
emit a new Flight Removed Event where the flight is the flight of this Removing Flight Event;
The Flight agent listens to Flight Removed Event, and processes the events in the following rule:
if
there are 2 Flight Removed Events
then
set 'the flight' to null;
Subscription from all agents to the deletion event
Another way to remove the references to the entity is for all agents to subscribe to the deletion event.
The Flight agent listens to the Remove Flight Event, and processes the event with a delay. This delay ensures that other agents that listen for this event have time to remove the reference to the entity.
when a Remove Flight Event has occurred 1 minute ago
if
'the flight' is not null
then
set 'the flight' to null;
The Airport agent processes the Remove Flight Event as soon as it arrives, and removes the reference to the flight.
when a Remove Flight Event occurs, called 'this event'
then
remove the flight of 'this event' from the departing flights of 'the airport';
remove the flight of 'this event' from the arrival flights of 'the airport';