public void AddState(BaseState state)
		{
			if (!this.CheckIsRunning())
			{
				state.MachineName = this.MachineName;
				this.states.Add(state);
			}
		}
		public StateStatusTransition(BaseState next, string statusKey) : base(next)
		{
			this.statusKey = statusKey;
		}
		public LambdaTransition(Func<bool> predicate, BaseState state) : base(state)
		{
			this.predicate = predicate;
		}
		public TransitionFromErrorState(BaseState state)
		{
			base.DefaultTransition = new DefaultTransition(state);
		}
Exemple #5
0
		private BaseState HandleTransitionException(BaseState state, Exception exception)
		{
			Error error = new Error(exception);
			BaseErrorState baseErrorState = this.NextErrorState(error);
			if (baseErrorState != null)
			{
				baseErrorState.Start(error);
				state = baseErrorState;
			}
			return state;
		}
		protected virtual void OnCurrentStateChanged(BaseState oldValue, BaseState newValue)
		{
		}
Exemple #7
0
		protected BaseTransition(BaseState next)
		{
			this.Next = next;
		}
		private void RaiseCurrentStateChanged(BaseState previousState, BaseState newState)
		{
			Action<BaseState, BaseState> currentStateChanged = this.CurrentStateChanged;
			if (currentStateChanged != null)
			{
				currentStateChanged(previousState, newState);
			}
		}
		public void SetStartState(BaseState state)
		{
			this.states.Remove(state);
			this.states.Insert(0, state);
		}
		public BaseStateMachine()
		{
			this.CurrentState = BaseState.NullObject();
			this.states = new List<BaseState>();
			this.machineState = BaseStateMachine.StateMachineState.Stopped;
		}
		private void SetErrorState(Error error)
		{
			if (!this.CheckIsRunning())
			{
				return;
			}
			if (this.IsCurrentStateEndErrorState())
			{
				this.StopStateMachine();
				this.RaiseStateMachineErroredEvent(error);
				return;
			}
			BaseErrorState baseErrorState = this.CurrentState.NextErrorState(error);
			if (baseErrorState != this.CurrentState)
			{
				this.CurrentState.Stop();
				this.CurrentState.Finished -= new EventHandler<TransitionEventArgs>(this.CurrentStateFinished);
				this.CurrentState.Errored -= new EventHandler<Error>(this.CurrentStateErrored);
				this.CurrentState = baseErrorState;
				baseErrorState.Finished += new EventHandler<TransitionEventArgs>(this.CurrentStateFinished);
				baseErrorState.Errored += new EventHandler<Error>(this.CurrentStateErrored);
				baseErrorState.Start(error);
			}
		}
		private void SetNextState(TransitionEventArgs eventArgs)
		{
			if (!this.CheckIsRunning())
			{
				return;
			}
			BaseState baseState = this.CurrentState.NextState(eventArgs);
			this.CurrentState.Stop();
			this.CurrentState.Finished -= new EventHandler<TransitionEventArgs>(this.CurrentStateFinished);
			this.CurrentState.Errored -= new EventHandler<Error>(this.CurrentStateErrored);
			this.CurrentState = baseState;
			this.CurrentState.Finished += new EventHandler<TransitionEventArgs>(this.CurrentStateFinished);
			this.CurrentState.Errored += new EventHandler<Error>(this.CurrentStateErrored);
			try
			{
				this.CurrentState.Start();
			}
			catch (OutOfMemoryException ex)
			{
				Tracer<BaseStateMachine>.WriteError(ex, "Cannot start a state", new object[0]);
				this.CurrentStateErrored(this.CurrentState, new Error(ex));
			}
			catch (Exception ex2)
			{
				Tracer<BaseStateMachine>.WriteError(ex2, "Cannot start a state", new object[0]);
				this.CurrentStateErrored(this.CurrentState, new Error(new InternalException(string.Empty, ex2)));
			}
			if (this.IsCurrentStateEndState())
			{
				string status = this.StopStateMachine();
				EventHandler<TransitionEventArgs> machineEnded = this.MachineEnded;
				if (machineEnded != null)
				{
					machineEnded(this, new TransitionEventArgs(status));
				}
			}
		}
		private string StopStateMachine()
		{
			this.machineState = BaseStateMachine.StateMachineState.Stopped;
			string result = string.Empty;
			EndState endState = this.CurrentState as EndState;
			if (endState != null)
			{
				result = endState.Status;
			}
			this.CurrentState.Finished -= new EventHandler<TransitionEventArgs>(this.CurrentStateFinished);
			this.CurrentState.Errored -= new EventHandler<Error>(this.CurrentStateErrored);
			this.CurrentState.Stop();
			this.CurrentState = BaseState.NullObject();
			return result;
		}
		private void StartStateMachine()
		{
			if (this.states.Count == 0)
			{
				UnexpectedErrorException ex = new UnexpectedErrorException("No states to run state machine!");
				Tracer<BaseStateMachine>.WriteError(ex);
				throw ex;
			}
			this.machineState = BaseStateMachine.StateMachineState.Running;
			this.CurrentState = this.states[0];
			this.CurrentState.Finished += new EventHandler<TransitionEventArgs>(this.CurrentStateFinished);
			this.CurrentState.Errored += new EventHandler<Error>(this.CurrentStateErrored);
			this.CurrentState.Start();
		}
		public DefaultTransition(BaseState state) : base(state)
		{
		}