StateEngineEvent.cs 5.2 KB

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