def test_interruption_in_machines_with_sequential_state(capsys):

    ws1 = WaitState("ws1", 0.2)
    ws2 = WaitState("ws2", 0.2)
    ps1 = PrintState("ps1", "Print1")
    es = IdleState("es")
    iss = IdleState("iss")
    sm = SequentialState("sm", children=[ws1, ws2, ps1])
    sm.add_transition_on_success(es)
    sm.add_transition(lambda s, b: s._curr_child.checkName('ws2'), iss)

    exe = Machine("exe", sm, ["es", "iss"], rate=100)
    exe.run()
    assert exe._exception_raised_state_name == ""
    assert exe._internal_exception is None
    assert exe._status == StateStatus.SUCCESS
    assert not exe._run_thread.is_alive()
    assert exe._curr_state._name == 'iss'
    assert exe.is_end()
    assert capsys.readouterr().out == ""
    assert not sm._run_thread.is_alive()
    assert not ws1._run_thread.is_alive()
    assert not ws2._run_thread.is_alive()
    assert sm._status == StateStatus.INTERRUPTED
    assert ws2._status == StateStatus.INTERRUPTED
    assert ws1._status == StateStatus.SUCCESS
    assert ps1._status == StateStatus.UNKNOWN
    assert ws2.checkStatus(StateStatus.INTERRUPTED)
    assert ws1.checkStatus(StateStatus.SUCCESS)
Esempio n. 2
0
def test_parallel_one_state_exception(capsys):
    class ExceptionAfter1SecState(State):
        def execute(self, board):
            time.sleep(1)
            return StateStatus.EXCEPTION

    ws = WaitState("ws1", 5)
    fs = ExceptionAfter1SecState("fs")
    es = IdleState("es")
    fes = IdleState("fs-terminal")
    pm = ParallelState('pm', [ws, fs])
    pm.add_transition_on_success(es)
    pm.add_transition_on_failed(fes)
    exe = Machine("main_machine",
                  pm,
                  end_state_ids=['es', 'fs-terminal'],
                  rate=10)

    # run machine and see how it reacts
    exe.start(None)
    # wait for 0.5 second to see it is still running
    assert not exe.wait(0.5)
    assert exe.check_status(StateStatus.RUNNING)
    assert exe._curr_state.check_name('pm')
    # at this point, it should throw or raise the exception
    # wait another 1.5 seconds
    assert exe.wait(1.5)
    assert exe.check_status(StateStatus.EXCEPTION)
    assert not pm._run_thread.is_alive()
Esempio n. 3
0
def test_parallel_one_state_fails(capsys):
    class FailAfter1SecState(State):
        def execute(self, board):
            time.sleep(1)
            return StateStatus.FAILED

    ws = WaitState("ws1", 5)
    fs = FailAfter1SecState("fs")
    es = IdleState("es")
    fes = IdleState("fs-terminal")
    pm = ParallelState('pm', [ws, fs])
    pm.add_transition_on_success(es)
    pm.add_transition_on_failed(fes)
    exe = Machine("main_machine",
                  pm,
                  end_state_ids=['es', 'fs-terminal'],
                  rate=10)

    # run machine and see how it reacts
    exe.start(None)
    # wait for one second
    assert not exe.wait(0.5)
    assert exe.check_status(StateStatus.RUNNING)
    assert exe._curr_state.check_name('pm')
    # at this point ws should be done but ws2 is still going
    # wait another one seconds
    assert exe.wait(2)
    assert exe._curr_state == fes
    assert not pm._run_thread.is_alive()
Esempio n. 4
0
def make_machine(name):
    s1 = IdleState("s1")
    s2 = IdleState("s2")

    s1.add_transition_on_success(s2)

    return Machine(name, s1)
Esempio n. 5
0
def test_atleastone_interrupt(capsys):

    interrupted = False

    class WaitAndPrint(State):
        def execute(self, board: Board) -> typing.Optional[StateStatus]:
            time.sleep(0.5)
            if self.is_interrupted():
                nonlocal interrupted
                interrupted = True
                return StateStatus.INTERRUPTED
            print("HelloWorld")
            return StateStatus.SUCCESS

    one = AtLeastOneState("one", children=[
        PrintState('p5', "ps5"),
        WaitAndPrint("ws")
    ])
    es = IdleState("endState")
    one.add_transition_on_success(es)
    exe = Machine("xe", one, end_state_ids=["endState"], rate=10)
    exe.run()

    assert capsys.readouterr().out == "ps5\n"
    assert interrupted
