Finite state machine: Difference between revisions

Include the state in the prompt
m (→‎REXX version 2: remove a line that was left in the pgm)
(Include the state in the prompt)
 
(48 intermediate revisions by 26 users not shown)
Line 32:
*[https://www.youtube.com/watch?v=vhiiia1_hC4 Computers Without Memory (Finite State Automata)], A Computerphile Video.
<br/><br/>
 
=={{header|11l}}==
{{trans|Python}}
 
<syntaxhighlight lang="11l">V states = [‘ready’ =
(‘Machine ready: (d)eposit, or (q)uit?’,
[String(‘d’), ‘q’]),
‘waiting’ =
(‘Machine waiting: (s)elect, or (r)efund?’,
[String(‘s’), ‘r’]),
‘dispense’ =
(‘Machine dispensing: please (r)emove product’,
[String(‘r’)]),
‘refunding’ =
(‘Refunding money’,
[String]())
]
V transitions = [‘ready’ =
[String(‘d’) = ‘waiting’,
String(‘q’) = ‘exit’],
‘waiting’ =
[String(‘s’) = ‘dispense’,
String(‘r’) = ‘refunding’],
‘dispense’ =
[String(‘r’) = ‘ready’],
‘refunding’ =
[‘’ = ‘ready’]]
 
F Acceptor(prompt, valids)
I valids.empty
print(prompt)
R ‘’
E
L
V resp = input(prompt)[0].lowercase()
I resp C valids
R String(resp)
 
F finite_state_machine(initial_state, exit_state)
V next_state = initial_state
V current_state = :states[next_state]
L
V response = Acceptor(current_state[0], current_state[1])
I response == exit_state
L.break
next_state = :transitions[next_state][response]
current_state = :states[next_state]
 
finite_state_machine(‘ready’, ‘q’)</syntaxhighlight>
 
{{out}}
<pre>
Machine ready: (d)eposit, or (q)uit?d
Machine waiting: (s)elect, or (r)efund?s
Machine dispensing: please (r)emove productr
Machine ready: (d)eposit, or (q)uit?d
Machine waiting: (s)elect, or (r)efund?r
Refunding money
Machine ready: (d)eposit, or (q)uit?q
</pre>
 
=={{header|ALGOL 68}}==
<syntaxhighlight lang="algol68">
BEGIN # finite state machine #
 
# mode representing a state in the FSM #
MODE FSMSTATE = STRUCT( INT state # code for the state #
, PROC INT next state # routine to change state #
);
# executes the FSM defined by states, starting from the initial state #
# and terminating when the exit state is reached #
PROC run fsm = ( []FSMSTATE states, INT initial state, exit state )VOID:
BEGIN
INT state := initial state;
WHILE state /= exit state DO
BOOL found := FALSE;
FOR s pos FROM LWB states TO UPB states WHILE NOT found DO
IF found := state OF states[ s pos ] = state THEN
state := next state OF states[ s pos ]
FI
OD;
IF NOT found THEN
# in an invalid state - restart #
print( ( "(resetting)", newline ) );
state := initial state
FI
OD
END # run fsm # ;
 
BEGIN # test FSM #
# possible states #
INT exit = 0, ready = 1, waiting = 2, dispense = 3, refunding = 4;
# prompts the user for a single character code and returns it #
# the user is re-prompted until they enter one of the characters in #
# answers #
PROC get code = ( STRING prompt, answers )CHAR:
BEGIN
CHAR response;
WHILE print( ( prompt, ": " ) );
STRING answer;
read( ( answer, newline ) );
response := IF answer = "" THEN REPR 0 ELSE answer[ LWB answer ] FI;
IF response >= "a" AND response <= "z" THEN
# convert lowercase response to upper #
response := REPR ( ABS response + ( ABS "A" - ABS "a" ) )
FI;
NOT char in string( response, NIL, answers )
DO SKIP OD;
response
END # get code # ;
 
run fsm( ( ( ready
, INT: IF "Q" = get code( "Ready : Enter D to deposit, Q to Quit", "DQ" )
THEN exit
ELSE waiting
FI
)
, ( waiting
, INT: IF "S" = get code( "Waiting : Enter S to Select, R to Refund", "SR" )
THEN dispense
ELSE refunding
FI
)
, ( dispense
, INT: BEGIN get code( "Dispensing: Remove your product and Enter R", "R" );
ready
END
)
, ( refunding
, INT: BEGIN print( ( "Refunding", newline ) ); ready END
)
)
, ready
, exit
)
 
END
 
END
</syntaxhighlight>
{{out}}
<pre>
Ready : Enter D to deposit, Q to Quit: d
Waiting : Enter S to Select, R to Refund: s
Dispensing: Remove your product and Enter R: r
Ready : Enter D to deposit, Q to Quit: d
Waiting : Enter S to Select, R to Refund: r
Refunding
Ready : Enter D to deposit, Q to Quit: q
</pre>
 
=={{header|BASIC}}==
==={{header|Commodore BASIC}}===
<syntaxhighlight lang="basic">
10 REM FINITE STATE MACHINE
20 LET MS=1: REM MACHINE STATE
30 REM 1=READY, 2=WAITING, 3=DISPENSE, 4=REFUND, 5=QUIT
40 :
50 REM MAIN LOOP
60 ON MS GOSUB 1000,2000,3000,4000,5000
70 GOTO 50
80:
1000 REM READY
1010 PRINT "MACHINE IS READY"
1020 PRINT "PRESS D-ISPENSE OR Q-UIT"
1030 INPUT KP$
1040 IF KP$ = "D" THEN MS=2: GOTO 1070
1050 IF KP$ = "Q" THEN MS=5: GOTO 1070
1060 GOTO 1030
1070 RETURN
1080 :
2000 REM WAITING
2010 PRINT "MACHINE IS WAITING"
2020 PRINT "PRESS S-ELECT OR R-EFUND"
2030 INPUT KP$
2040 IF KP$ = "S" THEN MS=3: GOTO 2070
2050 IF KP$ = "R" THEN MS=4: GOTO 2070
2060 GOTO 2030
2070 RETURN
2080 :
3000 REM DISPENSE
3010 PRINT "MACHINE DISPENSE"
3020 PRINT "PRESS C-OLLECTED PRODUCT."
3030 INPUT KP$
3040 IF KP$ = "C" THEN MS=1: GOTO 3060
3050 GOTO 3030
3060 RETURN
3070 :
4000 REM REFUND
4010 PRINT "MACHINE IS REFUND"
4020 PRINT "PRESS C-OLLECTED REFUND."
4030 INPUT KP$
4040 IF KP$ = "C" THEN MS=1: GOTO 4060
4050 GOTO 430
4060 RETURN
4070 :
5000 REM QUIT
5010 PRINT "MACHINE IS SHUTDOWN"
5020 END
</syntaxhighlight>
==={{header|Sinclair ZX81 BASIC}}===
Works with 1k of RAM.
Line 45 ⟶ 244:
Note that the program uses no variables and makes no use of the return stack: all the state is expressed in the (so to speak) state.
 
<langsyntaxhighlight lang="basic"> 10 PRINT "PRESS D(EPOSIT) OR Q(UIT)"
20 IF INKEY$="D" THEN GOTO 50
30 IF INKEY$="Q" THEN STOP
Line 59 ⟶ 258:
130 GOTO 120
140 PRINT "REFUNDED"
150 GOTO 10</langsyntaxhighlight>
{{out}}
It will be seen that the user has pressed, in order, <tt>D</tt>, <tt>R</tt>, <tt>D</tt>, <tt>S</tt>, <tt>R</tt>, and <tt>Q</tt>.
Line 72 ⟶ 271:
 
=={{header|C}}==
Here is a manually-constructed table-driven finite state machine that is fairly general and could be adapted to different applications.
This is an unapologetic implementation of goto. There have been a lot of curse words and obituaries written about it and the inventors of Java were glad to exclude it from the language, but to be fair, goto is one of the many things C inherited from languages such as Assembly or BASIC that make it truly awesome, especially when it comes to such requirements. After all, can there be a clearer and simpler implementation of a Finite State Machine (not counting BASIC ) ?
<syntaxhighlight lang="c">
<lang C>
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
 
int main(int argc, char **argv)
{
typedef enum State { READY, WAITING, REFUND, DISPENSE, COLLECT, QUIT } State;
char str[10];
 
typedef struct statechange {
ready: do{
const int in;
printf("\nMachine is READY. (D)eposit or (Q)uit :");
const State out;
scanf("%s",str);
} statechange;
}while(!(str[0]!='D'||str[0]!='d'||str[0]!='q'||str[0]!='Q'));
 
#define MAXINPUTS 3
if(str[0]=='q'||str[0]=='Q')
typedef struct FSM {
goto quit;
const State state;
goto waiting;
void (*Action)(void);
const statechange table[MAXINPUTS]; // would be nice if could be [] ...
waiting: do{
} FSM;
printf("(S)elect product or choose to (R)efund :");
 
scanf("%s",str);
char str[10];
}while(!(str[0]!='s'||str[0]!='S'||str[0]!='r'||str[0]!='R'));
void Ready(void) { fprintf(stderr, "\nMachine is READY. (D)eposit or (Q)uit :"); scanf("%s", str); }
void Waiting(void) { fprintf(stderr, "(S)elect product or choose to (R)efund :"); scanf("%s", str); }
if(str[0]=='s'||str[0]=='S'){
void Refund(void) { fprintf(stderr, "Please collect refund.\n"); }
printf("Dispensing product...");
void Dispense(void) { fprintf(stderr, "Dispensing product...\n"); }
goto dispense;
void Collect(void) { fprintf(stderr, "Please (C)ollect product. :"); scanf("%s", str); }
}
void Quit(void) { fprintf(stderr, "Thank you, shutting down now.\n"); exit(0); }
 
else{
const FSM fsm[] = {
printf("Please collect refund.");
{ READY, &Ready, {{'D', WAITING}, {'Q', QUIT }, {-1, READY} }},
goto ready;
{ WAITING, &Waiting, {{'S', DISPENSE}, {'R', REFUND}, {-1, WAITING} }},
}
{ REFUND, &Refund, {{ -1, READY} }},
{ DISPENSE, &Dispense, {{ -1, COLLECT} }},
dispense: do{
{ COLLECT, &Collect, {{'C', READY}, { -1, COLLECT } }},
printf("\nPlease (C)ollect product. :");
{ QUIT, &Quit, {{ -1, QUIT} }},
scanf("%s",str);
};
}while(!(str[0]!='c'||str[0]!='C'));
 
int each;
goto ready;
State state = READY;
 
quit: printf("Thank you, shutting down now.");
for (;;) {
fsm[state].Action();
return 0;
each = 0;
while (!( ((fsm[state].table[each].in == -1)
// -1 comes last and is catchall: exit, or loop to self, on no valid input.
|| (isalpha(str[0]) && fsm[state].table[each].in == toupper(str[0]) )))) each++;
state = fsm[state].table[each].out;
}
return 0;
}
</syntaxhighlight>
</lang>
Machine simulation :
<pre>
Line 133 ⟶ 342:
 
=={{header|C++}}==
<syntaxhighlight lang="c">
<lang C>
#include <map>
Line 298 ⟶ 507:
}
}
</syntaxhighlight>
</lang>
 
