Ejemplo n.º 1
0
def create_and_resize_state():
    import gtk
    from rafcon.gui.singleton import main_window_controller
    # gvm = rafcon.core.singleton.global_variable_manager
    menubar_ctrl = main_window_controller.get_controller('menu_bar_controller')
    # from rafcon.gui.controllers.menu_bar import MenuBarController
    # assert isinstance(menubar_ctrl, MenuBarController)
    # execution_engine = singletons.state_machine_execution_engine
    # state_machine_manager = singletons.state_machine_manager

    call_gui_callback(menubar_ctrl.on_new_activate, None)
    # time.sleep(1.0)
    time.sleep(0.5)

    testing_utils.wait_for_gui()

    sm_ctrls = main_window_controller.get_controller(
        'state_machines_editor_ctrl')
    graphical_editor_controller = sm_ctrls.get_controller(1)
    # get first state machine page in state machines notebook
    sm_page = sm_ctrls.tabs.items()[0][1]['page']
    sm_model = sm_ctrls.tabs.items()[0][1]['state_machine_m']

    state_view_for_root_state = graphical_editor_controller.canvas.get_view_for_model(
        sm_model.root_state)
    x, y = south_east_coordinates_of_model(state_view_for_root_state)

    # print x, y

    call_gui_callback(resize_state, sm_model, sm_page,
                      graphical_editor_controller)

    # testing_utils.wait_for_gui()
    # graphical_editor_controller.canvas.update()

    new_x, new_y = south_east_coordinates_of_model(state_view_for_root_state)
    sleep_time = 0.01
    max_counter = 10.0 / 0.01
    counter = 0
    while new_x == x and new_y == y:
        new_x, new_y = south_east_coordinates_of_model(
            state_view_for_root_state)
        counter += 1
        if counter > max_counter:
            break
        time.sleep(sleep_time)

    # print new_x, new_y

    assert x < new_x
    assert y < new_y
Ejemplo n.º 2
0
    def _on_apply_button_clicked(self, *args):
        """Apply button clicked: Apply the configuration
        """
        refresh_required = self.core_config_model.apply_preliminary_config()
        refresh_required |= self.gui_config_model.apply_preliminary_config()

        if not self.gui_config_model.config.get_config_value(
                "SESSION_RESTORE_ENABLED"):
            import rafcon.gui.backup.session as backup_session
            logger.info("Removing current session")
            backup_session.reset_session()
        if refresh_required:
            from rafcon.gui.singleton import main_window_controller
            main_window_controller.get_controller(
                'menu_bar_controller').on_refresh_all_activate(None, None)
        self._popup_message()
Ejemplo n.º 3
0
 def on_select_library_tree_element(widget, date=None, state_m=None):
     from rafcon.gui.singleton import main_window_controller
     library_tree_controller = main_window_controller.get_controller(
         'library_controller')
     library_tree_controller.select_library_tree_element_of_library_state_model(
         state_m)
     library__usagestree_controller.select_library_tree_element_of_library_state_model(
         state_m)
Ejemplo n.º 4
0
def close_gui(already_quit=False, force_quit=True):
    from rafcon.core.singleton import state_machine_execution_engine
    from rafcon.gui.singleton import main_window_controller
    if not already_quit:
        call_gui_callback(state_machine_execution_engine.stop)
        menubar_ctrl = main_window_controller.get_controller('menu_bar_controller')
        # delete_all_state_machines should be done by the quit gui method
        # TODO maybe add the force quit flag as option to the arguments
        call_gui_callback(menubar_ctrl.on_quit_activate, None, None, force_quit)
    if not wait_for_gui_quit():
        assert False, "Could not close the GUI"
Ejemplo n.º 5
0
def store_session():
    """ Stores reference backup information for all open tabs into runtime config

    The backup of never stored tabs (state machines) and not stored state machine changes will be triggered a last
    time to secure data lose.
    """
    from rafcon.gui.singleton import state_machine_manager_model, global_runtime_config
    from rafcon.gui.models.auto_backup import AutoBackupModel
    from rafcon.gui.models import AbstractStateModel
    from rafcon.gui.singleton import main_window_controller
    # check if there are dirty state machines -> use backup file structure maybe it is already stored
    for sm_m in state_machine_manager_model.state_machines.values():
        if sm_m.auto_backup:
            if sm_m.state_machine.marked_dirty:
                sm_m.auto_backup.perform_temp_storage()
        else:
            # generate a backup
            sm_m.auto_backup = AutoBackupModel(sm_m)

    # collect order of tab state machine ids from state machines editor and find selected state machine page number
    state_machines_editor_ctrl = main_window_controller.get_controller(
        'state_machines_editor_ctrl')
    number_of_pages = state_machines_editor_ctrl.view['notebook'].get_n_pages()
    selected_page_number = None
    list_of_tab_meta = []
    for page_number in range(number_of_pages):
        page = state_machines_editor_ctrl.view['notebook'].get_nth_page(
            page_number)
        sm_id = state_machines_editor_ctrl.get_state_machine_id_for_page(page)
        if sm_id == state_machine_manager_model.selected_state_machine_id:
            selected_page_number = page_number

        # backup state machine selection
        selection_of_sm = []
        for model in state_machine_manager_model.state_machines[
                sm_id].selection.get_all():
            if isinstance(model, AbstractStateModel):
                # TODO extend to full range of selection -> see core_identifier action-module
                selection_of_sm.append(model.state.get_path())

        list_of_tab_meta.append({
            'backup_meta':
            state_machine_manager_model.state_machines[sm_id].auto_backup.meta.
            to_dict(native_strings=True),
            'selection':
            selection_of_sm
        })

    # store final state machine backup meta data to backup session tabs and selection for the next run
    global_runtime_config.set_config_value('open_tabs', list_of_tab_meta)
    global_runtime_config.set_config_value(
        'selected_state_machine_page_number', selected_page_number)
