コード例 #1
0
ファイル: StateMachineHelper.cs プロジェクト: Vasar007/Items
        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
ファイル: StateMachineHelper.cs プロジェクト: Vasar007/Items
        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));
 }
コード例 #8
0
        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);
        }
コード例 #10
0
 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));
 }