Behavior Procedures: Encapsulating Complex Logic

Updated: 2026-01-27

Behavior Procedures allow you to encapsulate both internal entity behavior (processing times) and external events (machine failures, accidents, refueling) into a single modular block.

The Logic of the Example

We simulate a road trip divided into sections. For each section, two distinct behaviors are modeled:

  • Main Behavior (Gaussian): The travel time depends on distance (Km) and weather conditions (Meteo).
  • Secondary Event (Poisson): The probability of needing to refuel depends on the distance traveled. A Poisson lambda of 1/10 means that, on average, 1 out of 10 vehicles will stop at the gas station.

Encapsulation via CALL

By using CALL TRAMO_BEHAVIOR, origin, destination, the model remains clean and readable. The procedure automatically:

  1. Retrieves section data from the SAVEVALUE array.
  2. Calculates if a refueling event occurs using BF$(bTramoGas).
  3. Manages the resources (Gas Stations) and statistics (Queuers) dynamically.
  4. Applies the interpolated travel time using BF$(bTramoTiempo).

This approach is perfect for building large-scale digital twins where specific components (like workshops, road sections, or machines) have complex but repeatable behaviors.

Example

;SYSTEM {TYPE:OPTIONS,Speed:5}

initial tramos, [
	{ origen:1, destino:2, Km:90, Meteo:0.6 },
	{ origen:2, destino:3, Km:100, Meteo:0.8 },
	{ origen:3, destino:4, Km:50, Meteo:1.0 }
]

Graphic {NAME:Text_0,Type:TEXT,X:219,Y:178,Text:"Text_0"}
Graphic {NAME:Text_1,Type:TEXT,X:246,Y:454,Text:"Text_1"}
Graphic {NAME:Text_2,Type:TEXT,X:429,Y:354,Text:"Text_2"}

QUEUER {NAME:TRAMO_0,X:66,Y:350,E_BIN_start:0,E_BIN_SIZE:1,E_BIN_COUNT:180}
QUEUER {NAME:TRAMO_1,X:276,Y:554,E_BIN_start:0,E_BIN_SIZE:1,E_BIN_COUNT:180}
QUEUER {NAME:TRAMO_2,X:664,Y:360,E_BIN_start:0,E_BIN_SIZE:1,E_BIN_COUNT:180}

Facility {NAME:Gas_0,X:215,Y:125,capacity:10,R_BIN_start:0,R_BIN_SIZE:1,R_BIN_COUNT:10}
Facility {NAME:Gas_1,X:245,Y:406,capacity:10,R_BIN_start:0,R_BIN_SIZE:1,R_BIN_COUNT:10}
Facility {NAME:Gas_2,X:434,Y:405,capacity:10,R_BIN_start:0,R_BIN_SIZE:1,R_BIN_COUNT:10}

POSITION {NAME:POS_0,X:115,Y:92}
POSITION {NAME:POS_1,X:146,Y:449}
POSITION {NAME:POS_2,X:432,Y:506}
POSITION {NAME:POS_3,X:756,Y:33}

Function {behavior: bTramoTiempo, type:GAUSS, p0:50, p1:0, Name:gName1, a:1, b:40, Sigma1:3.0, Sigma2:3.0}
Function {behavior: bTramoTiempo, type:GAUSS, p0:100, p1:1, Name:gName4, a:1, b:120, Sigma1:10.0, Sigma2:10.0}

Function {behavior: bTramoGas, type:POISSON, p0:100, Name:fPoisson1, LAMBDA:0.125} ; 1/8
Function {behavior: bTramoGas, type:POISSON, p0:50, Name:fPoisson2, LAMBDA:0.05}   ; 1/20

START 2000

;*****************************************************
PROCEDURE TRAMO_BEHAVIOR
    assign nQueuer, "TRAMO_P$PARAM_A"
    assign nText, "Text_P$PARAM_A"
    assign nGas, "Gas_P$PARAM_A"

    assign Km,X$(tramos.P$PARAM_A.Km)
    assign Meteo,X$(tramos.P$PARAM_A.Meteo)

    ; Determine if refueling is needed based on Poisson behavior
    assign forceGas,BF$(bTramoGas,P$Km) 
    if (P$forceGas>=1)
        move {name:P$nText,text:"REFUELING P$Km / P$Meteo"}
        ADVANCE 10 {TO:P$nGas}
        seize P$nGas
        ADVANCE 30,0
        release P$nGas
        ADVANCE 10 {TO:"POS_P$PARAM_A"}
        move {name:P$nText,text:" "}
    endif

    queue P$nQueuer
    ADVANCE BF$(bTramoTiempo,P$Km,P$Meteo) {TO:"POS_P$PARAM_B"}
    depart P$nQueuer
ENDPROCEDURE 1

;*****************************************************
GENERATE 2,0,0,0 {NAME:GEN1,X:55,Y:41,enumber:0}
    ADVANCE 10,2 {TO:POS_0}
    ; Unified calls: logic is hidden inside the procedure
    CALL TRAMO_BEHAVIOR,0,1
    CALL TRAMO_BEHAVIOR,1,2
    CALL TRAMO_BEHAVIOR,2,3
TERMINATE 1