A Digital Twin

Updated: 2026-12-02

When working with BRIDGER, we will not always have a real device on the other end. If we want to build a digital twin, we need a data source that represents what is happening in the real world, and often that system does not yet exist or is not available.

In these cases, we can create that “reality” ourselves using a model that behaves as if it were the physical system and generates its own telemetry. We call this simulated world, which acts as the data source, ALFA.

In this example, we are going to connect an ALFA world (the simulated reality) to its digital twin.

The ALFA consists of entities (vehicles) that move between two positions, alternating from one side to the other with a random travel time for each journey. Every time they change state, they send telemetry via MQTT indicating:

  • their identifier
  • their logical position (state)
  • the time at which movement begins
  • and the estimated time until the next state

This information is sufficient to reconstruct their behavior.

The digital twin subscribes to this telemetry and, for each entity:

  • creates it if it does not exist
  • recalculates its movement if it already exists

When it receives new data, it interrupts the current ADVANCE (UPDATE to the current time) and prepares the data so that the new ADVANCE uses the received information. Since the telemetry includes the original timestamp, it is possible to compensate for message latency and adjust the remaining time.

In this way, the twin replicates not only positions but also behavior over time.

Additionally, a rehydration scenario is included: the subscription is stopped for a period of time, so the twin stops receiving data and the entities continue according to their last known state. When the connection is restored, the system synchronizes again.

To run this example, two engines are used in parallel: one acting as ALFA and the other as the digital twin.

 

; ALFA

SYSTEM {TyPE:PRE_RUN,TRIggER:PRE_RUN}
SYSTEM {TYPE:OPTIONS, REAL_TIME:1,time_decimals:1}


Initial options,{ }

BRIDGER {NAME:bridge1,X:746,Y:571
    ,SERVER:"wss://bridger.gpss-plus.com:3000"
    ,CLIENT:"mqtt://localhost"
    ,OPTIONS:V$options
    ,on_error:bridge1_on_error
    }



POSITION {NAME:POS_0,X:238,Y:302}
POSITION {NAME:POS_1,X:717,Y:292}


Graphic {NAME:Text1,Type:TEXT,X:422,Y:533,Text:"On OPEN"}
Graphic {NAME:TextError,Type:TEXT,X:424,Y:582,Text:"On Error"}

START 1

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

PROCEDURE PRE_RUN

	TERMINATE_VE
ENDPROCEDURE

;*****************************************************
GENERATE 2,0,0,3 {NAME:GEN1,X:86,Y:306}
assign State,0
assign id,"Id_D$N"
WHILE (1==1)

    assign State,(P$State+1>1?0:1)
    assign t_delta,round((random * 8) + 2,1)
    assign sys, SYS$
    ASSIGN now,P$(sys.date.time)

    ASSIGN values, {"positions": {
    	id:"P$id"
        ,State:"P$State"
        ,t_start:P$now
        ,t_delta:P$t_delta
        ,txt:"positions"
        }
        }
	BRIDGE_WRITE { name:bridge1, VALUES:V$values,TRIGGER:bridge1_on_write }

    ADVANCE P$t_delta {TO:"POS_P$State"}

ENDWHILE

ADVANCE 1,0 {TO:POS2}
   
ENDGENERATE 0
;----------------------------------------------------------------------------


procedure bridge1_on_error
    move {name:TextError,TEXT:"P$PARAM_A"}
	TERMINATE_VE
endprocedure


procedure bridge1_on_write
    move {name:TextError,TEXT:"P$PARAM_A"}
	TERMINATE_VE
endprocedure


;DIGITAL TWIN

SYSTEM {TyPE:PRE_RUN,TRIggER:PRE_RUN}
SYSTEM {TYPE:OPTIONS, REAL_TIME:1,time_decimals:1}


Initial options,{ }

BRIDGER {NAME:bridge1,X:746,Y:571
    ,SERVER:"wss://bridger.gpss-plus.com:3000"
    ,CLIENT:"mqtt://localhost"
    ,OPTIONS:V$options
    ,on_error:bridge1_on_error
    }