Esempio n. 6
0
def test_print_state(capsys):

    print_text = "this is a print_text"
    ps = PrintState("p1", print_text)
    es = IdleState("endState")
    ps.add_transition_on_success(es)
    exe = Machine("xe", ps, end_state_ids=["endState"], rate=10)
    exe.run()
    captured = capsys.readouterr()
    assert captured.out == print_text + '\n'
def test_parse_debug_info():

    from behavior_machine.library import SequentialState, IdleState, WaitState
    from behavior_machine.core import Machine

    s1 = IdleState('s1')
    exe = Machine('exe', s1)
    info = exe.get_debug_info()
    parse_str = logging.parse_debug_info(info)
    assert parse_str[0] == 'exe(Machine) -- UNKNOWN'
    assert parse_str[1] == '  -> s1(IdleState) -- UNKNOWN'
Esempio n. 8
0
def test_wait_state():

    s1 = WaitState("s1", 2)
    s2 = IdleState("s2")
    s1.add_transition_on_success(s2)

    exe = Machine("test", s1, end_state_ids=['s2'], rate=10)

    start_time = time.time()
    exe.run()
    duration = time.time() - start_time
    # Because the waut these are executed, its hard to know the margin
    assert duration == pytest.approx(2, rel=0.1)
Esempio n. 9
0
def test_object_get_in_transition(capsys):
    class SetState(State):
        def execute(self, board: Board):
            obj = {'hello': [1, 2, 3], 'name': {'first': 'test'}}
            board.set('obj', obj)
            obj = {}
            return StateStatus.SUCCESS

    s = SetState('s')
    w = WaitState('w', 1)
    i = IdleState('i')
    end = IdleState('end')

    s.add_transition_on_success(w)
    w.add_transition_on_success(i)
    i.add_transition(
        lambda state, board: board.get('obj')['name']['first'] == 'test', end)
    exe = Machine('xe', s, end_state_ids=['end'])
    exe.run()
    assert exe.is_end()
    # Idle state returns RUNNING instead of SUCCESS
    assert exe._curr_state._status == StateStatus.RUNNING
def test_nested_sequential_state(capsys):
    ps1 = PrintState("ps1", "Print1")
    ps2 = PrintState("ps2", "Print2")
    ps3 = PrintState("ps3", "Print3")
    ps4 = PrintState("ps4", "Print4")
    es = IdleState("endState")

    sm = SequentialState("sm", children=[ps3, ps2])
    sm2 = SequentialState("sm2", children=[ps4, sm, ps1])
    sm2.add_transition_on_success(es)
    mach = Machine("xe", sm2, end_state_ids=['endState'], rate=10)
    mach.run()

    assert capsys.readouterr().out == "Print4\nPrint3\nPrint2\nPrint1\n"
def test_sequential_state_success(capsys):
    ps1 = PrintState("ps1", "Print1")
    ps2 = PrintState("ps2", "Print2")
    es = IdleState("es")
    seqs = SequentialState("sm", children=[ps1, ps2])
    seqs.add_transition_on_success(es)
    exe = Machine("m1", seqs, ['es'])
    exe.run()

    assert capsys.readouterr().out == "Print1\nPrint2\n"
    assert exe.is_end()
    assert exe._curr_state == es
    assert seqs._status == StateStatus.SUCCESS
    assert ps1._status == StateStatus.SUCCESS
    assert ps2._status == StateStatus.SUCCESS
def test_interrupt_in_parallel_state(capsys):
    ws = WaitState("ws1", 1)
    ws2 = WaitState("ws2", 2)
    es = IdleState("es")
    pm = ParallelState('pm', [ws, ws2])
    pm.add_transition_on_success(es)
    pm.add_transition(lambda x, y: True, es)
    exe = Machine("main_machine", pm, end_state_ids=['es'], rate=10)
    # run machine
    exe.start(None)
    # because of the always transition, it should happen in about 10
    assert exe.wait(0.2)
    assert ws.checkStatus(StateStatus.INTERRUPTED)
    assert ws2.checkStatus(StateStatus.INTERRUPTED)
    assert not pm._run_thread.is_alive()
