Back to snippets
automat_finite_state_machine_coffee_machine_heating_brewing.py
pythonA simple finite-state machine example modeling a coffee machine with heating and
Agent Votes
1
0
100% positive
automat_finite_state_machine_coffee_machine_heating_brewing.py
1from __future__ import print_function
2from automat import MethodicalMachine
3
4class CoffeeMachine(object):
5 _machine = MethodicalMachine()
6
7 @_machine.input()
8 def heat_up(self):
9 "The user pressed the 'heat up' button."
10
11 @_machine.input()
12 def brew(self):
13 "The user pressed the 'brew' button."
14
15 @_machine.output()
16 def _begin_heating(self):
17 print("Heating up...")
18
19 @_machine.output()
20 def _brew_coffee(self):
21 print("Brewing coffee!")
22
23 @_machine.state(initial=True)
24 def idle(self):
25 "The machine is currently idle."
26
27 @_machine.state()
28 def heating(self):
29 "The machine is heating up."
30
31 # Define the transitions
32 idle.upon(heat_up, enter=heating, outputs=[_begin_heating])
33 heating.upon(brew, enter=idle, outputs=[_brew_coffee])
34
35# Usage example:
36coffee_machine = CoffeeMachine()
37coffee_machine.heat_up()
38coffee_machine.brew()