using System; using System.Collections.Generic; namespace Bro.Common.AbstractFSM { //actions that are performed when state changed public delegate void StateAction(object sender, StateEventArgs eventArgs); public class Transition { private State m_initialState; public State initialState { get { return m_initialState; } } private StateEventArgs m_eventArgs; public StateEventArgs eventArgs { get { return m_eventArgs; } } private State m_finalState; public State finalState { get { return m_finalState; } } private StateAction m_state_action; public StateAction action { get { return m_state_action; } } private bool m_autoMode = false; public bool AutoMode { get { return m_autoMode; } } private Transition m_autoTransition = null; public Transition AutoTransition { get { return m_autoTransition; } } public Transition(State initialState, StateEventArgs sevent, State finalState, StateAction action) { m_initialState = initialState; m_eventArgs = sevent; m_finalState = finalState; m_state_action = action; } public Transition(State initialState, StateEventArgs sevent, State finalState, StateAction action, bool autoMode, Transition autoTransition) : this(initialState, sevent, finalState, action) { m_autoMode = autoMode; m_autoTransition = autoTransition; } public override int GetHashCode() { //create a unique transition key return GetHashCode(m_initialState, m_eventArgs); } public static int GetHashCode(State state, StateEventArgs sevent) { return (state.GetHashCode() << 8) + sevent.Id; } } /// /// Represents a collection of transition objects. /// public class Transitions : System.Collections.Generic.Dictionary { /// Adds the specified transition to the collection. /// Transition object /// /// Key is null /// An transition with the same key already exists. public void Add(Transition transition) { // The Add method throws an exception if the new key is already in the dictionary. try { base.Add(transition.GetHashCode(), transition); } catch (ArgumentException) { throw new ArgumentException("A transition with the key (Initials state " + transition.initialState + ", Event " + transition.eventArgs + ") already exists."); } } // public Transition this[State state, StateEventArgs sevent] { get { try { return this[Transition.GetHashCode(state, sevent)]; } catch(KeyNotFoundException) { throw new KeyNotFoundException("The given transition was not found."); } } set { this[Transition.GetHashCode(state, sevent)] = value; } } // public bool Remove(State state, StateEventArgs sevent) { return base.Remove(Transition.GetHashCode(state, sevent)); } } }