By now you have probably realized what it can mean to build logic that simulates an entire workshop.
And we can go crazy if we are told there are two workshops. And then 10.
It seems that we should give up at that point, but no — that is exactly what GPSS-Plus contexts are for.
They are not classes, because in a discrete-event environment that concept does not really make sense, but we will see that they are very similar. They are more like a namespace, and everything becomes much simpler.
Let’s imagine a more trivial case: a graphic showing the level of a tank.
We draw the lines, colors and text, and then we want to reuse it.
That is where the problems begin: that object, class, drawing, element — whatever you want to call it — has its parameters so deeply embedded that it becomes overwhelming to work with.
And not only that: if we have two tanks, the work is not just double; we must also be very careful not to overwrite variables and to create as many LINE elements as needed.
This is the problem we are going to solve.
By analogy, it may look like classes with methods, local variables and instantiation, but it is not. It will be solved with a simple string-type variable.
Until now, to move a graphic we could do:
CALL grafico_set,75
And if our procedure was correct, it would redraw a given graphic to show that 75% fill level.
Graphics that we previously had to create.
Now we are going to do it differently, in such a way that everything we are about to see can be moved to another file that no longer gets in our way, and call it using INCLUDE.
First step: create a descriptive method
If our object is a tank, we will create a procedure called:
PROCEDURE deposito.init
This procedure will normally be invoked by an EV from PRE_RUN / CALL, and it will be responsible for creating all the graphics — instead of using GRAPHIC commands, it will use NEWGRAPHIC blocks.
Like all calls, it can receive parameters to be executed, for example:
call deposito.init, 20, 130,120, 100,"Carga","#ff0000"
…where deposito.init is the procedure and the rest are: width, height, X, Y, name and color. It can also be done more conveniently by defining an object with all the characteristics beforehand.
And with this we can start drawing our tank based on those parameters:
NEWGRAPHIC { NAME:TextoNivel, GROUP:Deposito, TYPE:TEXT, X:0, Y:0, TEXT:"Level: 0% ", COLOR:#000000 }
And so on with everything we want to add to our graphic.
Then we only need to add some methods to manage it:
PROCEDURE deposito.move
MOVE {NAME:Deposito, x:P$PARAM_A, y:P$PARAM_B}
ENDPROCEDURE
With this we will have a graphic… but only one!
That is the problem.
The solution comes from something we mentioned regarding the separator point in the procedure name.
To call this procedure init or locate, we can call it in as many ways as instances we want:
CALL aaa.deposito.init, 20, 130,120, 100,"Carga","#ff0000" CALL bbb.deposito.init, 20, 80,420, 100,"Nivel","#ffff00" CALL ccc.deposito.init, 20, 180,320, 100,"Nivel","#ff00ff" ... CALL aaa.deposito.locate,100,200 CALL bbb.deposito.locate,200,100 CALL ccc.deposito.locate,300,200
What we do is add another separator point to the instance name of that object.
And from here we must introduce the critical point: the value of the context string CX$
Calling a procedure "aaa.deposito.locate" means that:
- The real name of the procedure is
"deposito.locate" - The context (
CX$) is the string"aaa"
And this must be very clear and always kept in mind.
When an entity enters a PROCEDURE whose call is made with separator points, the activity will be executed under that CX$ value.
And this is enough to avoid overwriting variables or resources between instances.
For example:
SAVEVALUE CX$_altura , 100
…will actually create:
SAVEVALUE aaa_altura , 100
Or:
NEWGRAPHIC {NAME:CX$_Deposito, TYPE:GROUP, X:0, Y:0}
…will really be:
NEWGRAPHIC {NAME:aaa_Deposito, TYPE:GROUP, X:0, Y:0}
Therefore, as many SAVEVALUE, ASSIGN, NEWGRAPHIC, NEWRESTROOM elements will be generated as there are ways to call the init of each class.
So we only need to rename our internal ASSIGNs, SAVEVALUEs and NAMEs… prefixing them with "CX$_".
And when we want to access those same parameters, we must do so using parentheses, again avoiding unwanted nesting.
For example:
savevalue CX$_ALTO, 100
…is retrieved with the SNA:
X$(CX$_ALTO)
And with this, we will be able to create and move as many tank instances as we want
All this material is placed in a separate file and included from the main program using include.
Summary of key concepts of this technique:
The dot separation in a CALL of the form aaa.bbb.ccc is interpreted as follows:
- The context (CX$) is "aaa"
- The real name of the executed procedure is bbb.ccc
- CX$ is a key SNA that represents the current call context and can be used to build dynamic names.
- Variables (ASSIGN or SAVEVALUE) are overwritten if they are not inside the context. Always use:
ASSIGN CX$_name, value
…to avoid collisions between instances.
- If the method needs to return a value (like a "get"), you can do:
CALL aaa.bbb.get
…and then obtain the return with:
P$(get)
- All internal SAVEVALUEs should use CX$_ to be private to each instance.
Example:
SAVEVALUE CX$_estado, 1
IMPORTANT: EVs created with SIGNAL or TIMEOUT die when the procedure finishes, and their ASSIGNs disappear.
Only CALL uses a real entity that keeps its variables.
- For consistency and encapsulation, everything that is used from outside a "class" should be accessed through its methods:
CALL instance.object.method,...
…and not by directly accessing the internal names of graphics, variables or resources.
In the example:
There are two files: the main one and a library called ./library_graphics/tank.lib.
You can view the code by opening it from the "OPEN" menu.
The most interesting part is that the library is configured from the main program in PRE_RUN:
assign config,{title:"deposito"
,x:100,y:100
,width:50 ,height:180
,value:88
,"color":"#ff0000"}
call aaa.tank.init,V$config
And then used directly from the program with CALL:
CALL aaa.tank.set, 10
CALL aaa.tank.get
MOVE {NAME:Text1, TEXT:"Current value aaa: P$(get)"}Example
/*
Contexts and modules. Graphic libraries
*/
SYSTEM {TYPE:PRE_RUN,TRIGGER:PRE_RUN}
Graphic {NAME:Text1,Type:TEXT,X:100,Y:319,Text:"Current value"}
Graphic {NAME:Text2,Type:TEXT,X:300,Y:312,Text:"Current value"}
START 100
include ./library_graphics/tank.lib
;-----------------------------------------------------------
; We instantiate two tanks with different parameters
; Each one is generated in its own context: aaa and bbb
; The procedures will be the same, but data and graphics will be independent
PROCEDURE PRE_RUN
assign config,{title:"tank"
,x:100,y:100
,width:50 ,height:180
,value:88
,max_value:100
,"color":"#ff0000"}
call aaa.tank.init,V$config
assign config2,{title:"tank"
,x:300,y:100
,width:50 ,height:180
,value:88
,max_value:100
,"color":"#ff00ff"}
call bbb.tank.init,V$config2
TERMINATE_VE
ENDPROCEDURE
;============================================================
GENERATE 25,0 {NAME:Gen1,x:100,y:400}
CALL aaa.tank.set, 25
CALL bbb.tank.set, 35
ADVANCE 5
CALL aaa.tank.set, 52
CALL bbb.tank.setcolor, "blue"
ADVANCE 5
CALL aaa.tank.get
MOVE {NAME:Text1, TEXT:"Current value aaa: P$(get)"}
CALL bbb.tank.get
MOVE {NAME:Text2, TEXT:"Current value bbb: P$(get)"}
CALL aaa.tank.set, 75
CALL bbb.tank.set, 10
ADVANCE 5
CALL aaa.tank.set, 90
ADVANCE 5
CALL aaa.tank.set, 10
ADVANCE 5
ENDGENERATE 1