def execute_command_synchronized_on_state(state_machine,
                                          state_id,
                                          command,
                                          counter=1,
                                          join=True):
    old_run_id = state_machine.get_state_by_path(state_id).run_id
    old_execution_counter = state_machine.get_state_by_path(
        state_id)._execution_counter
    getattr(rafcon.core.singleton.state_machine_execution_engine, command)()
    # let the state start properly
    while old_run_id == state_machine.get_state_by_path(state_id).run_id:
        time.sleep(0.005)
    while old_execution_counter == state_machine.get_state_by_path(
            state_id)._execution_counter:
        time.sleep(0.005)
    if join:
        try:
            state_machine.get_state_by_path(state_id).join()
        except RuntimeError:
            # if the state is already executed then join() returns with an RuntimeError
            pass
    # let the hierarchy properly chose the next state
    wait_for_execution_engine_sync_counter(counter, logger)
Exemple #2
0
def test_run_this_state(caplog):
    # run_selected
    # Initialize testing environment
    testing_utils.initialize_environment_core()
    # Load State Machine
    sm = storage.load_state_machine_from_path(
        testing_utils.get_test_sm_path(
            os.path.join("unit_test_state_machines", "test_run_this_state")))
    rafcon.core.singleton.state_machine_manager.add_state_machine(sm)
    # Run selected state machine
    rafcon.core.singleton.global_variable_manager.set_variable("test_value", 1)
    state_machine_execution_engine.run_selected_state("BTWFZQ/EPQSTG",
                                                      sm.state_machine_id)
    wait_for_execution_engine_sync_counter(1, logger)
    state_machine_execution_engine.stop()
    rafcon.core.singleton.state_machine_execution_engine.join()
    # assert variable state
    try:
        assert rafcon.core.singleton.global_variable_manager.get_variable(
            "test_value") == 2
    # Shutdown testing environment
    finally:
        testing_utils.shutdown_environment_only_core(caplog=caplog)
def test_backward_stepping_library_state(gui):
    gui(initialize_global_variables)

    from rafcon.core.singleton import state_machine_execution_engine, state_machine_manager
    import rafcon.gui.singleton as gui_singleton

    menubar_ctrl = gui_singleton.main_window_controller.menu_bar_controller
    sm = gui(
        menubar_ctrl.on_open_activate, None, None,
        testing_utils.get_test_sm_path(os.path.join("unit_test_state_machines", "backward_step_library_execution_test"))
    )
    testing_utils.wait_for_gui()
    # reset the synchronization counter; although the tests run in different processes they share their memory
    # as the import statements are at the top of the file and not inside the parallel called functions
    with state_machine_execution_engine._status.execution_condition_variable:
        state_machine_execution_engine.synchronization_counter = 0

    gui(menubar_ctrl.on_step_mode_activate, None, None)
    current_state_machine_id = gui_singleton.state_machine_manager.active_state_machine_id
    state_machines_editor_tab_status_check(current_state_machine_id, active=True)  # execution start is synchronous
    wait_for_execution_engine_sync_counter(1, logger)

    # forward
    for i in range(5):
        gui(menubar_ctrl.on_step_into_activate, None, None)
        wait_for_execution_engine_sync_counter(1, logger)

    # backward
    for i in range(4):
        gui(menubar_ctrl.on_backward_step_activate, None, None)
        wait_for_execution_engine_sync_counter(1, logger)

    gui(menubar_ctrl.on_backward_step_activate, None, None)

    while not state_machine_execution_engine.finished_or_stopped():
        time.sleep(0.1)
    for key, sd in sm.root_state.scoped_data.items():
        if sd.name == "beer_count":
            assert sd.value == 100
    # stop or finished are asynchronous but the call_gui_callback makes the check synchronous
    gui(state_machines_editor_tab_status_check, current_state_machine_id, False)
