Exemple #1
0
def create_models_lib(output_list):
    from rafcon.core.states.library_state import LibraryState

    [state, sm_model, state_dict] = create_models()

    wait3 = LibraryState(name="Wait3",
                         library_path="generic",
                         library_name="wait")
    state_dict['Nested'].add_state(wait3)
    dialog2 = LibraryState(name="2 Option",
                           library_path=join("generic", "dialog"),
                           library_name="Dialog [2 options]")
    dialog3 = LibraryState(name="3 Option",
                           library_path=join("generic", "dialog"),
                           library_name="Dialog [3 options]")
    state_dict['Nested'].add_state(dialog2)
    state_dict['Container'].add_state(dialog3)
    last_wins = LibraryState(name="last wins",
                             library_path="unit_test_state_machines",
                             library_name="last_data_wins_test")
    new_state_id = state_dict['Container'].add_state(last_wins.state_copy)

    for state_id in sm_model.root_state.states[new_state_id].states:
        state_m = sm_model.root_state.states[new_state_id].states[state_id]
        print(state_m.state.state_id, state_m.state.get_path(), state_m.meta)

    # sm_loaded = storage.load_state_machine_from_path(
    #     os.path.join(testing_utils.TEST_PATH, "assets", "unit_test_state_machines", "last_data_wins_test"))
    # # root_state = sm_loaded.root_state
    # # state_machine = StateMachine(root_state)
    # rafcon.core.singleton.state_machine_manager.add_state_machine(sm_loaded)
    # sm_model = rafcon.gui.singleton.state_machine_manager_model.state_machines[sm_loaded.state_machine_id]
    # return sm_loaded.root_state, sm_model, {}
    output_list.append(sm_model)
def create_execution_state_library_state_machine():
    rafcon.core.singleton.library_manager.initialize()
    library_container_state = HierarchyState("libContainerState",
                                             state_id="libContainerState")
    lib_state = LibraryState("temporary_libraries",
                             "execution_library",
                             "0.1",
                             "library_execution_state",
                             state_id="library_execution_state")
    library_container_state.add_state(lib_state)
    library_container_state.set_start_state(lib_state.state_id)

    library_container_state.add_transition(lib_state.state_id, 0,
                                           library_container_state.state_id, 0)
    lib_container_input = library_container_state.add_input_data_port(
        "data_input_port1", "float", 32.0)
    lib_container_output = library_container_state.add_output_data_port(
        "data_output_port1", "float")
    library_container_state.add_data_flow(
        library_container_state.state_id, lib_container_input,
        lib_state.state_id,
        lib_state.get_io_data_port_id_from_name_and_type(
            "data_input_port1", InputDataPort))
    library_container_state.add_data_flow(
        lib_state.state_id,
        lib_state.get_io_data_port_id_from_name_and_type(
            "data_output_port1", OutputDataPort),
        library_container_state.state_id, lib_container_output)
    return StateMachine(library_container_state)
Exemple #3
0
def execute(self, inputs, outputs, gvm):
    self.logger.debug("Insert state")
    #libary_path = "unit_test_state_machines/bake_libraries"
    #libary_name = "bake_library1"
    libary_path = inputs["library_path"]
    libary_name = inputs["library_name"]
    s = LibraryState(libary_path,
                     libary_name,
                     name=libary_name,
                     state_id="test_state")
    call_gui_callback(self.parent.add_state, s)
    wait_for_gui()
    call_gui_callback(self.parent.add_transition, self.state_id, 0, s.state_id,
                      None)
    wait_for_gui()
    call_gui_callback(self.parent.add_transition, s.state_id, 0, "CXVBON",
                      None)
    wait_for_gui()
    parent_id = None
    for d_id, d in self.parent.input_data_ports.items():
        if d.name == "str_variable":
            parent_id = d_id
    call_gui_callback(self.parent.add_data_flow, self.parent.state_id,
                      parent_id, s.state_id, 0)
    wait_for_gui()
    outputs["generated_state_id"] = s.state_id
    time.sleep(1.0)
    return 0
 def get_library_instance(self, library_path, library_name):
     """Generate a Library instance from within libraries dictionary tree."""
     if self.is_library_in_libraries(library_path, library_name):
         from rafcon.core.states.library_state import LibraryState
         return LibraryState(library_path, library_name, "0.1")
     else:
         logger.warning("Library manager will not create a library instance which is not in the mounted libraries.")
Exemple #5
0
    def _get_selected_library_state(self):
        """Returns the LibraryState which was selected in the LibraryTree

        :return: selected state in TreeView
        :rtype: LibraryState
        """
        (model, row) = self.view.get_selection().get_selected()
        library_item_key = model[row][self.ID_STORAGE_ID]
        library_item = model[row][self.ITEM_STORAGE_ID]
        library_path = model[row][self.LIB_PATH_STORAGE_ID]

        if isinstance(library_item, dict):
            return None
        assert isinstance(library_item, str)
        library_file_system_path = library_item

        logger.debug(
            "Link library state '{0}' (with library tree path: {2} and file system path: {1}) into state "
            "machine.".format(
                str(library_item_key), library_file_system_path,
                self.convert_if_human_readable(str(library_path)) + "/" +
                str(library_item_key)))
        library_name = library_file_system_path.split(os.path.sep)[-1]
        return LibraryState(library_path, library_name, "0.1",
                            format_folder_name_human_readable(library_name))
def test_gui(gui):
    """The function triggers and test basic functions of the menu bar and configuration model to test partially 
    initiated models.

    - test partial initiated generic/dialog/Dialog [3 options] -> covers bug issue #658
        
    """
    from os.path import join

    from rafcon.core.states.library_state import LibraryState
    import rafcon.gui.helpers.state_machine as gui_helper_state_machine
    main_window_controller = gui.singletons.main_window_controller
    menubar_ctrl = main_window_controller.get_controller('menu_bar_controller')

    gui(menubar_ctrl.on_new_activate, None)

    lib_state = LibraryState(join("generic", "dialog"), "Dialog [3 options]", "0.1", "Dialog [3 options]")
    gui(gui_helper_state_machine.insert_state_into_selected_state, lib_state, False)

    lib_tree_ctrl = main_window_controller.get_controller('library_controller')
    from rafcon.gui.controllers.library_tree import LibraryTreeController
    assert isinstance(lib_tree_ctrl, LibraryTreeController)

    ##############################################################
    print("\n" * 3 + "#" * 60 + "\n# Insert a library with may library depth level 1 " + "\n" * 3)
    ##############################################################
    library_path, library_name = ('generic/dialog', 'Show dialog')
    gui(lib_tree_ctrl.select_library_tree_element_of_lib_tree_path, join(library_path, library_name))
    gui(lib_tree_ctrl.insert_button_clicked, None, False)

    ##############################################################
    print("\n" * 3 + "#" * 60 + "\n# Insert a library with may library depth level 2 " + "\n" * 3)
    ##############################################################
    library_path, library_name = ('generic/dialog', 'Dialog [3 options]')
    gui(lib_tree_ctrl.select_library_tree_element_of_lib_tree_path, join(library_path, library_name))
    gui(lib_tree_ctrl.insert_button_clicked, None, False)

    ##############################################################
    print("\n" * 3 + "#" * 60 + "\n# Insert a library with may library depth level 1 as template " + "\n" * 3)
    ##############################################################
    library_path, library_name = ('generic/dialog', 'Show dialog')
    gui(lib_tree_ctrl.select_library_tree_element_of_lib_tree_path, join(library_path, library_name))
    gui(lib_tree_ctrl.insert_button_clicked, None, True)

    ##############################################################
    print("\n" * 3 + "#" * 60 + "\n# Insert a library with may library depth level 2 as template " + "\n" * 3)
    ##############################################################
    library_path, library_name = ('generic/dialog', 'Dialog [3 options]')
    gui(lib_tree_ctrl.select_library_tree_element_of_lib_tree_path, join(library_path, library_name))
    gui(lib_tree_ctrl.insert_button_clicked, None, True)
