- Although SageMath does not have a dedicated built-in module to handle state
- machines, it can still be used to model, construct, display, and run relatively
+ Although SageMath does have a dedicated built-in module to handle state
+ machines, we can still model, construct, display, and run relatively
simple state machines, leveraging the general-purpose tools, such as graphs and
transition matrices, to represent and work with state machines.
+
+
+ Using Sage built-in 'FiniteStateMachine'
+
+ FiniteStateMachine() is used define an empty state machine.
+ FSMState() helps define state for the given label, the is_initial
+ flag can be set to true to indicate the current state will be the initial state
+ of the finite state machine. add_state() method is then used to append the state
+ to the state machine
+
+
+ go = FSMState('GO', is_initial=True)
+ fsm.add_state(go)
+
+ # Adding more states
+ hold = fsm.add_state('HOLD')
+ stop = fsm.add_state('STOP')
+
+ # the FiniteStateMachine instance
+ fsm
+
+
+
+
+ To check whether or not a finite state machine has a state defined, has_state()
+ method can be used by passing in the state label (case-sensitive).
+
+
+ fsm.has_state('GO')
+
+
+
+
+ states() method is used to enumerate the list of all defined states
+ of the state machine.
+
+
+ fsm.states()
+
+
+
+
+ initial_states() method lists the defined initial state(s)
+ of the state machine.
+
+
+ fsm.initial_states()
+
+
+
+
+ FSMTransition() defines a new transition between two states,
+ as well as the input (the transition trigger) and output associated
+ with the new state after teh transition.add_transition() method
+ attach the defined transition to the state machine. transitions()
+ method is used to enumerate the list of all defined transitions of the
+ state machine.
+
+
+ from sage.combinat.finite_state_machine import FSMTransition
+
+ # defining 3 transitions, and associating them the state machine
+ drive = fsm.add_transition(FSMTransition(stop, go, 0,10))
+ wait = fsm.add_transition(FSMTransition(go, hold, 1,20))
+ walk = fsm.add_transition(FSMTransition(hold, stop, 2,40))
+
+ fsm.transitions()
+
+
+
+
+ Once the states and transitions as defined, the state machine can berun
+ using process() method.
+
+
+
+ # pass in the initial state and the list of inputs
+ _, final_state, outputs_history = fsm.process(
+ initial_state=go,
+ input_tape=[1, 2, 0],
+ )
+
+ # display final/current state
+ final_state
+
+
+
+
+ process() method also returned the list of intermediary outputs during
+ the state machine run.
+
+
+ # print out the outputs of the state machine run
+ outputs_history
+
+
+
+
+ graph() command displays the graph representation of the state machine.
+
+
+ fsm.graph()
+
+
+
+
+ The FiniteStateMachine class also offers LATEX representation of the state
+ machine using the latex_options() method.
- This simple example shows a typical workflow of the use of a simple finite state
- machine. It can be customized and fine-tuned to reflect more complex scenarios
- (more states, different input sequences, ...etc). The general structure of a
- state machine can be adapted for different use cases.
+ The above are basic commands with a typical workflow of defining and running of simple finite
+ state machines. The general structure of the state machine can be adapted to fit different use
+ cases. The examples shown can be customized and fine-tuned to reflect more complex scenarios
+ (more states, different input sequences, ...etc).