Entity.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4. using System.Diagnostics;
  5. using Aitex.Core.RT.Event;
  6. using Aitex.Core.RT.Log;
  7. using CyberX8_Core;
  8. using System.Reflection;
  9. using DocumentFormat.OpenXml.Wordprocessing;
  10. namespace Aitex.Core.RT.Fsm
  11. {
  12. public class Entity
  13. {
  14. protected Thread thread = null;
  15. protected IStateMachine fsm = null;
  16. public bool Running { get; protected set;}
  17. /// <summary>
  18. /// 剩余时长ToReady
  19. /// </summary>
  20. public virtual int TimeToReady
  21. {
  22. get { return int.MaxValue / 2; }
  23. }
  24. public Entity()
  25. {
  26. }
  27. public bool Initialize()
  28. {
  29. Init();
  30. if (fsm != null)
  31. {
  32. fsm.Start();
  33. thread = new Thread(new ThreadStart(fsm.Loop));
  34. thread.Name = fsm.Name;
  35. thread.Start();
  36. LOG.WriteLog(eEvent.EV_SCHEDULER, "System", $"Create Entity Thread {fsm.Name}");
  37. while (!thread.IsAlive)
  38. Thread.Sleep(1);
  39. }
  40. return true;
  41. }
  42. public void Terminate()
  43. {
  44. if (fsm != null)
  45. {
  46. fsm.Stop();
  47. }
  48. Term();
  49. if (thread!=null&&thread.IsAlive)
  50. {
  51. Thread.Sleep(100);
  52. if (thread.IsAlive)
  53. {
  54. try
  55. {
  56. thread.Abort();
  57. }
  58. catch (Exception ex)
  59. {
  60. LOG.WriteExeption(String.Format("Entity terminate has exception."), ex);
  61. }
  62. }
  63. }
  64. //Term();
  65. }
  66. protected virtual bool Init()
  67. {
  68. return true;
  69. }
  70. protected virtual void Term()
  71. {
  72. }
  73. protected void Transition<T, V>(T state, V msg, FsmFunc func, T next)
  74. {
  75. Debug.Assert(typeof(T).IsEnum && typeof(V).IsEnum);
  76. int _state = Convert.ToInt32(state);
  77. int _next = Convert.ToInt32(next);
  78. int _msg = Convert.ToInt32(msg);
  79. Transition(_state, _msg, func, _next);
  80. }
  81. protected void Transition<T,V>(T state,V msg,FsmFunc func,FsmFunc waitFunc,T next)
  82. {
  83. Debug.Assert(typeof(T).IsEnum && typeof(V).IsEnum);
  84. int _state = Convert.ToInt32(state);
  85. int _next = Convert.ToInt32(next);
  86. int _msg = Convert.ToInt32(msg);
  87. Transition(_state, _msg, func,waitFunc, _next);
  88. }
  89. protected void Transition(int state, int msg, FsmFunc func, int next)
  90. {
  91. if (fsm != null)
  92. fsm.Transition(state, msg, func, next);
  93. }
  94. protected void Transition(int state, int msg, FsmFunc func,FsmFunc waitFunc, int next)
  95. {
  96. if (fsm != null)
  97. fsm.Transition(state, msg, func,waitFunc, next);
  98. }
  99. protected void AnyStateTransition(int msg, FsmFunc func, int next)
  100. {
  101. if (fsm != null)
  102. fsm.AnyStateTransition(msg, func, next);
  103. }
  104. //protected void AnyStateTransition(int msg, FsmFunc func, int next, FsmFunc monitorFunc)
  105. //{
  106. // if (fsm != null)
  107. // fsm.AnyStateTransition(msg, func, next,monitorFunc);
  108. //}
  109. protected void AnyStateTransition<T, V>(V msg, FsmFunc func, T next)
  110. {
  111. Debug.Assert(typeof(T).IsEnum && typeof(V).IsEnum);
  112. int _next = Convert.ToInt32(next);
  113. int _msg = Convert.ToInt32(msg);
  114. AnyStateTransition(_msg, func, _next);
  115. }
  116. protected void AnyStateTransition<T, V>(V msg, FsmFunc func, T next, FsmFunc monitorFunc)
  117. {
  118. Debug.Assert(typeof(T).IsEnum && typeof(V).IsEnum);
  119. int _next = Convert.ToInt32(next);
  120. int _msg = Convert.ToInt32(msg);
  121. AnyStateTransition(_msg, func, _next, monitorFunc);
  122. }
  123. protected void EnterExitTransition<T, V>(T state, FsmFunc enter, Nullable<V> msg, FsmFunc exit) where V : struct
  124. {
  125. Debug.Assert(typeof(T).IsEnum && ((msg == null) || typeof(V).IsEnum));
  126. int _state = Convert.ToInt32(state);
  127. int _msg = msg == null ? (int)FSM_MSG.NONE : Convert.ToInt32(msg);
  128. EnterExitTransition(_state, enter, _msg, exit);
  129. }
  130. protected void EnterExitTransition(int state, FsmFunc enter, int msg, FsmFunc exit)
  131. {
  132. if (fsm != null)
  133. fsm.EnterExitTransition(state, enter, msg, exit);
  134. }
  135. public void PostMsg<T>(T msg, params object[] args) where T : struct
  136. {
  137. Debug.Assert(typeof(T).IsEnum);
  138. int id = Convert.ToInt32(msg);
  139. PostMsg(id, args);
  140. }
  141. public void PostMsg(int msg, params object[] args)
  142. {
  143. if (fsm == null)
  144. {
  145. //LOG.Error($"fsm is null, post msg {msg}");
  146. return;
  147. }
  148. fsm.PostMsg(msg, args);
  149. }
  150. public bool CheckToPostMessage<T,V>(eEvent eEvent,string moduleName,int msg, params object[] args)
  151. {
  152. string state = Enum.GetName(typeof(T), fsm.State);
  153. string strMsg = Enum.GetName(typeof(V), msg);
  154. if (!fsm.FindTransition(fsm.State, msg))
  155. {
  156. LOG.WriteLog(eEvent, moduleName, $"{moduleName} is in {state} state,can not do {strMsg}");
  157. return false;
  158. }
  159. Running = true;
  160. fsm.PostMsg(msg, args);
  161. LOG.WriteLog(eEvent.EV_FSM_NOTIFY, moduleName, $"{moduleName} is in {state} state,do {strMsg}");
  162. return true;
  163. }
  164. public bool NullFunc(object[] param)
  165. {
  166. return true;
  167. }
  168. }
  169. }