예제 #1
0
        /// <summary>
        /// Fires the specified event.
        /// </summary>
        /// <param name="eventId">The event.</param>
        /// <param name="eventArgument">The event argument.</param>
        /// <param name="stateContainer">Contains all mutable state of of the state machine.</param>
        /// <param name="stateDefinitions">The definitions for all states of this state Machine.</param>
        public void Fire(
            TEvent eventId,
            object eventArgument,
            StateContainer <TState, TEvent> stateContainer,
            IStateDefinitionDictionary <TState, TEvent> stateDefinitions)
        {
            CheckThatStateMachineHasEnteredInitialState(stateContainer);

            stateContainer.ForEach(extension => extension.FiringEvent(ref eventId, ref eventArgument));

            var currentState = stateContainer
                               .CurrentStateId
                               .Map(x => stateDefinitions[x])
                               .ExtractOrThrow();
            var context = this.factory.CreateTransitionContext(currentState, new Missable <TEvent>(eventId), eventArgument, this);
            var result  = this.stateLogic.Fire(currentState, context, stateContainer, stateDefinitions);

            if (!result.Fired)
            {
                this.OnTransitionDeclined(context);
                return;
            }

            var newState = stateDefinitions[result.NewState];

            SwitchStateTo(newState, stateContainer, stateDefinitions);

            stateContainer.ForEach(extension => extension.FiredEvent(context));

            this.OnTransitionCompleted(context, stateContainer.CurrentStateId.ExtractOrThrow());
        }
예제 #2
0
        /// <summary>
        /// Enters the initial state as specified with <paramref name="initialState"/>.
        /// </summary>
        /// <param name="stateContainer">Contains all mutable state of of the state machine.</param>
        /// <param name="stateDefinitions">The definitions for all states of this state Machine.</param>
        /// <param name="initialState">The initial state the state machine should enter.</param>
        public void EnterInitialState(
            StateContainer <TState, TEvent> stateContainer,
            IStateDefinitionDictionary <TState, TEvent> stateDefinitions,
            TState initialState)
        {
            stateContainer.ForEach(extension => extension.EnteringInitialState(initialState));

            var context = this.factory.CreateTransitionContext(null, new Missable <TEvent>(), Missing.Value, this);

            this.EnterInitialState(context, stateContainer, stateDefinitions, initialState);

            stateContainer.ForEach(extension => extension.EnteredInitialState(initialState, context));
        }
예제 #3
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));
        }
예제 #4
0
        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));
        }