Finite state machine: Difference between revisions
m
syntax highlighting fixup automation
m (→{{header|J}}) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 36:
{{trans|Python}}
<
(‘Machine ready: (d)eposit, or (q)uit?’,
[String(‘d’), ‘q’]),
Line 80:
current_state = :states[next_state]
finite_state_machine(‘ready’, ‘q’)</
{{out}}
Line 95:
=={{header|BASIC}}==
==={{header|Commodore BASIC}}===
<
10 REM FINITE STATE MACHINE
20 LET MS=1: REM MACHINE STATE
Line 141:
5010 PRINT "MACHINE IS SHUTDOWN"
5020 END
</syntaxhighlight>
==={{header|Sinclair ZX81 BASIC}}===
Works with 1k of RAM.
Line 153:
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.
<
20 IF INKEY$="D" THEN GOTO 50
30 IF INKEY$="Q" THEN STOP
Line 167:
130 GOTO 120
140 PRINT "REFUNDED"
150 GOTO 10</
{{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 181:
=={{header|C}}==
Here is a manually-constructed table-driven finite state machine that is fairly general and could be adapted to different applications.
<syntaxhighlight lang="c">
#include <stdio.h>
#include <ctype.h>
Line 233:
return 0;
}
</syntaxhighlight>
Machine simulation :
<pre>
Line 251:
=={{header|C++}}==
<syntaxhighlight lang="c">
#include <map>
Line 416:
}
}
</syntaxhighlight>
{{out}}
Line 442:
=={{header|D}}==
{{trans|Kotlin}}
<
import std.range;
import std.stdio;
Line 506:
void main() {
fsm();
}</
=={{header|Delphi}}==
{{Trans|Go}}
<syntaxhighlight lang="delphi">
program Finite_state_machine;
Line 595:
begin
fsm;
end.</
=={{header|FreeBASIC}}==
{{trans|Phix}}
<
READY
WAITING
Line 647:
End Select
Loop
Sleep</
{{out}}
<pre>
Line 655:
=={{header|Go}}==
{{trans|Kotlin}}
<
import (
Line 754:
func main() {
fsm()
}</
{{out}}
Line 778:
=={{header|Groovy}}==
{{trans|Java}}
<
private enum State {
Ready(true, "Deposit", "Quit"),
Line 826:
}
}
}</
=={{header|Haskell}}==
<
import Data.Maybe
import Control.Monad
Line 900:
, Implicit "Refunding" "Ready" ]
finishStates = ["Exit"]
</syntaxhighlight>
=={{header|J}}==
Line 906:
This seems to be what the current draft task asks for:
<syntaxhighlight lang="text">NB. FSM builder:
explicit=: {{
states=: ~. states,x;y
Line 926:
Snm=: statename=: {{ ;:inv m{states }}
Tnm=: transitionname=: {{ ;:inv m{transitions }}
implicits=: {{ r=.'' while. next '' do. r=.r, current end. }}</
With the above implementation, the task example would look like:
<
start 'ready'
'ready' 'deposit'explicit 'waiting'
Line 958:
echo 'implicit transition to: ',i statename
end.
}}</
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}}==
<
public class FiniteStateMachine {
Line 1,014:
}
}
}</
<pre>[Deposit, Quit]
> Deposit
Line 1,033:
=={{header|JavaScript}}==
===On browser using blocking window methods===
<
var states = [{
'name': 'Ready',
Line 1,101:
}
}
</syntaxhighlight>
=={{header|Julia}}==
<
struct Ready <: State
Line 1,185:
runsim(Ready())
</
<pre>
Vending machine is ready. (q)uit, (d)eposit: d
Line 1,198:
=={{header|Kotlin}}==
<
enum class State { READY, WAITING, EXIT, DISPENSE, REFUNDING }
Line 1,254:
fun main(args: Array<String>) {
fsm()
}</
Sample input/output:
Line 1,277:
=={{header|Nim}}==
{{trans Kotlin}}
<
type State {.pure.} = enum Ready, Waiting, Exit, Dispense, Refunding
Line 1,321:
break
fsm()</
{{out}}
Line 1,341:
=={{header|Ol}}==
<
(import (scheme read))
Line 1,392:
; run
(state-machine states 'ready)
</syntaxhighlight>
{{Out}}
<pre>
Line 1,424:
</pre>
<syntaxhighlight lang="pascal">
{
fsm1.pas
Line 1,681:
end.
</syntaxhighlight>
Line 1,757:
=={{header|Perl}}==
Added a dummy input called "IMPLICIT" that does not actually require input but automatically transitions to next state.
<
use strict; # https://rosettacode.org/wiki/Finite_state_machine
Line 1,793:
waiting REFUND refunding take the refund
dispense REMOVE ready Thank You
refunding IMPLICIT ready</
{{out}}
<pre>
Line 1,821:
{{libheader|Phix/online}}
You can run this online [http://phix.x10.mx/p2js/fsm.htm here].
<!--<
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\Finite_State_Machine.exw
Line 1,900:
<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>
<!--</
=={{header|PicoLisp}}==
Non-interactive random switch between states.
<
(de atm NIL
(state '(ready)
Line 1,918:
(nil (prinl "quit")) ) ) )
(do 3
(while (atm)) )</
{{out}}
<pre>
Line 1,928:
=={{header|Prolog}}==
<
state(ready, quit, exit).
state(waiting, select, dispense).
Line 1,952:
act(NextState).
print_message(State) :- message(State, Message), format(Message).</
{{out}}
<pre>
Line 1,974:
=={{header|Python}}==
{{works with|Python 3}}
<
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.
Line 2,033:
if __name__ == "__main__":
finite_state_machine('ready','q')
</syntaxhighlight>
{{out}}
<pre>
Line 2,048:
=={{header|Racket}}==
<
(define states
Line 2,078:
(let/ec quit
(with-input-from-string "deposit select remove deposit refund quit"
(λ () (machine states void read quit)))))</
{{out}}
<pre>CURRENT STATE: ready
Line 2,099:
(formerly Perl 6)
<syntaxhighlight lang="raku"
class StateMachine {
Line 2,174:
$machine.add-transition("refunding", "ready");
$machine.run("ready");</
=={{header|REXX}}==
Line 2,188:
::* a mixture of uppercase and lowercase text is used for the output messages
::* messages have extra blanks for readability (and options are spelled out)
<
10: say "Press D (deposit) or Q (quit)" /*display a prompt (message) to term. */
20: $=inkey(); upper $ /*since this a terminal, uppercase KEY.*/
Line 2,210:
140: say "Refunded" /*display what action just happened. */
signal 10 /*go & re-start process (ready state). */</
{{out|output|text= when using (pressing) the exact same input(s) as the '''BASIC''' entry: <tt> D R D S R Q </tt>}}
<pre>
Line 2,231:
works withooRexx (and any other REXX).
key and Enter must be pressed-
<
10: k=inkey('D (deposit) or Q (quit)','DQ')
if k=="D" then signal 50 /*Is response a "D" ? Process deposit.*/
Line 2,258:
Say 'Invalid key, try again.'
End
Return k</
{{out}}
<pre>Press D (deposit) or Q (quit) and Enter
Line 2,278:
'''[dependencies]'''<br>
methods-enum = "0.2.4"
<
Ready,
Waiting,
Line 2,343:
std::io::stdin().read_line(&mut text).unwrap_or(0);
text.chars().next().unwrap_or('\x0d')
}</
{{out}}
<pre>Ready: d - deposit / q - quit
Line 2,363:
=={{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.
<
ready {deposit waiting quit exit} \
waiting {select dispense refund refunding} \
Line 2,390:
set state [dict get $fsm $state {}]
}
}</
{{out}}
<pre>$ tclsh fsm.tcl
Line 2,410:
=={{header|VBA}}==
{{trans|Phix}}
<
READY
WAITING
Line 2,461:
End Select
Loop
End Sub</
<pre>Machine is READY. (D)eposit or (Q)uit :
D
Line 2,481:
{{trans|Kotlin}}
{{libheader|Wren-str}}
<
import "io" for Stdin, Stdout
Line 2,532:
}
fsm.call()</
{{out}}
Line 2,558:
If we need true state to state hops, we could use tail recursion (another name for goto).
<
var bank=0, item=Void;
fcn deposit(coin){ bank=coin }
Line 2,573:
}
Vault.add(FSM); // put class FSM where I can find it</
<
program=program.replace("(",".fp("); // deposit(10)-->deposit.fp(10)
a,b,p := 0,0,Sink("class P(FSM){ state(); ");
Line 2,582:
// println(program); // WTH did I just do?
Compiler.Compiler.compileText(program)(); // compile and run our little FSM
}</
<
The above is converted to:
<
state();
act(select.fp());
Line 2,592:
act( select.fp("snickers"));
act( take.fp());
}</
The .fp() is function application (ie deferred execution) so I can extract the
function name and print it.
|