def test_backward_stepping_preemptive_state(gui):
    gui(initialize_global_variables)

    from rafcon.core.singleton import state_machine_execution_engine

    menubar_ctrl = gui.singletons.main_window_controller.menu_bar_controller

    state_machine = gui(
        menubar_ctrl.on_open_activate, None, None,
        testing_utils.get_test_sm_path(os.path.join("unit_test_state_machines", "backward_step_preemtive_test"))
    )
    testing_utils.wait_for_gui()

    # reset the synchronization counter; although the tests run in different processes they share their memory
    # as the import statements are at the top of the file and not inside the parallel called functions
    with state_machine_execution_engine._status.execution_condition_variable:
        state_machine_execution_engine.synchronization_counter = 0

    gui(menubar_ctrl.on_step_mode_activate, None, None)
    current_state_machine_id = gui.singletons.state_machine_manager.active_state_machine_id
    state_machines_editor_tab_status_check(current_state_machine_id, active=True)  # execution start is synchronous

    wait_for_execution_engine_sync_counter(1, logger)

    for i in range(3):
        gui(menubar_ctrl.on_step_into_activate, None, None)
        wait_for_execution_engine_sync_counter(2, logger)

    gui(menubar_ctrl.on_step_into_activate, None, None)
    wait_for_execution_engine_sync_counter(1, logger)

    # preemptive concurrency state must be finished before the next step
    while not state_machine.get_state_by_path("AOURYA/LXEMOO").final_outcome:
        time.sleep(0.010)

    gui(menubar_ctrl.on_step_into_activate, None, None)
    wait_for_execution_engine_sync_counter(1, logger)

    # "take turn" state reached

    # backward
    for i in range(1):
        gui(menubar_ctrl.on_backward_step_activate, None, None)
        wait_for_execution_engine_sync_counter(1, logger)

    for i in range(3):
        gui(menubar_ctrl.on_backward_step_activate, None, None)
        wait_for_execution_engine_sync_counter(2, logger)

    state_machines_editor_tab_status_check(current_state_machine_id, active=True)
    gui(menubar_ctrl.on_backward_step_activate, None, None)

    while not state_machine_execution_engine.finished_or_stopped():
        time.sleep(0.1)
    # stop or finished are asynchronous but the call_gui_callback makes the check synchronous
    gui(state_machines_editor_tab_status_check, current_state_machine_id, False)

    gui(verify_execute_preemptive_state_forwards_backwards)
def test_backward_stepping_barrier_state(gui):
    gui(initialize_global_variables)

    from rafcon.core.singleton import state_machine_execution_engine, state_machine_manager
    import rafcon.gui.singleton as gui_singleton

    menubar_ctrl = gui_singleton.main_window_controller.get_controller('menu_bar_controller')

    sm = gui(
        menubar_ctrl.on_open_activate, None, None,
        testing_utils.get_test_sm_path(os.path.join("unit_test_state_machines", "backward_step_barrier_test"))
    )
    testing_utils.wait_for_gui()

    # reset the synchronization counter; although the tests run in different processes they share their memory
    # as the import statements are at the top of the file and not inside the parallel called functions
    with state_machine_execution_engine._status.execution_condition_variable:
        state_machine_execution_engine.synchronization_counter = 0

    gui(menubar_ctrl.on_step_mode_activate, sm.state_machine_id, None)
    wait_for_execution_engine_sync_counter(1, logger)

    for i in range(2):
        gui(menubar_ctrl.on_step_into_activate, None, None)
        wait_for_execution_engine_sync_counter(3, logger)

    gui(menubar_ctrl.on_step_over_activate, None, None)
    wait_for_execution_engine_sync_counter(3, logger)

    gui(menubar_ctrl.on_step_out_activate, None, None)
    wait_for_execution_engine_sync_counter(1, logger)

    for i in range(3):
        gui(menubar_ctrl.on_step_into_activate, None, None)
        wait_for_execution_engine_sync_counter(1, logger)

    # backward
    for i in range(3):
        gui(menubar_ctrl.on_backward_step_activate, None, None)
        wait_for_execution_engine_sync_counter(1, logger)

    print("cp1")

    for i in range(4):
        gui(menubar_ctrl.on_backward_step_activate, None, None)
        wait_for_execution_engine_sync_counter(3, logger)

    print("cp2")

    gui(menubar_ctrl.on_backward_step_activate, None, None)

    print("cp3")

    while not state_machine_execution_engine.finished_or_stopped():
        time.sleep(0.1)

    print("cp4")

    testing_utils.wait_for_gui()

    print("cp5")

    for key, sd in sm.root_state.scoped_data.items():
        if sd.name == "beer_number":
            assert sd.value == 100
        elif sd.name == "wine_number":
            assert sd.value == 40
        elif sd.name == "whiskey_number":
            assert sd.value == 20

    gui.expected_warnings = 3
    gui(menubar_ctrl.on_stop_activate, None)
