Exemple #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="StateMachine{TState,TEvent}"/> class.
        /// </summary>
        /// <param name="name">The name of this state machine used in log messages.</param>
        /// <param name="factory">The factory used to create internal instances.</param>
        public StateMachine(string name, IFactory <TState, TEvent> factory)
        {
            this.name       = name;
            this.factory    = factory ?? new StandardFactory <TState, TEvent>(this, this);
            this.states     = new StateDictionary <TState, TEvent>(this.factory);
            this.extensions = new List <IExtension <TState, TEvent> >();

            this.initialStateId = new Initializable <TState>();
        }
Exemple #2
0
        private static void SwitchStateTo(IStateDefinition <TState, TEvent> newState, StateContainer <TState, TEvent> stateContainer, IStateMachineInformation <TState, TEvent> stateMachineInformation)
        {
            var oldState = stateContainer.CurrentState.ExtractOr(null);

            stateContainer.CurrentState = Initializable <IStateDefinition <TState, TEvent> > .Initialized(newState);

            stateContainer.ForEach(extension =>
                                   extension.SwitchedState(stateMachineInformation, oldState, newState));
        }
Exemple #3
0
        public StateMachine(string name, IFactory <TState, TEvent> factory, Executer executer)
        {
            this.name = name;

            this.factory  = factory ?? new StandardFactory <TState, TEvent>(this);
            this.executer = executer;

            this.states         = new StateDictionary <TState, TEvent>(this.factory);
            this.currentStates  = new List <IState <TState, TEvent> >();
            this.initialStateId = new Initializable <TState>();
        }
Exemple #4
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));
            }
        }
        private static void SwitchStateTo(
            IStateDefinition <TState, TEvent> newState,
            StateContainer <TState, TEvent> stateContainer,
            IStateDefinitionDictionary <TState, TEvent> stateDefinitions)
        {
            var oldState = stateContainer
                           .CurrentStateId
                           .Map(x => stateDefinitions[x])
                           .ExtractOr(null);

            stateContainer.CurrentStateId = Initializable <TState> .Initialized(newState.Id);

            stateContainer.ForEach(extension =>
                                   extension.SwitchedState(oldState, newState));
        }
Exemple #6
0
        private void LoadCurrentState(IStateMachineLoader <TState> stateMachineLoader)
        {
            Initializable <TState> loadedCurrentState = stateMachineLoader.LoadCurrentState();

            this.currentState = loadedCurrentState.IsInitialized ? this.states[loadedCurrentState.Value] : null;
        }
Exemple #7
0
 public StateContainer(string name)
 {
     this.Name         = name;
     this.CurrentState = Initializable <IStateDefinition <TState, TEvent> > .UnInitialized();
 }
 public StateContainer(string name)
 {
     this.Name           = name;
     this.CurrentStateId = Initializable <TState> .UnInitialized();
 }