FSM - Finite State Machine

Updated: 2026-03-07

FSM - Finite State Machine

The FSM block lets you model a Finite State Machine. It is a resource that manages internal states defined by a transition table, triggered by inputs (INPUT).

The behavior is defined with a JSON object with these fields:

  • STATES: List of possible states (optional if EVAL: 1 is used)
  • TRANSITIONS: List of transitions between states
  • INITIAL: Initial state (*only for LOCAL:0)
  • EVAL: (optional) If it is 1, it allows mathematical expressions in TO.

Each transition can contain:

  • FROM: previous state. If it is "" it works as a wildcard (accepts any state).
  • INPUT: input name. "" works as a wildcard.
  • TO: new state (direct value or expression if EVAL:1).
  • TRIGGER: (optional) procedure fired when the transition happens.
; BASIC EXAMPLE
initial logic, { STATES: ["open", "close"],
    TRANSITIONS: [
        {FROM: "open", INPUT: "close", TO: "close", TRIGGER: "doorOpened"},
        {FROM: "close", INPUT: "open", TO: "open"},
        {FROM: "close", INPUT: "switch", TO: "open"},
        {FROM: "open", INPUT: "switch", TO: "close"}
    ],
    INITIAL: "open"
    }

FSM {NAME:FSM1,X:434,Y:540, LOGIC:V$(logic)}


; CYCLIC EXAMPLE
initial logic2, { states: ["one", "two", "three"],
    TRANSITIONS: [
        {FROM: "one", INPUT: "next", TO: "two"},
        {FROM: "two", INPUT: "next", TO: "three"},
        {FROM: "three", INPUT: "next", TO: "one", TRIGGER: "reset"}
    ],
    INITIAL: "open"
    }
FSM {NAME:FSM2,X:134,Y:140, LOGIC:V$(logic2)}


; EVALUATED LOGIC EXAMPLE
; In this mode (eval: 1), states and transitions can be evaluated as dynamic mathematical expressions.

initial logicTemp3, {
  TRANSITIONS: [
    {FROM: "", INPUT: "up", TO: "(X + 1 <= 24 ? X + 1.5 : X)"},
    {FROM: "", INPUT: "down", TO: "(X - 1 >= 18 ? X - 1.5 : X)"},
    {FROM: "", INPUT: "reset", TO: "21"}
  ],
  EVAL: 1,
  INITIAL: 21
}

FSM {NAME:Stater3, X:434, Y:540, LOGIC:V$(logicTemp3)}


; EVALUATED LOGIC EXAMPLE WITH EXTRA PARAMETER Y
; YOU CAN ADD PARAMETERS Y, Z TO THE FUNCTIONS

initial logicTemp4, {
  TRANSITIONS: [
    {FROM: "", INPUT: "up", TO: "(X + 1 <= 24 ? X + Y : X)"},
    {FROM: "", INPUT: "down", TO: "(X - 1 >= 18 ? X - Y : X)"},
    {FROM: "", INPUT: "reset", TO: "21"}
  ],
  EVAL: 1,
  INITIAL: 21
}

FSM {NAME:Stater4, X:434, Y:540, LOGIC:V$(logicTemp4)}

Associated blocks:

  • FSM resourceName,input[, paramY [,paramZ]] ; Executes a state transition according to the defined logic.
FSM Fsm1,"OPEN" ; moves to state "CLOSE"
FSM Fsm1,"SWITCH" ; if it was "OPEN" it goes to "CLOSE" and vice versa.

FSM Fsm2,"next" ; if it was "TWO" it goes to "THREE", ...

FSM Fsm4,"up",10 ; increments the state by 10

SNA:

FSM is the only resource that offers an atomic read-and-update operation for the state.

  • R$(staterName,STATE) Returns the current state.
  • R$(staterName,IN_AFTER,input) Returns the current state and applies the input in an ATOMIC OPERATION.
  • R$(staterName,IN_BEFORE,input) Applies the input and returns the current state in an ATOMIC OPERATION.
  • R$(staterName,IN_BEFORE,input,Y,Z) Applies the input with Y,Z parameters for EVAL:1 FSM and returns the current state in an ATOMIC OPERATION.

