Example #1
0
 private IState <TState, TEvent> EnterHistoryShallow(ITransitionContext <TState, TEvent> context)
 {
     return(this.LastActiveState != null
                ?
            this.LastActiveState.EnterShallow(context)
                :
            this);
 }
Example #2
0
        public void Entry(ITransitionContext <TState, TEvent> context)
        {
            Ensure.ArgumentNotNull(context, "context");

            context.AddRecord(this.Id, RecordType.Enter);

            this.ExecuteEntryActions(context);
        }
Example #3
0
        public IState <TState, TEvent> EnterDeep(ITransitionContext <TState, TEvent> context)
        {
            this.Entry(context);

            return(this.LastActiveState == null ?
                   this :
                   this.LastActiveState.EnterDeep(context));
        }
Example #4
0
        public void Entry(ITransitionContext <TState, TEvent> context)
        {
            Ensure.ArgumentNotNull(context, "context");

            this.SetThisStateAsActiveStateOfRegion();
            this.ExecuteEntryActions(context);
            this.StartDoActions(context);
        }
Example #5
0
 protected ITransitionContext GetChildContext(ITransitionContext context)
 {
     return(new TransitionContextBuilder()
            .With(context)
            .WithRefBack(new DelegateReference(RefBack, context.RefBack))
            .WithStyles(GetChildStyles(context))
            .Build());
 }
Example #6
0
        public void Entry(ITransitionContext <TState, TEvent> context)
        {
            Guard.AgainstNullArgument("context", context);

            context.AddRecord(this.Id, RecordType.Enter);

            this.ExecuteEntryActions(context);
        }
Example #7
0
        public void Exit(ITransitionContext <TState, TEvent> context)
        {
            Ensure.ArgumentNotNull(context, "context");

            this.StopDoActions();
            this.ExecuteExitActions(context);
            this.SetThisStateAsLastStateOfRegion();
        }
Example #8
0
        public IState <TState, TEvent> EnterShallow(ITransitionContext <TState, TEvent> context)
        {
            this.Entry(context);

            return(this.initialState == null ?
                   this :
                   this.initialState.EnterShallow(context));
        }
Example #9
0
 private IState <TState, TEvent> EnterHistoryNone(ITransitionContext <TState, TEvent> context)
 {
     return(this.initialState != null
                ?
            this.initialState.EnterShallow(context)
                :
            this);
 }
Example #10
0
            public override Task ExecutedTransition(
                ITransitionDefinition <States, Events> transition,
                ITransitionContext <States, Events> transitionContext)
            {
                this.items.Add(new Item(transition.Source, transition.Target, transitionContext));

                return(Task.CompletedTask);
            }
Example #11
0
        private async Task EnterInitialState(IState <TState, TEvent> initialState, ITransitionContext <TState, TEvent> context)
        {
            var initializer     = this.factory.CreateStateMachineInitializer(initialState, context);
            var newCurrentState = await initializer.EnterInitialState().
                                  ConfigureAwait(false);

            await this.SetCurrentState(newCurrentState)
            .ConfigureAwait(false);
        }
 public Item(
     IStateDefinition <States, Events> source,
     IStateDefinition <States, Events> target,
     ITransitionContext <States, Events> transitionContext)
 {
     this.Source            = source;
     this.Target            = target;
     this.TransitionContext = transitionContext;
 }
Example #13
0
 private void ExecuteExitActions(
     IStateDefinition <TState, TEvent> stateDefinition,
     ITransitionContext <TState, TEvent> context)
 {
     foreach (var actionHolder in stateDefinition.ExitActions)
     {
         this.ExecuteExitAction(stateDefinition, actionHolder, context);
     }
 }
Example #14
0
            public override Task ExecutedTransition(
                IStateMachineInformation <States, Events> stateMachine,
                ITransition <States, Events> transition,
                ITransitionContext <States, Events> transitionContext)
            {
                this.items.Add(new Item(stateMachine, transition.Source, transition.Target, transitionContext));

                return(Task.CompletedTask);
            }
        public TransitionContextBuilder With(ITransitionContext context)
        {
            WithState(context.State)
            .WithRefBack(context.RefBack)
            .WithClasses(context.Classes)
            .WithStyles(context.Styles);

            return(this);
        }
Example #16
0
        public void Exit(ITransitionContext <TState, TEvent> context)
        {
            Ensure.ArgumentNotNull(context, "context");

            context.AddRecord(this.Id, RecordType.Exit);

            this.ExecuteExitActions(context);
            this.SetThisStateAsLastStateOfSuperState();
        }
Example #17
0
        private void StartDoActions(ITransitionContext <TState, TEvent> context)
        {
            cancellation = new CancellationTokenSource();
            var cancellationToken = cancellation.Token;

            var doActionTasks = this.doActions.Select(actionHolder => this.StartDoAction(actionHolder, context, cancellationToken));

            Task.WhenAll(doActionTasks).ContinueWith(t => this.DoActionsCompleted(), TaskContinuationOptions.OnlyOnRanToCompletion);
        }
