1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
|
from automat import MethodicalMachine
class Door(object):
def unlock(self):
print("Opening the door so you can get your food.")
def lock(self):
print("Locking the door so you can't steal the food.")
class Light(object):
def on(self):
print("Need some food over here.")
def off(self):
print("We're good on food for now.")
class FoodSlot(object):
"""
Automats were a popular kind of business in the 1950s and 60s; a sort of
restaurant-sized vending machine that served cooked food out of a
coin-operated dispenser.
This class represents the logic associated with a single food slot.
"""
machine = MethodicalMachine()
def __init__(self, door, light):
self._door = door
self._light = light
self.start()
@machine.state(initial=True)
def initial(self):
"""
The initial state when we are constructed.
Note that applications never see this state, because the constructor
provides an input to transition out of it immediately.
"""
@machine.state()
def empty(self):
"""
The machine is empty (and the light asking for food is on).
"""
@machine.input()
def start(self):
"""
A private input, for transitioning to the initial blank state to
'empty', making sure the door and light are properly configured.
"""
@machine.state()
def ready(self):
"""
We've got some food and we're ready to serve it.
"""
@machine.state()
def serving(self):
"""
The door is open, we're serving food.
"""
@machine.input()
def coin(self):
"""
A coin (of the appropriate denomination) was inserted.
"""
@machine.input()
def food(self):
"""
Food was prepared and inserted into the back of the machine.
"""
@machine.output()
def turnOnFoodLight(self):
"""
Turn on the 'we need food' light.
"""
self._light.on()
@machine.output()
def turnOffFoodLight(self):
"""
Turn off the 'we need food' light.
"""
self._light.off()
@machine.output()
def lockDoor(self):
"""
Lock the door, we don't need food.
"""
self._door.lock()
@machine.output()
def unlockDoor(self):
"""
Unock the door, it's chow time!.
"""
self._door.unlock()
@machine.input()
def closeDoor(self):
"""
The door was closed.
"""
initial.upon(start, enter=empty, outputs=[lockDoor, turnOnFoodLight])
empty.upon(food, enter=ready, outputs=[turnOffFoodLight])
ready.upon(coin, enter=serving, outputs=[unlockDoor])
serving.upon(closeDoor, enter=empty, outputs=[lockDoor,
turnOnFoodLight])
slot = FoodSlot(Door(), Light())
if __name__ == '__main__':
import sys
sys.stdout.writelines(FoodSlot.machine.asDigraph())
# raw_input("Hit enter to make some food and put it in the slot: ")
# slot.food()
# raw_input("Hit enter to insert a coin: ")
# slot.coin()
# raw_input("Hit enter to retrieve the food and close the door: ")
# slot.closeDoor()
# raw_input("Hit enter to make some more food: ")
# slot.food()
|