Example #1
0
        public void Load(IStateMachineLoader <TState> stateMachineLoader)
        {
            Guard.AgainstNullArgument("stateMachineLoader", stateMachineLoader);
            this.CheckThatStateMachineIsNotAlreadyInitialized();

            this.LoadCurrentState(stateMachineLoader);
            this.LoadHistoryStates(stateMachineLoader);
        }
Example #2
0
        public void Load(IStateMachineLoader <TState> stateMachineLoader)
        {
            Ensure.ArgumentNotNull(stateMachineLoader, "stateMachineLoader");
            this.CheckThatStateMachineIsNotAlreadyInitialized();

            this.LoadCurrentState(stateMachineLoader);
            this.LoadHistoryStates(stateMachineLoader);
        }
        /// <summary>
        /// Loads the current state and history states from a persisted state (<see cref="Save"/>).
        /// The loader should return exactly the data that was passed to the saver.
        /// </summary>
        /// <param name="stateMachineLoader">Loader providing persisted data.</param>
        public void Load(IStateMachineLoader <TState> stateMachineLoader)
        {
            Guard.AgainstNullArgument("stateMachineLoader", stateMachineLoader);

            this.CheckThatNotAlreadyInitialized();

            this.initialized = this.stateMachine.Load(stateMachineLoader);
        }
        /// <summary>
        /// Loads the current state and history states from a persisted state (<see cref="Save"/>).
        /// The loader should return exactly the data that was passed to the saver.
        /// </summary>
        /// <param name="stateMachineLoader">Loader providing persisted data.</param>
        public void Load(IStateMachineLoader <TState> stateMachineLoader)
        {
            Ensure.ArgumentNotNull(stateMachineLoader, "stateMachineLoader");

            this.CheckThatNotAlreadyInitialized();

            this.stateMachine.Load(stateMachineLoader);

            this.initialized = true;
        }
Example #5
0
        public bool Load(IStateMachineLoader <TState> stateMachineLoader)
        {
            Guard.AgainstNullArgument(nameof(stateMachineLoader), stateMachineLoader);
            this.CheckThatStateMachineIsNotAlreadyInitialized();

            Initializable <TState>       loadedCurrentState = stateMachineLoader.LoadCurrentState();
            IDictionary <TState, TState> historyStates      = stateMachineLoader.LoadHistoryStates();

            var initialized = SetCurrentState();

            LoadHistoryStates();
            NotifyExtensions();

            return(initialized);

            bool SetCurrentState()
            {
                if (loadedCurrentState.IsInitialized)
                {
                    this.currentState = this.states[loadedCurrentState.Value];
                    return(true);
                }

                this.currentState = null;
                return(false);
            }

            void LoadHistoryStates()
            {
                foreach (KeyValuePair <TState, TState> historyState in historyStates)
                {
                    IState <TState, TEvent> superState      = this.states[historyState.Key];
                    IState <TState, TEvent> lastActiveState = this.states[historyState.Value];

                    if (!superState.SubStates.Contains(lastActiveState))
                    {
                        throw new InvalidOperationException(ExceptionMessages.CannotSetALastActiveStateThatIsNotASubState);
                    }

                    superState.LastActiveState = lastActiveState;
                }
            }

            void NotifyExtensions()
            {
                this.extensions.ForEach(
                    extension => extension.Loaded(
                        this,
                        loadedCurrentState,
                        historyStates));
            }
        }
Example #6
0
        private void LoadHistoryStates(IStateMachineLoader <TState> stateMachineLoader)
        {
            IDictionary <TState, TState> historyStates = stateMachineLoader.LoadHistoryStates();

            foreach (KeyValuePair <TState, TState> historyState in historyStates)
            {
                IState <TState, TEvent> superState      = this.states[historyState.Key];
                IState <TState, TEvent> lastActiveState = this.states[historyState.Value];

                if (!superState.SubStates.Contains(lastActiveState))
                {
                    throw new InvalidOperationException(ExceptionMessages.CannotSetALastActiveStateThatIsNotASubState);
                }

                superState.LastActiveState = lastActiveState;
            }
        }
Example #7
0
        private void LoadCurrentState(IStateMachineLoader <TState> stateMachineLoader)
        {
            Initializable <TState> loadedCurrentState = stateMachineLoader.LoadCurrentState();

            this.currentState = loadedCurrentState.IsInitialized ? this.states[loadedCurrentState.Value] : null;
        }