Exemple #7
0
    def _get_selected_library_state(self):
        """Returns the LibraryState which was selected in the LibraryTree

        :return: selected state in TreeView
        :rtype: LibraryState
        """
        library_os_path, library_path, library_name, item_key = self.extract_library_properties_from_selected_row()
        if library_path is None:
            return None

        logger.debug("Link library state '{0}' (with library tree path: {2} and file system path: {1}) into state "
                     "machine.".format(str(item_key), library_os_path,
                                       self.convert_if_human_readable(str(library_path)) + "/" + str(item_key)))
        library_name = library_os_path.split(os.path.sep)[-1]
        return LibraryState(library_path, library_name, "0.1", format_folder_name_human_readable(library_name))
Exemple #8
0
def execute(self, inputs, outputs, gvm):
    self.logger.debug("Insert library state")
    #libary_path = "unit_test_state_machines/bake_libraries"
    #libary_name = "bake_library1"
    libary_path = "unit_test_state_machines/dynamic_library_insertion_libraries"
    libary_name = "test_library"
    e = LibraryState(libary_path, libary_name, name="test_library_state")
    self.parent.add_state(e)
    self.parent.add_transition(self.state_id, 0, e.state_id, None)
    wait_for_gui()
    self.parent.add_transition(e.state_id, 0, self.parent.state_id, 0)
    wait_for_gui()
    parent_id = None
    for d_id, d in self.parent.output_data_ports.items():
        if d.name == "output_0":
            parent_id = d_id
    self.parent.add_data_flow(e.state_id, 2, self.parent.state_id, parent_id)
    wait_for_gui()
    return 0
def create_models_lib(output_list):
    from rafcon.core.states.library_state import LibraryState
    import rafcon.gui.helpers.state as gui_helper_state
    import rafcon.gui.helpers.meta_data as gui_helper_meta_data
    [state, sm_model, state_dict] = create_models()
    root_state_m = sm_model.get_state_model_by_path(
        state_dict['Container'].get_path())

    wait3 = LibraryState(name="Wait3",
                         library_path="generic",
                         library_name="wait")
    state_dict['Nested'].add_state(wait3)
    dialog2 = LibraryState(name="2 Option",
                           library_path=join("generic", "dialog"),
                           library_name="Dialog [2 options]")
    dialog3 = LibraryState(name="3 Option",
                           library_path=join("generic", "dialog"),
                           library_name="Dialog [3 options]")
    state_dict['Nested'].add_state(dialog2)
    state_dict['Container'].add_state(dialog3)
    # insert useful meta data
    d3_m = sm_model.get_state_model_by_path(dialog3.get_path())
    gui_helper_meta_data.put_default_meta_on_state_m(
        d3_m, root_state_m,
        len(root_state_m.states) - 1)
    msg = gui_helper_meta_data.MetaSignalMsg(d3_m,
                                             'set_default_meta_data',
                                             affects_children=True)
    root_state_m.meta_signal.emit(msg)

    # insert library state with correct meta data -> this sm has at the moment only meta data for the root state
    last_wins = LibraryState(name="last wins",
                             library_path="unit_test_state_machines",
                             library_name="last_data_wins_test")
    last_wins_root_state = last_wins.state_copy
    gui_helper_state.insert_state_as(root_state_m, last_wins, True)
    new_state_id = last_wins_root_state.state_id

    for state_id in sm_model.root_state.states[new_state_id].states:
        state_m = sm_model.root_state.states[new_state_id].states[state_id]
        print(state_m.state.state_id, state_m.state.get_path(), state_m.meta)

    # sm_loaded = storage.load_state_machine_from_path(
    #     os.path.join(testing_utils.TEST_PATH, "assets", "unit_test_state_machines", "last_data_wins_test"))
    # # root_state = sm_loaded.root_state
    # # state_machine = StateMachine(root_state)
    # rafcon.core.singleton.state_machine_manager.add_state_machine(sm_loaded)
    # sm_model = rafcon.gui.singleton.state_machine_manager_model.state_machines[sm_loaded.state_machine_id]
    # return sm_loaded.root_state, sm_model, {}
    output_list.append(sm_model)
def test_nested_library_state_machine(caplog):
    with testing_utils.test_multithreading_lock:
        # TODO: the library_manager is initialized a second time here
        rafcon.core.singleton.library_manager.initialize()
        nested_library_state = LibraryState("temporary_libraries",
                                            "library_with_nested_library",
                                            "0.1", "nested_library_state_name",
                                            "nested_library_state_id")
        state_machine = StateMachine(nested_library_state)

        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()

        # print output_data["data_output_port1"]
        assert nested_library_state.output_data["data_output_port1"] == 42.0
        rafcon.core.singleton.state_machine_manager.remove_state_machine(
            state_machine.state_machine_id)
        testing_utils.assert_logger_warnings_and_errors(caplog)
