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
- }
-
-
-
-
- public class StateTransitionLookup
- {
- #region Fields
- private readonly StateTypeContainer stateTypeContainer;
- #endregion
- #region Construction
-
-
-
- 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
-
-
-
-
-
-
-
-
-
-
-
-
-
- public StateEntry GetNextState(string sourceStateFactoryName, Wayne.Lib.StateEngine.Transition transition)
- {
- if (transition == null)
- return null;
- StateLookupEntry stateLookupEntry;
-
- if (TryLookup(sourceStateFactoryName, transition.Type, out stateLookupEntry))
- {
- return new StateEntry(transition, stateLookupEntry.NextStateFactoryName, stateLookupEntry.HistoryType);
- }
-
- if (sourceStateFactoryName != AnyState.FactoryName)
- {
- 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
-
-
-
-
-
-
-
-
- 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;
- }
-
-
-
-
-
- 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;
- }
-
-
-
-
-
-
-
- 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);
- }
-
-
-
-
-
-
-
- public void AddTransition(string fromStateFactoryName, string toStateFactoryName, object transitionType)
- {
- InternalAddTransition(fromStateFactoryName, transitionType, toStateFactoryName, HistoryType.None, false);
- }
-
-
-
-
-
-
-
-
- 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)
-
-
-
-
-
-
- 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);
- }
-
-
-
-
-
-
- public void ReplaceTransition(string fromOldStateFactoryName, string toNewStateFactoryName, object transitionType)
- {
- InternalAddTransition(fromOldStateFactoryName, transitionType, toNewStateFactoryName, HistoryType.None, true);
- }
-
-
-
-
-
-
-
- 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)
-
-
-
-
-
- public void ReplaceTransitions<TOldToState, TNewToState>()
- where TOldToState : State
- where TNewToState : State
- {
- stateTypeContainer.Register<TOldToState>();
- stateTypeContainer.Register<TNewToState>();
- ReplaceTransitions(typeof(TOldToState).FullName, typeof(TNewToState).FullName);
- }
-
-
-
-
-
- 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
-
-
-
-
-
-
-
- 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);
- }
-
-
-
-
-
-
-
-
- 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));
- }
-
- ReplaceTransition<TFromState, TIntersectingState>(fromTransitionType);
- AddTransition<TIntersectingState, TOldToState>(toTransitionType);
- }
- #endregion
- #region Method: ReplaceState
-
-
-
-
-
-
- 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);
- }
-
-
-
-
-
-
-
- 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);
- }
-
-
-
-
-
-
- public void ReplaceState(string oldToStateFactoryName, string newToStateFactoryName)
- {
- ReplaceState(oldToStateFactoryName, newToStateFactoryName, false);
- }
-
-
-
-
-
-
-
-
- public void ReplaceState(string oldToStateFactoryName, string newToStateFactoryName, bool removeAllTransitionsFromState)
- {
- var transitionsForState = GetTransitionsForState(oldToStateFactoryName);
-
- var selfRefTransitions = transitionsForState
- .Where(x => x.Value.NextStateFactoryName == oldToStateFactoryName)
- .ToArray();
-
- var transitions = transitionsForState;
- LookupTable.Remove(oldToStateFactoryName);
- if (!removeAllTransitionsFromState)
- {
- LookupTable.Add(newToStateFactoryName, transitions);
- }
-
- LookupTable.Values
- .SelectMany(x => x.Values)
- .Where(x => x.NextStateFactoryName == oldToStateFactoryName)
- .ForEach(x => x.NextStateFactoryName = newToStateFactoryName);
-
-
- selfRefTransitions.ForEach(t =>
- {
- transitionsForState.Remove(t.Key);
- InternalAddTransition(oldToStateFactoryName, t.Key, newToStateFactoryName, t.Value.HistoryType, false);
- });
- }
- #endregion
- #region Method: OverrideState
-
-
-
-
-
-
- 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);
- }
-
-
-
-
-
-
- public void OverrideState(string oldToStateFactoryName, string newToStateFactoryName)
- {
-
- var transitions = GetTransitionsForState(oldToStateFactoryName);
- LookupTable.Remove(oldToStateFactoryName);
- LookupTable.Add(newToStateFactoryName, transitions);
-
- LookupTable.Values
- .SelectMany(x => x.Values)
- .Where(x => x.NextStateFactoryName == oldToStateFactoryName)
- .ForEach(x => x.NextStateFactoryName = newToStateFactoryName);
- }
- #endregion
- #region Methods: GetLists
-
-
-
-
- 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
- }
- }
|