using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace FiniteStateMachine { internal class Process { private Dictionary _transitions; public ProcessState CurrentState { get; private set; } public Process() { CurrentState = ProcessState.Inactive; _transitions = new Dictionary() { { new StateTransition(ProcessState.Inactive, Command.Exit), ProcessState.Trminated }, { new StateTransition(ProcessState.Inactive, Command.Begin), ProcessState.Active }, { new StateTransition(ProcessState.Active, Command.End), ProcessState.Inactive }, { new StateTransition(ProcessState.Active, Command.Paused), ProcessState.Paused }, { new StateTransition(ProcessState.Paused, Command.End), ProcessState.Inactive }, { new StateTransition (ProcessState.Paused, Command.Resume), ProcessState.Active }, }; } public ProcessState GetNext(Command command) { StateTransition transition = new StateTransition(CurrentState, command); ProcessState nextState; if (!_transitions.TryGetValue(transition, out nextState)) throw new Exception($"Invalid transition: {CurrentState} → {nextState}"); return nextState; } public ProcessState MoveNext(Command command) { CurrentState = GetNext(command); return CurrentState; } } }