StateEngineEvent.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*===============================================================================
  2. * StateEngineEvent.cs
  3. *
  4. * Change history
  5. * When Who Comment
  6. * ---------- ------ ----------------------------------
  7. * 2006-07-18 RMa Added Priority and ArrivalTime.
  8. * 2006-03-02 RMa FXCop updates: validate eventtype in constructor.
  9. * 2006-02-03 RMA FXCop updates: Changed from Event -> StateEngineEvent. (Event is a keyword)
  10. * 2006-01-27 RMa Removed interface Event. Use Event class instead.
  11. * 2006-01-09 RMa Added ToString() method.
  12. * 2005-12-05 RMa This header added.
  13. *
  14. ---------------------------------------------------------------------------------*/
  15. using System;
  16. namespace Wayne.Lib.StateEngine
  17. {
  18. /// <summary>
  19. /// Event class that implements the Event interface. It is the base
  20. /// class to be used to send events to states in the state machine.
  21. /// </summary>
  22. public class StateEngineEvent
  23. {
  24. #region Fields
  25. private bool handled;
  26. private object type;
  27. private DateTime arrivalTime = DateTime.MinValue;
  28. private int priority;
  29. private uint sequenceNumber;
  30. #endregion
  31. #region Construction
  32. /// <summary>
  33. /// Initializes a new instance of the StateEngine event with the default priority 0.
  34. /// </summary>
  35. /// <param name="eventType">Object identifying the event.</param>
  36. public StateEngineEvent(object eventType)
  37. : this(eventType, 0)
  38. {
  39. }
  40. /// <summary>
  41. /// Initializes a new instance of the StateEngine event with the specified priority
  42. /// </summary>
  43. /// <param name="eventType">Object identifying the event.</param>
  44. /// <param name="priority">Priority specifier, Lower number specifies that it will be handled sooner than higher numbers. Default priority is 0.</param>
  45. public StateEngineEvent(object eventType, int priority)
  46. {
  47. this.type = eventType;
  48. this.priority = priority;
  49. }
  50. #endregion
  51. #region Properties
  52. /// <summary>
  53. /// Indicates if the event has been handled. If the state is handled, it is the application's responsiblity
  54. /// to set this flag. the State engine never changes it.
  55. /// </summary>
  56. public bool Handled
  57. {
  58. get { return handled; }
  59. set { handled = value; }
  60. }
  61. /// <summary>
  62. /// Defines the type of the event. Can be any reference or value
  63. /// type (through boxing). Recommended is use of a user-defined enumeration.
  64. /// The EventType is a arbitary type, preferrably enums, but any type can be used.
  65. /// </summary>
  66. public object Type
  67. {
  68. get { return type; }
  69. }
  70. /// <summary>
  71. /// Specifies the priority of this event. High priority is defined by lower integers. Highest priority is thereby Int.Min.
  72. /// Default priority is 0.
  73. /// </summary>
  74. public int Priority
  75. {
  76. get { return priority; }
  77. set { priority = value; }
  78. }
  79. /// <summary>
  80. /// Specifies the time the event was sent into the state machine. This is used to determine
  81. /// the handling order when resending events to states.
  82. /// </summary>
  83. internal DateTime ArrivalTime
  84. {
  85. get { return arrivalTime; }
  86. set { arrivalTime = value; }
  87. }
  88. /// <summary>
  89. /// Specifies an arrival sequence number that is assigned when the event is sent to a state machine.
  90. /// </summary>
  91. internal uint SequenceNumber
  92. {
  93. get { return sequenceNumber; }
  94. set { sequenceNumber = value; }
  95. }
  96. #endregion
  97. #region Debug methods
  98. /// <summary>
  99. /// Presents the class as a string.
  100. /// </summary>
  101. /// <returns></returns>
  102. public virtual string ToString(string format, IFormatProvider provider)
  103. {
  104. return String.Format(provider, "Event(type={0}), Prio={1}, Arrived={2}, Sq={3}", type, priority, arrivalTime.ToString("HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture), sequenceNumber);
  105. }
  106. /// <summary>
  107. /// Presents the class as a string using the specified culture-specific format information.
  108. /// </summary>
  109. /// <returns></returns>
  110. public virtual string ToString(IFormatProvider provider)
  111. {
  112. return ToString("", provider);
  113. }
  114. /// <summary>
  115. /// Presents the class as a string using a format string.
  116. /// </summary>
  117. /// <returns></returns>
  118. public virtual string ToString(string format)
  119. {
  120. return ToString(format, System.Globalization.CultureInfo.InvariantCulture);
  121. }
  122. /// <summary>
  123. /// Presents the class as a string using a format string and the specified culture-specific format information.
  124. /// </summary>
  125. /// <returns></returns>
  126. public override string ToString()
  127. {
  128. return ToString("", System.Globalization.CultureInfo.InvariantCulture);
  129. }
  130. #endregion
  131. }
  132. }