Exemple #1
0
        public void GetStateTransitionWithTResultAndStateFunc_returns_StateTransitionDynamicParameterized()
        {
            var stateMachine = new StateMachine <Sale, SaleState, SaleEvent>(sale => sale.State, (sale, newState) => sale.State = newState);
            var result       = StateTransitionFactory <Sale, SaleState, SaleEvent> .GetStateTransition <string>(stateMachine
                                                                                                                , stateFunc : (sale, stringParam) => SaleState.Complete
                                                                                                                , name : "test"
                                                                                                                , priority : 1);

            Assert.IsType <StateTransitionDynamicParameterized <Sale, SaleState, SaleEvent, string> >(result);
        }
Exemple #2
0
        public void GetStateTransitionWithStateFunc_returns_StateTransitionDynamic()
        {
            var stateMachine = new StateMachine <Sale, SaleState, SaleEvent>(sale => sale.State, (sale, newState) => sale.State = newState);
            var result       = StateTransitionFactory <Sale, SaleState, SaleEvent> .GetStateTransition(stateMachine
                                                                                                       , stateFunc : (sale) => SaleState.Complete
                                                                                                       , name : "test"
                                                                                                       , priority : 1);

            Assert.IsType <StateTransitionDynamic <Sale, SaleState, SaleEvent> >(result);
        }
Exemple #3
0
        public void GetStateTransitionWithAsyncCondition_returns_StateTransitionAsync()
        {
            var stateMachine = new NStateManager.Async.StateMachine <Sale, SaleState, SaleEvent>(sale => sale.State, (sale, newState) => sale.State = newState);
            var result       = StateTransitionFactory <Sale, SaleState, SaleEvent> .GetStateTransition(stateMachine
                                                                                                       , toState : SaleState.Complete
                                                                                                       , conditionAsync : (sale, cancelToken) => Task.FromResult(true)
                                                                                                       , name : "test"
                                                                                                       , priority : 1);

            Assert.IsType <NStateManager.Async.StateTransition <Sale, SaleState, SaleEvent> >(result);
        }
Exemple #4
0
        public void GetStateTransitionWithTResult_returns_StateTransitionParameterized()
        {
            var stateMachine = new NStateManager.Sync.StateMachine <Sale, SaleState, SaleEvent>(sale => sale.State, (sale, newState) => sale.State = newState);
            var result       = StateTransitionFactory <Sale, SaleState, SaleEvent> .GetStateTransition <string>(stateMachine
                                                                                                                , toState : SaleState.Complete
                                                                                                                , condition : (sale, stringParam) => true
                                                                                                                , name : "test"
                                                                                                                , priority : 1);

            Assert.IsType <NStateManager.Sync.StateTransitionParameterized <Sale, SaleState, SaleEvent, string> >(result);
        }
Exemple #5
0
        public void GetStateTransition_returns_StateTransition()
        {
            var stateMachine = new NStateManager.Sync.StateMachine <Sale, SaleState, SaleEvent>(sale => sale.State, (sale, newState) => { });
            var result       = StateTransitionFactory <Sale, SaleState, SaleEvent> .GetStateTransition(stateMachine
                                                                                                       , toState : SaleState.Complete
                                                                                                       , condition : _ => true
                                                                                                       , name : "test"
                                                                                                       , priority : 1);

            Assert.IsType <NStateManager.Sync.StateTransition <Sale, SaleState, SaleEvent> >(result);
        }
Exemple #6
0
        public void GetStateTransitionWithTResultAndAsyncCondition_returns_StateTransitionParameterizedAsync()
        {
            var stateMachine = new StateMachineAsync <Sale, SaleState, SaleEvent>(sale => sale.State, (sale, newState) => sale.State = newState);
            var result       = StateTransitionFactory <Sale, SaleState, SaleEvent> .GetStateTransition <string>(stateMachine
                                                                                                                , toState : SaleState.Complete
                                                                                                                , conditionAsync : (sale, stringParam, cancelToken) => Task.FromResult(true)
                                                                                                                , name : "test"
                                                                                                                , priority : 1);

            Assert.IsType <StateTransitionParameterizedAsync <Sale, SaleState, SaleEvent, string> >(result);
        }