Exemple #11
0
def trigger_menu_bar_items(gui, with_refresh=True, with_substitute_library=True):
    """The function triggers and test basic functions of the menu bar.

    At the moment those functions are tested:
    - New State Machine
    - Open State Machine
    - Copy State/HierarchyState -> via GraphicalEditor
    - Cut State/HierarchyState -> via GraphicalEditor
    - Paste State/HierarchyState -> via GraphicalEditor
    - Refresh Libraries
    - Refresh All
    - Save as
    - Stop State Machine
    - Quit GUI
    """
    from rafcon.core.states.library_state import LibraryState
    import rafcon.core.singleton
    import rafcon.gui.singleton
    import rafcon.gui.helpers.state as gui_helper_state
    import rafcon.gui.helpers.state_machine as gui_helper_state_machine
    from rafcon.gui.controllers.library_tree import LibraryTreeController
    from rafcon.core.states.barrier_concurrency_state import UNIQUE_DECIDER_STATE_ID
    from rafcon.core.states.state import StateType
    sm_manager_model = rafcon.gui.singleton.state_machine_manager_model
    main_window_controller = rafcon.gui.singleton.main_window_controller
    menubar_ctrl = main_window_controller.menu_bar_controller
    lib_tree_ctrl = main_window_controller.library_controller
    state_machines_ctrl = main_window_controller.state_machines_editor_ctrl
    assert isinstance(lib_tree_ctrl, LibraryTreeController)

    state_machine = create_state_machine()
    first_sm_id = state_machine.state_machine_id
    gui(rafcon.core.singleton.state_machine_manager.add_state_machine, state_machine)

    current_sm_length = len(sm_manager_model.state_machines)
    gui(menubar_ctrl.on_new_activate, None)

    # test decider state removal of barrier state
    sm_m = sm_manager_model.state_machines[first_sm_id + 1]
    page = state_machines_ctrl.get_page_for_state_machine_id(sm_m.state_machine_id)

    # Tests for issue #712
    # Copy an InputDataPort from a root state to an ExecutionState already having an InputDataPort
    gui(sm_m.selection.set, [sm_m.root_state])
    gui(gui_helper_state_machine.add_new_state, sm_m, StateType.EXECUTION)
    execution_state_m = list(sm_m.root_state.states.values())[0]
    gui(sm_m.root_state.state.add_input_data_port, "i1", int, data_port_id=0)
    gui(execution_state_m.state.add_input_data_port, "i1", int, data_port_id=0)
    root_input_port_m = sm_m.root_state.get_input_data_port_m(0)
    copy_port(gui, sm_m, root_input_port_m, menubar_ctrl)
    paste(gui, sm_m, execution_state_m, main_window_controller, menubar_ctrl, page)
    gui(sm_m.selection.set, execution_state_m)
    gui(menubar_ctrl.on_delete_activate, None, None)

    # TODO check why I have had to move this test above the bug tests below?!
    # Create BarrierConcurrencyState and try to delete DeciderState (should fail with exception)
    gui(sm_m.selection.set, [sm_m.root_state])
    gui(gui_helper_state_machine.add_new_state, sm_m, StateType.BARRIER_CONCURRENCY)
    barrier_state_m = list(sm_m.root_state.states.values())[0]
    decider_state_path = "/".join([barrier_state_m.state.get_path(), UNIQUE_DECIDER_STATE_ID])
    gui(sm_m.selection.set, sm_m.get_state_model_by_path(decider_state_path))
    gui(menubar_ctrl.on_delete_activate, None, None)
    gui(sm_m.root_state.state.remove_state, barrier_state_m.state.state_id)

    # Tests for issue #717 and #726
    # create self transition and self data flow and perform state type change and substitute state
    gui(sm_m.selection.set, [sm_m.root_state])
    gui(gui_helper_state_machine.add_new_state, sm_m, StateType.EXECUTION)
    execution_state_m = list(sm_m.root_state.states.values())[0]
    new_state_id = execution_state_m.state.state_id
    idp_id = gui(execution_state_m.state.add_input_data_port, "i1", int, data_port_id=0)
    odp_id = gui(execution_state_m.state.add_output_data_port, "o1", int, data_port_id=1)
    # add self transition and self data flow
    gui(execution_state_m.parent.state.add_transition, new_state_id, 0, new_state_id, None)
    gui(execution_state_m.parent.state.add_data_flow, new_state_id, odp_id, new_state_id, idp_id)
    gui(sm_m.selection.set, execution_state_m)
    # test issue #717
    from tests.gui.widget.test_state_type_change import get_state_editor_ctrl_and_store_id_dict
    from rafcon.core.states.hierarchy_state import HierarchyState
    [state_editor_ctrl, list_store_id_from_state_type_dict] = gui(get_state_editor_ctrl_and_store_id_dict,
                                                                  sm_m, execution_state_m, main_window_controller, 5.,
                                                                  logger)
    state_type_row_id = list_store_id_from_state_type_dict[HierarchyState.__name__]
    gui(state_editor_ctrl.get_controller('properties_ctrl').view['type_combobox'].set_active, state_type_row_id)
    new_state = sm_m.root_state.states[new_state_id]
    assert len(new_state.parent.transitions) == 1 and len(new_state.parent.data_flows) == 1
    # test issue #726
    hierarchy_state_m = sm_m.root_state.states[new_state_id]
    gui(hierarchy_state_m.outcomes[0].__setattr__, "name", "end")  # rename to let the transition survive
    gui(sm_m.selection.set, hierarchy_state_m)
    library_path, library_name = ('generic', 'wait')
    gui(lib_tree_ctrl.select_library_tree_element_of_lib_tree_path, join(library_path, library_name))
    gui(lib_tree_ctrl.substitute_as_library_clicked, None, True)
    assert len(sm_m.root_state.states) == 1
    wait_state_m = list(sm_m.root_state.states.values())[0]
    assert len(new_state.parent.transitions) == 1
    t_m = list(new_state.parent.transitions)[0]
    assert t_m.transition.from_state == t_m.transition.to_state and \
           t_m.transition.from_state == wait_state_m.state.state_id
    gui(sm_m.root_state.state.remove_state, wait_state_m.state.state_id)
    first_sm_id += 1  # count state machine id once up because of library substitution (loads a state machine, too)

    assert len(sm_manager_model.state_machines) == current_sm_length + 1
    gui(menubar_ctrl.on_open_activate, None, None, join(testing_utils.TUTORIAL_PATH, "basic_turtle_demo_sm"))
    gui(testing_utils.wait_for_gui)
    assert len(sm_manager_model.state_machines) == current_sm_length + 2

    sm_m = sm_manager_model.state_machines[first_sm_id + 2]
    testing_utils.wait_for_gui()
    # MAIN_WINDOW NEEDS TO BE FOCUSED (for global input focus) TO OPERATE PASTE IN GRAPHICAL VIEWER
    main_window_controller.view['main_window'].grab_focus()
    gui(sm_manager_model.__setattr__, "selected_state_machine_id", first_sm_id + 2)
    page_id = state_machines_ctrl.get_page_num(first_sm_id + 2)
    page = state_machines_ctrl.view.notebook.get_nth_page(page_id)
    gui(focus_graphical_editor_in_page, page)

    # TODO keep core interface, too
    # ##########################################################
    # # group states
    # # TODO improve test to related data flows
    # state_m_parent = sm_m.get_state_model_by_path('CDMJPK/RMKGEW/KYENSZ')
    # state_ids_old = [state_id for state_id in state_m_parent.state.states]
    # gui(state_m_parent.state.group_states, ['PAYECU', 'UEPNNW', 'KQDJYS'])
    #
    # ##########################################################
    # # ungroup new state
    # state_new = None
    # for state_id in state_m_parent.state.states:
    #     if state_id not in state_ids_old:
    #         state_new = state_m_parent.state.states[state_id]
    # gui(state_m_parent.state.ungroup_state, state_new.state_id)

    ##########################################################
    # group states
    # TODO improve test to related data flows
    print("#" * 30, "\n", '#### group states \n', "#" * 30, "\n")
    state_m_parent = sm_m.get_state_model_by_path('CDMJPK/RMKGEW/KYENSZ')
    state_ids_old = [state_id for state_id in state_m_parent.state.states]
    state_m_list = [state_m_parent.states[child_state_id] for child_state_id in ['PAYECU', 'UEPNNW', 'KQDJYS']]
    gui(gui_helper_state.group_states_and_scoped_variables, state_m_list, [])

    ##########################################################
    # ungroup new state
    print("#" * 30, "\n", '#### ungroup state \n', "#" * 30, "\n")
    new_state = None
    for state_id in state_m_parent.state.states:
        if state_id not in state_ids_old:
            new_state = state_m_parent.state.states[state_id]
    gui(gui_helper_state.ungroup_state, sm_m.get_state_model_by_path(new_state.get_path()))

    #########################################################
    print("select & copy an execution state -> and paste it somewhere")
    select_and_paste_state(gui, sm_m, sm_m.get_state_model_by_path('CDMJPK/RMKGEW/KYENSZ'),
                           sm_m.get_state_model_by_path(
                               'CDMJPK/RMKGEW'), menubar_ctrl, 'copy', main_window_controller, page)

    ###########################################################
    print("select & copy a hierarchy state -> and paste it some where")
    select_and_paste_state(gui, sm_m, sm_m.get_state_model_by_path('CDMJPK/RMKGEW/KYENSZ/VCWTIY'),
                           sm_m.get_state_model_by_path('CDMJPK'), menubar_ctrl, 'copy', main_window_controller, page)

    ##########################################################
    print("select a library state -> and paste it some where WITH CUT !!!")
    state_m, old_child_state_count = select_and_paste_state(gui, sm_m,
                                                            sm_m.get_state_model_by_path('CDMJPK/RMKGEW/KYENSZ/VCWTIY'),
                                                            sm_m.get_state_model_by_path('CDMJPK'), menubar_ctrl, 'cut',
                                                            main_window_controller, page)

    ##########################################################
    # create complex state with all elements
    gui(sm_m.selection.set, [sm_m.get_state_model_by_path('CDMJPK'), ])
    lib_state = 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 len(state_m.state.states) == old_child_state_count + 2

    state = None
    for state in state_m.state.states.values():
        if state.name == "Dialog [3 options]":
            break
    assert state is not None
    new_template_state = state
    gui(new_template_state.add_scoped_variable, 'scoopy', float, 0.3)
    state_m_to_copy = sm_m.get_state_model_by_path('CDMJPK/' + new_template_state.state_id)

    ##########################################################
    print("copy & paste complex state into itself")

    copy_and_paste_state_into_itself(gui, sm_m, state_m_to_copy, page, menubar_ctrl)
    print("increase complexity by doing it twice -> increase the hierarchy-level")
    copy_and_paste_state_into_itself(gui, sm_m, state_m_to_copy, page, menubar_ctrl)

    ##########################################################
    # substitute state with template
    old_keys = list(state_m_parent.state.states.keys())
    transitions_before, data_flows_before = state_m_parent.state.get_connections_for_state('RQXPAI')

    # lib_state = gui(rafcon.gui.singleton.library_manager.get_library_instance, 'generic', 'wait')
    # CORE LEVEL VERSION OF A SUBSTITUTE STATE EXAMPLE
    # gui(state_m_parent.state.substitute_state, 'RQXPAI', lib_state.state_copy)
    # SAME WITH THE GUI AND HELPER METHOD
    # gui(gui_helper_state.substitute_state_as, state_m_parent.states['RQXPAI'], lib_state, True)
    # GUI LEVEL SUBSTITUTE STATE EXAMPLE WITH CORRECT META DATA HANDLING TODO why those above produce wrong meta data?
    gui(sm_m.selection.set, [state_m_parent.states['RQXPAI'], ])
    library_path, library_name = ('generic', 'wait')
    gui(lib_tree_ctrl.select_library_tree_element_of_lib_tree_path, join(library_path, library_name))
    gui(lib_tree_ctrl.substitute_as_template_clicked, None, True)

    new_state_id = None
    for state_id in state_m_parent.state.states.keys():
        if state_id not in old_keys:
            new_state_id = state_id
    transitions_after, data_flows_after = state_m_parent.state.get_connections_for_state(new_state_id)
    # transition is not preserved because of unequal outcome naming
    assert len(transitions_before['external']['ingoing']) == 1
    assert len(transitions_after['external']['ingoing']) == 1
    assert len(transitions_before['external']['outgoing']) == 1
    assert len(transitions_after['external']['outgoing']) == 0
    gui(state_m_parent.state.add_transition, new_state_id, 0, 'MCOLIQ', None)
    print("XXX1")
    # modify the template with other data type and respective data flows to parent
    gui(list(state_m_parent.states[new_state_id].state.input_data_ports.items())[0][1].__setattr__, "data_type", "int")
    gui(state_m_parent.state.add_input_data_port, 'in_time', "int")
    gui(state_m_parent.state.add_data_flow,
        state_m_parent.state.state_id,
        list(state_m_parent.state.input_data_ports.items())[0][1].data_port_id,
        new_state_id,
        list(state_m_parent.states[new_state_id].state.input_data_ports.items())[0][1].data_port_id)

    ##########################################################
    # substitute state with library
    old_keys = list(state_m_parent.state.states.keys())
    transitions_before, data_flows_before = state_m_parent.state.get_connections_for_state(new_state_id)

    # lib_state = gui(rafcon.gui.singleton.library_manager.get_library_instance, 'generic', 'wait')
    # CORE LEVEL VERSION OF A SUBSTITUTE STATE EXAMPLE
    # gui(state_m_parent.state.substitute_state, new_state_id, lib_state)
    # SAME WITH THE GUI AND HELPER METHOD
    # gui(gui_helper_state.substitute_state_as, state_m_parent.states[new_state_id], lib_state, False)
    # GUI LEVEL SUBSTITUTE STATE EXAMPLE WITH CORRECT META DATA HANDLING TODO why those above produce wrong meta data?
    gui(sm_m.selection.set, [state_m_parent.states[new_state_id], ])
    library_path, library_name = ('generic', 'wait')
    gui(lib_tree_ctrl.select_library_tree_element_of_lib_tree_path, join(library_path, library_name))
    gui(lib_tree_ctrl.substitute_as_library_clicked, None, True)

    new_state_id = None
    for state_id in list(state_m_parent.state.states.keys()):
        if state_id not in old_keys:
            new_state_id = state_id
    transitions_after, data_flows_after = state_m_parent.state.get_connections_for_state(new_state_id)
    # test if data flow is ignored
    assert len(transitions_before['external']['ingoing']) == 1
    assert len(transitions_after['external']['ingoing']) == 1
    assert len(transitions_before['external']['outgoing']) == 1
    assert len(transitions_after['external']['outgoing']) == 1
    assert len(data_flows_before['external']['ingoing']) == 1
    assert len(data_flows_after['external']['ingoing']) == 0

    # data flow is preserved if right data type and name is used
    gui(list(state_m_parent.state.input_data_ports.items())[0][1].__setattr__, "data_type", "float")
    if isinstance(state_m_parent.state.states[new_state_id], LibraryState):
        data_port_id = list(state_m_parent.state.states[new_state_id].input_data_ports.items())[0][0]
        state_m_parent.state.states[new_state_id].use_runtime_value_input_data_ports[data_port_id] = True
        state_m_parent.state.states[new_state_id].input_data_port_runtime_values[data_port_id] = 2.0
        print()
    else:
        raise
        # state_m_parent.state.states[new_state_id].input_data_ports.items()[0][1].default_value = 2.0
    gui(state_m_parent.state.add_data_flow,
        state_m_parent.state.state_id,
        list(state_m_parent.state.input_data_ports.items())[0][1].data_port_id,
        new_state_id,
        list(state_m_parent.states[new_state_id].state.input_data_ports.items())[0][1].data_port_id)

    old_keys = list(state_m_parent.state.states.keys())
    transitions_before, data_flows_before = state_m_parent.state.get_connections_for_state(new_state_id)
    # CORE LEVEL VERSION OF A SUBSTITUTE STATE EXAMPLE
    # lib_state = rafcon.gui.singleton.library_manager.get_library_instance('generic', 'wait')
    # gui(state_m_parent.state.substitute_state, new_state_id, lib_state.state_copy)
    # GUI LEVEL SUBSTITUTE STATE EXAMPLE WITH CORRECT META DATA HANDLING TODO why those above produce wrong meta data?
    gui(sm_m.selection.set, [state_m_parent.states[new_state_id], ])
    library_path, library_name = ('generic', 'wait')
    gui(lib_tree_ctrl.select_library_tree_element_of_lib_tree_path, join(library_path, library_name))
    gui(lib_tree_ctrl.substitute_as_template_clicked, None, True)
    new_state_id = None
    for state_id in list(state_m_parent.state.states.keys()):
        if state_id not in old_keys:
            new_state_id = state_id
    transitions_after, data_flows_after = state_m_parent.state.get_connections_for_state(new_state_id)
    # test if data flow is ignored
    assert len(transitions_before['external']['ingoing']) == 1
    assert len(transitions_after['external']['ingoing']) == 1
    assert len(transitions_before['external']['outgoing']) == 1
    assert len(transitions_after['external']['outgoing']) == 1
    assert len(data_flows_before['external']['ingoing']) == 1
    assert len(data_flows_after['external']['ingoing']) == 1
    assert list(state_m_parent.state.states[new_state_id].input_data_ports.items())[0][1].default_value == 2.0

    ##########################################################
    # open separately
    gui(sm_m.selection.set, [sm_m.get_state_model_by_path('CDMJPK'), ])
    lib_state = LibraryState(join("generic", "dialog"), "Dialog [3 options]", "0.1", "Dialog [3 options]")
    gui(gui_helper_state_machine.insert_state_into_selected_state, lib_state, False)

    lib_hash = lib_state.state_copy.mutable_hash()
    # assert lib_state.mutable_hash().hexdigest() == lib_hash.hexdigest()
    sm_m = sm_manager_model.state_machines[lib_state.get_state_machine().state_machine_id]
    gui(sm_m.selection.set, [sm_m.get_state_model_by_path(lib_state.get_path())])
    gui(gui_helper_state_machine.open_library_state_separately)
    sm_m = sm_manager_model.get_selected_state_machine_model()
    assert sm_m.root_state.state.mutable_hash().hexdigest() == lib_hash.hexdigest()
    ##########################################################

    if with_substitute_library:
        ##########################################################
        # check substitute library state as template -> keep name
        old_parent = lib_state.parent
        state_ids = list(old_parent.states.keys())
        gui(lib_state.__setattr__, 'name', 'DIALOG_X')
        gui(sm_manager_model.__setattr__, 'selected_state_machine_id',
            lib_state.get_state_machine().state_machine_id)
        gui(gui_helper_state_machine.substitute_selected_library_state_with_template, True)  # keep_name=True
        new_states = [state for state in list(old_parent.states.values()) if state.state_id not in state_ids]
        assert new_states and len(new_states) == 1 and new_states[0].name == 'DIALOG_X'
        ##########################################################

    if with_refresh:
        gui(menubar_ctrl.on_refresh_libraries_activate)
        gui(testing_utils.wait_for_gui)
        gui(menubar_ctrl.on_refresh_all_activate, None, None, True)
        gui(testing_utils.wait_for_gui)
        assert len(sm_manager_model.state_machines) == 2

    gui.expected_errors = 1