This lets you model critical sections, traffic lights, and synchronization between processes.

Typical uses

  • Critical-section control
  • Semaphores
  • Mutual exclusion
  • Process synchronization
  • Stateful math functions with parameters
waituntil Conditions1,("R$(Fsm1,IN_AFTER,close)"=="open") ; one entity passes and blocks the next ones
...
FSM Fsm1,"open" ; ends the mutual-exclusion zone
waitcheck Conditions1 ; lets the next one pass

And as a store of mathematical functions with two extra input parameters:

{ FROM: "", INPUT: "incr", TO: "(X + Y * Z)" } ; X: current state, Y,Z: extra input parameters

Available events: TRIGGER

Each transition can include a trigger that will call a PROCEDURE:

PROCEDURE doorOpened
    move {name:text7,text:"Entity P$ENTITYNUMBER has opened the door"}
    TERMINATE_VE
endprocedure

Storage modes:

Default mode is global (one state per STATER), but if you set:

LOCAL:1

Then it becomes one state per entity. Great as a path discriminator that avoids nested SWITCH.

Since it is not assigned initially, it can be set through the associated assign directly:

assign Fsm1,10

 

Example

POSITION {NAME:Exit,X:648,Y:383}


initial posY,500
initial logic, { STATES: ["open", "close"],
    TRANSITIONS: [
        {FROM: "open", INPUT: "close", TO: "close", TRIGGER: "doorOpened"},
        {FROM: "close", INPUT: "open", TO: "open"},
        {FROM: "close", INPUT: "switch", TO: "open"},
        {FROM: "open", INPUT: "switch", TO: "close"}
    ],
    INITIAL: "open"
    }

FSM {NAME:FSM1,X:434,Y:540, LOGIC:V$(logic)}




Graphic {NAME:Text1,Type:TEXT,X:324,Y:X$posY - 0}
Graphic {NAME:Text2,Type:TEXT,X:324,Y:X$posY - 20}
Graphic {NAME:Text3,Type:TEXT,X:324,Y:X$posY - 40}
Graphic {NAME:Text4,Type:TEXT,X:324,Y:X$posY - 60}
Graphic {NAME:Text5,Type:TEXT,X:324,Y:X$posY - 80}
Graphic {NAME:Text6,Type:TEXT,X:324,Y:X$posY - 100}
Graphic {NAME:Text7,Type:TEXT,X:324,Y:X$posY - 120}
Graphic {NAME:Text8,Type:TEXT,X:324,Y:X$posY - 140}

Conditions {NAME:Conditions1,X:317,Y:539,
    expression:("R$(FSM1,IN_AFTER,close)"=="open"),
    ON_ATTEMPT:Conditions1_ATTEMPT,
    ON_CHECK:Conditions1_CHECK,
    ON_QUEUE:Conditions1_QUEUE
    }


START 100

;-------------------------------

GENERATE 20,0 {NAME:GEN1,X:62,Y:396}
    advance 10 {to:Conditions1}
    waituntil Conditions1
    advance 15,25 {to:Exit}
    STATE FSM1,"open"
    waitcheck Conditions1
terminate 1
;-------------------------------
PROCEDURE Conditions1_QUEUE
	move {name:Text3,text:"AC1$ I am the VE [D$N] from ON_QUEUE, entity P$ENTITYNUMBER"}
    TERMINATE_VE
endprocedure 1
PROCEDURE Conditions1_CHECK
	move {name:Text4,text:"AC1$ I am the VE [D$N] from ON_CHECK, entity P$ENTITYNUMBER"}
    TERMINATE_VE
endprocedure 1
PROCEDURE Conditions1_ATTEMPT
	move {name:Text5,text:"AC1$ I am the VE [D$N] from ON_ATTEMPT, entity P$ENTITYNUMBER"}
    TERMINATE_VE
endprocedure 1
;-------------------------------
PROCEDURE doorOpened
	move {name:Text7,text:"Entity P$ENTITYNUMBER has opened the door"}
    TERMINATE_VE
endprocedure 1
;-------------------------------