private void NextState(TransitionEventArgs eventArgs) { lock (this.sync) { if (this.CheckIsRunning()) { this.SetNextState(eventArgs); } } }
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 }); }
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); }
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 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]); } } }
public override bool ConditionsAreMet(object sender, TransitionEventArgs eventArgs) { return eventArgs.Status == this.statusKey; }
public override bool ConditionsAreMet(object sender, TransitionEventArgs eventArgs) { return this.predicate(); }
public abstract bool ConditionsAreMet(object sender, TransitionEventArgs eventArgs);
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; }
protected virtual void MachineEnded(object sender, TransitionEventArgs args) { Tracer<StateMachineState>.WriteInformation("Machine Ended {0} ({1})", new object[] { this.ToString(), base.MachineName }); this.RaiseStateFinished(args); }
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 }); }
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]); } } }
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)); } } }
public override bool ConditionsAreMet(object sender, TransitionEventArgs eventArgs) { return true; }