Esempio n. 1
0
def test_add_transiction_to_state():
    initial_state = State('Idle')
    target = State('Target')
    event = Event('D1CL', 'Door closed')
    initial_state.add_transition(event, target)
    assert len(initial_state.transitions) == 1
    assert initial_state.transitions[event.code] == target
Esempio n. 2
0
def state_machine(request):
    idle = State('Idle')
    a = State('A')
    b = State('B')
    idle.add_transition(Event('EVT1', 'Primer evento'), a)
    a.add_transition(Event('EVT2', 'Segundo evento'), b)
    sm = StateMachine(idle)
    return sm
Esempio n. 3
0
def test_state_machine():
    idle = State('idle')
    active = State('active')
    d1cl = Event('D1CL', 'Door 1 closed')
    idle.add_transition(d1cl, active)
    sm = StateMachine(idle)
    assert sm.start == sm.current_state == idle
    assert sm.all_states() == set([idle, active])
    sm.handle('D1CL')
    assert sm.current_state == active
Esempio n. 4
0
# States

idle = State('idle', 'En espera')
active = State('active', 'Activo')
key1 = State('k1', 'keyboard key 1 pressed')
key2 = State('k2', 'keyboard key 2 pressed')

# Events
door_closed = Event('D1CL', 'Door closed')
key_one_pressed = Event('KEY1', 'Key 1 pressed')
key_two_pressed = Event('KEY2', 'Key 2 pressed')
open_batcave = Event('OPEN', 'Open batcave')

# Transitions
idle.add_transition(door_closed, active)
active.add_transition(key_one_pressed, key1)
key1.add_transition(key_two_pressed, key2)
key2.add_action(open_batcave)
key2.add_transition(open_batcave, idle)

sm = StateMachine(idle)

print('All states:', ', '.join([_.name for _ in sm.all_states()]))

bus = NetworkBus()
bus.subscribe(sm)
bus.send('D1CL')
bus.send('KEY1')
bus.send('KEY2')