Пример #1
0
        public static TContext PerformStraightforward <TContext, TStateId>(
            TContext context,
            IStatefulTask <TContext, TStateId> initialTask,
            IReadOnlyDictionary <TStateId, IStatefulTask <TContext, TStateId> > transitions)
        {
            // We can log type names of state and tasks but it will be helpful for debugging.
            Logger.Debug($"Initial state: {context}");

            IStatefulTask <TContext, TStateId> currentTask = initialTask;

            try
            {
                Logger.Debug("Starting performing.");

                while (!currentTask.IsFinal)
                {
                    Logger.Debug($"Executing task: {currentTask}.");
                    TStateId stateId = currentTask.DoAction(context);
                    currentTask = transitions[stateId];
                    Logger.Debug($"Current state: {context}.");
                }

                // Perform the final task.
                Logger.Debug($"Executing task: {currentTask}.");
                currentTask.DoAction(context);
            }
            catch (Exception ex)
            {
                Logger.Exception(ex, "Exception occurred during state machine performing.");
            }

            Logger.Debug($"Final state: {context}");
            return(context);
        }
Пример #2
0
 public static IStateMachineExecutor <TState, TStateId> GoTo <TState, TStateId>(
     this IStateMachineFiller <TState, TStateId> filler,
     IStatefulTask <TState, TStateId> statefulTask)
     where TState : class
 {
     return(filler.FillExecutor(statefulTask));
 }
Пример #3
0
        public static TState PerformStraightforward <TState>(TState initialState,
                                                             IStatefulTask <TState> initialTask)
            where TState : class
        {
            Logger.Message($"Initial state: {initialState}");

            TState currentState = initialState;
            IStatefulTask <TState> currentTask = initialTask;

            try
            {
                Logger.Message("Starting performing.");

                while (!currentTask.IsFinal)
                {
                    Logger.Message($"Executing task: {currentTask}.");
                    currentTask = currentTask.DoAction(currentState);
                    Logger.Message($"Current state: {currentState}.");
                }

                // Perform the final task.
                Logger.Message($"Executing task: {currentTask}.");
                currentTask.DoAction(currentState);
            }
            catch (Exception ex)
            {
                Logger.Exception(ex, "Exception occured during state machine performing.");
            }

            Logger.Message($"Final state: {currentState}");
            return(currentState);
        }
Пример #4
0
        public static TState PerformStraightforward <TState, TStateId>(TState initialState,
                                                                       IStatefulTask <TState, TStateId> initialTask,
                                                                       IReadOnlyDictionary <TStateId, IStatefulTask <TState, TStateId> > transitions)
            where TState : class
        {
            Logger.Message($"Initial state: {initialState}");

            TState currentState = initialState;
            IStatefulTask <TState, TStateId> currentTask = initialTask;

            try
            {
                Logger.Message("Starting performing.");

                while (!currentTask.IsFinal)
                {
                    Logger.Message($"Executing task: {currentTask}.");
                    TStateId stateId = currentTask.DoAction(currentState);
                    currentTask = transitions[stateId];
                    Logger.Message($"Current state: {currentState}.");
                }

                // Perform the final task.
                Logger.Message($"Executing task: {currentTask}.");
                currentTask.DoAction(currentState);
            }
            catch (Exception ex)
            {
                Logger.Exception(ex, "Exception occured during perform.");
            }

            Logger.Message($"Final state: {currentState}");
            return(currentState);
        }
Пример #5
0
        public override bool MoveNext()
        {
            bool isFinal = _current.IsFinal;

            // Perform the task before getting IsFinal flag because it can be changed.
            _current = _current.DoAction(State);
            return(!isFinal);
        }
Пример #6
0
        public StateMachineUntilFinalStateExecutor(
            TState initialState,
            IStatefulTask <TState> initialTask)
        {
            State        = initialState.ThrowIfNull(nameof(initialTask));
            _initialTask = initialTask.ThrowIfNull(nameof(initialTask));

            _current = initialTask;
        }
Пример #7
0
 private static IStateMachineBuilderWithoutStateId <Context, StateId, IStatefulTask <Context, StateId> > FillExecutor(
     IStatefulTask <Context, StateId> initialTask)
 {
     return(initialTask.AsInitial(StateId.Initial)
            .On(StateId.StateA).GoTo(new TaskA())
            .On(StateId.StateB).GoTo(new TaskB())
            .On(StateId.StateC).GoTo(new TaskC())
            .OnFinalGoToSelfLoop(StateId.Final));
 }
        public override bool MoveNext()
        {
            if (_current is null)
            {
                throw new InvalidOperationException("Invalid stateful task to process.");
            }

            bool isFinal = _current.IsFinal;

            // Perform the task after getting 'IsFinal' flag because we can face final task and do not call it 'DoAction' method.
            _current = _current.DoAction(State);
            return(!isFinal);
        }
Пример #9
0
        public IStateMachineExecutor <TState, TStateId> FillExecutor(
            IStatefulTask <TState, TStateId> statefulTask)
        {
            if (statefulTask is null)
            {
                throw new ArgumentException(
                          "Invalid stateful task to fill executor.", nameof(statefulTask)
                          );
            }

            Executor[StateIdToFill] = statefulTask;
            return(Executor);
        }
 public IStateMachineExecutor <TState, TStateId> FillExecutor(
     IStatefulTask <TState, TStateId> statefulTask)
 {
     Executor[StateIdToFill] = statefulTask;
     return(Executor);
 }
Пример #11
0
 public NoOpNonFinalStatefulTask(IStatefulTask <TState> stateIdToMove)
 {
     _stateToMove = stateIdToMove;
 }
Пример #12
0
 public static IStateMachineExecutor <TState, TStateId> PerformUntilFinalState <TState, TStateId>(
     this IStatefulTask <TState, TStateId> initialTask, TState initialState)
     where TState : class
 {
     return(StateMachineUntilFinalStateExecutor.CreateNew(initialState, initialTask));
 }
Пример #13
0
 public static IStateMachineExecutor <TState> PerformUntilFinalState <TState>(
     this IStatefulTask <TState> initialTask, TState initialState)
     where TState : class
 {
     return(new StateMachineUntilFinalStateExecutor <TState>(initialState, initialTask));
 }
Пример #14
0
 // To simplify call (no need in generic arguments).
 public static IStateMachineBuilderWithoutStateId <TContext, TStateId, IStatefulTask <TContext, TStateId> > AsInitial <TContext, TStateId>(
     this IStatefulTask <TContext, TStateId> initialTask,
     TStateId initialStateId)
 {
     return(initialTask.AsInitial <TContext, TStateId, IStatefulTask <TContext, TStateId> >(initialStateId));
 }