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_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_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"
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)
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
def test_interruption_in_sequential_state(capsys): ws1 = WaitState("ws1", 0.1) ws2 = WaitState("ws2", 0.1) ps1 = PrintState("ps1", "Print1") sm = SequentialState("sm", children=[ws1, ws2, ps1]) sm.start(None) sm.wait(0.15) sm.interrupt() assert capsys.readouterr().out == "" assert sm.checkStatus(StateStatus.INTERRUPTED) assert ws2.checkStatus(StateStatus.INTERRUPTED) assert ws1.checkStatus(StateStatus.SUCCESS) assert ps1.checkStatus(StateStatus.UNKNOWN) assert not sm._run_thread.is_alive() assert not ws1._run_thread.is_alive() assert not ws2._run_thread.is_alive()
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"
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_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"
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")