def test_step_into_over_out_no_library(caplog):

    testing_utils.initialize_environment_core()

    state_machine = storage.load_state_machine_from_path(
        testing_utils.get_test_sm_path(
            os.path.join("unit_test_state_machines", "stepping_test")))

    rafcon.core.singleton.state_machine_manager.add_state_machine(
        state_machine)

    with state_machine_execution_engine._status.execution_condition_variable:
        state_machine_execution_engine.synchronization_counter = 0

    rafcon.core.singleton.state_machine_execution_engine.step_mode(
        state_machine.state_machine_id)
    wait_for_execution_engine_sync_counter(1, logger)

    # sm structure

    # GLSUJY
    # GLSUJY/PXTKIH
    # GLSUJY/NDIVLD
    # GLSUJY/SFZGMH

    # GLSUJY/SMCOIB
    # GLSUJY/SMCOIB/YSBJGK
    # GLSUJY/SMCOIB/OUWQUJ
    # GLSUJY/SMCOIB/UGGFFI

    execute_command_synchronized_on_state(state_machine, "GLSUJY/PXTKIH",
                                          "step_over", 1)
    execute_command_synchronized_on_state(state_machine, "GLSUJY/NDIVLD",
                                          "step_over", 1)
    execute_command_synchronized_on_state(state_machine, "GLSUJY/SFZGMH",
                                          "step_over", 1)
    execute_command_synchronized_on_state(state_machine, "GLSUJY/SMCOIB",
                                          "step_over", 1)

    execute_command_synchronized_on_state(state_machine, "GLSUJY/PXTKIH",
                                          "step_into")
    execute_command_synchronized_on_state(state_machine, "GLSUJY/NDIVLD",
                                          "step_into")
    execute_command_synchronized_on_state(state_machine, "GLSUJY/SFZGMH",
                                          "step_into")
    execute_command_synchronized_on_state(state_machine,
                                          "GLSUJY/SMCOIB",
                                          "step_into",
                                          join=False)
    execute_command_synchronized_on_state(state_machine,
                                          "GLSUJY/SMCOIB/YSBJGK", "step_into")

    rafcon.core.singleton.state_machine_execution_engine.step_out()
    wait_for_execution_engine_sync_counter(1, logger)

    rafcon.core.singleton.state_machine_execution_engine.stop()
    rafcon.core.singleton.state_machine_execution_engine.join()

    try:
        assert rafcon.core.singleton.global_variable_manager.get_variable(
            "bottles") == 95
    finally:
        testing_utils.shutdown_environment_only_core(caplog=caplog)
def test_step_through_library(caplog):

    testing_utils.initialize_environment_core()

    testing_utils.rewind_and_set_libraries({
        "unit_test_state_machines":
        os.path.join(testing_utils.TEST_ASSETS_PATH,
                     "unit_test_state_machines")
    })

    state_machine = storage.load_state_machine_from_path(
        testing_utils.get_test_sm_path(
            os.path.join("unit_test_state_machines",
                         "stepping_test_with_library")))

    rafcon.core.singleton.state_machine_manager.add_state_machine(
        state_machine)

    with state_machine_execution_engine._status.execution_condition_variable:
        state_machine_execution_engine.synchronization_counter = 0

    rafcon.core.singleton.state_machine_execution_engine.step_mode(
        state_machine.state_machine_id)
    wait_for_execution_engine_sync_counter(1, logger)

    # step till library
    rafcon.core.singleton.state_machine_execution_engine.step_over()
    wait_for_execution_engine_sync_counter(1, logger)

    rafcon.core.singleton.state_machine_execution_engine.step_over()
    wait_for_execution_engine_sync_counter(1, logger)

    rafcon.core.singleton.state_machine_execution_engine.step_over()
    wait_for_execution_engine_sync_counter(1, logger)

    # step into library and step out
    rafcon.core.singleton.state_machine_execution_engine.step_into()
    wait_for_execution_engine_sync_counter(1, logger)

    rafcon.core.singleton.state_machine_execution_engine.step_out()
    wait_for_execution_engine_sync_counter(1, logger)

    # step till library
    rafcon.core.singleton.state_machine_execution_engine.step_over()
    wait_for_execution_engine_sync_counter(1, logger)

    rafcon.core.singleton.state_machine_execution_engine.step_over()
    wait_for_execution_engine_sync_counter(1, logger)

    rafcon.core.singleton.state_machine_execution_engine.step_over()
    wait_for_execution_engine_sync_counter(1, logger)

    # step into library and step over library children
    rafcon.core.singleton.state_machine_execution_engine.step_into()
    wait_for_execution_engine_sync_counter(1, logger)

    rafcon.core.singleton.state_machine_execution_engine.step_over()
    wait_for_execution_engine_sync_counter(1, logger)

    # step over last library item
    rafcon.core.singleton.state_machine_execution_engine.step_over()
    wait_for_execution_engine_sync_counter(1, logger)

    rafcon.core.singleton.state_machine_execution_engine.stop()
    rafcon.core.singleton.state_machine_execution_engine.join()

    try:
        from rafcon.core.state_elements.scope import ScopedVariable
        for s in state_machine.root_state.scoped_data.values():
            if s.name == 'bottles' and s.data_port_type == ScopedVariable:
                assert s.value == 4
    finally:
        testing_utils.shutdown_environment_only_core(caplog=caplog)