Entity management

Updated: 2025-09-28

In this chapter we explore how an agent can be responsible for monitoring the passage of entities through a zone of the model and computing global metrics such as total usage time or average residence time. The agent does not act directly on the entities; instead, it listens to events automatically generated by a QUEUER (through its ON_QUEUE and ON_DEPART hooks) and, based on those events, records when entities enter and leave. To do this, the agent maintains a data structure (associative array) where it stores entry times and later computes differences upon exit. This allows full tracking without interfering with the logical flow of the rest of the model. This kind of logic is useful for implementing custom statistics, usage auditing, alerts, or any control that depends on external events and entity history.

Example

/*

  	 	Entity management
 
*/
SYSTEM {TYPE:PRE_RUN,TRIGGER:PRE_RUN}

Restroom {NAME:RestroomAgents,X:100,Y:121,visible:1}
Queuer {NAME:Queuer1,X:321,Y:123,on_queue:Queuer1on_queue,on_depart:Queuer1on_depart}

POSITION {NAME:POS1,X:251,Y:335}
POSITION {NAME:POS2,X:469,Y:341}

Graphic {NAME:Text1,Type:TEXT,X:425,Y:514,Text:"Entity"}
Graphic {NAME:TextAgent1,Type:TEXT,X:433,Y:462,Text:"---"}
Graphic {NAME:TextAgent2,Type:TEXT,X:433,Y:416,Text:"---"}

initial nAgent,0
START 500

;*****************************************************
PROCEDURE PRE_RUN
	timeout agent.main,0
	TERMINATE_VE
ENDPROCEDURE 1

;*****************************************************
GENERATE 10,0 {NAME:GEN1,X:43,Y:300}

	move {name:Text1,TEXT:"Entity result P$(result,X$nAgent) AC1$"}
	
	ADVANCE 20,0 {TO:POS1}
	queue Queuer1
	ADVANCE 20,60 {TO:POS2}
	depart Queuer1

ENDGENERATE 1

;*******************************
procedure agent.main
	savevalue nAgent,D$N
    assign data,{}
    assign result,0
    assign average,0
    assign nEntities,0
    assign totalTime,0
    while (1==1)
        REST RestroomAgents
    endwhile
    terminate_ve
endprocedure

;**********************************
procedure agent.IN
    assign data.entity_P$PARAM_A,P$PARAM_B
    move {name:TextAgent1,TEXT:"IN Entity P$PARAM_A : Start time: P$(data.entity_P$PARAM_A)"}
    RETURN_RESTORE
endprocedure

;**********************************
procedure agent.OUT
    assign nEntities,P$nEntities + 1
    assign startTime,P$(data.entity_P$PARAM_A)
    assign endTime,P$PARAM_B
    assign totalTime,P$totalTime + P$endTime - P$startTime
    assign average,P$totalTime / P$nEntities
    move {name:TextAgent2,TEXT:"OUT Entity: P$PARAM_A Total time: P$totalTime Average: P$average"}
    ASSIGN.DELETE data,entity_P$PARAM_A
    RETURN_RESTORE
endprocedure

procedure Queuer1on_queue
	SIGNAL agent.IN,X$nAgent,P$ENTITYNUMBER,AC1$
    TERMINATE_VE
endprocedure

procedure Queuer1on_depart
    SIGNAL agent.OUT,X$nAgent,P$ENTITYNUMBER,AC1$
	TERMINATE_VE
endprocedure