Пример #1
0
    def __init__(self, root_state=None, version=None, creation_time=None, last_update=None, state_machine_id=None):
        Observable.__init__(self)

        self._modification_lock = RLock()

        if state_machine_id is None:
            self.state_machine_id = generate_state_machine_id()
        else:
            self.state_machine_id = state_machine_id

        if root_state:
            self.root_state = root_state

        if version:
            self.version = version

        if creation_time:
            self.creation_time = creation_time
        else:
            self.creation_time = get_current_time_string()

        # this case happens if old state machines are loaded which do not have statemachine.json yet
        if isinstance(last_update, datetime):
            # see https://stackoverflow.com/questions/8022161/python-converting-from-datetime-datetime-to-time-time
            last_update = time.mktime(last_update.timetuple()) + last_update.microsecond / 1E6

        if last_update:
            self.last_update = last_update
        else:
            self.last_update = get_current_time_string()

        self._execution_histories = []

        # specifies if this state machine supports saving states with state_name + state_id
        self._supports_saving_state_names = True
Пример #2
0
    def __init__(self, root_state=None, version=None, creation_time=None, state_machine_id=None):
        Observable.__init__(self)

        self._modification_lock = RLock()

        if state_machine_id is None:
            self.state_machine_id = generate_state_machine_id()
        else:
            self.state_machine_id = state_machine_id

        if root_state:
            self.root_state = root_state

        if version:
            self.version = version

        if creation_time:
            self.creation_time = creation_time
        else:
            self.creation_time = get_current_time_string()

        self._execution_histories = []

        # specifies if this state machine supports saving states with state_name + state_id
        self._supports_saving_state_names = True
Пример #3
0
def save_state_machine_to_path(state_machine,
                               base_path,
                               delete_old_state_machine=False,
                               as_copy=False):
    """Saves a state machine recursively to the file system

    The `as_copy` flag determines whether the state machine is saved as copy. If so (`as_copy=True`), some state
    machine attributes will be left untouched, such as the `file_system_path` or the `dirty_flag`.

    :param rafcon.core.state_machine.StateMachine state_machine: the state_machine to be saved
    :param str base_path: base_path to which all further relative paths refers to
    :param bool delete_old_state_machine: Whether to delete any state machine existing at the given path
    :param bool as_copy: Whether to use a copy storage for the state machine
    """
    # warns the user in the logger when using deprecated names
    clean_path_from_deprecated_naming(base_path)

    state_machine.acquire_modification_lock()
    try:
        root_state = state_machine.root_state

        # clean old path first
        if delete_old_state_machine:
            if os.path.exists(base_path):
                shutil.rmtree(base_path)

        # Ensure that path is existing
        if not os.path.exists(base_path):
            os.makedirs(base_path)

        old_update_time = state_machine.last_update
        state_machine.last_update = storage_utils.get_current_time_string()
        state_machine_dict = state_machine.to_dict()
        storage_utils.write_dict_to_json(
            state_machine_dict, os.path.join(base_path, STATEMACHINE_FILE))

        # set the file_system_path of the state machine
        if not as_copy:
            state_machine.file_system_path = copy.copy(base_path)
        else:
            state_machine.last_update = old_update_time

        # add root state recursively
        remove_obsolete_folders([root_state], base_path)
        save_state_recursively(root_state, base_path, "", as_copy)

        if state_machine.marked_dirty and not as_copy:
            state_machine.marked_dirty = False
        logger.debug("State machine with id {0} was saved at {1}".format(
            state_machine.state_machine_id, base_path))
    except Exception:
        raise
    finally:
        state_machine.release_modification_lock()