def test_library_relocation_exception():
    logger.info("Load not existing library, expect exception to be raised...")
    with raises(LibraryNotFoundException):
        print(LibraryState('aasdasd', 'basdasd', allow_user_interaction=False))
Exemple #13
0
def trigger_gui_signals(with_refresh=True, with_substitute_library=True):
    """The function triggers and test basic functions of the menu bar.

    At the moment those functions are tested:
    - New State Machine
    - Open State Machine
    - Copy State/HierarchyState -> via GraphicalEditor
    - Cut State/HierarchyState -> via GraphicalEditor
    - Paste State/HierarchyState -> via GraphicalEditor
    - Refresh Libraries
    - Refresh All
    - Save as
    - Stop State Machine
    - Quit GUI
    """
    from os.path import join
    from rafcon.core.states.library_state import LibraryState
    import rafcon.core.singleton
    import rafcon.gui.singleton
    import rafcon.gui.helpers.state as gui_helper_state
    import rafcon.gui.helpers.state_machine as gui_helper_state_machine
    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')

    state_machine = create_state_machine()
    first_sm_id = state_machine.state_machine_id
    call_gui_callback(rafcon.core.singleton.state_machine_manager.add_state_machine, state_machine)

    current_sm_length = len(sm_manager_model.state_machines)
    call_gui_callback(menubar_ctrl.on_new_activate, None)

    # test decider state removal of barrier state
    sm_m = sm_manager_model.state_machines[first_sm_id + 1]
    call_gui_callback(sm_m.selection.set, [sm_m.root_state])
    from rafcon.core.states.barrier_concurrency_state import UNIQUE_DECIDER_STATE_ID
    from rafcon.core.states.state import StateType
    call_gui_callback(gui_helper_state_machine.add_new_state, sm_m, StateType.BARRIER_CONCURRENCY)
    decider_state_path = "/".join([list(sm_m.root_state.states.values())[0].state.get_path(), UNIQUE_DECIDER_STATE_ID])
    call_gui_callback(sm_m.selection.set, sm_m.get_state_model_by_path(decider_state_path))
    call_gui_callback(menubar_ctrl.on_delete_activate, None, None)

    assert len(sm_manager_model.state_machines) == current_sm_length + 1
    call_gui_callback(menubar_ctrl.on_open_activate, None, None, join(testing_utils.TUTORIAL_PATH,
                                                                      "basic_turtle_demo_sm"))
    call_gui_callback(testing_utils.wait_for_gui)
    assert len(sm_manager_model.state_machines) == current_sm_length + 2

    sm_m = sm_manager_model.state_machines[first_sm_id + 2]
    testing_utils.wait_for_gui()
    # MAIN_WINDOW NEEDS TO BE FOCUSED (for global input focus) TO OPERATE PASTE IN GRAPHICAL VIEWER
    main_window_controller.view['main_window'].grab_focus()
    call_gui_callback(sm_manager_model.__setattr__, "selected_state_machine_id", first_sm_id + 2)
    state_machines_ctrl = main_window_controller.get_controller('state_machines_editor_ctrl')
    page_id = state_machines_ctrl.get_page_num(first_sm_id + 2)
    page = state_machines_ctrl.view.notebook.get_nth_page(page_id)
    call_gui_callback(focus_graphical_editor_in_page, page)

    # TODO keep core interface, too
    # ##########################################################
    # # group states
    # # TODO improve test to related data flows
    # state_m_parent = sm_m.get_state_model_by_path('CDMJPK/RMKGEW/KYENSZ')
    # state_ids_old = [state_id for state_id in state_m_parent.state.states]
    # call_gui_callback(state_m_parent.state.group_states, ['PAYECU', 'UEPNNW', 'KQDJYS'])
    #
    # ##########################################################
    # # ungroup new state
    # state_new = None
    # for state_id in state_m_parent.state.states:
    #     if state_id not in state_ids_old:
    #         state_new = state_m_parent.state.states[state_id]
    # call_gui_callback(state_m_parent.state.ungroup_state, state_new.state_id)

    ##########################################################
    # group states
    # TODO improve test to related data flows
    print("#"*30, "\n", '#### group states \n', "#"*30, "\n")
    state_m_parent = sm_m.get_state_model_by_path('CDMJPK/RMKGEW/KYENSZ')
    state_ids_old = [state_id for state_id in state_m_parent.state.states]
    state_m_list = [state_m_parent.states[child_state_id] for child_state_id in ['PAYECU', 'UEPNNW', 'KQDJYS']]
    call_gui_callback(gui_helper_state.group_states_and_scoped_variables, state_m_list, [])

    ##########################################################
    # ungroup new state
    print("#"*30, "\n", '#### ungroup state \n', "#"*30, "\n")
    new_state = None
    for state_id in state_m_parent.state.states:
        if state_id not in state_ids_old:
            new_state = state_m_parent.state.states[state_id]
    call_gui_callback(gui_helper_state.ungroup_state, sm_m.get_state_model_by_path(new_state.get_path()))

    #########################################################
    print("select & copy an execution state -> and paste it somewhere")
    select_and_paste_state(sm_m, sm_m.get_state_model_by_path('CDMJPK/RMKGEW/KYENSZ'), sm_m.get_state_model_by_path(
        'CDMJPK/RMKGEW'), menubar_ctrl, 'copy', main_window_controller, page)

    ###########################################################
    print("select & copy a hierarchy state -> and paste it some where")
    select_and_paste_state(sm_m, sm_m.get_state_model_by_path('CDMJPK/RMKGEW/KYENSZ/VCWTIY'),
                           sm_m.get_state_model_by_path('CDMJPK'), menubar_ctrl, 'copy', main_window_controller, page)

    ##########################################################
    print("select a library state -> and paste it some where WITH CUT !!!")
    state_m, old_child_state_count = select_and_paste_state(sm_m,
                                                            sm_m.get_state_model_by_path('CDMJPK/RMKGEW/KYENSZ/VCWTIY'),
                                                            sm_m.get_state_model_by_path('CDMJPK'), menubar_ctrl, 'cut',
                                                            main_window_controller, page)

    ##########################################################
    # create complex state with all elements
    call_gui_callback(sm_m.selection.set, [sm_m.get_state_model_by_path('CDMJPK'), ])
    lib_state = LibraryState(join("generic", "dialog"), "Dialog [3 options]", "0.1", "Dialog [3 options]")
    call_gui_callback(gui_helper_state_machine.insert_state_into_selected_state, lib_state, True)
    assert len(state_m.state.states) == old_child_state_count + 2

    state = None
    for state in state_m.state.states.values():
        if state.name == "Dialog [3 options]":
            break
    assert state is not None
    new_template_state = state
    call_gui_callback(new_template_state.add_scoped_variable, 'scoopy', float, 0.3)
    state_m_to_copy = sm_m.get_state_model_by_path('CDMJPK/' + new_template_state.state_id)

    ##########################################################
    print("copy & paste complex state into itself")

    copy_and_paste_state_into_itself(sm_m, state_m_to_copy, page, menubar_ctrl)
    print("increase complexity by doing it twice -> increase the hierarchy-level")
    copy_and_paste_state_into_itself(sm_m, state_m_to_copy, page, menubar_ctrl)

    ##########################################################
    # substitute state with template
    lib_state = rafcon.gui.singleton.library_manager.get_library_instance('generic', 'wait')
    old_keys = list(state_m_parent.state.states.keys())
    transitions_before, data_flows_before = state_m_parent.state.related_linkage_state('RQXPAI')
    call_gui_callback(state_m_parent.state.substitute_state, 'RQXPAI', lib_state.state_copy)
    new_state_id = None
    for state_id in state_m_parent.state.states.keys():
        if state_id not in old_keys:
            new_state_id = state_id
    transitions_after, data_flows_after = state_m_parent.state.related_linkage_state(new_state_id)
    # transition is not preserved because of unequal outcome naming
    assert len(transitions_before['external']['ingoing']) == 1
    assert len(transitions_after['external']['ingoing']) == 1
    assert len(transitions_before['external']['outgoing']) == 1
    assert len(transitions_after['external']['outgoing']) == 0
    call_gui_callback(state_m_parent.state.add_transition, new_state_id, 0, 'MCOLIQ', None)

    # modify the template with other data type and respective data flows to parent
    call_gui_callback(list(state_m_parent.states[new_state_id].state.input_data_ports.items())[0][1].__setattr__, "data_type", "int")
    call_gui_callback(state_m_parent.state.add_input_data_port, 'in_time', "int")
    call_gui_callback(state_m_parent.state.add_data_flow,
                      state_m_parent.state.state_id,
                      list(state_m_parent.state.input_data_ports.items())[0][1].data_port_id,
                      new_state_id,
                      list(state_m_parent.states[new_state_id].state.input_data_ports.items())[0][1].data_port_id)

    old_keys = list(state_m_parent.state.states.keys())
    transitions_before, data_flows_before = state_m_parent.state.related_linkage_state(new_state_id)
    lib_state = rafcon.gui.singleton.library_manager.get_library_instance('generic', 'wait')
    call_gui_callback(state_m_parent.state.substitute_state, new_state_id, lib_state)
    new_state_id = None
    for state_id in list(state_m_parent.state.states.keys()):
        if state_id not in old_keys:
            new_state_id = state_id
    transitions_after, data_flows_after = state_m_parent.state.related_linkage_state(new_state_id)
    # test if data flow is ignored
    assert len(transitions_before['external']['ingoing']) == 1
    assert len(transitions_after['external']['ingoing']) == 1
    assert len(transitions_before['external']['outgoing']) == 1
    assert len(transitions_after['external']['outgoing']) == 1
    assert len(data_flows_before['external']['ingoing']) == 1
    assert len(data_flows_after['external']['ingoing']) == 0

    # data flow is preserved if right data type and name is used
    call_gui_callback(list(state_m_parent.state.input_data_ports.items())[0][1].__setattr__, "data_type", "float")
    if isinstance(state_m_parent.state.states[new_state_id], LibraryState):
        data_port_id = list(state_m_parent.state.states[new_state_id].input_data_ports.items())[0][0]
        state_m_parent.state.states[new_state_id].use_runtime_value_input_data_ports[data_port_id] = True
        state_m_parent.state.states[new_state_id].input_data_port_runtime_values[data_port_id] = 2.0
        print()
    else:
        raise
        # state_m_parent.state.states[new_state_id].input_data_ports.items()[0][1].default_value = 2.0
    call_gui_callback(state_m_parent.state.add_data_flow,
                      state_m_parent.state.state_id,
                      list(state_m_parent.state.input_data_ports.items())[0][1].data_port_id,
                      new_state_id,
                      list(state_m_parent.states[new_state_id].state.input_data_ports.items())[0][1].data_port_id)

    old_keys = list(state_m_parent.state.states.keys())
    transitions_before, data_flows_before = state_m_parent.state.related_linkage_state(new_state_id)
    lib_state = rafcon.gui.singleton.library_manager.get_library_instance('generic', 'wait')
    call_gui_callback(state_m_parent.state.substitute_state, new_state_id, lib_state.state_copy)
    new_state_id = None
    for state_id in list(state_m_parent.state.states.keys()):
        if state_id not in old_keys:
            new_state_id = state_id
    transitions_after, data_flows_after = state_m_parent.state.related_linkage_state(new_state_id)
    # test if data flow is ignored
    assert len(transitions_before['external']['ingoing']) == 1
    assert len(transitions_after['external']['ingoing']) == 1
    assert len(transitions_before['external']['outgoing']) == 1
    assert len(transitions_after['external']['outgoing']) == 1
    assert len(data_flows_before['external']['ingoing']) == 1
    assert len(data_flows_after['external']['ingoing']) == 1
    assert list(state_m_parent.state.states[new_state_id].input_data_ports.items())[0][1].default_value == 2.0

    ##########################################################
    # open separately
    call_gui_callback(sm_m.selection.set, [sm_m.get_state_model_by_path('CDMJPK'), ])
    lib_state = LibraryState(join("generic", "dialog"), "Dialog [3 options]", "0.1", "Dialog [3 options]")
    call_gui_callback(gui_helper_state_machine.insert_state_into_selected_state, lib_state, False)

    lib_hash = lib_state.state_copy.mutable_hash()
    # assert lib_state.mutable_hash().hexdigest() == lib_hash.hexdigest()
    sm_m = sm_manager_model.state_machines[lib_state.get_state_machine().state_machine_id]
    call_gui_callback(sm_m.selection.set, [sm_m.get_state_model_by_path(lib_state.get_path())])
    call_gui_callback(gui_helper_state_machine.open_library_state_separately)
    sm_m = sm_manager_model.get_selected_state_machine_model()
    assert sm_m.root_state.state.mutable_hash().hexdigest() == lib_hash.hexdigest()
    ##########################################################

    if with_substitute_library:
        ##########################################################
        # check substitute library state as template -> keep name
        old_parent = lib_state.parent
        state_ids = list(old_parent.states.keys())
        call_gui_callback(lib_state.__setattr__, 'name', 'DIALOG_X')
        call_gui_callback(sm_manager_model.__setattr__, 'selected_state_machine_id',
                          lib_state.get_state_machine().state_machine_id)
        call_gui_callback(gui_helper_state_machine.substitute_selected_library_state_with_template, True)  # keep_name=True
        new_states = [state for state in list(old_parent.states.values()) if state.state_id not in state_ids]
        assert new_states and len(new_states) == 1 and new_states[0].name == 'DIALOG_X'
        ##########################################################

    if with_refresh:
        call_gui_callback(menubar_ctrl.on_refresh_libraries_activate)
        call_gui_callback(testing_utils.wait_for_gui)
        call_gui_callback(menubar_ctrl.on_refresh_all_activate, None, None, True)
        call_gui_callback(testing_utils.wait_for_gui)
        assert len(sm_manager_model.state_machines) == 2