Ejemplo n.º 6
0
def restore_session_from_runtime_config():
    """ Restore stored tabs from runtime config

    The method checks if the last status of a state machine is in the backup or in tis original path and loads it
    from there. The original path of these state machines are also insert into the recently opened state machines
    list.
    """
    # TODO add a dirty lock for a crashed rafcon instance also into backup session feature
    # TODO in case a dialog is needed to give the user control
    # TODO combine this and auto-backup in one structure/controller/observer
    from rafcon.gui.singleton import state_machine_manager_model, global_runtime_config, global_gui_config
    from rafcon.gui.models.auto_backup import recover_state_machine_from_backup
    from rafcon.gui.singleton import main_window_controller
    # check if session storage exists
    open_tabs = global_runtime_config.get_config_value('open_tabs', None)
    if open_tabs is None:
        logger.info("No session found for recovery")
        return

    # load and restore state machines like they were opened before
    open_sm = []
    for idx, tab_meta_dict in enumerate(open_tabs):
        start_time = time.time()
        backup_meta_dict = tab_meta_dict['backup_meta']
        from_backup_path = None
        open_sm.append(None)
        # TODO do this decision before storing or maybe store the last stored time in the auto backup?!
        # pick folder name dependent on time, and backup meta data existence
        # problem is that the backup time is maybe not the best choice
        if 'last_backup' in backup_meta_dict:
            last_backup_time = storage_utils.get_float_time_for_string(
                backup_meta_dict['last_backup']['time'])
            if 'last_saved' in backup_meta_dict:
                last_save_time = storage_utils.get_float_time_for_string(
                    backup_meta_dict['last_saved']['time'])
                backup_marked_dirty = backup_meta_dict['last_backup'][
                    'marked_dirty']
                if last_backup_time > last_save_time and backup_marked_dirty:
                    from_backup_path = backup_meta_dict['last_backup'][
                        'file_system_path']
            else:
                from_backup_path = backup_meta_dict['last_backup'][
                    'file_system_path']
        elif 'last_saved' in backup_meta_dict:
            # print("### open last saved", sm_meta_dict['last_saved']['file_system_path'])
            pass
        else:
            logger.error(
                "A tab was stored into session storage dictionary {0} without any recovery path"
                "".format(backup_meta_dict))
            continue

        # check in case that the backup folder is valid or use last saved path
        if from_backup_path is not None and not os.path.isdir(
                from_backup_path):
            logger.warning(
                "The backup of tab {0} from backup path {1} was not possible. "
                "The last saved path will be used for recovery, which could result is loss of changes."
                "".format(idx, from_backup_path))
            from_backup_path = None

        # open state machine
        if from_backup_path is not None:
            # open state machine, recover mark dirty flags, cleans dirty lock files
            logger.info("Restoring from backup {0}".format(from_backup_path))
            state_machine_m = recover_state_machine_from_backup(
                from_backup_path)
        else:
            if 'last_saved' not in backup_meta_dict or backup_meta_dict[
                    'last_saved']['file_system_path'] is None:
                continue
            path = backup_meta_dict['last_saved']['file_system_path']
            if not os.path.isdir(path):
                logger.warning(
                    "The tab can not be open. The backup of tab {0} from common path {1} was not "
                    "possible.".format(idx, path))
                continue
            # logger.info("backup from last saved", path, sm_meta_dict)
            state_machine = storage.load_state_machine_from_path(path)
            state_machine_manager_model.state_machine_manager.add_state_machine(
                state_machine)
            wait_for_gui()
            state_machine_m = state_machine_manager_model.state_machines[
                state_machine.state_machine_id]

        duration = time.time() - start_time
        stat = state_machine_m.state_machine.root_state.get_states_statistics(
            0)
        logger.info(
            "It took {0:.3}s to restore {1} states with {2} hierarchy levels.".
            format(duration, stat[0], stat[1]))

        open_sm[idx] = state_machine_m

    global_runtime_config.extend_recently_opened_by_current_open_state_machines(
    )

    wait_for_gui()

    # restore all state machine selections separate to avoid states-editor and state editor creation problems
    for idx, tab_meta_dict in enumerate(open_tabs):
        state_machine_m = open_sm[idx]
        if state_machine_m is None:  # state machine could not be open
            return

        # restore state machine selection
        selected_model_set = []
        for core_element_identifier in tab_meta_dict['selection']:
            selected_model_set.append(
                state_machine_m.get_state_model_by_path(
                    core_element_identifier))
        state_machine_m.selection.set(selected_model_set)

    # restore backup-ed tab selection
    selected_page_number = global_runtime_config.get_config_value(
        'selected_state_machine_page_number', None)
    if selected_page_number is not None:
        selected_state_machine_page_number = selected_page_number
        if selected_state_machine_page_number is None:
            return
        state_machines_editor_ctrl = main_window_controller.get_controller(
            'state_machines_editor_ctrl')
        if not state_machines_editor_ctrl.view['notebook'].get_n_pages(
        ) >= selected_page_number:
            logger.warning(
                "Page id {0} does not exist so session restore can not re-create selection."
                "".format(selected_page_number))
            return
        notebook = state_machines_editor_ctrl.view['notebook']
        page = state_machines_editor_ctrl.on_switch_page(
            notebook, None, selected_page_number)
        selected_sm_id = state_machine_manager_model.selected_state_machine_id
        if not selected_sm_id == state_machines_editor_ctrl.get_state_machine_id_for_page(
                page):
            logger.warning(
                "Selection of page was not set correctly so session restore can not re-create selection."
            )
            return