Example #18
0
        public void Exit(ITransitionContext <TState, TEvent> context)
        {
            Guard.AgainstNullArgument("context", context);

            context.AddRecord(this.Id, RecordType.Exit);

            this.ExecuteExitActions(context);
            this.SetThisStateAsLastStateOfSuperState();
        }
Example #19
0
        public async Task <ITransitionResult <TState> > Fire(
            ITransitionDefinition <TState, TEvent> transitionDefinition,
            ITransitionContext <TState, TEvent> context,
            ILastActiveStateModifier <TState, TEvent> lastActiveStateModifier)
        {
            Guard.AgainstNullArgument("context", context);

            var shouldFire = await this.ShouldFire(transitionDefinition, context).ConfigureAwait(false);

            if (!shouldFire)
            {
                await this.extensionHost
                .ForEach(extension => extension.SkippedTransition(
                             this.stateMachineInformation,
                             transitionDefinition,
                             context))
                .ConfigureAwait(false);

                return(TransitionResult <TState> .NotFired);
            }

            context.OnTransitionBegin();

            await this.extensionHost
            .ForEach(extension => extension.ExecutingTransition(
                         this.stateMachineInformation,
                         transitionDefinition,
                         context))
            .ConfigureAwait(false);

            var newState = context.StateDefinition.Id;

            if (!transitionDefinition.IsInternalTransition)
            {
                await this.UnwindSubStates(transitionDefinition, context, lastActiveStateModifier).ConfigureAwait(false);

                await this.Fire(transitionDefinition, transitionDefinition.Source, transitionDefinition.Target, context, lastActiveStateModifier)
                .ConfigureAwait(false);

                newState = await this.stateLogic.EnterByHistory(transitionDefinition.Target, context, lastActiveStateModifier)
                           .ConfigureAwait(false);
            }
            else
            {
                await this.PerformActions(transitionDefinition, context).ConfigureAwait(false);
            }

            await this.extensionHost
            .ForEach(extension => extension.ExecutedTransition(
                         this.stateMachineInformation,
                         transitionDefinition,
                         context))
            .ConfigureAwait(false);

            return(new TransitionResult <TState>(true, newState));
        }
 private void OnTransitionCompleted(ITransitionContext <TState, TEvent> transitionContext, TState currentStateId)
 {
     this.RaiseEvent(
         this.TransitionCompleted,
         new TransitionCompletedEventArgs <TState, TEvent>(
             currentStateId,
             transitionContext),
         transitionContext,
         true);
 }
Example #21
0
 /// <summary>
 /// Fires the <see cref="TransitionCompleted"/> event.
 /// </summary>
 /// <param name="transitionContext">The transition event context.</param>
 /// <param name="stateMachineInformation">The state machine information.</param>
 private void OnTransitionCompleted(ITransitionContext <TState, TEvent> transitionContext, IStateMachineInformation <TState, TEvent> stateMachineInformation)
 {
     this.RaiseEvent(
         this.TransitionCompleted,
         new TransitionCompletedEventArgs <TState, TEvent>(
             stateMachineInformation.CurrentStateId.ExtractOrThrow(),
             transitionContext),
         transitionContext,
         true);
 }
Example #22
0
 private async Task ExecuteExitActions(
     IStateDefinition <TState, TEvent> stateDefinition,
     ITransitionContext <TState, TEvent> context)
 {
     foreach (var actionHolder in stateDefinition.ExitActions)
     {
         await this.ExecuteExitAction(stateDefinition, actionHolder, context)
         .ConfigureAwait(false);
     }
 }
