def test_concurrency_barrier_save_load(caplog): concurrency_barrier_state = create_concurrency_barrier_state() state_machine = StateMachine(concurrency_barrier_state) test_path = testing_utils.get_unique_temp_path() storage.save_state_machine_to_path(state_machine, test_path) sm_loaded = storage.load_state_machine_from_path(test_path) root_state = sm_loaded.root_state input_data = {"input_data_port1": 0.1, "input_data_port2": 0.1} output_data = {"output_data_port1": None} root_state.input_data = input_data root_state.output_data = output_data state_machine = StateMachine(root_state) testing_utils.test_multithreading_lock.acquire() rafcon.core.singleton.state_machine_manager.add_state_machine(state_machine) rafcon.core.singleton.state_machine_execution_engine.start(state_machine.state_machine_id) rafcon.core.singleton.state_machine_execution_engine.join() try: assert rafcon.core.singleton.global_variable_manager.get_variable("var_x") == 10 assert rafcon.core.singleton.global_variable_manager.get_variable("var_y") == 20 assert root_state.final_outcome.outcome_id == 4 with pytest.raises(ValueError): concurrency_barrier_state.remove(UNIQUE_DECIDER_STATE_ID) with pytest.raises(AttributeError): concurrency_barrier_state.remove_state(UNIQUE_DECIDER_STATE_ID) rafcon.core.singleton.state_machine_manager.remove_state_machine(state_machine.state_machine_id) finally: testing_utils.shutdown_environment_only_core(caplog=caplog, expected_warnings=0, expected_errors=1)
def trigger_ungroup_signals(gui): import rafcon.gui.helpers.state as gui_helper_state import rafcon.gui.helpers.state_machine as gui_helper_state_machine sm_manager_model = gui.singletons.state_machine_manager_model main_window_controller = gui.singletons.main_window_controller menubar_ctrl = main_window_controller.menu_bar_controller state_machine = create_state_machine() first_sm_id = state_machine.state_machine_id gui(gui.core_singletons.state_machine_manager.add_state_machine, state_machine) gui(main_window_controller.view['main_window'].grab_focus) gui(sm_manager_model.__setattr__, "selected_state_machine_id", first_sm_id) state_machines_ctrl = main_window_controller.state_machines_editor_ctrl page_id = state_machines_ctrl.get_page_num(first_sm_id) page = state_machines_ctrl.view.notebook.get_nth_page(page_id) gui(focus_graphical_editor_in_page, page) sm_m = sm_manager_model.get_selected_state_machine_model() assert sm_m gui(gui_helper_state.change_state_type, sm_m.get_state_model_by_path("ROOTSTATE/STATE3"), gui_helper_state.BarrierConcurrencyState) gui(sm_m.selection.set, sm_m.get_state_model_by_path("ROOTSTATE/STATE3")) gui(gui_helper_state_machine.ungroup_selected_state) gui(menubar_ctrl.on_save_as_activate, None, None, testing_utils.get_unique_temp_path()) gui(menubar_ctrl.on_stop_activate, None)
def test_on_clean_storing_with_name_in_path(caplog): testing_utils.dummy_gui(None) testing_utils.initialize_environment(gui_config={ 'HISTORY_ENABLED': False, 'AUTO_BACKUP_ENABLED': False }, gui_already_started=False) path_old_format = testing_utils.get_test_sm_path( os.path.join("unit_test_state_machines", "id_to_name_plus_id_storage_format_test_do_not_update")) path_new_format = os.path.join( testing_utils.get_unique_temp_path(), "id_to_name_plus_id_storage_format_test_do_not_update") # gui imports better after initialization from rafcon.gui.models.state_machine import StateMachineModel shutil.copytree(path_old_format, path_new_format) from rafcon.core.storage import storage sm = storage.load_state_machine_from_path(path_new_format) check_state_recursively_if_state_scripts_are_valid(sm.root_state) sm.base_path = path_new_format sm_m = StateMachineModel(sm) try: on_save_activate(sm_m, logger) check_that_all_files_are_there(sm, with_print=False) check_id_and_name_plus_id_format(path_old_format, path_new_format, sm_m) finally: testing_utils.shutdown_environment(caplog=caplog, unpatch_threading=False)
def test_default_values_of_data_ports(caplog): storage_path = testing_utils.get_unique_temp_path() print(storage_path) sm = create_state_machine() storage.save_state_machine_to_path(sm, storage_path) sm_loaded = storage.load_state_machine_from_path(storage_path) root_state = sm_loaded.root_state state_machine = StateMachine(root_state) testing_utils.test_multithreading_lock.acquire() rafcon.core.singleton.state_machine_manager.add_state_machine(state_machine) rafcon.core.singleton.state_machine_execution_engine.start(state_machine.state_machine_id) rafcon.core.singleton.state_machine_execution_engine.join() rafcon.core.singleton.state_machine_manager.remove_state_machine(state_machine.state_machine_id) print(root_state.output_data) try: assert root_state.output_data["output_data_port1"] == "default_value" finally: testing_utils.shutdown_environment_only_core(caplog=caplog)
def save_state_machine(with_gui=True): import rafcon from rafcon.core.singleton import state_machine_execution_engine import rafcon.gui.singleton as gui_singleton from rafcon.core.storage import storage path = testing_utils.get_unique_temp_path() if with_gui: state_machine = call_gui_callback(create_models) else: state_machine = create_models() if with_gui: sm_model = rafcon.gui.singleton.state_machine_manager_model.state_machines[ state_machine.state_machine_id] menubar_ctrl = gui_singleton.main_window_controller.get_controller( 'menu_bar_controller') # sm_model.state_machine.base_path = path call_gui_callback(menubar_ctrl.on_save_as_activate, None, None, path) # call_gui_callback(menubar_ctrl.on_quit_activate, None) call_gui_callback(check_that_all_files_are_there, state_machine, with_print=False) else: storage.save_state_machine_to_path(state_machine, path, delete_old_state_machine=False) check_that_all_files_are_there(state_machine, with_print=False)
def test_runtime_checks_for_data_port_data_types(caplog): storage_path = testing_utils.get_unique_temp_path() print(storage_path) sm = create_state_machine2() # test output port type check script_text = 'def execute(self, inputs, outputs, gvm):\n' \ ' outputs["output_data_port1"] = [1, 2, 3]\n' \ ' return 3' sm.get_state_by_path('FirstLevel1/ZeroLevel2').script_text = script_text # TODO comment (next 2 lines) in after final fix of tuple storing bug is applied and remove 3 line (4 storage check) # storage.save_state_machine_to_path(sm, storage_path) # sm_loaded = storage.load_state_machine_from_path(storage_path) sm_loaded = sm root_state = sm_loaded.root_state state_machine = StateMachine(root_state) testing_utils.test_multithreading_lock.acquire() rafcon.core.singleton.state_machine_manager.add_state_machine(state_machine) rafcon.core.singleton.state_machine_execution_engine.start(state_machine.state_machine_id) rafcon.core.singleton.state_machine_execution_engine.join() rafcon.core.singleton.state_machine_manager.remove_state_machine(state_machine.state_machine_id) # 6 errors -> IN ORDER output port-, root state scoped-, input port-, output port-, root state scoped- and # root state output port-data-type-errors testing_utils.shutdown_environment_only_core(caplog=caplog, expected_errors=6)
def test_unchanged_storage_format(caplog): """This test ensures that the state machine storage format does not change in patch releases""" from rafcon.core.storage import storage from rafcon.gui.models.state_machine import StateMachineModel import rafcon path = get_backward_compatibility_state_machines_path() if not os.path.exists(path): logger.info( "test_unchanged_storage_format: the current python interpreter version is not supported" ) return testing_utils.initialize_environment( gui_config={ 'HISTORY_ENABLED': False, 'AUTO_BACKUP_ENABLED': False }, libraries={ 'unit_test_state_machines': testing_utils.get_test_sm_path("unit_test_state_machines") }, gui_already_started=False) try: current_rafcon_version = StrictVersion(rafcon.__version__).version current_minor = "{}.{}".format(current_rafcon_version[0], current_rafcon_version[1]) for filename in os.listdir(path): if filename.startswith(current_minor): old_state_machine_path = os.path.join(path, filename) break else: assert False, "There is no state machine for the current RAFCON minor version {}".format( current_minor) state_machine = storage.load_state_machine_from_path( old_state_machine_path) state_machine_m = StateMachineModel(state_machine) new_state_machine_path = testing_utils.get_unique_temp_path() storage.save_state_machine_to_path(state_machine, new_state_machine_path, True, True) state_machine_m.store_meta_data(copy_path=new_state_machine_path) old_state_machine_hash = calculate_state_machine_hash( old_state_machine_path) new_state_machine_hash = calculate_state_machine_hash( new_state_machine_path) assert old_state_machine_hash.digest( ) == new_state_machine_hash.digest() except Exception: raise finally: testing_utils.shutdown_environment(caplog=caplog, unpatch_threading=False)
def shutdown_gui(duration_wait_for_gui): # wait for menu bar and main window controller menubar_ctrl = None time.sleep(2) while menubar_ctrl is None: time.sleep(duration_wait_for_gui) menubar_ctrl = rafcon.gui.singleton.main_window_controller.get_controller('menu_bar_controller') if \ rafcon.gui.singleton.main_window_controller else None # quit rafcon by menu bar testing_utils.call_gui_callback(menubar_ctrl.on_save_as_activate, None, None, testing_utils.get_unique_temp_path()) testing_utils.call_gui_callback(menubar_ctrl.on_stop_activate, None) testing_utils.call_gui_callback(menubar_ctrl.on_quit_activate, None)
def test_storage_with_gui(gui): from rafcon.core.storage import storage path = testing_utils.get_unique_temp_path() state_machine = gui(create_models) if gui.with_gui: sm_model = gui.singletons.state_machine_manager_model.state_machines[ state_machine.state_machine_id] menubar_ctrl = gui.singletons.main_window_controller.menu_bar_controller gui(menubar_ctrl.on_save_as_activate, None, None, path) else: storage.save_state_machine_to_path(state_machine, path, delete_old_state_machine=False) gui(check_that_all_files_are_there, state_machine, with_print=False)
def trigger_issue_586_reproduction_sequence(): from os.path import join import rafcon.gui.singleton sm_manager_model = rafcon.gui.singleton.state_machine_manager_model main_window_controller = rafcon.gui.singleton.main_window_controller menubar_ctrl = main_window_controller.get_controller('menu_bar_controller') current_sm_length = len(sm_manager_model.state_machines) assert current_sm_length == 0 # backward step barrier test is chosen to work on an existing test state machine including equal child state ids call_gui_callback( menubar_ctrl.on_open_activate, None, None, join(testing_utils.TEST_ASSETS_PATH, "unit_test_state_machines", "backward_step_barrier_test")) sm_m = list(sm_manager_model.state_machines.values())[0] assert sm_m.state_machine_id == sm_manager_model.selected_state_machine_id concurrent_decimate_state_m = sm_m.get_state_model_by_path("GLSUJY/OOECFM") # check start conditions overlapping ids -> all states have four states and those have the same ids import rafcon.core.constants state_ids = list(concurrent_decimate_state_m.states.keys()) state_ids.remove(rafcon.core.constants.UNIQUE_DECIDER_STATE_ID) state_ids_set = set() for state_id in state_ids: for child_state_id in concurrent_decimate_state_m.states[ state_id].states.keys(): state_ids_set.add(child_state_id) assert len(state_ids_set) == 4 call_gui_callback(sm_m.selection.set, concurrent_decimate_state_m) call_gui_callback(menubar_ctrl.on_ungroup_state_activate, None, None) testing_utils.wait_for_gui() # ungroup all three child states which all have the same state ids as there child states plus data flows for state_id in state_ids: print("ungroup state:", state_id) assert state_id in sm_m.root_state.states child_state_m = sm_m.get_state_model_by_path("GLSUJY/" + state_id) call_gui_callback(sm_m.selection.set, child_state_m) call_gui_callback(menubar_ctrl.on_ungroup_state_activate, None, None) # store and refresh selected call_gui_callback(menubar_ctrl.on_save_as_activate, None, None, testing_utils.get_unique_temp_path()) call_gui_callback(menubar_ctrl.on_refresh_selected_activate, None, None)
def test_scoped_data(caplog): storage_path = testing_utils.get_unique_temp_path() sm = create_state_machine() storage.save_state_machine_to_path(sm, storage_path) sm_loaded = storage.load_state_machine_from_path(storage_path) state_machine = StateMachine(sm_loaded.root_state) with testing_utils.test_multithreading_lock: rafcon.core.singleton.state_machine_manager.add_state_machine(state_machine) rafcon.core.singleton.state_machine_execution_engine.start(state_machine.state_machine_id) rafcon.core.singleton.state_machine_execution_engine.join() rafcon.core.singleton.state_machine_manager.remove_state_machine(state_machine.state_machine_id) assert state_machine.root_state.output_data["data_output_port1"] == 42.0 testing_utils.assert_logger_warnings_and_errors(caplog)
def test_execution_log_without_file_system(caplog): try: testing_utils.initialize_environment_core( core_config={ 'IN_MEMORY_EXECUTION_HISTORY_ENABLE': True, 'FILE_SYSTEM_EXECUTION_HISTORY_ENABLE': False, 'EXECUTION_LOG_PATH': testing_utils.get_unique_temp_path() + '/test_execution_log' }) state_machine = global_storage.load_state_machine_from_path( testing_utils.get_test_sm_path( os.path.join("unit_test_state_machines", "execution_file_log_test"))) rafcon.core.singleton.state_machine_manager.add_state_machine( state_machine) rafcon.core.singleton.state_machine_execution_engine.start( state_machine.state_machine_id) while not state_machine.root_state.final_outcome: time.sleep(0.1) rafcon.core.singleton.state_machine_execution_engine.join() execution_history = state_machine.execution_histories[0] assert len(execution_history) == 32 assert isinstance(execution_history[0], StateMachineStartItem) assert isinstance(execution_history[1], CallItem) assert isinstance(execution_history[2], CallItem) assert isinstance(execution_history[3], ReturnItem) rafcon.core.singleton.state_machine_manager.remove_state_machine( state_machine.state_machine_id) except ImportError: # if pandas is not installed print("test_execution_log skipped as pandas is not installed") finally: testing_utils.shutdown_environment_only_core(caplog=caplog, expected_warnings=0, expected_errors=0)
def test_transition_creation(caplog): storage_path = testing_utils.get_unique_temp_path() sm = create_state_machine() storage.save_state_machine_to_path(sm, storage_path) sm_loaded = storage.load_state_machine_from_path(storage_path) root_state = sm_loaded.root_state state_machine = StateMachine(root_state) with testing_utils.test_multithreading_lock: rafcon.core.singleton.state_machine_manager.add_state_machine( state_machine) rafcon.core.singleton.state_machine_execution_engine.start( state_machine.state_machine_id) rafcon.core.singleton.state_machine_execution_engine.join() testing_utils.assert_logger_warnings_and_errors(caplog) rafcon.core.singleton.state_machine_manager.delete_all_state_machines()
def test_execution_log(caplog): testing_utils.initialize_environment_core( core_config={ 'IN_MEMORY_EXECUTION_HISTORY_ENABLE': False, 'FILE_SYSTEM_EXECUTION_HISTORY_ENABLE': False, 'EXECUTION_LOG_PATH': testing_utils.get_unique_temp_path() + '/test_execution_log' }) # this state machine features: # * hierarchies # * barrier concurrency # * execution states # * data flows # * logic flows state_machine = global_storage.load_state_machine_from_path( testing_utils.get_test_sm_path( os.path.join("unit_test_state_machines", "execution_file_log_test"))) rafcon.core.singleton.state_machine_manager.add_state_machine( state_machine) rafcon.core.singleton.state_machine_execution_engine.start( state_machine.state_machine_id) while not state_machine.root_state.final_outcome: time.sleep(0.1) rafcon.core.singleton.state_machine_execution_engine.join() rafcon.core.singleton.state_machine_manager.remove_state_machine( state_machine.state_machine_id) # the test assertions are that there are no errors/warnings testing_utils.shutdown_environment_only_core(caplog=caplog, expected_warnings=0, expected_errors=0)
def test_hierarchy_save_load_test(caplog): storage_path = testing_utils.get_unique_temp_path() hierarchy_state = create_hierarchy_state() sm = StateMachine(hierarchy_state) storage.save_state_machine_to_path(sm, storage_path) sm_loaded = storage.load_state_machine_from_path(storage_path) state_machine = StateMachine(sm_loaded.root_state) with testing_utils.test_multithreading_lock: rafcon.core.singleton.state_machine_manager.add_state_machine( state_machine) rafcon.core.singleton.state_machine_execution_engine.start( state_machine.state_machine_id) rafcon.core.singleton.state_machine_execution_engine.join() rafcon.core.singleton.state_machine_manager.remove_state_machine( state_machine.state_machine_id) assert state_machine.root_state.output_data["output1"] == 52.0 # 2 type error -> one child output port data type error and root state scoped data type error testing_utils.assert_logger_warnings_and_errors(caplog, expected_errors=2)
def trigger_gui_signals(gui): """The function triggers and test basic functions of the menu bar. At the moment those functions are TESTED, SHOULD or are THOUGHT about: - TESTED menu-bar: New State Machine and save state machine (check in list and in menu) -> after save list updated - TESTED menu-bar: Open State Machine no library (check in list and in menu) - TESTED lib-tree: Open library State Machine (check in list and in menu) - THOUGHT sub-dialog-lib-tree: sub with library State Machine (check in list and in menu) - no update - THOUGHT sub-dialog-lib-tree: sub with library State Machine as template (check in list and in menu) - no update - SHOULD menu-bar: Save state machine with existing state machine no changes (check in list and in menu) - with update - TESTED menu-bar: Save changed state machine with existing state machine (check in list and in menu) - menu-bar: Save state machine as with existing state machine (check in list and in menu) - THOUGHT tool-bar: Save state machine with existing state machine no changes (check in list and in menu) - with update - THOUGHT tool-bar: Save changed state machine with existing state machine (check in list and in menu) - TESTED tool-bar: Save state machine as with existing state machine (check in list and in menu) - THOUGHT right-click menu: Save state as (check in list and in menu) - TESTED auto backup: recover state machine from backup functionality (check in list and in menu) - no update - TESTED menu bar: open state machine by recent opened sub-menu - TESTED menu bar: try to open state machine that is not there by recent opened sub-menu without fatal failure - THOUGHT menu-bar: refresh - THOUGHT tool-bar: refresh - THOUGHT tool-bar: refresh selected """ # gui elements from rafcon.gui.controllers.main_window import MenuBarController from rafcon.gui.models.state_machine_manager import StateMachineManagerModel import rafcon.gui.helpers.state_machine as gui_helper_state_machine import rafcon.core.config from rafcon.core.states.library_state import LibraryState from rafcon.gui.helpers.label import get_label_of_menu_item_box sm_manager_model = gui.singletons.state_machine_manager_model main_window_controller = gui.singletons.main_window_controller menubar_ctrl = main_window_controller.menu_bar_controller global_runtime_config = gui.singletons.global_runtime_config state_machine = gui(create_state_machine) gui(rafcon.core.singleton.state_machine_manager.add_state_machine, state_machine) assert isinstance(menubar_ctrl, MenuBarController) assert isinstance(sm_manager_model, StateMachineManagerModel) #################### # POSITIVE EXAMPLES -> supposed to be added to the recently opened state machines list #################### # menu-bar: New State Machine and save state machine (check in list and in menu) -> after save list updated gui(testing_utils.wait_for_gui) current_sm_length = len(sm_manager_model.state_machines) first_sm_id = list(sm_manager_model.state_machines.keys())[0] gui(menubar_ctrl.on_new_activate, None) gui(sm_manager_model.__setattr__, "selected_state_machine_id", first_sm_id) gui(testing_utils.wait_for_gui) assert len(sm_manager_model.state_machines) == current_sm_length + 1 gui(menubar_ctrl.on_save_as_activate, None, None, testing_utils.get_unique_temp_path()) recently_opened_state_machines_paths = get_recently_opened_state_machines( gui) assert sm_manager_model.state_machines[ first_sm_id].state_machine.file_system_path == recently_opened_state_machines_paths[ 0] check_order_and_consistency_of_menu(gui, menubar_ctrl) gui(sm_manager_model.__setattr__, "selected_state_machine_id", first_sm_id + 1) gui(menubar_ctrl.on_save_as_activate, None, None, testing_utils.get_unique_temp_path()) gui(testing_utils.wait_for_gui) recently_opened_state_machines_paths = get_recently_opened_state_machines( gui) assert sm_manager_model.state_machines[ first_sm_id + 1].state_machine.file_system_path == recently_opened_state_machines_paths[ 0] # menu-bar: Open State Machine no library (check in list and in menu) basic_turtle_sm_path = join(testing_utils.TUTORIAL_PATH, "basic_turtle_demo_sm") gui(menubar_ctrl.on_open_activate, None, None, basic_turtle_sm_path) gui(testing_utils.wait_for_gui) recently_opened_state_machines_paths = get_recently_opened_state_machines( gui) assert basic_turtle_sm_path == recently_opened_state_machines_paths[0] check_order_and_consistency_of_menu(gui, menubar_ctrl) # menu-bar: Open State Machine no library and re-save it somewhere (check in list and in menu) turtle_state_machine_m = sm_manager_model.get_selected_state_machine_model( ) assert turtle_state_machine_m.state_machine.file_system_path == basic_turtle_sm_path gui(menubar_ctrl.on_save_as_activate, None, None, testing_utils.get_unique_temp_path()) recently_opened_state_machines_paths = get_recently_opened_state_machines( gui) assert turtle_state_machine_m.state_machine.file_system_path == recently_opened_state_machines_paths[ 0] check_order_and_consistency_of_menu(gui, menubar_ctrl) # lib-tree: Open library State Machine (check in list and library_path = join("generic", "dialog") library_name = "Dialog [3 options]" library_os_path = gui.singletons.library_manager.get_os_path_to_library( library_path, library_name)[0] gui(menubar_ctrl.on_open_activate, None, None, library_os_path) recently_opened_state_machines_paths = get_recently_opened_state_machines( gui) gui(testing_utils.wait_for_gui) lib_sm_m = sm_manager_model.get_selected_state_machine_model() assert library_os_path == recently_opened_state_machines_paths[0] assert lib_sm_m.state_machine.file_system_path == library_os_path check_order_and_consistency_of_menu(gui, menubar_ctrl) gui(sm_manager_model.__setattr__, "selected_state_machine_id", first_sm_id) gui(sm_manager_model.get_selected_state_machine_model().selection.set, sm_manager_model.get_selected_state_machine_model().root_state) gui(menubar_ctrl.on_add_state_activate, None, None) gui(menubar_ctrl.on_save_activate, None, None) assert sm_manager_model.state_machines[ first_sm_id].state_machine.file_system_path == recently_opened_state_machines_paths[ 0] assert library_os_path == recently_opened_state_machines_paths[1] check_order_and_consistency_of_menu(gui, menubar_ctrl) # open state machine by recent opened sub-menu in menu bar gui(global_runtime_config.update_recently_opened_state_machines_with, sm_manager_model.state_machines[first_sm_id].state_machine) first_sm_path = sm_manager_model.state_machines[ first_sm_id].state_machine.file_system_path gui(testing_utils.wait_for_gui) assert first_sm_path in get_label_of_menu_item_box( menubar_ctrl.view.sub_menu_open_recently.get_children()[2]) gui(sm_manager_model.state_machine_manager.remove_state_machine, first_sm_id) gui(menubar_ctrl.view.sub_menu_open_recently.get_children()[2].activate) gui(testing_utils.wait_for_gui) reopen_first_sm_id = sm_manager_model.selected_state_machine_id assert sm_manager_model.state_machines[ reopen_first_sm_id].state_machine.file_system_path == first_sm_path check_order_and_consistency_of_menu(gui, menubar_ctrl) # clean check after every update and re-save of library (both paths are in -> after enforcing by marked dirty flag) # TODO think about to remove this enforcement by marked dirty flag # change name to set marked dirty flag and enforce update of recent opened gui(lib_sm_m.state_machine.root_state.__setattr__, "name", "Different") shutil.rmtree(sm_manager_model.state_machines[reopen_first_sm_id]. state_machine.file_system_path) gui(sm_manager_model.__setattr__, "selected_state_machine_id", lib_sm_m.state_machine.state_machine_id) gui(testing_utils.wait_for_gui) gui(menubar_ctrl.on_save_as_activate, None, None, testing_utils.get_unique_temp_path()) print(recently_opened_state_machines_paths) assert lib_sm_m.state_machine.file_system_path == recently_opened_state_machines_paths[ 0] print( recently_opened_state_machines_paths, library_os_path, sm_manager_model.state_machines[reopen_first_sm_id].state_machine. file_system_path) assert sm_manager_model.state_machines[ reopen_first_sm_id].state_machine.file_system_path == recently_opened_state_machines_paths[ 1] gui(global_runtime_config.clean_recently_opened_state_machines) assert not sm_manager_model.state_machines[ reopen_first_sm_id].state_machine.file_system_path == recently_opened_state_machines_paths[ 1] assert library_os_path == recently_opened_state_machines_paths[1] check_order_and_consistency_of_menu(gui, menubar_ctrl) #################### # NEGATIVE EXAMPLES -> supposed to not been added to the recently opened state machines list #################### recently_opened_state_machines_paths = get_recently_opened_state_machines( gui) # if a LibraryState is created and insert no change should be in the recently opened state machine list gui(menubar_ctrl.on_new_activate, None) lib_state = gui(LibraryState, join("generic", "dialog"), "Dialog [3 options]", "0.1", "Dialog [3 options]") gui(gui_helper_state_machine.insert_state_into_selected_state, lib_state, True) assert recently_opened_state_machines_paths == get_recently_opened_state_machines( gui) # try to open state machine that is not there -> no fatal failure print("OPEN FAILURE CASE") gui(global_runtime_config.update_recently_opened_state_machines_with, lib_sm_m.state_machine) lib_sm_path = lib_sm_m.state_machine.file_system_path shutil.rmtree(lib_sm_m.state_machine.file_system_path) lib_sm_before_remove = lib_sm_m.state_machine gui(sm_manager_model.state_machine_manager.remove_state_machine, lib_sm_m.state_machine.state_machine_id) gui(testing_utils.wait_for_gui) gui(menubar_ctrl.view.sub_menu_open_recently.get_children()[2].activate) gui.expected_errors += 1 # was not open selected_sm_id = sm_manager_model.selected_state_machine_id assert not sm_manager_model.state_machines[ selected_sm_id].state_machine.file_system_path == lib_sm_path # is still in and after clean removed assert lib_sm_path in get_label_of_menu_item_box( menubar_ctrl.view.sub_menu_open_recently.get_children()[2]) gui(global_runtime_config.clean_recently_opened_state_machines) assert lib_sm_path not in get_label_of_menu_item_box( menubar_ctrl.view.sub_menu_open_recently.get_children()[2]) gui(global_runtime_config.update_recently_opened_state_machines_with, lib_sm_before_remove) assert 'NOT_ACCESSIBLE' in get_label_of_menu_item_box( menubar_ctrl.view.sub_menu_open_recently.get_children()[2]) # TODO maybe finally move this into the auto-backup or restore test module print("AUTO BACKUP TEST") number_of_open_sm = len(sm_manager_model.state_machines) backup_path = sm_manager_model.state_machines[ reopen_first_sm_id].auto_backup.meta['last_backup']['file_system_path'] from rafcon.gui.models import auto_backup gui(auto_backup.recover_state_machine_from_backup, backup_path) assert recently_opened_state_machines_paths == get_recently_opened_state_machines( gui) assert number_of_open_sm == len(sm_manager_model.state_machines) gui(sm_manager_model.state_machine_manager.remove_state_machine, reopen_first_sm_id) gui(testing_utils.wait_for_gui) assert recently_opened_state_machines_paths == get_recently_opened_state_machines( gui) assert number_of_open_sm == len(sm_manager_model.state_machines) + 1 gui(auto_backup.recover_state_machine_from_backup, backup_path, None, None, True) assert recently_opened_state_machines_paths == get_recently_opened_state_machines( gui) assert number_of_open_sm == len(sm_manager_model.state_machines) check_order_and_consistency_of_menu(gui, menubar_ctrl)
from rafcon.core.states.hierarchy_state import HierarchyState from rafcon.core.states.preemptive_concurrency_state import PreemptiveConcurrencyState from rafcon.core.states.barrier_concurrency_state import BarrierConcurrencyState from rafcon.core.storage import storage from rafcon.utils import log from tests import utils as testing_utils from tests.gui.widget.test_state_type_change import get_state_editor_ctrl_and_store_id_dict, check_elements_ignores from tests.gui.widget.test_states_editor import select_child_states_and_state_sequentially logger = log.get_logger(__name__) logger.setLevel(logging.VERBOSE) NO_SAVE = False TEST_PATH = testing_utils.get_unique_temp_path() def on_save_activate(state_machine_m, logger): if state_machine_m is None or NO_SAVE: return save_path = state_machine_m.state_machine.file_system_path if save_path is None: return logger.debug("Saving state machine to {0}".format(save_path)) storage.save_state_machine_to_path( state_machine_m.state_machine, state_machine_m.state_machine.file_system_path, delete_old_state_machine=True)
def test_execution_log(caplog): try: testing_utils.initialize_environment_core( core_config={ 'IN_MEMORY_EXECUTION_HISTORY_ENABLE': True, 'FILE_SYSTEM_EXECUTION_HISTORY_ENABLE': True, 'EXECUTION_LOG_PATH': testing_utils.get_unique_temp_path() + '/test_execution_log' }) state_machine = global_storage.load_state_machine_from_path( testing_utils.get_test_sm_path( os.path.join("unit_test_state_machines", "execution_file_log_test"))) rafcon.core.singleton.state_machine_manager.add_state_machine( state_machine) rafcon.core.singleton.state_machine_execution_engine.start( state_machine.state_machine_id) while not state_machine.root_state.final_outcome: time.sleep(0.1) rafcon.core.singleton.state_machine_execution_engine.join() import shelve import json ss = shelve.open(state_machine.get_last_execution_log_filename()) target_dict = {} # can be used for debugging for key, value in ss.items(): # print(value) target_dict[int(key[-3:].lstrip("0"))] = value assert len(ss) == 36 start, next, concurrent, hierarchy, collapsed_items = log_helper.log_to_collapsed_structure( ss) prod1_id = [ k for k, v in collapsed_items.items() if v['state_name'] == 'MakeProd1' ][0] prod1 = collapsed_items[prod1_id] assert prod1['scoped_data_ins']['product'] == 2 assert prod1['scoped_data_outs']['product'] == 2 assert prod1['outcome_name'] == 'success' assert prod1['semantic_data']['test_key'] == 'TestValue' prod2_id = [ k for k, v in collapsed_items.items() if v['state_name'] == 'MakeProd2' ][0] prod2 = collapsed_items[prod2_id] assert prod2['data_ins']['input_1'] == 0 assert prod2['data_outs']['output_1'] == 3 assert prod2['scoped_data_ins']['product'] == 1 assert prod2['scoped_data_outs']['product'] == 1 start_states = [ k for k, v in collapsed_items.items() if v['state_name'] == 'Start' and v['state_type'] == 'ExecutionState' ] assert len( start_states ) == 3 # Start state is executed three times until state machine is done start_id = start_states[0] start_item = collapsed_items[start_id] assert 'Starts the factory' in start_item['description'] df = log_helper.log_to_DataFrame(ss) all_starts = df.groupby('state_name').get_group('Start') assert len(all_starts) == 3 assert list( all_starts['outcome_name']) == ['success', 'success', 'done'] execution_history = state_machine.execution_histories[0] assert len(execution_history) == 32 assert isinstance(execution_history[0], StateMachineStartItem) assert isinstance(execution_history[1], CallItem) assert isinstance(execution_history[2], CallItem) assert isinstance(execution_history[3], ReturnItem) rafcon.core.singleton.state_machine_manager.remove_state_machine( state_machine.state_machine_id) except ImportError: # if pandas is not installed print("test_execution_log skipped as pandas is not installed") finally: testing_utils.shutdown_environment_only_core(caplog=caplog, expected_warnings=0, expected_errors=0)
def trigger_gui_signals_first_run(gui, open_state_machines): """The function triggers the creation of different state machines that should be backup-ed. In another run those are restored and checked onto correctness. At the moment TESTED, SHOULD or are THOUGHT about to generate the following state machines: - TESTED new state machine without storage - TESTED new state machine with storage and no changes - TESTED new state machine with storage and changes - TESTED state machine loaded and no changes - TESTED state machine loaded and changes - TESTED library not changed - TESTED library changed - TESTED change tab position - SHOULD not stored state machine that was removed/moved before restart - SHOULD stored state machine and no changes that was removed/moved before restart - SHOULD stored state machine and no changes that was removed/moved before restart """ import rafcon.gui.singleton from rafcon.core.states.hierarchy_state import HierarchyState from rafcon.gui.controllers.main_window import MenuBarController from rafcon.gui.models.state_machine_manager import StateMachineManagerModel from tests.gui.widget.test_state_type_change import get_state_editor_ctrl_and_store_id_dict testing_utils.wait_for_gui() main_window_controller = rafcon.gui.singleton.main_window_controller sm_manager_model = rafcon.gui.singleton.state_machine_manager_model library_manager = rafcon.gui.singleton.library_manager menubar_ctrl = main_window_controller.get_controller('menu_bar_controller') assert isinstance(menubar_ctrl, MenuBarController) assert isinstance(sm_manager_model, StateMachineManagerModel) def add_two_states_to_root_state_of_selected_state_machine(): sm_m = sm_manager_model.get_selected_state_machine_model() current_number_states = len(sm_m.root_state.states) call_gui_callback(sm_m.selection.set, sm_m.root_state) call_gui_callback(menubar_ctrl.on_add_state_activate, None) call_gui_callback(menubar_ctrl.on_add_state_activate, None) assert len(sm_m.root_state.states) == current_number_states + 2 assert sm_manager_model.get_selected_state_machine_model( ).state_machine.marked_dirty #################### # POSITIVE EXAMPLES -> supposed to be added to the open tabs list #################### # new state machine without storage state_machine = create_state_machine() gui(sm_manager_model.state_machine_manager.add_state_machine, state_machine) gui(testing_utils.wait_for_gui) print(sm_manager_model.state_machines.keys()) current_sm_id = list(sm_manager_model.state_machines.keys())[0] current_number_of_sm = len(sm_manager_model.state_machines) # new state machine with storage and no changes current_number_of_sm += 1 current_sm_id += 1 gui(menubar_ctrl.on_new_activate, None) gui(sm_manager_model.__setattr__, 'selected_state_machine_id', current_sm_id) assert len(sm_manager_model.state_machines) == current_number_of_sm gui(menubar_ctrl.on_save_as_activate, None, None, testing_utils.get_unique_temp_path()) # new state machine with storage and with changes current_number_of_sm += 1 current_sm_id += 1 gui(menubar_ctrl.on_new_activate, None) gui(sm_manager_model.__setattr__, 'selected_state_machine_id', current_sm_id) assert len(sm_manager_model.state_machines) == current_number_of_sm gui(menubar_ctrl.on_save_as_activate, None, None, testing_utils.get_unique_temp_path()) add_two_states_to_root_state_of_selected_state_machine() # state machine loaded and no changes current_number_of_sm += 1 current_sm_id += 1 basic_turtle_sm_path = join(testing_utils.TUTORIAL_PATH, "basic_turtle_demo_sm") gui(menubar_ctrl.on_open_activate, None, None, basic_turtle_sm_path) gui(sm_manager_model.__setattr__, 'selected_state_machine_id', current_sm_id) move_this_sm_id = sm_manager_model.selected_state_machine_id assert len(sm_manager_model.state_machines) == current_number_of_sm # state machine loaded and changes current_number_of_sm += 1 current_sm_id += 1 print("BUGS") basic_turtle_sm_path = join(testing_utils.TUTORIAL_PATH, "99_bugs") gui(menubar_ctrl.on_open_activate, None, None, basic_turtle_sm_path) gui(testing_utils.wait_for_gui) assert len(sm_manager_model.state_machines) == current_number_of_sm assert sm_manager_model.get_selected_state_machine_model( ).state_machine.file_system_path == basic_turtle_sm_path add_two_states_to_root_state_of_selected_state_machine() # library not changed (needs state machine that has meta data already -> that should not be changed by opening) print("LIB no changes") library_os_path = library_manager.get_os_path_to_library( "turtle_libraries", "clear_field")[0] gui(menubar_ctrl.on_open_activate, None, None, library_os_path) gui(testing_utils.wait_for_gui) # use artificial marked dirty to check for recovery of the flag assert not sm_manager_model.get_selected_state_machine_model( ).state_machine.marked_dirty sm_manager_model.get_selected_state_machine_model( ).state_machine._marked_dirty = True assert sm_manager_model.get_selected_state_machine_model( ).state_machine.marked_dirty # library with changes print("LIB with changes") library_os_path = library_manager.get_os_path_to_library( "turtle_libraries", "teleport_turtle")[0] gui(menubar_ctrl.on_open_activate, None, None, library_os_path) gui(testing_utils.wait_for_gui) lib_sm_m = sm_manager_model.get_selected_state_machine_model() def do_type_change(): [state_editor_ctrl, list_store_id_from_state_type_dict] = \ get_state_editor_ctrl_and_store_id_dict(lib_sm_m, lib_sm_m.root_state, main_window_controller, 5., logger) # - do state type change state_type_row_id = list_store_id_from_state_type_dict[ HierarchyState.__name__] state_editor_ctrl.get_controller('properties_ctrl').view[ 'type_combobox'].set_active(state_type_row_id) gui(lib_sm_m.selection.set, [lib_sm_m.root_state]) print(lib_sm_m.root_state) gui(do_type_change) print(lib_sm_m.root_state) add_two_states_to_root_state_of_selected_state_machine() print(lib_sm_m.root_state) # change tab position state_machines_editor_ctrl = main_window_controller.get_controller( 'state_machines_editor_ctrl') gui(state_machines_editor_ctrl.rearrange_state_machines, {move_this_sm_id: 0}) # defined selection sm_m = sm_manager_model.state_machines[move_this_sm_id] gui(sm_manager_model.__setattr__, 'selected_state_machine_id', move_this_sm_id) gui(sm_m.selection.set, list(sm_m.root_state.states.values())[0]) print("last state machine:", sm_m.state_machine.file_system_path) #################### # NEGATIVE EXAMPLES -> supposed to not been added to the recently opened state machines list #################### # state machine that was removed/moved before restart -> result in second run #################### # collect open state machine data #################### gui(prepare_tab_data_of_open_state_machines, main_window_controller, sm_manager_model, open_state_machines) #################### # shout down gui #################### gui(menubar_ctrl.on_stop_activate, None) # TODO why this is some how important for correct restore
def test_execution_log(caplog): try: testing_utils.initialize_environment_core( core_config={ 'EXECUTION_LOG_ENABLE': True, 'EXECUTION_LOG_PATH': testing_utils.get_unique_temp_path() + '/test_execution_log' }) state_machine = global_storage.load_state_machine_from_path( testing_utils.get_test_sm_path( os.path.join("unit_test_state_machines", "execution_file_log_test"))) rafcon.core.singleton.state_machine_manager.add_state_machine( state_machine) rafcon.core.singleton.state_machine_execution_engine.start( state_machine.state_machine_id) rafcon.core.singleton.state_machine_execution_engine.join() import shelve import json ss = shelve.open(state_machine.get_last_execution_log_filename()) assert len(ss) == 36 start, next, concurrent, hierarchy, collapsed_items = log_helper.log_to_collapsed_structure( ss) prod1_id = [ k for k, v in collapsed_items.items() if v['state_name'] == 'MakeProd1' ][0] prod1 = collapsed_items[prod1_id] assert prod1['scoped_data_ins']['product'] == 2 assert prod1['scoped_data_outs']['product'] == 2 assert prod1['outcome_name'] == 'success' assert prod1['semantic_data']['test_key'] == 'TestValue' prod2_id = [ k for k, v in collapsed_items.items() if v['state_name'] == 'MakeProd2' ][0] prod2 = collapsed_items[prod2_id] assert prod2['data_ins']['input_1'] == 0 assert prod2['data_outs']['output_1'] == 3 assert prod2['scoped_data_ins']['product'] == 1 assert prod2['scoped_data_outs']['product'] == 1 start_states = [ k for k, v in collapsed_items.items() if v['state_name'] == 'Start' and v['state_type'] == 'ExecutionState' ] assert len( start_states ) == 3 # Start state is executed three times until state machine is done start_id = start_states[0] start_item = collapsed_items[start_id] assert 'Starts the factory' in start_item['description'] df = log_helper.log_to_DataFrame(ss) all_starts = df.groupby('state_name').get_group('Start') assert len(all_starts) == 3 assert list( all_starts['outcome_name']) == ['success', 'success', 'done'] rafcon.core.singleton.state_machine_manager.remove_state_machine( state_machine.state_machine_id) except ImportError: # if pandas is not installed pass finally: testing_utils.shutdown_environment_only_core(caplog=caplog, expected_warnings=0, expected_errors=0)