예제 #1
0
 private void NextState(TransitionEventArgs eventArgs)
 {
     lock (this.sync)
     {
         if (this.CheckIsRunning())
         {
             this.SetNextState(eventArgs);
         }
     }
 }
예제 #2
0
        protected virtual void RaiseStateFinished(TransitionEventArgs eventArgs)
        {
            EventHandler <TransitionEventArgs> finished = this.Finished;

            if (finished != null)
            {
                finished(this, eventArgs);
                return;
            }
            Tracer <BaseState> .WriteWarning("Finishing state without handler {0} ({1}), status: {2}", new object[]
            {
                this.ToString(),
                this.MachineName,
                (eventArgs == null || string.IsNullOrEmpty(eventArgs.Status)) ? "empty" : eventArgs.Status
            });
        }
예제 #3
0
        public virtual BaseState NextState(TransitionEventArgs eventArgs)
        {
            BaseState      baseState      = this;
            BaseTransition baseTransition = this.DefaultTransition;

            Tracer <BaseState> .WriteInformation(string.Format("Getting Next state of {0} ({1})", this.ToString(), this.MachineName), new object[0]);

            try
            {
                foreach (BaseTransition current in this.ConditionalTransitions)
                {
                    if (current.ConditionsAreMet(this, eventArgs))
                    {
                        baseTransition = current;
                        Tracer <BaseState> .WriteInformation("Conditions are met for {0}", new object[]
                        {
                            current.ToString()
                        });

                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                Tracer <BaseState> .WriteError(ex, "Checking transitions is failed: unexpected error", new object[0]);

                return(this.HandleTransitionException(baseState, ex));
            }
            if (baseTransition != null)
            {
                if (baseTransition == this.DefaultTransition)
                {
                    Tracer <BaseState> .WriteInformation("Selecting Default transition {0}", new object[]
                    {
                        baseTransition.Next.ToString()
                    });
                }
                Tracer <BaseState> .WriteInformation(string.Format("Next state of {0} is {1}", this.ToString(), baseTransition.Next), new object[0]);

                baseState = baseTransition.Next;
            }
            return(baseState);
        }
예제 #4
0
        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));
                }
            }
        }
예제 #5
0
        private void CurrentStateFinished(object sender, TransitionEventArgs eventArgs)
        {
            BaseState baseState = sender as BaseState;

            lock (this.sync)
            {
                if (this.CurrentState == baseState)
                {
                    this.NextState(eventArgs);
                }
                else
                {
                    Tracer <BaseStateMachine> .WriteWarning(string.Concat(new object[]
                    {
                        "Blocked state change attempt from:",
                        this.CurrentState,
                        " to:",
                        baseState
                    }), new object[0]);
                }
            }
        }
예제 #6
0
		public override bool ConditionsAreMet(object sender, TransitionEventArgs eventArgs)
		{
			return eventArgs.Status == this.statusKey;
		}
예제 #7
0
		public override bool ConditionsAreMet(object sender, TransitionEventArgs eventArgs)
		{
			return this.predicate();
		}
예제 #8
0
 public abstract bool ConditionsAreMet(object sender, TransitionEventArgs eventArgs);
예제 #9
0
		public virtual BaseState NextState(TransitionEventArgs eventArgs)
		{
			BaseState baseState = this;
			BaseTransition baseTransition = this.DefaultTransition;
			Tracer<BaseState>.WriteInformation(string.Format("Getting Next state of {0} ({1})", this.ToString(), this.MachineName), new object[0]);
			try
			{
				foreach (BaseTransition current in this.ConditionalTransitions)
				{
					if (current.ConditionsAreMet(this, eventArgs))
					{
						baseTransition = current;
						Tracer<BaseState>.WriteInformation("Conditions are met for {0}", new object[]
						{
							current.ToString()
						});
						break;
					}
				}
			}
			catch (Exception ex)
			{
				Tracer<BaseState>.WriteError(ex, "Checking transitions is failed: unexpected error", new object[0]);
				return this.HandleTransitionException(baseState, ex);
			}
			if (baseTransition != null)
			{
				if (baseTransition == this.DefaultTransition)
				{
					Tracer<BaseState>.WriteInformation("Selecting Default transition {0}", new object[]
					{
						baseTransition.Next.ToString()
					});
				}
				Tracer<BaseState>.WriteInformation(string.Format("Next state of {0} is {1}", this.ToString(), baseTransition.Next), new object[0]);
				baseState = baseTransition.Next;
			}
			return baseState;
		}
예제 #10
0
		protected virtual void MachineEnded(object sender, TransitionEventArgs args)
		{
			Tracer<StateMachineState>.WriteInformation("Machine Ended {0} ({1})", new object[]
			{
				this.ToString(),
				base.MachineName
			});
			this.RaiseStateFinished(args);
		}
예제 #11
0
		protected virtual void RaiseStateFinished(TransitionEventArgs eventArgs)
		{
			EventHandler<TransitionEventArgs> finished = this.Finished;
			if (finished != null)
			{
				finished(this, eventArgs);
				return;
			}
			Tracer<BaseState>.WriteWarning("Finishing state without handler {0} ({1}), status: {2}", new object[]
			{
				this.ToString(),
				this.MachineName,
				(eventArgs == null || string.IsNullOrEmpty(eventArgs.Status)) ? "empty" : eventArgs.Status
			});
		}
예제 #12
0
		public abstract bool ConditionsAreMet(object sender, TransitionEventArgs eventArgs);
예제 #13
0
		private void CurrentStateFinished(object sender, TransitionEventArgs eventArgs)
		{
			BaseState baseState = sender as BaseState;
			lock (this.sync)
			{
				if (this.CurrentState == baseState)
				{
					this.NextState(eventArgs);
				}
				else
				{
					Tracer<BaseStateMachine>.WriteWarning(string.Concat(new object[]
					{
						"Blocked state change attempt from:",
						this.CurrentState,
						" to:",
						baseState
					}), new object[0]);
				}
			}
		}
예제 #14
0
		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));
				}
			}
		}
예제 #15
0
		private void NextState(TransitionEventArgs eventArgs)
		{
			lock (this.sync)
			{
				if (this.CheckIsRunning())
				{
					this.SetNextState(eventArgs);
				}
			}
		}
예제 #16
0
		public override bool ConditionsAreMet(object sender, TransitionEventArgs eventArgs)
		{
			return true;
		}