RootStateMachine.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. CurrentState.IncomingEvent(stateEngineEvent, ref transition);
  41. if (transition != null)
  42. {
  43. this.HandleTransition(ref transition);
  44. //Unhandled transition
  45. if (transition != null)
  46. {
  47. if ((debugLogger != null) && (debugLogger.IsActive(logCategory)))
  48. {
  49. debugLogger.Add(string.Format(System.Globalization.CultureInfo.InvariantCulture,
  50. "State {0} issued transition {1} that was not configured to be handled anywhere.",
  51. transition.Sender.LogName, transition.Name), logCategory);
  52. }
  53. }
  54. }
  55. }
  56. }
  57. protected void PerformInitialTransition()
  58. {
  59. lock (handlingEventSyncObject)
  60. {
  61. Transition transition = null;
  62. started = true;
  63. this.EnterInitialState(null, ref transition);
  64. //Unhandled transition
  65. if (transition != null)
  66. {
  67. if ((debugLogger != null) && (debugLogger.IsActive(logCategory)))
  68. {
  69. debugLogger.Add(string.Format(System.Globalization.CultureInfo.InvariantCulture,
  70. "State [{0}] issued transition [{1}] that was not configured to be handled anywhere.",
  71. transition.Sender.LogName, transition.Name), logCategory);
  72. }
  73. }
  74. }
  75. }
  76. protected override void Dispose(bool disposing)
  77. {
  78. base.Dispose(disposing);
  79. started = false;
  80. }
  81. #endregion
  82. #region Properties
  83. /// <summary>
  84. /// Indicates that the state machine is started.
  85. /// </summary>
  86. public override bool Started
  87. {
  88. get { return started; }
  89. }
  90. #endregion
  91. }
  92. }