RootStateMachine.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #region --------------- Copyright Dresser Wayne Pignone -------------
  2. /*
  3. * $Log: /Wrk/WayneLibraries/Wrk/StateEngine/RootStateMachine.cs $
  4. *
  5. * 4 08-02-26 14:08 Mattias.larsson
  6. * Use the LogName-property to log a state.
  7. *
  8. * 3 07-08-15 15:28 roger.månsson
  9. * Support for new debug log feature.
  10. *
  11. * 2 07-03-12 14:59 roger.månsson
  12. * Updated for the changes with the new state machine hiearchy.
  13. */
  14. #endregion
  15. using System;
  16. using System.Collections.Generic;
  17. using System.Text;
  18. using Wayne.Lib.Log;
  19. namespace Wayne.Lib.StateEngine
  20. {
  21. //[System.Diagnostics.DebuggerNonUserCode()]
  22. abstract class RootStateMachine : StateMachine
  23. {
  24. #region Fields
  25. object handlingEventSyncObject = new object();
  26. bool started;
  27. #endregion
  28. #region Construction
  29. internal RootStateMachine(string name, IDebugLogger debugLogger, object logCategory)
  30. : base(name, debugLogger, logCategory)
  31. {
  32. }
  33. #endregion
  34. #region Protected methods
  35. protected virtual void HandleEventInCurrentState(StateEngineEvent stateEngineEvent)
  36. {
  37. lock (handlingEventSyncObject)
  38. {
  39. Transition transition = null;
  40. if (CurrentState != null)
  41. {
  42. CurrentState.IncomingEvent(stateEngineEvent, ref transition);
  43. }
  44. if (transition != null)
  45. {
  46. this.HandleTransition(ref transition);
  47. //Unhandled transition
  48. if (transition != null)
  49. {
  50. if ((debugLogger != null) && (debugLogger.IsActive(logCategory)))
  51. {
  52. debugLogger.Add(string.Format(System.Globalization.CultureInfo.InvariantCulture,
  53. "State {0} issued transition {1} that was not configured to be handled anywhere.",
  54. transition.Sender.LogName, transition.Name), logCategory);
  55. }
  56. }
  57. }
  58. }
  59. }
  60. protected void PerformInitialTransition()
  61. {
  62. lock (handlingEventSyncObject)
  63. {
  64. Transition transition = null;
  65. started = true;
  66. this.EnterInitialState(null, ref transition);
  67. //Unhandled transition
  68. if (transition != null)
  69. {
  70. if ((debugLogger != null) && (debugLogger.IsActive(logCategory)))
  71. {
  72. debugLogger.Add(string.Format(System.Globalization.CultureInfo.InvariantCulture,
  73. "State [{0}] issued transition [{1}] that was not configured to be handled anywhere.",
  74. transition.Sender.LogName, transition.Name), logCategory);
  75. }
  76. }
  77. }
  78. }
  79. protected override void Dispose(bool disposing)
  80. {
  81. base.Dispose(disposing);
  82. started = false;
  83. }
  84. #endregion
  85. #region Properties
  86. /// <summary>
  87. /// Indicates that the state machine is started.
  88. /// </summary>
  89. public override bool Started
  90. {
  91. get { return started; }
  92. }
  93. #endregion
  94. }
  95. }