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
|
#
# trafficlightstate.pystate
#
# state machine model of the states and associated behaviors and properties for each
# different state of a traffic light
# define state machine with transitions
# (states will be implemented as Python classes, so use name case appropriate for class names)
statemachine TrafficLightState:
Red -> Green
Green -> Yellow
Yellow -> Red
# statemachine only defines the state->state transitions - actual behavior and properties
# must be added separately
# define some class level constants
Red.cars_can_go = False
Yellow.cars_can_go = True
Green.cars_can_go = True
# setup some class level methods
def flash_crosswalk(s):
def flash():
print(f"{s}...{s}...{s}")
return flash
Red.crossing_signal = staticmethod(flash_crosswalk("WALK"))
Yellow.crossing_signal = staticmethod(flash_crosswalk("DONT WALK"))
Green.crossing_signal = staticmethod(flash_crosswalk("DONT WALK"))
# setup some instance methods
def wait(nSeconds):
def waitFn(self):
print("<wait %d seconds>" % nSeconds)
return waitFn
Red.delay = wait(20)
Yellow.delay = wait(3)
Green.delay = wait(15)
|