Esempio n. 13
0
def test_interrupt_in_parallel_state(capsys):
    ws = WaitState("ws1", 1)
    ws2 = WaitState("ws2", 2)
    es = IdleState("es")
    pm = ParallelState('pm', [ws, ws2])
    pm.add_transition_on_success(es)
    pm.add_transition_after_elapsed(es, 0.1)
    exe = Machine("main_machine", pm, end_state_ids=['es'], rate=10)
    # run machine
    exe.start(None)
    # because of the elapsed transition, the machine will immediate transition to the end state in 0.1 seconds
    assert exe.wait(0.2)
    assert ws._status == StateStatus.INTERRUPTED
    assert ws2._status == StateStatus.INTERRUPTED
    assert not pm._run_thread.is_alive()
def test_sequential_state(capsys):

    ps1 = PrintState("ps1", "Print1")
    ps2 = PrintState("ps1", "Print2")
    ps3 = PrintState("ps1", "Print3")
    es = IdleState("endState")

    sm = SequentialState("sm", children=[ps2, ps3])
    sm.add_children(ps1)

    sm.add_transition_on_success(es)
    exe = Machine("xe", sm, end_state_ids=["endState"], rate=10)
    exe.run()

    assert capsys.readouterr().out == "Print2\nPrint3\nPrint1\n"
Esempio n. 15
0
def test_atleastone_state(capsys):

    ps1 = PrintState('p1', "ps1")
    ws1 = WaitState("w1", 0.5)
    ps2 = PrintState('p2', "ps2")

    one = AtLeastOneState("one", children=[
        ps2,
        SequentialState("seq", children=[
            ws1,
            ps1
        ])
    ])
    es = IdleState("endState")
    one.add_transition_on_success(es)
    exe = Machine("xe", one, end_state_ids=["endState"], rate=10)
    exe.run()

    assert capsys.readouterr().out == "ps2\n"
Esempio n. 16
0
def test_parallel_state_in_machine(capsys):
    ws = WaitState("ws1", 1)
    ws2 = WaitState("ws2", 2)
    es = IdleState("es")
    pm = ParallelState('pm', [ws, ws2])
    pm.add_transition_on_success(es)
    exe = Machine("main_machine", pm, end_state_ids=['es'], rate=10)
    # run machine and see how it reacts
    exe.start(None)
    # wait for one second
    assert not exe.wait(1.1)
    assert ws.check_status(StateStatus.SUCCESS)
    assert ws2.check_status(StateStatus.RUNNING)
    assert exe.check_status(StateStatus.RUNNING)
    # at this point ws should be done but ws2 is still going
    # wait another one seconds
    assert exe.wait(2)
    assert exe.check_status(StateStatus.SUCCESS)
    assert not pm._run_thread.is_alive()
def test_selector_state(capsys):
    class FailState(State):
        def execute(self, board: Board) -> StateStatus:
            print("failed1")
            return StateStatus.FAILED

    fs1 = FailState("fs")
    ps1 = PrintState("ps1", "Print1")
    ps2 = PrintState("ps2", "Print2")
    ps3 = PrintState("ps3", "Print3")
    es = IdleState("endState")

    sm = SelectorState("ss", children=[fs1, ps2, ps3])
    sm.add_children(ps1)

    sm.add_transition_on_success(es)
    exe = Machine("xe", sm, end_state_ids=["endState"], rate=10)
    exe.run()

    assert capsys.readouterr().out == "failed1\nPrint2\n"
def test_selector_state_all_failed():
    class FailState(State):
        def execute(self, board: Board) -> StateStatus:
            return StateStatus.FAILED

    fs1 = FailState("f1")
    fs2 = FailState("f2")
    fs3 = FailState("f3")
    es = IdleState("endState")

    sm = SelectorState("ss", children=[fs1, fs2, fs3])
    sm.add_transition_on_failed(es)

    exe = Machine("exe", sm, end_state_ids=['endState'], rate=10)
    exe.run()

    assert fs1.check_status(StateStatus.FAILED)
    assert fs2.check_status(StateStatus.FAILED)
    assert fs3.check_status(StateStatus.FAILED)
    assert sm.check_status(StateStatus.FAILED)