Exemple #14
0
def trigger_gui_signals(*args):
    """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
    import rafcon.gui.singleton
    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

    print "WT: ", threading.currentThread()
    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')
    global_runtime_config = rafcon.gui.singleton.global_runtime_config
    call_gui_callback(
        rafcon.core.singleton.state_machine_manager.add_state_machine,
        create_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
    call_gui_callback(testing_utils.wait_for_gui)
    current_sm_length = len(sm_manager_model.state_machines)
    first_sm_id = sm_manager_model.state_machines.keys()[0]
    call_gui_callback(menubar_ctrl.on_new_activate, None)
    call_gui_callback(sm_manager_model.__setattr__,
                      "selected_state_machine_id", first_sm_id)

    call_gui_callback(testing_utils.wait_for_gui)
    assert len(sm_manager_model.state_machines) == current_sm_length + 1

    call_gui_callback(menubar_ctrl.on_save_as_activate, None, None,
                      testing_utils.get_unique_temp_path())
    recently_opened_state_machines_paths = global_runtime_config.get_config_value(
        'recently_opened_state_machines')
    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(menubar_ctrl)
    call_gui_callback(sm_manager_model.__setattr__,
                      "selected_state_machine_id", first_sm_id + 1)

    call_gui_callback(menubar_ctrl.on_save_as_activate, None, None,
                      testing_utils.get_unique_temp_path())
    call_gui_callback(testing_utils.wait_for_gui)
    recently_opened_state_machines_paths = global_runtime_config.get_config_value(
        'recently_opened_state_machines')
    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")
    call_gui_callback(menubar_ctrl.on_open_activate, None, None,
                      basic_turtle_sm_path)
    call_gui_callback(testing_utils.wait_for_gui)
    recently_opened_state_machines_paths = global_runtime_config.get_config_value(
        'recently_opened_state_machines')
    assert basic_turtle_sm_path == recently_opened_state_machines_paths[0]
    check_order_and_consistency_of_menu(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

    call_gui_callback(menubar_ctrl.on_save_as_activate, None, None,
                      testing_utils.get_unique_temp_path())
    recently_opened_state_machines_paths = global_runtime_config.get_config_value(
        'recently_opened_state_machines')
    assert turtle_state_machine_m.state_machine.file_system_path == recently_opened_state_machines_paths[
        0]
    check_order_and_consistency_of_menu(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 = rafcon.gui.singleton.library_manager.get_os_path_to_library(
        library_path, library_name)[0]
    call_gui_callback(menubar_ctrl.on_open_activate, None, None,
                      library_os_path)
    recently_opened_state_machines_paths = global_runtime_config.get_config_value(
        'recently_opened_state_machines')
    call_gui_callback(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(menubar_ctrl)

    call_gui_callback(sm_manager_model.__setattr__,
                      "selected_state_machine_id", first_sm_id)
    call_gui_callback(
        sm_manager_model.get_selected_state_machine_model().selection.set,
        sm_manager_model.get_selected_state_machine_model().root_state)
    call_gui_callback(menubar_ctrl.on_add_state_activate, None, None)
    call_gui_callback(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(menubar_ctrl)

    # open state machine by recent opened sub-menu in menu bar
    call_gui_callback(
        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
    call_gui_callback(testing_utils.wait_for_gui)
    assert first_sm_path in menubar_ctrl.view.sub_menu_open_recently.get_children(
    )[2].get_label()
    call_gui_callback(
        sm_manager_model.state_machine_manager.remove_state_machine,
        first_sm_id)
    call_gui_callback(
        menubar_ctrl.view.sub_menu_open_recently.get_children()[2].activate)
    call_gui_callback(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(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
    call_gui_callback(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)
    call_gui_callback(sm_manager_model.__setattr__,
                      "selected_state_machine_id",
                      lib_sm_m.state_machine.state_machine_id)
    call_gui_callback(testing_utils.wait_for_gui)
    call_gui_callback(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]
    call_gui_callback(
        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(menubar_ctrl)

    ####################
    # NEGATIVE EXAMPLES -> supposed to not been added to the recently opened state machines list
    ####################
    recently_opened_state_machines_paths = global_runtime_config.get_config_value(
        'recently_opened_state_machines')

    # if a LibraryState is created and insert no change should be in the recently opened state machine list
    call_gui_callback(menubar_ctrl.on_new_activate, None)
    lib_state = LibraryState(join("generic", "dialog"), "Dialog [3 options]",
                             "0.1", "Dialog [3 options]")
    call_gui_callback(
        gui_helper_state_machine.insert_state_into_selected_state, lib_state,
        True)
    assert recently_opened_state_machines_paths == global_runtime_config.get_config_value(
        'recently_opened_state_machines')

    # try to open state machine that is not there -> no fatal failure
    print "OPEN FAILURE CASE"
    call_gui_callback(
        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
    call_gui_callback(
        sm_manager_model.state_machine_manager.remove_state_machine,
        lib_sm_m.state_machine.state_machine_id)
    call_gui_callback(testing_utils.wait_for_gui)
    call_gui_callback(
        menubar_ctrl.view.sub_menu_open_recently.get_children()[2].activate)
    # 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 menubar_ctrl.view.sub_menu_open_recently.get_children(
    )[2].get_label()
    call_gui_callback(
        global_runtime_config.clean_recently_opened_state_machines)
    assert lib_sm_path not in menubar_ctrl.view.sub_menu_open_recently.get_children(
    )[2].get_label()
    call_gui_callback(
        global_runtime_config.update_recently_opened_state_machines_with,
        lib_sm_before_remove)
    assert 'NOT_ACCESSIBLE' in menubar_ctrl.view.sub_menu_open_recently.get_children(
    )[2].get_label()

    # 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
    call_gui_callback(auto_backup.recover_state_machine_from_backup,
                      backup_path)
    assert recently_opened_state_machines_paths == global_runtime_config.get_config_value(
        'recently_opened_state_machines')
    assert number_of_open_sm == len(sm_manager_model.state_machines)
    call_gui_callback(
        sm_manager_model.state_machine_manager.remove_state_machine,
        reopen_first_sm_id)
    call_gui_callback(testing_utils.wait_for_gui)
    assert recently_opened_state_machines_paths == global_runtime_config.get_config_value(
        'recently_opened_state_machines')
    assert number_of_open_sm == len(sm_manager_model.state_machines) + 1
    call_gui_callback(auto_backup.recover_state_machine_from_backup,
                      backup_path, None, None, True)
    assert recently_opened_state_machines_paths == global_runtime_config.get_config_value(
        'recently_opened_state_machines')
    assert number_of_open_sm == len(sm_manager_model.state_machines)
    check_order_and_consistency_of_menu(menubar_ctrl)
Exemple #15
0
def create_turtle_statemachine(base_path, example_path):
    basic_turtle_demo_state = HierarchyState("BasicTurtleDemo")
    init_ros_node = LibraryState("ros_libraries", "init_ros_node", "0.1",
                                 "init ros node")

    basic_turtle_demo_state.add_state(init_ros_node)
    basic_turtle_demo_state.set_start_state(init_ros_node.state_id)

    ########################################################
    # Turtle Concurrency State
    ########################################################

    preemptive_concurrency_state = PreemptiveConcurrencyState(
        "Turtle Concurrency State")
    basic_turtle_demo_state.add_state(preemptive_concurrency_state)
    basic_turtle_demo_state.add_transition(
        init_ros_node.state_id, 0, preemptive_concurrency_state.state_id, None)
    basic_turtle_demo_state.add_transition(
        preemptive_concurrency_state.state_id, 0,
        basic_turtle_demo_state.state_id, 0)

    ########################################################
    # Subscribe to turtle position concurrency State
    ########################################################

    subscribe_to_turtle_position_hierarchy_state = HierarchyState(
        "Turtle Position Subscriber Hierarchy State")

    preemptive_concurrency_state.add_state(
        subscribe_to_turtle_position_hierarchy_state)

    spawn_turtle = LibraryState("turtle_libraries",
                                "turtle_position_subscriber", "0.1",
                                "subscribe to turtle position")
    subscribe_to_turtle_position_hierarchy_state.add_state(spawn_turtle)
    subscribe_to_turtle_position_hierarchy_state.set_start_state(
        spawn_turtle.state_id)
    subscribe_to_turtle_position_hierarchy_state.add_transition(
        spawn_turtle.state_id, 0, spawn_turtle.state_id, None)

    ########################################################
    # Move Turtle Hierarchy State
    ########################################################
    move_turtle_hierarchy_state = HierarchyState("Move Turtle Hierarchy State")
    preemptive_concurrency_state.add_state(move_turtle_hierarchy_state)
    preemptive_concurrency_state.add_transition(
        move_turtle_hierarchy_state.state_id, 0,
        preemptive_concurrency_state.state_id, 0)

    spawn_turtle = LibraryState("turtle_libraries", "spawn_turtle", "0.1",
                                "spawn turtle")
    move_turtle_hierarchy_state.add_state(spawn_turtle)
    move_turtle_hierarchy_state.set_start_state(spawn_turtle.state_id)

    wait1 = ExecutionState("Wait1", path=base_path, filename="wait.py")
    move_turtle_hierarchy_state.add_state(wait1)
    move_turtle_hierarchy_state.add_transition(spawn_turtle.state_id, 0,
                                               wait1.state_id, None)

    teleport_turtle = LibraryState("turtle_libraries", "teleport_turtle",
                                   "0.1", "teleport turtle")
    move_turtle_hierarchy_state.add_state(teleport_turtle)
    move_turtle_hierarchy_state.add_transition(wait1.state_id, 0,
                                               teleport_turtle.state_id, None)

    wait2 = ExecutionState("Wait2", path=base_path, filename="wait.py")
    move_turtle_hierarchy_state.add_state(wait2)
    move_turtle_hierarchy_state.add_transition(teleport_turtle.state_id, 0,
                                               wait2.state_id, None)

    clear_field = LibraryState("turtle_libraries", "clear_field", "0.1",
                               "clear field")
    move_turtle_hierarchy_state.add_state(clear_field)
    move_turtle_hierarchy_state.add_transition(wait2.state_id, 0,
                                               clear_field.state_id, None)

    wait3 = LibraryState(name="Wait3",
                         library_path="generic",
                         library_name="wait")
    move_turtle_hierarchy_state.add_state(wait3)
    move_turtle_hierarchy_state.add_transition(clear_field.state_id, 0,
                                               wait3.state_id, None)

    set_velocity1 = LibraryState("turtle_libraries", "set_velocity", "0.1",
                                 "set velocity1")
    move_turtle_hierarchy_state.add_state(set_velocity1)
    move_turtle_hierarchy_state.add_transition(wait3.state_id, 0,
                                               set_velocity1.state_id, None)

    wait4 = ExecutionState("Wait4", path=base_path, filename="wait.py")
    move_turtle_hierarchy_state.add_state(wait4)
    move_turtle_hierarchy_state.add_transition(set_velocity1.state_id, 0,
                                               wait4.state_id, None)

    move_to_position = LibraryState("turtle_libraries", "move_to_position",
                                    "0.1", "move to position")
    move_turtle_hierarchy_state.add_state(move_to_position)
    move_turtle_hierarchy_state.add_transition(wait4.state_id, 0,
                                               move_to_position.state_id, None)

    move_turtle_hierarchy_state.add_transition(move_to_position.state_id, 1,
                                               move_to_position.state_id, None)

    kill_turtle = LibraryState("turtle_libraries", "kill_turtle", "0.1",
                               "kill turtle")
    move_turtle_hierarchy_state.add_state(kill_turtle)
    move_turtle_hierarchy_state.add_transition(move_to_position.state_id, 0,
                                               kill_turtle.state_id, None)

    move_turtle_hierarchy_state.add_transition(
        kill_turtle.state_id, 0, move_turtle_hierarchy_state.state_id, 0)

    return basic_turtle_demo_state