Transition.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #region --------------- Copyright Dresser Wayne Pignone -------------
  2. /*
  3. * $Log: /Wrk/WayneLibraries/Wrk/StateEngine/Transition.cs $
  4. *
  5. * 4 08-02-26 14:11 Mattias.larsson
  6. * Removed Obsolete method.
  7. */
  8. #endregion
  9. #region Old file header
  10. /*===============================================================================
  11. * Transition
  12. *
  13. * Change history
  14. * When Who Comment
  15. * ---------- ------ ------------------------------------
  16. * 2006-02-03 RMa FXCop updates: BasicTransitionType renamed to BasicTransitionTypes.
  17. * 2006-01-27 RMa Removed interface Event. Use Event class instead.
  18. * 2005-06-05 RMa Added Basic transition type 'Error'.
  19. * 2005-12-09 RMa Added static method GetTransitionName for internal use.
  20. * 2005-12-05 RMa This header added.
  21. *
  22. ---------------------------------------------------------------------------------*/
  23. #endregion
  24. using System;
  25. namespace Wayne.Lib.StateEngine
  26. {
  27. /// <summary>
  28. /// The transition class is used by state objects to signal that a change has occured.
  29. /// The transition is interpreted by the statemachine's State-transition lookup table. it
  30. /// determines which the next state should be.
  31. ///
  32. /// The transition type identifies the transition and is supplied by the application. It is recommended
  33. /// that enums are used as transition types, but any type can be used.
  34. /// </summary>
  35. public class Transition
  36. {
  37. #region Fields
  38. private State sender;
  39. private StateEngineEvent sourceEvent;
  40. private object type;
  41. #endregion
  42. #region Properties
  43. /// <summary>
  44. /// Type of the transistion.
  45. /// </summary>
  46. public object Type
  47. {
  48. get
  49. {
  50. return type;
  51. }
  52. set
  53. {
  54. type = value;
  55. }
  56. }
  57. /// <summary>
  58. /// Represents the type name. It returns the Type.ToString(). This is
  59. /// used by the statemachine to find the transition in the lookup table.
  60. /// </summary>
  61. public string Name
  62. {
  63. get
  64. {
  65. return GetTransitionName(this.type);
  66. }
  67. }
  68. /// <summary>
  69. /// The state that issued the transition.
  70. /// </summary>
  71. public State Sender
  72. {
  73. get
  74. {
  75. return sender;
  76. }
  77. }
  78. /// <summary>
  79. /// If the transition is generated when a state has received an event, it can be
  80. /// supplied in this property.
  81. /// </summary>
  82. public StateEngineEvent SourceEvent
  83. {
  84. get
  85. {
  86. return sourceEvent;
  87. }
  88. }
  89. /// <summary>
  90. /// Internal writable source event. Assigned statemachine connects the event to the transition.
  91. /// </summary>
  92. internal StateEngineEvent WritableSourceEvent
  93. {
  94. get { return sourceEvent; }
  95. set { sourceEvent = value; }
  96. }
  97. #endregion
  98. #region Construction
  99. /// <summary>
  100. /// Transition constructor. assumes that the Source Event is null.
  101. /// </summary>
  102. /// <param name="sender">State that issued the transition</param>
  103. /// <param name="type">Type of the transition</param>
  104. public Transition(State sender, object type)
  105. {
  106. this.sender = sender;
  107. this.type = type;
  108. }
  109. #endregion
  110. #region Static Methods
  111. /// <summary>
  112. /// Returns the name of the transition to use when persisting configuration etc.
  113. /// </summary>
  114. /// <param name="transitionType"></param>
  115. /// <returns></returns>
  116. public static string GetTransitionName(object transitionType)
  117. {
  118. if (transitionType == null)
  119. return "-";
  120. else if (transitionType is Enum)
  121. return transitionType.GetType().FullName + "." + transitionType.ToString();
  122. else if (transitionType.GetType() == typeof(string))
  123. return transitionType.ToString();
  124. else
  125. return transitionType.GetType().FullName;
  126. }
  127. #endregion
  128. }
  129. }