Example #1
0
def a1():
    states = [
        'standing', # 站立
        'walking', # 不行
        {
            'name': 'caffeinated', # 精神抖擞
            'initial': 'dithering',
            'children':[
                'dithering', # 抖动
                'running', # 跑
            ]
        }
    ]
    transitions = [
        ['walk', 'standing', 'walking'], # 走 : 站立->步行
        ['stop', 'walking', 'standing'], # 停 : 步行->站立
        ['drink', "standing", 'caffeinated'], # 喝 : *->精神抖擞
        ['walk', ['caffeinated', 'caffeinated_dithering'], 'caffeinated_running'], # 走 :
        ['relax', 'caffeinated', 'standing'], # 放松 :
    ]

    machine = Machine(states=states, transitions=transitions, initial='standing', ignore_invalid_triggers=True)

    # machine.walk() # Walking now
    # machine.stop() # let's stop for a moment
    machine.drink() # coffee time
    print(machine.state)
Example #2
0
# states = ['standing', 'walking',
#           {'name': 'caffeinated', 'children':['dithering', 'running']}]
# transitions = [
#   ['walk', 'standing', 'walking'],
#   ['stop', 'walking', 'standing'],
#   ['drink', '*', 'caffeinated'],
#   ['walk', ['caffeinated', 'caffeinated_dithering'], 'caffeinated_running'],
#   ['relax', 'caffeinated', 'standing']
# ]

machine = Machine(states=states,
                  transitions=transitions,
                  initial='standing',
                  ignore_invalid_triggers=True)

machine.walk()  # Walking now
machine.stop()  # let's stop for a moment
machine.drink()  # coffee time
print(machine.state)

machine.walk()  # we have to go faster
print(machine.state)

machine.stop()  # can't stop moving!
print(machine.state)

machine.relax()  # leave nested state
print(machine.state)  # phew, what a ride

# machine.on_enter_caffeinated_running('callback_method')