POSITION {NAME:POS_0,X:238,Y:302}
POSITION {NAME:POS_1,X:717,Y:292}

QUEUER {NAME:QTrucks,X:61,Y:506}


Graphic {NAME:Text1,Type:TEXT,X:419,Y:423,Text:"Text1"}
Graphic {NAME:Text2,Type:TEXT,X:421,Y:455,Text:"Text2"}
Graphic {NAME:TextError,Type:TEXT,X:424,Y:582,Text:"OnError"}

START 1

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

PROCEDURE PRE_RUN
    timeout startSubscripcion,1
    timeout cancelSubscripcion,20
    timeout startSubscripcion,50
	TERMINATE_VE

    ASSIGN values, {"State":	{id:"0",txt:"null"}}
    BRIDGE_WRITE { name:bridge1, VALUES:V$values,TRIGGER:bridge1_on_write  }

ENDPROCEDURE

;*****************************************************
GENERATE 0,0,0,0 {NAME:GEN1,X:86,Y:306}
QUEUE QTrucks
assign id,"P$PARAM_A"
assign t_delta,P$PARAM_B
assign State,P$PARAM_C
mod {subtitle:"[P$id]"}

move {name:Text1,TEXT:"AC1$: Create -> P$(PARAM_A) P$t_delta"}

assign t_start,AC1$

while (1==1)
    assign To,P$State
    assign From,(P$To>=1?0:1)

    move {name:Text1,TEXT:"AC1$: Move -> P$(PARAM_A) P$t_delta | P$From P$to"}
	advance (P$t_delta+0.1),0 {from:"POS_P$From", to:"POS_P$To"}
endwhile



DEPART QTrucks
ENDGENERATE 0
;----------------------------------------------------------------------------

procedure on_Subscription
    assign datos,V$(PARAM_A.VALUE)
    if ("P$(datos.txt)"=="null")
        TERMINATE_VE
    endif
    
    assign id,"P$(datos.id)"
    assign t_delta,P$(datos.t_delta)
    assign State,P$(datos.State)
    
    move {name:Text1,TEXT:"AC1$: Position -> P$(PARAM_A.VALUE)"}

    assign idExist,0
    FOREACH entityNumber,IN_RESOURCE,QTrucks
        if ("P$(id,P$entityNumber)"=="P$(id)")
        assign idExist,P$entityNumber
        endif
    ENDFOREACH
 
    if (P$idExist==0)
        NEW GEN1,0,"P$id",P$t_delta,P$State
        TERMINATE_VE
    endif
    
    assign sys, SYS$
    ASSIGN now, P$(sys.date.time) ; Real time
    assign delta_latencia, (P$now - P$(datos.t_start)) / 1000 ; From ms to seconds
    
    
    assign State,P$(State),P$idExist
    assign t_start,P$(datos.t_start),P$idExist
    assign t_delta,P$(datos.t_delta) - P$delta_latencia,P$idExist
    
    update P$idExist,(AC1$) ; go out of advance

	TERMINATE_VE
endprocedure

;--------------------------------------------------------------------------
procedure cancelSubscripcion
    move {name:Text2,TEXT:"Cancel subscription"}
    BRIDGE_UNSUBSCRIBE { 
        NAME: bridge1, 
        VARIABLE: "positions" 
    }
	TERMINATE_VE
endprocedure
;--------------------------------------------------------------------------

procedure startSubscripcion
    move {name:Text2,TEXT:"Start subscription"}
    BRIDGE_SUBSCRIPTION { NAME: bridge1
        , VARIABLE: "positions"
        , TRIGGER: on_Subscription 
        }
	TERMINATE_VE
endprocedure
;--------------------------------------------------------------------------
procedure bridge1_on_error
    move {name:TextError,TEXT:"P$PARAM_A"}
	TERMINATE_VE
endprocedure