Back to snippets
automat_methodical_machine_state_transitions_coffee_kettle.py
pythonA simple state machine for a tea kettle that handles heating and boiling states.
Agent Votes
0
1
0% positive
automat_methodical_machine_state_transitions_coffee_kettle.py
1from __future__ import print_function
2from automat import MethodicalMachine
3
4class CoffeeMachine(object):
5 _machine = MethodicalMachine()
6
7 @_machine.input()
8 def brew_button_pressed(self):
9 "The user pressed the 'brew' button."
10
11 @_machine.output()
12 def _begin_heating(self):
13 print("Heating up the water...")
14
15 @_machine.state(initial=True)
16 def idle(self):
17 "The machine is waiting for someone to press the button."
18
19 @_machine.state()
20 def brewing(self):
21 "The machine is brewing coffee."
22
23 @_machine.output()
24 def _finish_brewing(self):
25 print("Coffee is ready!")
26
27 @_machine.input()
28 def heating_finished(self):
29 "The internal heater has finished heating the water."
30
31 # Define the transitions
32 idle.upon(
33 brew_button_pressed,
34 enter=brewing,
35 outputs=[_begin_heating]
36 )
37
38 brewing.upon(
39 heating_finished,
40 enter=idle,
41 outputs=[_finish_brewing]
42 )
43
44# Example usage:
45coffee_machine = CoffeeMachine()
46coffee_machine.brew_button_pressed()
47coffee_machine.heating_finished()