public FluentStateMachine( IStateMachine <TStateModel> stateMachine, IEnumerable <State <TStateModel> > states, State <TStateModel> initialState, IEnumerable <Action <TransitionEventArgs <TStateModel> > > globalTransitionings, IEnumerable <Action <TransitionEventArgs <TStateModel> > > globalTransitioneds, IEnumerable <Transition <TStateModel> > globalTransitions) { if (stateMachine == null) { throw new ArgumentNullException("stateMachine"); } if (states == null) { throw new ArgumentNullException("states"); } this.stateMachine = stateMachine; if (globalTransitionings != null) { foreach (var callback in globalTransitionings) { stateMachine.Transitioning += (s, e) => callback(e); } } if (globalTransitioneds != null) { foreach (var callback in globalTransitioneds) { stateMachine.Transitioned += (s, e) => callback(e); } } if (globalTransitions != null) { foreach (var transition in globalTransitions) { stateMachine.AddGlobalTransition(transition); } } allStatesByName = states.ToDictionary(s => s.Name); allStatesByCode = states.Where(s => s.Code.HasValue).ToDictionary(s => s.Code.Value); InitialState = initialState; }
/// <summary> /// Adds a global transition to a state machine by trigger and state /// </summary> /// <typeparam name="TStateModel"></typeparam> /// <param name="stateMachine">state machine to add global transition</param> /// <param name="trigger">trigger to raise transition</param> /// <param name="to">target state to transition to</param> public static void AddGlobalTransition <TStateModel>(this IStateMachine <TStateModel> stateMachine, Trigger trigger, State <TStateModel> to) where TStateModel : IStateModel { if (stateMachine == null) { throw new ArgumentNullException("stateMachine"); } if (trigger == null) { throw new ArgumentNullException("trigger"); } if (to == null) { throw new ArgumentNullException("to"); } stateMachine.AddGlobalTransition(new Transition <TStateModel>(trigger, null, to)); }