Exemple #7
0
        public void GetStateTransactionWTRequestAsyncStateMachineStartStateAndTriggerState_returns_StateTransitionAutoDynamicAsync()
        {
            var stateMachine = new NStateManager.Async.StateMachine <Sale, SaleState, SaleEvent>(sale => sale.State, (sale, newState) => sale.State = newState);
            var result       = StateTransitionFactory <Sale, SaleState, SaleEvent> .GetStateTransition <string>(stateMachine
                                                                                                                , startState : SaleState.Open
                                                                                                                , stateFunction : (sale, stringParam) => SaleState.Complete
                                                                                                                , triggerState : SaleState.ChangeDue
                                                                                                                , name : "test"
                                                                                                                , priority : 1);

            Assert.IsType <NStateManager.Async.StateTransitionAutoDynamicParameterized <Sale, SaleState, SaleEvent, string> >(result);
        }
Exemple #8
0
        public void GetStateTransitionWithStateMachineAndStateFunction_returns_StateTransitionAutoDynamic()
        {
            var stateMachine = new NStateManager.Sync.StateMachine <Sale, SaleState, SaleEvent>(sale => sale.State, (sale, newState) => sale.State = newState);
            var result       = StateTransitionFactory <Sale, SaleState, SaleEvent> .GetStateTransition(stateMachine
                                                                                                       , startState : SaleState.Open
                                                                                                       , stateFunction : _ => SaleState.Complete
                                                                                                       , triggerState : SaleState.ChangeDue
                                                                                                       , name : "test"
                                                                                                       , priority : 1);

            Assert.IsType <NStateManager.Sync.StateTransitionAutoDynamic <Sale, SaleState, SaleEvent> >(result);
        }
        /// <summary>
        /// Adds a transition that automatically forwards to a new state that's determined at runtime.
        /// </summary>
        /// <param name="trigger">The trigger this transition is used with.</param>
        /// <param name="function">The function to determine the next state.</param>
        /// <param name="name">The name of the transition.</param>
        /// <param name="priority">The priority of the transition.</param>
        /// <returns></returns>
        public IStateConfiguration <T, TState, TTrigger> AddAutoDynamicTransition(TTrigger trigger, Func <T, TState> function, string name = null, uint priority = 1)
        {
            var initialTransition = StateTransitionFactory <T, TState, TTrigger> .GetStateTransition(_stateMachine
                                                                                                     , State
                                                                                                     , function
                                                                                                     , State
                                                                                                     , name
                                                                                                     , priority);

            AddAutoTransition(trigger, initialTransition);

            return(this);
        }
        /// <summary>
        /// Adds a conditional transition to be applied on the specified <see cref="TTrigger"/>.
        /// </summary>
        /// <param name="trigger">The <see cref="TTrigger"/> to use this transition.</param>
        /// <param name="toState">The <see cref="TState"/> to transition to.</param>
        /// <param name="condition">StateFunction that must be met to complete the transition.</param>
        /// <param name="name"></param>
        /// <param name="priority"></param>
        /// <returns></returns>
        public IStateConfiguration <T, TState, TTrigger> AddTransition(TTrigger trigger
                                                                       , TState toState
                                                                       , Func <T, bool> condition = null
                                                                       , string name   = null
                                                                       , uint priority = 1)
        {
            var transition = StateTransitionFactory <T, TState, TTrigger> .GetStateTransition(_stateMachine
                                                                                              , toState
                                                                                              , condition
                                                                                              , name
                                                                                              , priority);

            AddTransition(trigger, transition);

            return(this);
        }
        /// <summary>
        /// Defines an automatic, but conditional, transition from this state to a new state.
        /// </summary>
        /// <param name="trigger">The <see cref="TTrigger"/> to initiate this transition.</param>
        /// <param name="toState">The <see cref="TState"/> to transition to.</param>
        /// <param name="condition">The condition required to make the transition.</param>
        /// <param name="name"></param>
        /// <param name="priority"></param>
        /// <returns></returns>
        public IStateConfigurationAsync <T, TState, TTrigger> AddAutoForwardTransition(TTrigger trigger
                                                                                       , TState toState
                                                                                       , Func <T, CancellationToken, Task <bool> > condition = null
                                                                                       , string name   = null
                                                                                       , uint priority = 1)
        {
            var transition = StateTransitionFactory <T, TState, TTrigger> .GetStateTransition(_stateMachine
                                                                                              , triggerState : State
                                                                                              , toState : toState
                                                                                              , conditionAsync : condition
                                                                                              , name : name
                                                                                              , priority : priority);

            AddAutoTransition(trigger, transition);

            return(this);
        }
        /// <summary>
        /// Defines a state transition where the end state is defined by a function.
        /// </summary>
        /// <param name="trigger">The <see cref="TTrigger"/> to use initiate this transition.</param>
        /// <param name="function">The function to determine the state.</param>
        /// <param name="name"></param>
        /// <param name="priority"></param>
        /// <returns></returns>
        public IStateConfiguration <T, TState, TTrigger> AddDynamicTransition(TTrigger trigger
                                                                              , Func <T, TState> function
                                                                              , string name   = null
                                                                              , uint priority = 1)
        {
            if (function == null)
            {
                throw new ArgumentNullException(nameof(function));
            }

            var transition = StateTransitionFactory <T, TState, TTrigger> .GetStateTransition(_stateMachine
                                                                                              , function
                                                                                              , name
                                                                                              , priority);

            AddTransition(trigger, transition);

            return(this);
        }
        /// <summary>
        /// Defines a conditional transition to apply on the given <see cref="TTrigger"/>.
        /// </summary>
        /// <typeparam name="TRequest">Parameter to be passed in from StateMachine.FireTrigger.</typeparam>
        /// <param name="trigger">The <see cref="TTrigger"/> on which to execute this transition.</param>
        /// <param name="toState">The <see cref="TState"/> to transition to.</param>
        /// <param name="conditionAsync">The asynchronous function required to complete the transition.</param>
        /// <param name="name"></param>
        /// <param name="priority"></param>
        /// <returns></returns>
        public IStateConfigurationAsync <T, TState, TTrigger> AddTransition <TRequest>(TTrigger trigger
                                                                                       , TState toState
                                                                                       , Func <T, TRequest, CancellationToken, Task <bool> > conditionAsync
                                                                                       , string name   = null
                                                                                       , uint priority = 1)
            where TRequest : class
        {
            if (conditionAsync == null)
            {
                throw new ArgumentNullException(nameof(conditionAsync));
            }

            var transition = StateTransitionFactory <T, TState, TTrigger> .GetStateTransition(_stateMachine
                                                                                              , toState
                                                                                              , conditionAsync
                                                                                              , name
                                                                                              , priority);

            AddTransition(trigger, transition);

            return(this);
        }
        /// <summary>
        /// Defines an automatic, but conditional, transition from this state to a new state.
        /// </summary>
        /// <typeparam name="TRequest">Parameter to be passed in from StateMachine.FireTrigger.</typeparam>
        /// <param name="trigger">The <see cref="TTrigger"/> to initiate this transition.</param>
        /// <param name="toState">The <see cref="TState"/> to transition to.</param>
        /// <param name="condition">The condition required to make the transition.</param>
        /// <param name="name"></param>
        /// <param name="priority"></param>
        /// <returns></returns>
        public IStateConfiguration <T, TState, TTrigger> AddAutoForwardTransition <TRequest>(TTrigger trigger
                                                                                             , TState toState
                                                                                             , Func <T, TRequest, bool> condition = null
                                                                                             , string name   = null
                                                                                             , uint priority = 1)
            where TRequest : class
        {
            if (condition == null)
            {
                throw new ArgumentNullException(nameof(condition));
            }

            var transition = StateTransitionFactory <T, TState, TTrigger> .GetStateTransition(_stateMachine
                                                                                              , toState
                                                                                              , condition
                                                                                              , State
                                                                                              , name
                                                                                              , priority);

            AddAutoTransition(trigger, transition);

            return(this);
        }