Finite state machine: Difference between revisions

J
(J)
Line 901:
finishStates = ["Exit"]
</lang>
 
=={{header|J}}==
 
This seems to be what the current draft task asks for:
 
<lang>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. }}</lang>
 
With the above implementation, the task example would look like:
 
<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.
}}</lang>
 
=={{header|Java}}==
6,962

edits