123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace Wayne.Lib.StateEngine
- {
- public class StateLookupEntry
- {
- #region Fields
- public string NextStateFactoryName { get; internal set; }
- public HistoryType HistoryType { get; private set; }
- #endregion
- #region Construction
- public StateLookupEntry(string nextStateFactoryName, HistoryType historyType)
- {
- NextStateFactoryName = nextStateFactoryName;
- HistoryType = historyType;
- }
- #endregion
- }
- /// <summary>
- /// StateTransitionLookup is used by the State machine to find the state to
- /// change to when a transition occurs. It uses a dataset that contains the actual data.
- /// </summary>
- public class StateTransitionLookup
- {
- #region Fields
- private readonly StateTypeContainer stateTypeContainer;
- #endregion
- #region Construction
- /// <summary>
- /// Constructor
- /// </summary>
- public StateTransitionLookup(StateTypeContainer stateTypeContainer)
- {
- LookupTable = new Dictionary<string, Dictionary<object, StateLookupEntry>>();
- this.stateTypeContainer = stateTypeContainer;
- }
- public Dictionary<string, Dictionary<object, StateLookupEntry>> LookupTable { get; private set; }
- #endregion
- #region Method: GetNextState
- /// <summary>
- /// Performs a Lookup for the next state when in the source state, and
- /// are going to perform the specified transition
- /// </summary>
- /// <param name="sourceStateFactoryName">
- /// The state the state machine is in when performing transition
- /// </param>
- /// <param name="transition">
- /// The requested transition.
- /// </param>
- /// <returns>
- /// Returns the next state that should be entered based on the transition.
- /// </returns>
- public StateEntry GetNextState(string sourceStateFactoryName, Wayne.Lib.StateEngine.Transition transition)
- {
- if (transition == null)
- return null;
- StateLookupEntry stateLookupEntry;
- //Try look up with the current state name
- if (TryLookup(sourceStateFactoryName, transition.Type, out stateLookupEntry))
- {
- return new StateEntry(transition, stateLookupEntry.NextStateFactoryName, stateLookupEntry.HistoryType);
- }
- //If not matching with current state name, try lookup recursively with AnyState
- if (sourceStateFactoryName != AnyState.FactoryName) //Unless we are already in recursion.
- {
- return GetNextState(AnyState.FactoryName, transition);
- }
- return null;
- }
- public bool TryLookup(string sourceStateFactoryName, object transitionType, out StateLookupEntry stateLookupEntry)
- {
- stateLookupEntry = null;
- Dictionary<object, StateLookupEntry> lookup1;
- if (LookupTable.TryGetValue(sourceStateFactoryName, out lookup1))
- {
- return lookup1.TryGetValue(transitionType, out stateLookupEntry);
- }
- return false;
- }
- #endregion
- #region Method: AddTransition
- /// <summary>
- /// Internal add transition.
- /// </summary>
- /// <param name="sourceStateFactoryName"></param>
- /// <param name="transitionType"></param>
- /// <param name="nextStateType"></param>
- /// <param name="historyType"></param>
- /// <param name="replace"></param>
- private void InternalAddTransition(string sourceStateFactoryName, object transitionType, string nextStateType, HistoryType historyType, bool replace)
- {
- if (historyType == HistoryType.Explicit)
- throw new StateEngineException("Cannot configure a transition to have a explicit history type. It is only used internally when working with explicit transitions");
- var stateLookup = GetTransitionsForState(sourceStateFactoryName);
- var exists = stateLookup.ContainsKey(transitionType);
- if (exists && !replace)
- {
- throw new ArgumentException(string.Format("The State-Transition combination \"{0}-{1}\" has already been defined.", sourceStateFactoryName, Transition.GetTransitionName(transitionType)));
- }
- if (!exists && replace)
- {
- throw new ArgumentException(string.Format("The State-Transition combination \"{0}-{1}\" is not defined.", sourceStateFactoryName, Transition.GetTransitionName(transitionType)));
- }
- StateLookupEntry lookupEntry = new StateLookupEntry(nextStateType, historyType);
- stateLookup[transitionType] = lookupEntry;
- }
- /// <summary>
- /// Returns a transition dictionary for the specified state.
- /// </summary>
- /// <param name="sourceStateFactoryName"></param>
- /// <returns></returns>
- public Dictionary<object, StateLookupEntry> GetTransitionsForState(string sourceStateFactoryName)
- {
- Dictionary<object, StateLookupEntry> result;
- if (!LookupTable.TryGetValue(sourceStateFactoryName, out result))
- {
- LookupTable.Add(sourceStateFactoryName, result = new Dictionary<object, StateLookupEntry>());
- }
- return result;
- }
- /// <summary>
- /// Adds a transition from state of class TFromState to the class TToState, when the transitiontype
- /// is inserted.
- /// </summary>
- /// <typeparam name="TFromState">Class type of the source state.</typeparam>
- /// <typeparam name="TToState">Class type of target state.</typeparam>
- /// <param name="transitionType">Transition type.</param>
- public void AddTransition<TFromState, TToState>(object transitionType)
- where TFromState : State
- where TToState : State
- {
- stateTypeContainer.Register<TFromState>();
- stateTypeContainer.Register<TToState>();
- InternalAddTransition(typeof(TFromState).FullName, transitionType, typeof(TToState).FullName, HistoryType.None, false);
- }
- /// <summary>
- /// Adds a transition from state of class fromStateFactoryName to the class toStateFactoryName, when the transitiontype
- /// is inserted.
- /// </summary>
- /// <param name="fromStateFactoryName">String containig the factory name for the fromState.</param>
- /// <param name="toStateFactoryName">String containig the factory name for the toState.</param>
- /// <param name="transitionType">Transition type.</param>
- public void AddTransition(string fromStateFactoryName, string toStateFactoryName, object transitionType)
- {
- InternalAddTransition(fromStateFactoryName, transitionType, toStateFactoryName, HistoryType.None, false);
- }
- /// <summary>
- /// Adds a transition from state of class TFromState to the class TToState, when the transitiontype
- /// is inserted.
- /// </summary>
- /// <typeparam name="TFromState">Class type of the source state.</typeparam>
- /// <typeparam name="TToState">Class type of target state.</typeparam>
- /// <param name="transitionType">Transition type.</param>
- /// <param name="historyType"></param>
- public void AddTransition<TFromState, TToState>(object transitionType, HistoryType historyType)
- where TFromState : State
- where TToState : State
- {
- stateTypeContainer.Register<TFromState>();
- stateTypeContainer.Register<TToState>();
- InternalAddTransition(typeof(TFromState).FullName, transitionType, typeof(TToState).FullName, historyType, false);
- }
- #endregion
- #region Method: ReplaceTransition (Specifying both From and To state)
- /// <summary>
- /// Replaces the existing transition from a state and the given TransitionType, to another state.
- /// </summary>
- /// <typeparam name="TFromOldState">Class type of the source state.</typeparam>
- /// <typeparam name="TToNewState">Class type of new target state.</typeparam>
- /// <param name="transitionType">Transition type.</param>
- public void ReplaceTransition<TFromOldState, TToNewState>(object transitionType)
- where TFromOldState : State
- where TToNewState : State
- {
- stateTypeContainer.Register<TFromOldState>();
- stateTypeContainer.Register<TToNewState>();
- InternalAddTransition(typeof(TFromOldState).FullName, transitionType, typeof(TToNewState).FullName, HistoryType.None, true);
- }
- /// <summary>
- /// Replaces the existing transition from a state and the given TransitionType, to another state.
- /// </summary>
- /// <param name="fromOldStateFactoryName">String containig the factory name for the fromState.</param>
- /// <param name="toNewStateFactoryName">String containig the factory name for the new toState.</param>
- /// <param name="transitionType">Transition type.</param>
- public void ReplaceTransition(string fromOldStateFactoryName, string toNewStateFactoryName, object transitionType)
- {
- InternalAddTransition(fromOldStateFactoryName, transitionType, toNewStateFactoryName, HistoryType.None, true);
- }
- /// <summary>
- /// Replaces the existing transition from a state and the given TransitionType, to another state.
- /// </summary>
- /// <typeparam name="TFromOldState">Class type of the source state.</typeparam>
- /// <typeparam name="TToNewState">Class type of new target state.</typeparam>
- /// <param name="transitionType">Transition type.</param>
- /// <param name="historyType"></param>
- public void ReplaceTransition<TFromOldState, TToNewState>(object transitionType, HistoryType historyType)
- where TFromOldState : State
- where TToNewState : State
- {
- stateTypeContainer.Register<TFromOldState>();
- stateTypeContainer.Register<TToNewState>();
- InternalAddTransition(typeof(TFromOldState).FullName, transitionType, typeof(TToNewState).FullName, historyType, true);
- }
- #endregion
- #region Method: ReplaceTransitions (Specifying only To state)
- /// <summary>
- /// Replaces all the existing transitions to the given OldToState to the NewToState.
- /// </summary>
- /// <typeparam name="TOldToState">Class type of old target state.</typeparam>
- /// <typeparam name="TNewToState">Class type of new target state.</typeparam>
- public void ReplaceTransitions<TOldToState, TNewToState>()
- where TOldToState : State
- where TNewToState : State
- {
- stateTypeContainer.Register<TOldToState>();
- stateTypeContainer.Register<TNewToState>();
- ReplaceTransitions(typeof(TOldToState).FullName, typeof(TNewToState).FullName);
- }
- /// <summary>
- /// Replaces all the existing transitions to the given OldToState to the NewToState.
- /// </summary>
- /// <param name="oldToStateFactoryName">String containig the factory name for the old ToState.</param>
- /// <param name="newToStateFactoryName">String containig the factory name for the new ToState.</param>
- public void ReplaceTransitions(string oldToStateFactoryName, string newToStateFactoryName)
- {
- LookupTable.Values
- .SelectMany(x => x.Values)
- .Where(x => x.NextStateFactoryName == oldToStateFactoryName)
- .ForEach(x => x.NextStateFactoryName = newToStateFactoryName);
- }
- #endregion
- #region Method: IntersectTransition
- /// <summary>
- /// Puts a state in-between two states that has a transition between them.
- /// </summary>
- /// <typeparam name="TFromState"></typeparam>
- /// <typeparam name="TIntersectingState"></typeparam>
- /// <typeparam name="TOldToState"></typeparam>
- /// <param name="transitionType"></param>
- public void IntersectTransition<TFromState, TIntersectingState, TOldToState>(object transitionType)
- where TFromState : State
- where TIntersectingState : State
- where TOldToState : State
- {
- stateTypeContainer.Register<TFromState>();
- stateTypeContainer.Register<TIntersectingState>();
- stateTypeContainer.Register<TOldToState>();
- IntersectTransition<TFromState, TIntersectingState, TOldToState>(transitionType, transitionType);
- }
- /// <summary>
- /// Puts a state in-between two states that has a transition between them.
- /// </summary>
- /// <typeparam name="TFromState"></typeparam>
- /// <typeparam name="TIntersectingState"></typeparam>
- /// <typeparam name="TOldToState"></typeparam>
- /// <param name="fromTransitionType"></param>
- /// <param name="toTransitionType"></param>
- public void IntersectTransition<TFromState, TIntersectingState, TOldToState>(object fromTransitionType, object toTransitionType)
- where TFromState : State
- where TIntersectingState : State
- where TOldToState : State
- {
- stateTypeContainer.Register<TFromState>();
- stateTypeContainer.Register<TIntersectingState>();
- stateTypeContainer.Register<TOldToState>();
- StateLookupEntry dummy;
- if (!TryLookup(typeof(TFromState).FullName, fromTransitionType, out dummy))
- {
- throw new StateEngineException(string.Format("No previous Transition {0}, from {1} to {2} exists to intersect.", Transition.GetTransitionName(fromTransitionType), typeof(TFromState).FullName, typeof(TOldToState).FullName));
- }
- //If the Transition TFromState to TOldState with toTransitionType doesn't exist throw an Error.
- ReplaceTransition<TFromState, TIntersectingState>(fromTransitionType);
- AddTransition<TIntersectingState, TOldToState>(toTransitionType);
- }
- #endregion
- #region Method: ReplaceState
- /// <summary>
- /// Replaces all the existing transitions to the given OldToState to the NewToState.
- /// Also adds the transitions from the OldToState to also from the NewToState.
- /// </summary>
- /// <typeparam name="TOldToState">Class type of old target state.</typeparam>
- /// <typeparam name="TNewToState">Class type of new target state.</typeparam>
- public void ReplaceState<TOldToState, TNewToState>()
- where TOldToState : State
- where TNewToState : State
- {
- stateTypeContainer.Register<TOldToState>();
- stateTypeContainer.Register<TNewToState>();
- ReplaceState(typeof(TOldToState).FullName, typeof(TNewToState).FullName, false);
- }
- /// <summary>
- /// Replaces all the existing transitions to the given OldToState to the NewToState.
- /// Also adds the transitions from the OldToState to also from the NewToState.
- /// </summary>
- /// <param name="removeAllTransitionsFromState">Default=false. Should all existing transitions *From* this state be removed?</param>
- /// <typeparam name="TOldToState">Class type of old target state.</typeparam>
- /// <typeparam name="TNewToState">Class type of new target state.</typeparam>
- public void ReplaceState<TOldToState, TNewToState>(bool removeAllTransitionsFromState)
- where TOldToState : State
- where TNewToState : State
- {
- stateTypeContainer.Register<TOldToState>();
- stateTypeContainer.Register<TNewToState>();
- ReplaceState(typeof(TOldToState).FullName, typeof(TNewToState).FullName, removeAllTransitionsFromState);
- }
- /// <summary>
- /// Replaces all the existing transitions to the given OldToState to the NewToState.
- /// Also adds the transitions from the OldToState to also from the NewToState.
- /// </summary>
- /// <param name="oldToStateFactoryName">String containig the factory name for the old ToState.</param>
- /// <param name="newToStateFactoryName">String containig the factory name for the new ToState.</param>
- public void ReplaceState(string oldToStateFactoryName, string newToStateFactoryName)
- {
- ReplaceState(oldToStateFactoryName, newToStateFactoryName, false);
- }
- /// <summary>
- /// Replaces all the existing transitions to the given OldToState to the NewToState.
- /// Also adds the transitions from the OldToState to also from the NewToState.
- /// Note: 'OverrideState' also exists, to be used e.g. when replacing state that transists to itself
- /// </summary>
- /// <param name="oldToStateFactoryName">The factory name for the old ToState.</param>
- /// <param name="newToStateFactoryName">The factory name for the new ToState.</param>
- /// <param name="removeAllTransitionsFromState">Default=false. Should all existing transitions *From* this state be removed?</param>
- public void ReplaceState(string oldToStateFactoryName, string newToStateFactoryName, bool removeAllTransitionsFromState)
- {
- var transitionsForState = GetTransitionsForState(oldToStateFactoryName);
- //Get a list of the self-ref transitions
- var selfRefTransitions = transitionsForState
- .Where(x => x.Value.NextStateFactoryName == oldToStateFactoryName)
- .ToArray();
- //Update all transitions going FROM the old state
- var transitions = transitionsForState;
- LookupTable.Remove(oldToStateFactoryName);
- if (!removeAllTransitionsFromState)
- {
- LookupTable.Add(newToStateFactoryName, transitions);
- }
- //Update all transitions going TO the old state.
- LookupTable.Values
- .SelectMany(x => x.Values)
- .Where(x => x.NextStateFactoryName == oldToStateFactoryName)
- .ForEach(x => x.NextStateFactoryName = newToStateFactoryName);
- //Warning :
- //Add super-weird old broken self links for backwards compatibility
- selfRefTransitions.ForEach(t =>
- {
- transitionsForState.Remove(t.Key);
- InternalAddTransition(oldToStateFactoryName, t.Key, newToStateFactoryName, t.Value.HistoryType, false);
- });
- }
- #endregion
- #region Method: OverrideState
- /// <summary>
- /// Makes an override of a state.
- /// This could be used when you have made an inheritance of a state and want to replace the old one.
- /// </summary>
- /// <typeparam name="TOldState">The state to override.</typeparam>
- /// <typeparam name="TNewState">The overrider.</typeparam>
- public void OverrideState<TOldState, TNewState>()
- where TOldState : State
- where TNewState : State
- {
- stateTypeContainer.Register<TOldState>();
- stateTypeContainer.Register<TNewState>();
- string oldToStateFactoryName = typeof(TOldState).FullName;
- string newToStateFactoryName = typeof(TNewState).FullName;
- OverrideState(oldToStateFactoryName, newToStateFactoryName);
- }
- /// <summary>
- /// Makes an override of a state.
- /// This could be used when you have made an inheritance of a state and want to replace the old one.
- /// </summary>
- /// <param name="oldToStateFactoryName"></param>
- /// <param name="newToStateFactoryName"></param>
- public void OverrideState(string oldToStateFactoryName, string newToStateFactoryName)
- {
- //Update all transitions going FROM the old state
- var transitions = GetTransitionsForState(oldToStateFactoryName);
- LookupTable.Remove(oldToStateFactoryName);
- LookupTable.Add(newToStateFactoryName, transitions);
- //Update all transitions going TO the old state.
- LookupTable.Values
- .SelectMany(x => x.Values)
- .Where(x => x.NextStateFactoryName == oldToStateFactoryName)
- .ForEach(x => x.NextStateFactoryName = newToStateFactoryName);
- }
- #endregion
- #region Methods: GetLists
- /// <summary>
- /// Returns a list of state factory names that is in the lookup table.
- /// </summary>
- /// <param name="includeAnyStates">Should the AnyStates be included?</param>
- public string[] GetStateNameList(bool includeAnyStates)
- {
- var stateNames = LookupTable.Keys
- .Concat(LookupTable
- .Values
- .SelectMany(dict => dict.Values.Select(entry => entry.NextStateFactoryName)))
- .Distinct();
- if (!includeAnyStates)
- {
- stateNames = stateNames.Where(x => x != AnyState.FactoryName);
- }
- return stateNames.ToArray();
- }
- #endregion
- }
- }
|