In GPSS-Plus, a virtual entity (VE) is usually short-lived: it is created, executes a sequence of blocks, and ends. However, some models require permanent processes that can act as controllers, managers, or passive processes that wait for orders. We call these special VEs agents.
What an agent is
An agent is simply a virtual entity that never finishes its execution. To do this, it is initialized with the TIMEOUT block (outside of GENERATE blocks), which makes it start without depending on any arrival stream.
Once created, an agent can behave in two main ways:
Active: it runs cycles continuously (for example, using
ADVANCE) to check and make decisions. Like a TIMER that is always the same entity.Passive: it waits using
HOLDand stays there until it is ordered to do something else.
How it is created
In PRE_RUN we call its initializer PROCEDURE:
timeout agent1.main,0 ; Will execute at time 0.
By convention, it is named .main.
This PROCEDURE stores its identifier in a SAVEVALUE so that any entity can access it, and then enters an infinite loop with active or passive waiting:
PROCEDURE agent1.main
SAVEVALUE nAgent1,D$N
WHILE (1==1)
ADVANCE 100
... ; Periodic actions
ENDWHILE
TERMINATE_VE ; Will never be reached
ENDPROCEDURE
;----------------------------------------
PROCEDURE agent2.main
SAVEVALUE nAgent2,D$N
HOLD HOLDER_AGENTS
TERMINATE_VE ; Will never be reached
ENDPROCEDURE
Its associated methods (PROCEDUREs) follow the convention agent.method, for example:
procedure agent2.full_release
MOVE {NAME:INFO2, TEXT:"T= AC1$ Agent 2 releasing everything"}
UNHOLD HOLDER1
UNHOLD HOLDER2
RETURN_RESTORE ; Returns exactly to the previous state
ENDPROCEDURE
The most important part to highlight is its special termination: RETURN_RESTORE.
This block returns the agent to the exact situation it was in when it was interrupted via SIGNAL/SIGNALNOW.
If it was in an ADVANCE, it resumes with the remaining time properly adjusted.
If it was waiting in a resource queue, its situation is not altered.
Note that if RETURN or ENDPROCEDURE were used, execution would continue to the next line of code. That can be useful if the agent is using active waiting and you want to restart its periodic action timer.
In the example:
We see two agents of both types releasing entities from two HOLDERS in arbitrary ways.
The second one, every 20 entities that complete the route, releases all those trapped.
Example
/*
Persistent virtual entity. The agent
*/
SYSTEM {TYPE:PRE_RUN,TRIGGER:PRE_RUN}
START 100
;===> Resource and position setup
Restroom {NAME:Restroom1, X:337, Y:344}
Restroom {NAME:Restroom2, X:337, Y:258}
Restroom {NAME:Restroom_AGENT2, X:567, Y:104}
POSITION {NAME:ENTRY, X:179, Y:301}
POSITION {NAME:EXIT, X:559, Y:299}
GRAPHIC {NAME:INFO1, TYPE:TEXT, X:335, Y:178, TEXT:"Agent1"}
GRAPHIC {NAME:INFO2, TYPE:TEXT, X:569, Y:62, TEXT:"Agent2"}
INITIAL counter,0
;*****************************************************
;===> Agent initialization
PROCEDURE PRE_RUN
timeout agent1.main,0
timeout agent2.main,0
TERMINATE_VE
ENDPROCEDURE
;*****************************************************
;===> Main flow of entities
GENERATE 15,5 {NAME:GEN1, X:50, Y:300}
ADVANCE 10 {TO:ENTRY}
ASSIGN randomChoice, FLOOR(RANDOM * 2) + 1
IF (P$randomChoice==1)
MOD {color:#990000}
ADVANCE 10 {TO:Restroom1}
REST Restroom1
ELSE
MOD {color:#999900}
ADVANCE 10 {TO:Restroom2}
REST Restroom2
ENDIF
ADVANCE 30,30 {TO:EXIT}
if (D$N %20 == 0)
SIGNAL agent2.full_release,X$nAgent2
endif
ENDGENERATE 1
;*******************************
procedure agent2.main
; Save its unique id as a variable if needed
SAVEVALUE nAgent2,D$N
MOVE {NAME:INFO2, TEXT:"Agent 1 active"}
WHILE (1==1) ; useless if RETURN_RESTORE is always used
REST Restroom_AGENT2
ENDWHILE
TERMINATE_VE ; Will never be reached
ENDPROCEDURE
;*******************************
procedure agent2.full_release
MOVE {NAME:INFO2, TEXT:"T= AC1$ Agent 2 releasing everything"}
WAKE Restroom1
WAKE Restroom2
RETURN_RESTORE
ENDPROCEDURE
;**********************************
PROCEDURE agent1.main
; Save its unique id as a variable if needed
SAVEVALUE nAgent1,D$N
MOVE {NAME:INFO1, TEXT:"Agent 2 active"}
WHILE (1==1)
ADVANCE 50 ; The agent checks every 50 units
SAVEVALUE counter, X$counter + 1
MOVE {NAME:INFO1, TEXT:"Agent: X$counter"}
IF (X$counter % 3==0)
assign toRelease,R$(Restroom1,IN)
MOVE {NAME:INFO1, TEXT:"Releasing Restroom 1 (P$toRelease OF R$(Restroom1,IN))"}
WAKE Restroom1
ENDIF
IF (X$counter % 5 ==0)
assign toRelease,round(R$(Restroom2,IN) * 2 / 3)
MOVE {NAME:INFO1, TEXT:"Releasing Restroom 2 (P$toRelease OF R$(Restroom2,IN))"}
WAKE Restroom2,P$toRelease
ENDIF
ENDWHILE
TERMINATE_VE ; Will never be reached
ENDPROCEDURE
;**********************************