Second structural step

Updated: 2025-11-20

Now that you already know how to use procedures and simple conditional decisions (IF), let’s look at a case where an entity chooses between multiple possible routes.

For this, we use the SWITCH structure, very similar to a “decision menu”.

What does this example do?

  • Generates an entity every 10 time units.
  • Assigns it a random number (P$RANDOM_VALUE).
  • Depending on that number, chooses one of four routes (CALL PATH1 … CALL PATH4).
  • In two of those routes (PATH3 and PATH4), a visual block is activated showing “Lock” or “Unlock”, and resources are locked or unlocked. If an entity enters path 3, the use of resources 1, 2, and 3 is locked, although entities already inside them are allowed to finish their work. When an entity takes path 4, those resources become available again immediately. Locking does not expel entities already using the resources, but prevents new ones from seizing them until they are unlocked.

We also define a purely visual aspect: “flow”.
By enabling it in ADVANCE, we will see a route line that initially runs vertically or horizontally depending on the LAYOUT. It may pass through a VIA or VIA2, and can converge or diverge according to MERGE or DECISION.

Example

/* Blocks: SWITCH and LOCK/UNLOCK
Conditional routing and management of lockable resources.
*/

FACILITY {NAME:COUNTER1,X:320,Y:450,CAPACITY:3}
FACILITY {NAME:COUNTER2,X:320,Y:300,CAPACITY:3}
FACILITY {NAME:COUNTER3,X:320,Y:150}
FACILITY {NAME:COUNTER4,X:320,Y:50}

POSITION {NAME:POS1,X:158,Y:301}
POSITION {NAME:POS2,X:497,Y:300}
POSITION {NAME:POS3,X:627,Y:300,TYPE:TERMINATE,TITLE:END}

GRAPHIC {NAME:Text1,Type:TEXT,X:320,Y:104,Text:"Unlock"}

START 30

GENERATE 10,0 {NAME:GEN1,X:57,Y:300}
ADVANCE 20,0 {TO:POS1,flow:1}

ASSIGN RANDOM_VALUE,(RANDOM)

SWITCH P$RANDOM_VALUE
    CASE <,0.4
        CALL PATH1
    ENDCASE

    CASE <,0.8
        CALL PATH2
    ENDCASE

    CASE <,0.92
        CALL PATH3
        MOVE {NAME:Text1,text:"Lock"}
        LOCK COUNTER1
        LOCK COUNTER2
        LOCK COUNTER3
    ENDCASE

    DEFAULT
        CALL PATH4
        MOVE {NAME:Text1,text:"Unlock"}
        UNLOCK COUNTER1
        UNLOCK COUNTER2
        UNLOCK COUNTER3
    ENDCASE
ENDSWITCH

ADVANCE 20,10 {TO:POS2,flow:1,MERGE:"exit"}
ADVANCE 20,0 {TO:POS3,flow:1}

ENDGENERATE 1
;****************************************

PROCEDURE PATH1
    ADVANCE 20  {TO:COUNTER1,flow:1,DECISION:"start"}
    SEIZE COUNTER1
    ADVANCE 45,10
    RELEASE COUNTER1
ENDPROCEDURE

PROCEDURE PATH2
    ADVANCE 20 {TO:COUNTER2,flow:1,DECISION:"start"}
    SEIZE COUNTER2
    ADVANCE 40,10
    RELEASE COUNTER2
ENDPROCEDURE

PROCEDURE PATH3
    ADVANCE 20 {TO:COUNTER3,flow:1,DECISION:"start"}
    SEIZE COUNTER3
    ADVANCE 40,20
    RELEASE COUNTER3
ENDPROCEDURE

PROCEDURE PATH4
    ADVANCE 20 {TO:COUNTER4,flow:1,DECISION:"start"}
    SEIZE COUNTER4
    ADVANCE 10,2
    RELEASE COUNTER4
ENDPROCEDURE

;***************************************************************