File: turnstile_example.py

package info (click to toggle)
automat 25.4.16-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 492 kB
  • sloc: python: 3,657; makefile: 15
file content (64 lines) | stat: -rw-r--r-- 1,364 bytes parent folder | download
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
from automat import MethodicalMachine


class Lock(object):
    "A sample I/O device."

    def engage(self):
        print("Locked.")

    def disengage(self):
        print("Unlocked.")


class Turnstile(object):
    machine = MethodicalMachine()

    def __init__(self, lock):
        self.lock = lock

    @machine.input()
    def arm_turned(self):
        "The arm was turned."

    @machine.input()
    def fare_paid(self):
        "The fare was paid."

    @machine.output()
    def _engage_lock(self):
        self.lock.engage()

    @machine.output()
    def _disengage_lock(self):
        self.lock.disengage()

    @machine.output()
    def _nope(self):
        print("**Clunk!**  The turnstile doesn't move.")

    @machine.state(initial=True)
    def _locked(self):
        "The turnstile is locked."

    @machine.state()
    def _unlocked(self):
        "The turnstile is unlocked."

    _locked.upon(fare_paid, enter=_unlocked, outputs=[_disengage_lock])
    _unlocked.upon(arm_turned, enter=_locked, outputs=[_engage_lock])
    _locked.upon(arm_turned, enter=_locked, outputs=[_nope])


turner = Turnstile(Lock())
print("Paying fare 1.")
turner.fare_paid()
print("Walking through.")
turner.arm_turned()
print("Jumping.")
turner.arm_turned()
print("Paying fare 2.")
turner.fare_paid()
print("Walking through 2.")
turner.arm_turned()
print("Done.")