Esempio n. 19
0
def test_exception_in_parallel_state(capsys):

    error_text = "IndexErrorInTestEXCEPTION"

    class RaiseExceptionState(State):
        def execute(self, board):
            raise IndexError(error_text)

    ws = WaitState("ws1", 10)
    re = RaiseExceptionState("re")
    pm = ParallelState('pm', [ws, re])
    es = IdleState("es")
    pm.add_transition_on_success(es)
    exe = Machine("xe", pm, end_state_ids=['es', 'onException'], rate=10)
    # run machine and see how it reacted
    exe.start(None)
    exe.wait(0.5)
    assert exe._curr_state == pm
    assert exe.check_status(StateStatus.EXCEPTION)
    assert str(exe._internal_exception) == error_text
    assert exe._exception_raised_state_name == "xe.pm.re"
    assert not pm._run_thread.is_alive()
Esempio n. 20
0
def test_sequential_state_flow(capsys):

    flow_in_text = "test_sequential_state_flow"
    first_time = True

    class PreState(State):
        def execute(self, board: Board) -> StateStatus:
            self.flow_out = flow_in_text

    class ReceiveState(State):
        def execute(self, board):
            nonlocal first_time
            if first_time:
                assert self.flow_in == flow_in_text
                first_time = False
                print("one")
                return StateStatus.SUCCESS
            else:
                assert self.flow_in == None
                print("two")
                return StateStatus.FAILED
    
    ps = PreState("pre")
    ws = WaitState("ws1", 0.1)
    rs = ReceiveState("rs")
    es = IdleState("es")
    seqs = SequentialState('seqs', [rs, ws])
    
    ps.add_transition_on_complete(seqs)
    seqs.add_transition_on_success(seqs)
    seqs.add_transition_on_failed(es)

    me = Machine("me", ps, end_state_ids=['es'])
    me.start(None)
    me.wait()
    assert capsys.readouterr().out == "one\ntwo\n"
def test_exception_in_sequential_state(capsys):

    error_text = "IndexErrorInTestEXCEPTION"

    class RaiseExceptionState(State):
        def execute(self, board):
            raise IndexError(error_text)

    ps1 = PrintState("ps1", "Print1")
    ps2 = PrintState("ps1", "Print2")
    ps3 = PrintState("ps1", "Print3")
    rs = RaiseExceptionState("rs1")
    es = IdleState("endState")

    sm = SequentialState("sm", children=[ps2, ps3, rs])
    sm.add_children(ps1)

    sm.add_transition_on_success(es)
    exe = Machine("xe", sm, end_state_ids=["endState"])
    exe.run()

    assert capsys.readouterr().out == "Print2\nPrint3\n"
    assert str(exe._internal_exception) == error_text
    assert exe._exception_raised_state_name == "xe.sm.rs1"
Esempio n. 22
0
from behavior_machine.visualization import visualize_behavior_machine


def make_machine(name):
    s1 = IdleState("s1")
    s2 = IdleState("s2")

    s1.add_transition_on_success(s2)

    return Machine(name, s1)


m1 = make_machine("m1")
m2 = make_machine("m2")
m1.add_transition_on_success(m2)
xe = IdleState('xe')
m2.add_transition_on_failed(xe)

ss = SequentialState("ss",
                     children=[
                         IdleState("i1"),
                         IdleState("i2"),
                         IdleState("i3"),
                         IdleState("i4"),
                     ])
m1.add_transition(lambda s, b: True, ss)

pp = ParallelState("pp", children=[
    IdleState("i1"),
    IdleState("i2"),
])
Esempio n. 23
0
from behavior_machine.core import State, Machine
from behavior_machine.library import PrintState, SequentialState, IdleState
from behavior_machine.visualization import visualize_behavior_machine


ps1 = PrintState("ps1", "Hello World 1")
ps2 = PrintState("ps2", "Hello World 2")
is1 = IdleState("is1")
ps3 = PrintState("ps3", "Hello World 3")

ss = SequentialState("ss", children=[ps1, ps2])
ss.add_transition_on_success(ps3)

m1 = Machine("m1", ss, rate=10)
m1.add_transition(lambda state, board: state._curr_state._name == "ps3", is1)
m2 = Machine("m2", m1, end_state_ids=['is1'], rate=10)
m2.run()


visualize_behavior_machine(m2, "readme.png")