{{out}}
Line 321 ⟶ 530:
[ready] Enter the next transition (deposit, quit):
> quit
</pre>
=={{header|D}}==
{{trans|Kotlin}}
<syntaxhighlight lang="d">import std.conv;
import std.range;
import std.stdio;
import std.string;
 
enum State {
READY,
WAITING,
EXIT,
DISPENSE,
REFUNDING,
}
 
void fsm() {
writeln("PLease enter your option when prompted");
writeln("(any characters after the first will be ignored)");
auto state = State.READY;
string trans;
 
while (true) {
final switch (state) {
case State.READY:
do {
write("(D)ispense or (Q)uit : ");
trans = readln().toLower.take(1).to!string;
} while (trans != "d" && trans != "q");
if (trans == "d") {
state = State.WAITING;
} else {
state = State.EXIT;
}
break;
case State.WAITING:
writeln("OK, put your money in the slot");
do {
write("(S)elect product or choose a (R)efund : ");
trans = readln().toLower.take(1).to!string;
} while (trans != "s" && trans != "r");
if (trans == "s") {
state = State.DISPENSE;
} else {
state = State.REFUNDING;
}
break;
case State.DISPENSE:
do {
write("(R)emove product : ");
trans = readln().toLower.take(1).to!string;
} while (trans != "r");
state = State.READY;
break;
case State.REFUNDING:
writeln("OK, refunding your money");
state = State.READY;
break;
case State.EXIT:
writeln("OK, quitting");
return;
}
}
}
 
void main() {
fsm();
}</syntaxhighlight>
 
=={{header|Delphi}}==
{{Trans|Go}}
<syntaxhighlight lang="delphi">
program Finite_state_machine;
 
{$APPTYPE CONSOLE}
 
type
TState = (stReady, stWaiting, stDispense, stRefunding, stExit);
 
var
state: TState = stReady;
 