Example #23
0
        private void DoFire(ITransition <TState, TEvent> transition, ITransitionContext <TState, TEvent> context)
        {
            this.OnTransitionBegin(context);

            var result = transition.Fire(context);

            this.ChangeStates(context.SourceState, result.NewStates);

            this.OnTransitionCompleted(context, result.NewStates.First().Id);
        }
        /// <summary>
        /// Recursively traverses the state hierarchy, exiting states along
        /// the way, performing the action, and entering states to the target.
        /// </summary>
        /// <remarks>
        /// There exist the following transition scenarios:
        /// 0. there is no target state (internal transition)
        ///    --> handled outside this method.
        /// 1. The source and target state are the same (self transition)
        ///    --> perform the transition directly:
        ///        Exit source state, perform transition actions and enter target state
        /// 2. The target state is a direct or indirect sub-state of the source state
        ///    --> perform the transition actions, then traverse the hierarchy
        ///        from the source state down to the target state,
        ///        entering each state along the way.
        ///        No state is exited.
        /// 3. The source state is a sub-state of the target state
        ///    --> traverse the hierarchy from the source up to the target,
        ///        exiting each state along the way.
        ///        Then perform transition actions.
        ///        Finally enter the target state.
        /// 4. The source and target state share the same super-state
        /// 5. All other scenarios:
        ///    a. The source and target states reside at the same level in the hierarchy
        ///       but do not share the same direct super-state
        ///    --> exit the source state, move up the hierarchy on both sides and enter the target state
        ///    b. The source state is lower in the hierarchy than the target state
        ///    --> exit the source state and move up the hierarchy on the source state side
        ///    c. The target state is lower in the hierarchy than the source state
        ///    --> move up the hierarchy on the target state side, afterward enter target state.
        /// </remarks>
        /// <param name="transitionDefinition">The transition definition.</param>
        /// <param name="source">The source state.</param>
        /// <param name="target">The target state.</param>
        /// <param name="context">The event context.</param>
        /// <param name="lastActiveStateModifier">The last active state modifier.</param>
        private void Fire(
            ITransitionDefinition <TState, TEvent> transitionDefinition,
            IStateDefinition <TState, TEvent> source,
            IStateDefinition <TState, TEvent> target,
            ITransitionContext <TState, TEvent> context,
            ILastActiveStateModifier <TState> lastActiveStateModifier)
        {
            if (source == transitionDefinition.Target)
            {
                // Handles 1.
                // Handles 3. after traversing from the source to the target.
                this.stateLogic.Exit(source, context, lastActiveStateModifier);
                this.PerformActions(transitionDefinition, context);
                this.stateLogic.Entry(transitionDefinition.Target, context);
            }
            else if (source == target)
            {
                // Handles 2. after traversing from the target to the source.
                this.PerformActions(transitionDefinition, context);
            }
            else if (source.SuperState == target.SuperState)
            {
                //// Handles 4.
                //// Handles 5a. after traversing the hierarchy until a common ancestor if found.
                this.stateLogic.Exit(source, context, lastActiveStateModifier);
                this.PerformActions(transitionDefinition, context);
                this.stateLogic.Entry(target, context);
            }
            else
            {
                // traverses the hierarchy until one of the above scenarios is met.

                // Handles 3.
                // Handles 5b.
                if (source.Level > target.Level)
                {
                    this.stateLogic.Exit(source, context, lastActiveStateModifier);
                    this.Fire(transitionDefinition, source.SuperState, target, context, lastActiveStateModifier);
                }
                else if (source.Level < target.Level)
                {
                    // Handles 2.
                    // Handles 5c.
                    this.Fire(transitionDefinition, source, target.SuperState, context, lastActiveStateModifier);
                    this.stateLogic.Entry(target, context);
                }
                else
                {
                    // Handles 5a.
                    this.stateLogic.Exit(source, context, lastActiveStateModifier);
                    this.Fire(transitionDefinition, source.SuperState, target.SuperState, context, lastActiveStateModifier);
                    this.stateLogic.Entry(target, context);
                }
            }
        }
Example #25
0
 private void ExecuteExitAction(IActionHolder actionHolder, ITransitionContext <TState, TEvent> context)
 {
     try
     {
         actionHolder.Execute(context.EventArgument);
     }
     catch (Exception exception)
     {
         this.HandleExitActionException(context, exception);
     }
 }
Example #26
0
        private TState EnterHistoryNone(
            IStateDefinition <TState, TEvent> stateDefinition,
            ITransitionContext <TState, TEvent> context)
        {
            if (stateDefinition.InitialState != null)
            {
                return(this.EnterShallow(stateDefinition.InitialState, context));
            }

            return(stateDefinition.Id);
        }
Example #27
0
 public Item(
     IStateMachineInformation <States, Events> stateMachine,
     IState <States, Events> source,
     IState <States, Events> target,
     ITransitionContext <States, Events> transitionContext)
 {
     this.StateMachine      = stateMachine;
     this.Source            = source;
     this.Target            = target;
     this.TransitionContext = transitionContext;
 }
Example #28
0
        private async Task <TState> EnterHistoryNone(
            IStateDefinition <TState, TEvent> stateDefinition,
            ITransitionContext <TState, TEvent> context)
        {
            if (stateDefinition.InitialState != null)
            {
                return(await this.EnterShallow(stateDefinition.InitialState, context)
                       .ConfigureAwait(false));
            }

            return(stateDefinition.Id);
        }
Example #29
0
        public void Exit(
            IStateDefinition <TState, TEvent> stateDefinition,
            ITransitionContext <TState, TEvent> context,
            ILastActiveStateModifier <TState> lastActiveStateModifier)
        {
            Guard.AgainstNullArgument("context", context);

            context.AddRecord(stateDefinition.Id, RecordType.Exit);

            this.ExecuteExitActions(stateDefinition, context);
            SetThisStateAsLastStateOfSuperState(stateDefinition, lastActiveStateModifier);
        }
        private void EnterInitialState(
            ITransitionContext <TState, TEvent> context,
            StateContainer <TState, TEvent> stateContainer,
            IStateDefinitionDictionary <TState, TEvent> stateDefinitions,
            TState initialStateId)
        {
            var initialState       = stateDefinitions[initialStateId];
            var initializer        = this.factory.CreateStateMachineInitializer(initialState, context);
            var newStateId         = initializer.EnterInitialState(this.stateLogic, stateContainer, stateDefinitions);
            var newStateDefinition = stateDefinitions[newStateId];

            SwitchStateTo(newStateDefinition, stateContainer, stateDefinitions);
        }