Modules

Updated: 2026-01-07

We have already seen contexts CX$, which—very roughly—are a special assign of type string used so that variable names do not collide, and which is inherited from caller to callee. That is, if we do TIMEOUT, the VE that executes the PROCEDURE will do so with the same CX$ value as the one that called it.

It can also be set with the CX BLOCK:

CX "sevilla"

And from that point on, the SNA CX$ will have that value.

Now that we know how to create graphic elements with NEWGRAPHIC passing the context, let’s see how to create a complete module with the same system.

A module is a set of GENERATEs, NEWFACILITYs, PROCEDUREs... that would work on their own, normally under a context, and that usually contain at least one GENERATE. All of this goes into a single file that we will call with the ".mod" extension.

The most distinctive thing about these modules is that they are used with an INCLUDE, and the parameters will affect what is in the block area, but not what is a GENERATE because it is the BLOCK/COMMAND that is not exactly a BLOCK, but is also a program point that creates entities and is the safe place to send an entity with SCAPE or UNLOAD.

So it makes no sense to do something like:

GENERATE {name: CX$_gen,x:100,y:200}

because a virtual entity is not going to pass through it to create the GENERATE, nor will it be able to perform a NEWGENERATE.

This is what a GENERATE looks like inside a module:

GENERATE 0,0,0,0 {name:hub,visible:0,x:700,y:100}
    advance 10,30 {from: hub , to:"CX$_posicion"}
ENDGENERATE
;-------------------------------------------
PROCEDURE hub.init
;... resource creation
ENDPROCEDURE

When INCLUDE is executed, the only thing that happens is that this file becomes part of the general code. The GPSS-Plus code texts are concatenated as-is.

So the code will now have a new GENERATE called "hub", which can be the landing point for any entity that arrives via SCAPE or UNLOAD. The important thing is that those entities land with the correct CX$.

For this reason, a module without a GENERATE cannot receive entities from outside, so it cannot work as an autonomous subsystem. In this example, "hub" is the reference to the program point.

In short, what we will normally have in a module is one or several GENERATEs that will process entities coming from other contexts or modules, or from the main program, with access to resources under that same context.

In the example we will see a basic system.

From the main GENERATE, entities branch out to end up in a set of resources that, as a whole, are a module called "plantaReciclaje".

This module has, in addition to an ".init" similar to the initialization of any library with NewGraphic, NewFacility... the GENERATE so that entities can reach it from the main program through SCAPE:

scape plantaReciclaje {cx:"plantaA"}

And it will make each entity arrive at the "plantaReciclaje" generate with an empty return-address stack but all assigns intact and the context value set.

generate 0,0,0,0 {name:plantaReciclaje,visible:0}
    call procesar
    terminate 1
endgenerate

procedure procesar
    advance 10,10 {to:CX$_pos_in}
    advance 10,10 {to:CX$_fac1}
    seize CX$_fac1
    advance 10,10
    release CX$_fac1
    advance 10,10 {to:CX$_pos_out}
endprocedure

 In a way, it is somewhat like the classic "GOTO" instruction, but where the jump is only allowed to a safe program point, which is the GENERATE.

 

Example

/*

Contexts and modules. Simple module


*/
SYSTEM {TYPE:OPTIONS, SPEED:5}
SYSTEM {TYPE:PRE_RUN,TRIGGER:PRE_RUN}
Position {NAME:POS1,X:147,Y:311}

START 200

include "./manual_es/Season_07_Contextos_y_modulos/modulo.mod"

PROCEDURE PRE_RUN

	assign config,{title:"Plant_A"
    		,x:350,y:500
            }
	CALL plantaA.plantaReciclaje.init,V$config
	
	assign config,{title:"Plant B"
    		,x:350,y:100
            }
	CALL plantaB.plantaReciclaje.init,V$config

    TERMINATE_VE
ENDPROCEDURE

;------------------------------------------------------
; --- Entity flow ---
GENERATE 6,0 {NAME:GEN1,x:58,y:310} 

advance 15,0 {to:POS1}

if (RANDOM > 0.5)
scape plantaReciclaje {cx:"plantaA"}
else
scape plantaReciclaje {cx:"plantaB"}
endif

TERMINATE 1