procedure fsm();
var
line: string;
option: char;
begin
Writeln('Please enter your option when prompted');
Writeln('(any characters after the first will be ignored)'#10);
state := stReady;
repeat
case state of
stReady:
begin
Writeln('(D)ispense or (Q)uit : ');
Readln(line);
if line = '' then
Continue;
option := UpCase(line[1]);
case option of
'D':
state := stWaiting;
'Q':
state := stExit;
end;
end;
stWaiting:
begin
Writeln('OK, put your money in the slot');
while state = stWaiting do
begin
Writeln('(S)elect product or choose a (R)efund : ');
Readln(line);
if line = '' then
Continue;
option := UpCase(line[1]);
case option of
'S':
state := stDispense;
'R':
state := stRefunding;
end;
end;
end;
 
stDispense:
begin
while state = stDispense do
begin
Writeln('(R)emove product : '#10);
Readln(line);
if line = '' then
Continue;
option := UpCase(line[1]);
case option of
'R':
state := stReady;
end;
end;
end;
stRefunding:
begin
Writeln('OK, refunding your money');
state := stReady;
end;
stExit:
begin
Writeln('OK, quitting');
state := stExit;
end;
end;
until state = stExit;
end;
 
begin
fsm;
end.</syntaxhighlight>
 
 
=={{header|FreeBASIC}}==
{{trans|Phix}}
<syntaxhighlight lang="freebasic">Enum states
READY
WAITING
DISPENSE
REFUND
QUIT
End Enum '-- (or just use strings if you prefer)
 
Dim As states state = READY
Dim As String KBD = " "
Do
Print KBD
Select Case state
Case READY
Print "Machine is READY. (D)eposit or (Q)uit : ";
Do
Do: KBD = Ucase(Inkey): Loop While KBD = ""
If KBD = "D" Then state = WAITING : Exit Do
If KBD = "Q" Then state = QUIT : Exit Do
Loop
Case WAITING
Print "(S)elect product or choose to (R)efund : ";
Do
Do: KBD = Ucase(Inkey): Loop While KBD = ""
If KBD = "S" Then state = DISPENSE : Exit Do
If KBD = "R" Then state = REFUND : Exit Do
Loop
Case DISPENSE
Print "Dispensing product... ";
Print "Please (C)ollect product. : ";
Do
Do: KBD = Ucase(Inkey): Loop While KBD = ""
If KBD = "C" Then state = READY : Exit Do
Loop
Case REFUND
Print "Please collect refund."
state = READY
KBD = " "
Case QUIT
Print !"Thank you, shuttingwn now.\n"
Exit Do
End Select
Loop
Sleep</syntaxhighlight>
{{out}}
<pre>
Igual que la entrada de Phix.
</pre>
 
=={{header|Go}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 424 ⟶ 845:
func main() {
fsm()
}</langsyntaxhighlight>
 
{{out}}
Line 445 ⟶ 866:
OK, quitting
</pre>
 
=={{header|Groovy}}==
{{trans|Java}}
<syntaxhighlight lang="groovy">class FiniteStateMachine {
private enum State {
Ready(true, "Deposit", "Quit"),
Waiting(true, "Select", "Refund"),
Dispensing(true, "Remove"),
Refunding(false, "Refunding"),
Exiting(false, "Quiting");
 
State(boolean exp, String... input) {
inputs = Arrays.asList(input);
explicit = exp
}
 
State nextState(String input, State current) {
if (inputs.contains(input)) {
return map.getOrDefault(input, current)
}
return current
}
 
final List<String> inputs
final static Map<String, State> map = new HashMap<>()
final boolean explicit
 
static {
map.put("Deposit", Waiting)
map.put("Quit", Exiting)
map.put("Select", Dispensing)
map.put("Refund", Refunding)
map.put("Remove", Ready)
map.put("Refunding", Ready)
}
}
 
static void main(String[] args) {
Scanner sc = new Scanner(System.in)
State state = State.Ready
 
while (state != State.Exiting) {
println(state.inputs)
if (state.explicit){
print("> ")
state = state.nextState(sc.nextLine().trim(), state)
} else {
state = state.nextState(state.inputs.get(0), state)
}
}
}
}</syntaxhighlight>
 
=={{header|Haskell}}==
<syntaxhighlight lang="haskell">import System.Exit
import Data.Maybe
import Control.Monad
import Data.List
import System.IO
 
type Name = String
type Sequence = String
type State = String
 
data Trigger = Trigger { name :: Name
, tseq :: Sequence } deriving (Eq)
 
instance Show Trigger where
show (Trigger name tseq) = name ++ "(" ++ tseq ++ ")"
 
data Transition = Implicit { start :: State
, end :: State }
| Explicit { start :: State
, trigger :: Trigger
, end :: State }
 
findEndState :: Sequence -> [(Trigger, State)] -> Maybe State
findEndState sequence lst = if (isJust pair)
then snd <$> pair
else Nothing
where
pair = find (\t -> (tseq . fst) t == sequence) lst
 
findRelevantTransitions :: State -> [Transition] -> [Transition]
findRelevantTransitions state transitions = filter (\t -> state == start t) transitions
 
findImplicitTransition :: [Transition] -> Maybe Transition
findImplicitTransition [] = Nothing
findImplicitTransition (transition@(Implicit _ _):xs) = Just transition
findImplicitTransition (x:xs) = findImplicitTransition xs
 
runFSM :: State -> [Transition] -> [State] -> IO ()
runFSM state transitions finishStates = do
putStrLn $ "State: " ++ state
when (state `elem` finishStates) $ do
putStrLn "Exiting.."
exitWith ExitSuccess
let relTransitions = findRelevantTransitions state transitions
let implTransition = findImplicitTransition relTransitions
when (isJust implTransition) $ do
putStrLn "Implicit transition"
runFSM (end $ fromJust implTransition) transitions finishStates
let triggers = map (\t -> (trigger t, end t)) relTransitions
handleExplicitTransition triggers
where handleExplicitTransition triggers = do
let prompt = (intercalate " or " (map (show . fst) triggers)) ++ ":"
putStr prompt
resp <- getLine
let endState = findEndState resp triggers
case endState of
(Just e) -> runFSM e transitions finishStates
Nothing -> putStrLn "invalid input" >> handleExplicitTransition triggers
 
main = do
hSetBuffering stdout $ BlockBuffering $ Just 1
runFSM initialState transitions finishStates
 
initialState = "Ready"
transitions = [ Explicit "Ready" (Trigger "Deposit" "d") "Waiting"
, Explicit "Ready" (Trigger "Quit" "q") "Exit"
, Explicit "Waiting" (Trigger "Select" "s") "Dispense"
, Explicit "Waiting" (Trigger "Refund" "r") "Refunding"
, Explicit "Dispense" (Trigger "Remove" "rm") "Ready"
, Implicit "Refunding" "Ready" ]
finishStates = ["Exit"]
</syntaxhighlight>
 
=={{header|J}}==
 
This seems to be what the current draft task asks for:
 
<syntaxhighlight lang="text">NB. FSM builder:
explicit=: {{
states=: ~. states,x;y
transitions=: ~. transitions,<m
FSM=: y S (<x S, m T)} (states ,&# transitions){.!._ FSM
EMPTY
}}
implicit=: ''explicit
start=: {{ '' implicit y [current=: 0 [transitions=: states=: <,FSM=: EMPTY }}
 
NB. FSM utilities
S=: state=: {{ states i.<m }}
T=: transition=: {{transitions i.<m }}
N=: next=: {{
try. 1: current=: ([ {&states) current next y catch. 0 end.
:
(<x, y transition) { FSM
}}
Snm=: statename=: {{ ;:inv m{states }}
Tnm=: transitionname=: {{ ;:inv m{transitions }}
implicits=: {{ r=.'' while. next '' do. r=.r, current end. }}</syntaxhighlight>
 
With the above implementation, the task example would look like:
 
<syntaxhighlight lang="j">NB. task example FSM:
start 'ready'
'ready' 'deposit'explicit 'waiting'
'ready' 'quit'explicit 'exit'
'waiting' 'select'explicit 'dispense'
'waiting' 'refund'explicit 'refunding'
'dispense' 'remove'explicit 'ready'
'refunding' implicit 'ready'
 
example=: {{
current=: 0
machine 'deposit'
machine 'select'
machine 'remove'
machine 'deposit'
machine 'refund'
machine 'quit'
echo 'final state: ',current statename
}}
 
machine=: {{
echo 'state: ',current statename
echo 'transition: ',y
next y
i=. implicits ''
if. #i do.
echo 'implicit transition to: ',i statename
end.
}}</syntaxhighlight>
 
More advanced examples might put the FSM in a locale (allowing for multiple, independent FSMs), add callbacks and/or parameterization on transitions, or maybe include hardware specific code.
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">import java.util.*;
 
public class FiniteStateMachine {
Line 498 ⟶ 1,105:
}
}
}</langsyntaxhighlight>
<pre>[Deposit, Quit]
> Deposit
Line 514 ⟶ 1,121:
[Deposit, Quit]
> Quit</pre>
 
=={{header|JavaScript}}==
===On browser using blocking window methods===
<syntaxhighlight lang="javascript">//States
var states = [{
'name': 'Ready',
'initial': true,
'events': {
'Deposit': 'Waiting',
'Quit': 'Exiting',
}
}, {
'name': 'Waiting',
'events': {
'Select': 'Dispensing',
'Refund': 'Refunding'
}
}, {
'name': 'Dispensing',
'events': {
'Remove': 'Ready'
}
}, {
'name': 'Refunding',
'events': {
getReady: 'Ready'
}
}, {
'name': 'Exiting',
'events': {}
}];
 
function StateMachine(states) {
this.states = states;
this.indexes = {};
for (var i = 0; i < this.states.length; i++) {
this.indexes[this.states[i].name] = i;
if (this.states[i].initial) {
this.currentState = this.states[i];
}
}
};
StateMachine.prototype.consumeEvent = function(e) {
if (this.currentState.events[e]) {
this.currentState = this.states[this.indexes[this.currentState.events[e]]];
}
}
StateMachine.prototype.getStatus = function() {
return this.currentState.name;
}
var fsm = new StateMachine(states);
var s, currentButtons, answer;
while ((s = fsm.getStatus()) !== "Exiting") {
switch (s) {
case "Refunding":
window.alert('Refunding');
fsm.consumeEvent("getReady")
break;
case "Dispensing":
case "Waiting":
case "Ready":
currentButtons = Object.keys(fsm.states[fsm.indexes[s]].events)
answer = window.prompt(currentButtons.join(' ') + '?');
answer = currentButtons.find(function(key) {
return key.match(new RegExp('^' + answer, 'i'))
});
if (answer) {
fsm.consumeEvent(answer);
}
}
}
</syntaxhighlight>
 
=={{header|jq}}==
{{works with|jq}}
 
'''Also works with gojq and fq''' provided the line defining
keys_unsorted is uncommented.
 
In this entry, we adopt an approach which emphasizes
separating code from data: we define a format for
representing state-transition tables as JSON objects,
which can be stored for example in separate files;
and we illustrate one possible FSM engine for
animating such state-transition tables.
 
The format of the JSON object specifying a state-transition table is
as follows, it being assumed that each "state" has a distinct
description as a JSON string:
 
* each top-level key represents a state of the FSM, with "exit" meaning stop;
* the value of each top-level key is another JSON object, which we will refer to as the trigger dictionary;
* each trigger dictionary consists of one or more key-value pairs, in which the "key" is the name of a trigger and the value is the name of a state.
 
Triggers can be of three kinds:
1. external (corresponding to external inputs)
2. automatic (corresponding to determinate internal transitions)
3. indeterminate (corresponding to non-determinism)
 
For external transitions, the keys should be non-empty strings that do not match "^[0-9]+ ".
For automatic transitions, the trigger object should have "" as its only key.
For indeterminate transitions, the trigger object should have keys matching the regex "^[0-9]+ "
 
The FSM engine presented here is intended to allow a person to provide
the "external inputs" as well as to pace automatic transitions and
to simulate the indeterminate transitions. In general, a menu of valid
input choices is presented, and the first match of the response with
these options determines the state transition. In addition, "?" as a
user input is recognized as a request for help.
 
'''fsm.json'''
<syntaxhighlight lang=jq>
{
"ready": {
"deposit": "waiting",
"quit": "exit"
},
"waiting": {
"select": "dispense",
"refund": "refunding"
},
"dispense": {
"confirm": "confirming",
"refund": "refunding"
},
"refunding": {
"1 REFUND MONEY": "ready",
"2 SORRY": "ready"
},
"confirming": {
"": "ready"
}
}
</syntaxhighlight>
'''fsm.jq'''
<syntaxhighlight lang=jq>
# Uncomment the following line if using gojq or fq:
# def keys_unsorted: keys;
 
# next($action) determines the next state.
# Global: $fsm (the transition-table)
# $action specifies an action, which can be abbreviated: the first possible match is selected.
# Input: {state}
def next($action):
($fsm[.state] | keys_unsorted) as $keys
| if ($action|length) == 0
then if $keys|index("") then fsm[.state][""]
else null
end
else (first($keys[] | select( startswith($action) )) // null) as $k
| if $k then fsm[.state][$k] else null end
end;
 
def start: {"state": "ready"};
 
# The FSM engine - progress from state to state based on user input
def progress:
def options: fsm[.state]|keys_unsorted;
def prompt:
options
| if length == 1 and .[0]=="" then "Enter anything to proceed."
elif .[0]|test("^[0-9]+ ") then "options: \(.) (simulated non-deterministic transition)"
else "options: \(.)"
end;
 
def help:
options
| if length == 1 and .[0]=="" then "(internal state transition awaiting your input)"
elif .[0]|startswith("1 ") then "(simulated NDFSM awaiting your input in the form of an initial substring): \(.)"
else
"Make a selection by typing an initial substring of the option you wish to select: \(.)"
end;
 
start
| label $out
| "Initial state: \(.state)\nMake your selection (at least one letter) from these options: \(options))",
foreach inputs as $in (.;
.previous=.state
| .error = null
| if $in == "?" then .error = true #
else next($in) as $next
| if $next then .state=$next else .error = "try again or enter ? for help" end
end;
if .error == true then help
elif .error then .error
elif .state == "exit" then break $out
else
"\(.previous) + \($in) => \(.state)",
prompt
end
) ;
progress
</syntaxhighlight>
'''Illustrative Transcript''':
<pre>
$ jq -nRr --argfile fsm fsm.json -f fsm.jq
Initial state: ready
Make your selection (at least one letter) from these options: ["deposit","quit"])
?
Make a selection by typing an initial substring of the option you wish to select: ["deposit","quit"]
d
ready + d => waiting
options: ["refund","select"]
s
waiting + s => dispense
options: ["confirm","refund"]
c
dispense + c => confirming
Enter anything to proceed.
 
confirming + => ready
options: ["deposit","quit"]
d
ready + d => waiting
options: ["refund","select"]
r
waiting + r => refunding
options: ["1 REFUND MONEY","2 SORRY"] (simulated non-deterministic transition)
1
refunding + 1 => ready
options: ["deposit","quit"]
q
</pre>
 
=={{header|Julia}}==
<syntaxhighlight lang="julia">abstract type State end
 
struct Ready <: State
transitiontable::Dict
implicit::Union{State, Nothing}
prompt::String
end
 
struct Waiting <: State
transitiontable::Dict
implicit::Union{State, Nothing}
prompt::String
end
 
struct Dispense <: State
transitiontable::Dict
implicit::Union{State, Nothing}
prompt::String
end
 
struct Refunding <: State
transitiontable::Dict
implicit::Union{State, Nothing}
prompt::String
end
 
struct Exit <: State
transitiontable::Dict
implicit::Union{State, Nothing}
prompt::String
end
 
Ready() = Ready(Dict("deposit" => Waiting, "quit" => Exit), nothing, "Vending machine is ready.")
Waiting() = Waiting(Dict("select" => Dispense, "refund" => Refunding), nothing, "Waiting with funds.")
Dispense() = Dispense(Dict("remove" => Ready), nothing, "Thank you! Product dispensed.")
Refunding() = Refunding(Dict(), Ready(), "Please take refund.")
Exit() = Exit(Dict(), nothing, "Halting.")
 
makeinstance(Ready) = Ready()
makeinstance(Waiting) = Waiting()
makeinstance(Dispense) = Dispense()
makeinstance(Refunding) = Refunding()
makeinstance(Exit) = Exit()
 
function queryprompt(query, typ)
print(query, ": ")
entry = uppercase(strip(readline(stdin)))
return (typ <: Integer) ? parse(Int, entry) :
(typ <: Vector) ? map(x -> parse(Int, x), split(entry, r"\s+")) :
entry
end
 
function promptinput(state)
choices = [(s[1], s[2:end]) for s in keys(state.transitiontable)]
print(state.prompt, join([" ($(w[1]))$(w[2])" for w in choices], ","), ": ")
while true
choice = readline()
if !isempty(choice) && (x = findfirst(s -> s[1] == choice[1], choices)) != nothing
return state.transitiontable[join(choices[x], "")]
end
end
end
 
quitting(s::State) = false
quitting(s::Exit) = true
 
function runsim(state)
while true
if state.implicit != nothing
println(state.prompt)
state = state.implicit
elseif quitting(state)
println(state.prompt)
break
else
state = makeinstance(promptinput(state))
end
end
end
 
runsim(Ready())
</syntaxhighlight>{{out}}
<pre>
Vending machine is ready. (q)uit, (d)eposit: d
Waiting with funds. (s)elect, (r)efund: s
Thank you! Product dispensed. (r)emove: r
Vending machine is ready. (q)uit, (d)eposit: d
Waiting with funds. (s)elect, (r)efund: r
Please take refund.
Vending machine is ready. (q)uit, (d)eposit: q
Halting.
</pre>
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.1.51
 
enum class State { READY, WAITING, EXIT, DISPENSE, REFUNDING }
Line 572 ⟶ 1,498:
fun main(args: Array<String>) {
fsm()
}</langsyntaxhighlight>
 
Sample input/output:
Line 593 ⟶ 1,519:
</pre>
 
=={{header|PASCAL (Free Pascal 3.0.0)Nim}}==
{{trans Kotlin}}
<syntaxhighlight lang="nim">import strutils
 
type State {.pure.} = enum Ready, Waiting, Exit, Dispense, Refunding
 
 
proc getAnswer(message: string; answers: set[char]): char =
while true:
stdout.write message, ' '
stdout.flushFile
result = (stdin.readLine().toLowerAscii & ' ')[0]
if result in answers: return
 
 
proc fsm =
 
echo "Please enter your option when prompted"
echo "(any characters after the first will be ignored)"
var state = State.Ready
 
while true:
case state
 
of State.Ready:
let trans = getAnswer("\n(D)ispense or (Q)uit :", {'d', 'q'})
state = if trans == 'd': State.Waiting else: State.Exit
 
of State.Waiting:
echo "OK, put your money in the slot"
let trans = getAnswer("(S)elect product or choose a (R)efund :", {'s', 'r'})
state = if trans == 's': State.Dispense else: State.Refunding
 
of State.Dispense:
discard getAnswer("(R)emove product :", {'r'})
state = State.Ready
 
of State.Refunding:
# No transitions defined.
echo "OK, refunding your money"
state = State.Ready
 
of State.Exit:
echo "OK, quitting"
break
 
fsm()</syntaxhighlight>
 
{{out}}
<pre>Please enter your option when prompted
(any characters after the first will be ignored)
 
(D)ispense or (Q)uit : d
OK, put your money in the slot
(S)elect product or choose a (R)efund : s
(R)emove product : r
 
(D)ispense or (Q)uit : d
OK, put your money in the slot
(S)elect product or choose a (R)efund : r
OK, refunding your money
 
(D)ispense or (Q)uit : q
OK, quitting</pre>
 
=={{header|Ol}}==
<syntaxhighlight lang="scheme">
(import (scheme read))
 
; finite state machine
(define (state-machine states initial-state)
(let loop ((state initial-state))
(let*((action ((states state) 'enter #f))
(process-enter (if (function? action) (action)))
(next-state (if (symbol? action) action
else
((states state) (string->symbol (symbol->string (read))) state))))
(loop next-state))))
 
; task states
(define states {
'ready {
'enter (lambda () (print "Write (d)eposit for deposit and (q)uit to exit."))
 
'd 'waiting
'deposit 'waiting
'q 'exit
'quit 'exit
}
 
'exit {
'enter (lambda () (halt 1))
}
 
'waiting {
'enter (lambda () (print "Write (s)elect for dispense or (r)efund for refund."))
 
's 'dispense
'select 'dispense
'r 'refunding
'refund 'refunding
}
 
'dispense {
'enter (lambda () (print "Write (r)emove to finish action."))
 
'r 'ready
'remove 'ready
}
 
'refunding {
'enter 'ready
}
})
 
; run
(state-machine states 'ready)
</syntaxhighlight>
{{Out}}
<pre>
Write (d)eposit for deposit and (q)uit to exit.
d
Write (s)elect for dispense or (r)efund for refund.
f
Write (s)elect for dispense or (r)efund for refund.
f
Write (s)elect for dispense or (r)efund for refund.
s
Write (r)emove to finish action.
r
Write (d)eposit for deposit and (q)uit to exit.
d
Write (s)elect for dispense or (r)efund for refund.
s
Write (r)emove to finish action.
r
Write (d)eposit for deposit and (q)uit to exit.
q
</pre>
 
=={{header|Pascal}}==
=== (Free Pascal 3.0.0)===
<pre>
This version uses fairly vanilla pascal to implement the task. I have
Line 602 ⟶ 1,668:
 
</pre>
<syntaxhighlight lang="pascal">
<lang Pascal>
{
fsm1.pas
Line 859 ⟶ 1,925:
end.
 
</syntaxhighlight>
</lang>
 
 
Line 931 ⟶ 1,997:
 
 
</pre>
 
=={{header|Perl}}==
Added a dummy input called "IMPLICIT" that does not actually require input but automatically transitions to next state.
<syntaxhighlight lang="perl">#!/usr/bin/perl
 
use strict; # https://rosettacode.org/wiki/Finite_state_machine
use warnings;
 
my ($state, $action, %fsm) = 'ready';
while( <DATA> )
{
my ($start, $action, $end, $message) = split ' ', $_, 4;
$fsm{$start}{$action} = { next => $end, message => $message || "\n" };
}
 
while( $state ne 'exit' )
{
print "in state $state\n";
do
{
($action) = grep $_ eq 'IMPLICIT', my @actions = sort keys %{$fsm{$state}};
if( not $action )
{
print "Enter ", join(' or ', @actions), " : ";
chomp($action = uc <STDIN>);
}
}
until $fsm{$state}{$action};
print $fsm{$state}{$action}{message};
$state = $fsm{$state}{$action}{next};
}
 
# state input newstate displaytext
__DATA__
ready DEPOSIT waiting deposit coins
ready QUIT exit
waiting SELECT dispense remove item
waiting REFUND refunding take the refund
dispense REMOVE ready Thank You
refunding IMPLICIT ready</syntaxhighlight>
{{out}}
<pre>
in state ready
Enter DEPOSIT or QUIT : deposit
deposit coins
in state waiting
Enter REFUND or SELECT : select
remove item
in state dispense
Enter REMOVE : remove
Thank You
in state ready
Enter DEPOSIT or QUIT : deposit
deposit coins
in state waiting
Enter REFUND or SELECT : refund
take the refund
in state refunding
 
in state ready
Enter DEPOSIT or QUIT : quit
</pre>
 
=={{header|Phix}}==
{{libheader|Phix/pGUI}}
{{libheader|Phix/online}}
You can run this online [http://phix.x10.mx/p2js/fsm.htm here].
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\Finite_State_Machine.exw
-- =====================================
--</span>
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #000080;font-style:italic;">-- First, let's define our state machine textually, why not:</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">state_string</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"""
Ready,Deposit-&gt;Waiting,Quit
Waiting,Select-&gt;Dispense,Refund
Dispense,Remove-&gt;Ready:Remove product
Refund-&gt;Ready:Refunding money
Quit:Bye
"""</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">decode</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">state_string</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">states</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{},</span>
<span style="color: #000000;">messages</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{},</span>
<span style="color: #000000;">valid_keys</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">line</span> <span style="color: #008080;">in</span> <span style="color: #7060A8;">split</span><span style="color: #0000FF;">(</span><span style="color: #000000;">state_string</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">state</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">keyable</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">""</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">m</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #008000;">':'</span><span style="color: #0000FF;">,</span><span style="color: #000000;">line</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">messages</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">messages</span><span style="color: #0000FF;">,</span><span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">m</span><span style="color: #0000FF;">?</span><span style="color: #000000;">line</span><span style="color: #0000FF;">[</span><span style="color: #000000;">m</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..$]:</span><span style="color: #008000;">""</span><span style="color: #0000FF;">))</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">phrase</span> <span style="color: #008080;">in</span> <span style="color: #7060A8;">split</span><span style="color: #0000FF;">(</span><span style="color: #000000;">line</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">m</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">],</span><span style="color: #008000;">","</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">state</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">state</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">split</span><span style="color: #0000FF;">(</span><span style="color: #000000;">phrase</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"-&gt;"</span><span style="color: #0000FF;">))</span>
<span style="color: #000000;">keyable</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">phrase</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #000000;">states</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">states</span><span style="color: #0000FF;">,</span><span style="color: #000000;">state</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">valid_keys</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">valid_keys</span><span style="color: #0000FF;">,</span><span style="color: #000000;">keyable</span><span style="color: #0000FF;">[</span><span style="color: #000000;">2</span><span style="color: #0000FF;">..$])</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">return</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">states</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">messages</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">valid_keys</span><span style="color: #0000FF;">}</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">constant</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">states</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">messages</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">valid_keys</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">decode</span><span style="color: #0000FF;">(</span><span style="color: #000000;">state_string</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">valid_states</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">vslice</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">vslice</span><span style="color: #0000FF;">(</span><span style="color: #000000;">states</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">),</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">include</span> <span style="color: #000000;">pGUI</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
<span style="color: #004080;">Ihandle</span> <span style="color: #000000;">dlg</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">vbox</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">state</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">status</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">options</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">transition_to</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">sdx</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">IupSetAttribute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">status</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"TITLE"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">messages</span><span style="color: #0000FF;">[</span><span style="color: #000000;">sdx</span><span style="color: #0000FF;">])</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">states</span><span style="color: #0000FF;">[</span><span style="color: #000000;">sdx</span><span style="color: #0000FF;">][</span><span style="color: #000000;">1</span><span style="color: #0000FF;">])=</span><span style="color: #000000;">2</span> <span style="color: #008080;">then</span> <span style="color: #000080;font-style:italic;">-- (implicit)</span>
<span style="color: #000000;">sdx</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #000000;">states</span><span style="color: #0000FF;">[</span><span style="color: #000000;">sdx</span><span style="color: #0000FF;">][</span><span style="color: #000000;">1</span><span style="color: #0000FF;">][</span><span style="color: #000000;">2</span><span style="color: #0000FF;">],</span><span style="color: #000000;">valid_states</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #7060A8;">IupSetAttribute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">state</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"TITLE"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">valid_states</span><span style="color: #0000FF;">[</span><span style="color: #000000;">sdx</span><span style="color: #0000FF;">])</span>
<span style="color: #7060A8;">IupSetStrAttribute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">options</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"TITLE"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">vslice</span><span style="color: #0000FF;">(</span><span style="color: #000000;">states</span><span style="color: #0000FF;">[</span><span style="color: #000000;">sdx</span><span style="color: #0000FF;">][</span><span style="color: #000000;">2</span><span style="color: #0000FF;">..$],</span><span style="color: #000000;">1</span><span style="color: #0000FF;">),</span><span style="color: #008000;">" or "</span><span style="color: #0000FF;">))</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">finite_state_machine</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">sdx</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">IupGetAttribute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">state</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"TITLE"</span><span style="color: #0000FF;">),</span><span style="color: #000000;">valid_states</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">cdx</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #000000;">c</span><span style="color: #0000FF;">,</span><span style="color: #000000;">valid_keys</span><span style="color: #0000FF;">[</span><span style="color: #000000;">sdx</span><span style="color: #0000FF;">])</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">cdx</span> <span style="color: #008080;">then</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">newstate</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">states</span><span style="color: #0000FF;">[</span><span style="color: #000000;">sdx</span><span style="color: #0000FF;">][</span><span style="color: #000000;">cdx</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">][$]</span>
<span style="color: #000000;">sdx</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #000000;">newstate</span><span style="color: #0000FF;">,</span><span style="color: #000000;">valid_states</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">transition_to</span><span style="color: #0000FF;">(</span><span style="color: #000000;">sdx</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">return</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">valid_states</span><span style="color: #0000FF;">[</span><span style="color: #000000;">sdx</span><span style="color: #0000FF;">]=</span><span style="color: #008000;">`Quit`</span><span style="color: #0000FF;">?</span><span style="color: #004600;">IUP_CLOSE</span><span style="color: #0000FF;">:</span><span style="color: #004600;">IUP_CONTINUE</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">key_cb</span><span style="color: #0000FF;">(</span><span style="color: #004080;">Ihandle</span> <span style="color: #000080;font-style:italic;">/*dlg*/</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">atom</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">=</span><span style="color: #004600;">K_ESC</span> <span style="color: #008080;">then</span> <span style="color: #008080;">return</span> <span style="color: #004600;">IUP_CLOSE</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span> <span style="color: #000080;font-style:italic;">-- (standard practice for me)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">=</span><span style="color: #004600;">K_F5</span> <span style="color: #008080;">then</span> <span style="color: #008080;">return</span> <span style="color: #004600;">IUP_DEFAULT</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span> <span style="color: #000080;font-style:italic;">-- (let browser reload work)</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">finite_state_machine</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">upper</span><span style="color: #0000FF;">(</span><span style="color: #000000;">c</span><span style="color: #0000FF;">))</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #7060A8;">IupOpen</span><span style="color: #0000FF;">()</span>
<span style="color: #000000;">state</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupLabel</span><span style="color: #0000FF;">(</span><span style="color: #008000;">""</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"EXPAND=YES"</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">status</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupLabel</span><span style="color: #0000FF;">(</span><span style="color: #008000;">""</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"EXPAND=YES"</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">options</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupLabel</span><span style="color: #0000FF;">(</span><span style="color: #008000;">""</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"EXPAND=YES"</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">vbox</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupVbox</span><span style="color: #0000FF;">({</span><span style="color: #000000;">state</span><span style="color: #0000FF;">,</span><span style="color: #000000;">status</span><span style="color: #0000FF;">,</span><span style="color: #000000;">options</span><span style="color: #0000FF;">},</span><span style="color: #008000;">`MARGIN=40x40`</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">dlg</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupDialog</span><span style="color: #0000FF;">(</span><span style="color: #000000;">vbox</span><span style="color: #0000FF;">,</span><span style="color: #008000;">`TITLE="Finite State Machine",SIZE=200x100`</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">IupSetCallback</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dlg</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"KEY_CB"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">Icallback</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"key_cb"</span><span style="color: #0000FF;">))</span>
<span style="color: #000000;">transition_to</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- Ready</span>
<span style="color: #7060A8;">IupShow</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dlg</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">platform</span><span style="color: #0000FF;">()!=</span><span style="color: #004600;">JS</span> <span style="color: #008080;">then</span>
<span style="color: #7060A8;">IupMainLoop</span><span style="color: #0000FF;">()</span>
<span style="color: #7060A8;">IupHide</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dlg</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<!--</syntaxhighlight>-->
 
=={{header|PicoLisp}}==
Non-interactive random switch between states.
<syntaxhighlight lang="picolisp">(seed (in "/dev/urandom" (rd 8)))
(de atm NIL
(state '(ready)
(ready (if (rand T) 'waiting 'quit)
(prin "ready->") )
(waiting (if (rand T) 'dispense 'refund)
(prin "wait->") )
(dispense 'ready
(prin "dispense->") )
(refund 'ready
(prin "refund->") )
(quit 'ready
(nil (prinl "quit")) ) ) )
(do 3
(while (atm)) )</syntaxhighlight>
{{out}}
<pre>
ready->wait->dispense->ready->wait->dispense->ready->quit
ready->wait->refund->ready->quit
ready->wait->dispense->ready->quit
</pre>
 
=={{header|Prolog}}==
 
<syntaxhighlight lang="prolog">state(ready, deposit, waiting).
state(ready, quit, exit).
state(waiting, select, dispense).
state(waiting, refund, refunding).
state(dispense, remove, ready).
 
message(ready, 'Please deposit coins.~n').
message(waiting, 'Please select an item, or refund coins.~n').
message(dispense, 'Please remove your item.~n').
message(refunding, 'Coins have been refunded~n').
 
act :- act(ready).
 
act(exit).
act(refunding) :-
print_message(refunding),
act(ready).
act(State) :-
dif(State, exit),
print_message(State),
read(Action),
state(State, Action, NextState),
act(NextState).
print_message(State) :- message(State, Message), format(Message).</syntaxhighlight>
{{out}}
<pre>
2 ?- act.
Please deposit coins.
|: deposit.
Please select an item, or refund coins.
|: select.
Please remove your item.
|: remove.
Please deposit coins.
|: deposit.
Please select an item, or refund coins.
|: refund.
Coins have been refunded
Please deposit coins.
|: quit.
 
true .
</pre>
=={{header|Python}}==
{{works with|Python 3}}
<syntaxhighlight lang="python">''' Finite State Machine for Rosetta Code
Actually two of them. The main FSM described in the task and a second one of the Acceptor variety described on
the WP page to get the input from the user.
 
I handled the implicit transition by defining a null list as the valid inputs. and made my Acceptor return the
null string ('') for the instance of no valid inputs. Then just defined the the transition for current state and null
string for input.
 
I find it interesting that the rules for such a simple fsm took more lines of code than the actual code for the fsm which
can be fed many different sets of rules. Storing the rules in a databse would reduce the lines required for storing
the rules'''
 
states = { 'ready':{
'prompt' : 'Machine ready: (d)eposit, or (q)uit?',
'responses' : ['d','q']},
'waiting':{
'prompt' : 'Machine waiting: (s)elect, or (r)efund?',
'responses' : ['s','r']},
'dispense' : {
'prompt' : 'Machine dispensing: please (r)emove product',
'responses' : ['r']},
'refunding' : {
'prompt' : 'Refunding money',
'responses' : []},
'exit' :{}
}
transitions = { 'ready': {
'd': 'waiting',
'q': 'exit'},
'waiting' : {
's' : 'dispense',
'r' : 'refunding'},
'dispense' : {
'r' : 'ready'},
'refunding' : {
'' : 'ready'}}
 
def Acceptor(prompt, valids):
''' Acceptor style finite state machine to prompt for user input'''
if not valids:
print(prompt)
return ''
else:
while True:
resp = input(prompt)[0].lower()
if resp in valids:
return resp
 
def finite_state_machine(initial_state, exit_state):
response = True
next_state = initial_state
current_state = states[next_state]
while response != exit_state:
response = Acceptor(current_state['prompt'], current_state['responses'])
next_state = transitions[next_state][response]
current_state = states[next_state]
 
if __name__ == "__main__":
finite_state_machine('ready','q')
</syntaxhighlight>
{{out}}
<pre>
PS C:\alan\programming> & "C:/Program Files (x86)/Python38-32/python.exe" c:/alan/programming/fsm.py
Machine ready: (d)eposit, or (q)uit?d
Machine waiting: (s)elect, or (r)efund?s
Machine dispensing: please (r)emove productr
Machine ready: (d)eposit, or (q)uit?d
Machine waiting: (s)elect, or (r)efund?r
Refunding money
Machine ready: (d)eposit, or (q)uit?q
PS C:\alan\programming>
</pre>
 
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">#lang racket
 
(define states
Line 964 ⟶ 2,322:
(let/ec quit
(with-input-from-string "deposit select remove deposit refund quit"
(λ () (machine states void read quit)))))</langsyntaxhighlight>
{{out}}
<pre>CURRENT STATE: ready
Line 981 ⟶ 2,339:
ready -> quit -> exit
CURRENT STATE: exit</pre>
 
=={{header|Raku}}==
(formerly Perl 6)
 
<syntaxhighlight lang="raku" line>#===== The state machine =====#
 
class StateMachine {
class State {...}
class Transition {...}
 
has State %!state;
has &.choose-transition is rw;
 
method add-state(Str $id, &action)
{
%!state{$id} = State.new(:$id, :&action);
}
 
multi method add-transition(Str $from, Str $to)
{
%!state{$from}.implicit-next = %!state{$to};
}
 
multi method add-transition(Str $from, $id, Str $to)
{
%!state{$from}.explicit-next.push: Transition.new(:$id, to => %!state{$to});
}
 
method run(Str $initial-state)
{
my $state = %!state{$initial-state};
loop {
$state.action.();
if $state.implicit-next -> $_ { $state = $_; }
elsif $state.explicit-next -> $_ { $state = &.choose-transition.(|$_).to; }
else { last; }
}
}
 
class Transition {
has $.id;
has State $.to;
}
class State {
has $.id;
has &.action;
has State $.implicit-next is rw;
has Transition @.explicit-next;
}
}
 
 
#===== Usage example: Console-based vending machine =====#
 
my StateMachine $machine .= new;
 
$machine.choose-transition = sub (*@transitions) {
say "[{.key + 1}] {.value.id}" for @transitions.pairs;
loop {
my $n = val get;
return @transitions[$n - 1] if $n ~~ Int && $n ~~ 1..@transitions;
say "Invalid input; try again.";
}
}
 
$machine.add-state("ready", { say "Please deposit coins."; });
$machine.add-state("waiting", { say "Please select a product."; });
$machine.add-state("dispense", { sleep 2; say "Please remove product from tray."; });
$machine.add-state("refunding", { sleep 1; say "Refunding money..."; });
$machine.add-state("exit", { say "Shutting down..."; });
 
$machine.add-transition("ready", "quit", "exit");
$machine.add-transition("ready", "deposit", "waiting");
$machine.add-transition("waiting", "select", "dispense");
$machine.add-transition("waiting", "refund", "refunding");
$machine.add-transition("dispense", "remove", "ready");
$machine.add-transition("refunding", "ready");
 
$machine.run("ready");</syntaxhighlight>
 
=={{header|REXX}}==
Line 994 ⟶ 2,432:
::* a mixture of uppercase and lowercase text is used for the output messages
::* messages have extra blanks for readability &nbsp; (and options are spelled out)
<langsyntaxhighlight lang="rexx">/*REXX pgm simulates a FSM (Finite State Machine), input is recognized by pressing keys.*/
10: say "Press D (deposit) or Q (quit)" /*display a prompt (message) to term. */
20: $=inkey(); upper $ /*since this a terminal, uppercase KEY.*/
Line 1,016 ⟶ 2,454:
 
140: say "Refunded" /*display what action just happened. */
signal 10 /*go & re-start process (ready state). */</langsyntaxhighlight>
{{out|output|text= &nbsp; when using (pressing) the exact same input(s) as the '''BASIC''' entry: &nbsp; &nbsp; <tt> D &nbsp; R &nbsp; D &nbsp; S &nbsp; R &nbsp; Q </tt>}}
<pre>
Line 1,037 ⟶ 2,475:
works withooRexx (and any other REXX).
key and Enter must be pressed-
<langsyntaxhighlight lang="rexx">/*REXX pgm simulates a FSM (Finite State Machine), input is recognized by pressing keys.*/
10: k=inkey('D (deposit) or Q (quit)','DQ')
if k=="D" then signal 50 /*Is response a "D" ? Process deposit.*/
Line 1,064 ⟶ 2,502:
Say 'Invalid key, try again.'
End
Return k</langsyntaxhighlight>
{{out}}
<pre>Press D (deposit) or Q (quit) and Enter
Line 1,077 ⟶ 2,515:
r
Refunded
Press D (deposit) or Q (quit) and Enter</pre>
 
=={{header|Rust}}==
For abstraction, it is desirable to implement the transitions of the state machine through its methods.
Here it is done transparently using the method_enum::gen macro.<br>
'''[dependencies]'''<br>
methods-enum = "0.2.4"
<syntaxhighlight lang="rust">enum State {
Ready,
Waiting,
Dispense,
Refunding,
Exit,
}
 
#[methods_enum::gen(Act: run)]
impl State {
pub fn set(&mut self);
pub fn input_char(&mut self, ch: char);
 
fn run(&mut self, act: Act) {
match self {
State::Ready => match act {
Act::set() => println!("Ready: d - deposit / q - quit "),
Act::input_char('d') => self.set_state(State::Waiting),
Act::input_char('q') => self.set_state(State::Exit),
_ => self.set(),
},
State::Waiting => match act {
Act::set() => println!("Waiting: s - select / r - refund "),
Act::input_char('s') => self.set_state(State::Dispense),
Act::input_char('r') => self.set_state(State::Refunding),
_ => self.set(),
},
State::Dispense => match act {
Act::set() => println!("Dispense: r - remove "),
Act::input_char('r') => self.set_state(State::Ready),
_ => self.set(),
},
State::Refunding => match act {
Act::set() => {
println!("Refunding: refund of the deposit...");
self.set_state(State::Ready)
}
_ => (), // never - ignore
},
State::Exit => match act {
Act::set() => println!("Exit: goodbye! "),
_ => panic!("!! Invalid command for State::Exit: '{act:?}'"),
},
}
}
 
fn set_state(&mut self, new_state: State) {
*self = new_state;
self.set();
}
}
 
fn main() {
let mut machine = State::Ready;
machine.set();
 
while !matches!(&machine, State::Exit) {
machine.input_char(char_entered());
}
}
 
fn char_entered() -> char {
let mut text = String::new();
std::io::stdin().read_line(&mut text).unwrap_or(0);
text.chars().next().unwrap_or('\x0d')
}</syntaxhighlight>
{{out}}
<pre>Ready: d - deposit / q - quit
d
Waiting: s - select / r - refund
r
Refunding: refund of the deposit...
Ready: d - deposit / q - quit
d
Waiting: s - select / r - refund
s
Dispense: r - remove
r
Ready: d - deposit / q - quit
q
Exit: goodbye!
</pre>
 
=={{header|Tcl}}==
Using a nested dict where the leafs contain the output state corresponding to an action, and empty actions are implicit transitions. Would be marginally cleaner using a do..while proc.
<syntaxhighlight lang="tcl">set fsm [dict create \
ready {deposit waiting quit exit} \
waiting {select dispense refund refunding} \
dispense {remove ready} \
refunding {{} ready} \
]
set state ready
 
proc prompt {fsm state} {
set choices [dict keys [dict get $fsm $state]]
while {1} {
puts -nonewline "state: $state, possible actions: $choices\n>"
if {[gets stdin line] == -1} {
exit
}
if {$line in $choices} {
return $line
}
}
}
 
while {$state ne "exit"} {
set action [prompt $fsm $state]
set state [dict get $fsm $state $action]
while {[dict exists $fsm $state {}]} {
set state [dict get $fsm $state {}]
}
}</syntaxhighlight>
{{out}}
<pre>$ tclsh fsm.tcl
state: ready, possible actions: deposit quit
>deposit
state: waiting, possible actions: select refund
>select
state: dispense, possible actions: remove
>remove
state: ready, possible actions: deposit quit
>deposit
state: waiting, possible actions: select refund
>re
state: waiting, possible actions: select refund
>refund
state: ready, possible actions: deposit quit
>quit</pre>
 
=={{header|VBA}}==
{{trans|Phix}}
<syntaxhighlight lang="vb">Enum states
READY
WAITING
DISPENSE
REFUND
QU1T
End Enum '-- (or just use strings if you prefer)
Public Sub finite_state_machine()
Dim state As Integer: state = READY: ch = " "
Do While True
Debug.Print ch
Select Case state
Case READY: Debug.Print "Machine is READY. (D)eposit or (Q)uit :"
Do While True
If ch = "D" Then
state = WAITING
Exit Do
End If
If ch = "Q" Then
state = QU1T
Exit Do
End If
ch = InputBox("Machine is READY. (D)eposit or (Q)uit :")
Loop
Case WAITING: Debug.Print "(S)elect product or choose to (R)efund :"
Do While True
If ch = "S" Then
state = DISPENSE
Exit Do
End If
If ch = "R" Then
state = REFUND
Exit Do
End If
ch = InputBox("(S)elect product or choose to (R)efund :")
Loop
Case DISPENSE: Debug.Print "Dispensing product..."
Do While True
If ch = "C" Then
state = READY
Exit Do
End If
ch = InputBox("Please (C)ollect product. :")
Loop
Case REFUND: Debug.Print "Please collect refund."
state = READY
ch = " "
Case QU1T: Debug.Print "Thank you, shutting down now."
Exit Sub
End Select
Loop
End Sub</syntaxhighlight>{{out}}
<pre>Machine is READY. (D)eposit or (Q)uit :
D
(S)elect product or choose to (R)efund :
S
Dispensing product...
C
Machine is READY. (D)eposit or (Q)uit :
D
(S)elect product or choose to (R)efund :
R
Please collect refund.
Machine is READY. (D)eposit or (Q)uit :
Q
Thank you, shutting down now.</pre>
 
=={{header|Wren}}==
{{trans|Kotlin}}
{{libheader|Wren-str}}
<syntaxhighlight lang="wren">import "./str" for Str
import "io" for Stdin, Stdout
 
var READY = 0
var WAITING = 1
var EXIT = 2
var DISPENSE = 3
var REFUNDING = 4
 
var fsm = Fn.new {
System.print("Please enter your option when prompted")
System.print("(any characters after the first will be ignored)")
var state = READY
var trans = ""
while (true) {
if (state == READY) {
while (true) {
System.write("\n(D)ispense or (Q)uit : ")
Stdout.flush()
trans = Str.lower(Stdin.readLine())[0]
if (trans == "d" || trans == "q") break
}
state = (trans == "d") ? WAITING : EXIT
} else if (state == WAITING) {
System.print("OK, put your money in the slot")
while (true) {
System.write("(S)elect product or choose a (R)efund : ")
Stdout.flush()
trans = Str.lower(Stdin.readLine())[0]
if (trans == "s" || trans == "r") break
}
state = (trans == "s") ? DISPENSE : REFUNDING
} else if (state == DISPENSE) {
while (true) {
System.write("(R)emove product : ")
Stdout.flush()
trans = Str.lower(Stdin.readLine())[0]
if (trans == "r") break
}
state = READY
} else if (state == REFUNDING) {
// no transitions defined
System.print("OK, refunding your money")
state = READY
} else if (state == EXIT) {
System.print("OK, quitting")
return
}
}
}
 
fsm.call()</syntaxhighlight>
 
{{out}}
Sample output:
<pre>
Please enter your option when prompted
(any characters after the first will be ignored)
 
(D)ispense or (Q)uit : d
OK, put your money in the slot
(S)elect product or choose a (R)efund : s
(R)emove product : r
 
(D)ispense or (Q)uit : d
OK, put your money in the slot
(S)elect product or choose a (R)efund : r
OK, refunding your money
 
(D)ispense or (Q)uit : q
OK, quitting
</pre>
 
=={{header|XPL0}}==
<syntaxhighlight lang "XPL0">int State, Trans, Table, Msg;
def \State\ Ready, Waiting, Dispense, Refunding, Exit;
def \Trans\ Deposit, Select, Refund, Collect, Quit; \State:
[Table:=[[Waiting, Ready, Ready, Ready, Exit], \Ready
[Waiting, Dispense, Refunding, Waiting, Waiting], \Waiting
[Dispense, Dispense, Dispense, Ready, Dispense], \Dispense
[Ready, Ready, Ready, Ready, Ready], \Refunding
[Exit, Exit, Exit, Exit, Exit]]; \Exit
State:= Ready;
loop [Msg:= ["Ready, choose (D)eposit or (Q)uit: ",
"Waiting, choose (S)elect or (R)efund: ",
"Dispensing, please (C)ollect product: ",
"Refunding, please collect refund.",
"Shutting down."];
Text(0, Msg(State));
case State of
Exit: quit;
Refunding: Trans:= Refund \implicit transition
other case ChIn(1) of \explicit transitions
^D,^d: Trans:= Deposit;
^S,^s: Trans:= Select;
^R,^r: Trans:= Refund;
^C,^c: Trans:= Collect;
^Q,^q: Trans:= Quit
other []; \illegal entries don't change state
CrLf(0);
State:= Table(State, Trans);
];
CrLf(0);
]</syntaxhighlight>
{{out}}
<pre>
Ready, choose (D)eposit or (Q)uit: D
Waiting, choose (S)elect or (R)efund: S
Dispensing, please (C)ollect product: C
Ready, choose (D)eposit or (Q)uit: D
Waiting, choose (S)elect or (R)efund: R
Refunding, please collect refund.
Ready, choose (D)eposit or (Q)uit: Q
Shutting down.
</pre>
 
=={{header|zkl}}==
Line 1,083 ⟶ 2,845:
 
If we need true state to state hops, we could use tail recursion (another name for goto).
<langsyntaxhighlight lang="zkl">class FSM{ // our Finite State Machine
var bank=0, item=Void;
fcn deposit(coin){ bank=coin }
Line 1,098 ⟶ 2,860:
}
 
Vault.add(FSM); // put class FSM where I can find it</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">fcn run(program){ // convert text to FSM instructions and run them
program=program.replace("(",".fp("); // deposit(10)-->deposit.fp(10)
a,b,p := 0,0,Sink("class P(FSM){ state(); ");
Line 1,107 ⟶ 2,869:
// println(program); // WTH did I just do?
Compiler.Compiler.compileText(program)(); // compile and run our little FSM
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">run("select(); take(); deposit(10); select(\"snickers\"); take();");</langsyntaxhighlight>
The above is converted to:
<langsyntaxhighlight lang="zkl">class P(FSM){
state();
act(select.fp());
Line 1,117 ⟶ 2,879:
act( select.fp("snickers"));
act( take.fp());
}</langsyntaxhighlight>
The .fp() is function application (ie deferred execution) so I can extract the
function name and print it.
3,022

edits