Entity.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. namespace Aitex.Core.RT.Fsm
  8. {
  9. public class Entity
  10. {
  11. protected Thread thread = null;
  12. protected IStateMachine fsm = null;
  13. protected Dictionary<int, int> _dictStateTime = new Dictionary<int, int>();
  14. public bool Running { get; protected set;}
  15. public Entity()
  16. {
  17. }
  18. public bool Initialize()
  19. {
  20. Debug.Assert(fsm != null);
  21. Init();
  22. fsm.Start();
  23. thread = new Thread(new ThreadStart(fsm.Loop));
  24. thread.Name = fsm.Name;
  25. thread.Start();
  26. while (!thread.IsAlive)
  27. Thread.Sleep(1);
  28. return true;
  29. }
  30. public void Terminate()
  31. {
  32. fsm.Stop();
  33. Term();
  34. if (thread.IsAlive)
  35. {
  36. Thread.Sleep(100);
  37. if (thread.IsAlive)
  38. {
  39. try
  40. {
  41. thread.Abort();
  42. }
  43. catch (Exception ex)
  44. {
  45. LOG.WriteExeption(String.Format("Entity terminate has exception."), ex);
  46. }
  47. }
  48. }
  49. //Term();
  50. }
  51. protected virtual bool Init()
  52. {
  53. return true;
  54. }
  55. protected virtual void Term()
  56. {
  57. }
  58. protected void Transition<T, V>(T state, V msg, FsmFunc func, T next)
  59. {
  60. Debug.Assert(typeof(T).IsEnum && typeof(V).IsEnum);
  61. int _state = Convert.ToInt32(state);
  62. int _next = Convert.ToInt32(next);
  63. int _msg = Convert.ToInt32(msg);
  64. Transition(_state, _msg, func, _next);
  65. }
  66. protected void Transition(int state, int msg, FsmFunc func, int next)
  67. {
  68. if (fsm != null)
  69. fsm.Transition(state, msg, func, next);
  70. }
  71. protected void AnyStateTransition(int msg, FsmFunc func, int next)
  72. {
  73. if (fsm != null)
  74. fsm.AnyStateTransition(msg, func, next);
  75. }
  76. //protected void AnyStateTransition(int msg, FsmFunc func, int next, FsmFunc monitorFunc)
  77. //{
  78. // if (fsm != null)
  79. // fsm.AnyStateTransition(msg, func, next,monitorFunc);
  80. //}
  81. protected void AnyStateTransition<T, V>(V msg, FsmFunc func, T next)
  82. {
  83. Debug.Assert(typeof(T).IsEnum && typeof(V).IsEnum);
  84. int _next = Convert.ToInt32(next);
  85. int _msg = Convert.ToInt32(msg);
  86. AnyStateTransition(_msg, func, _next);
  87. }
  88. protected void AnyStateTransition<T, V>(V msg, FsmFunc func, T next, FsmFunc monitorFunc)
  89. {
  90. Debug.Assert(typeof(T).IsEnum && typeof(V).IsEnum);
  91. int _next = Convert.ToInt32(next);
  92. int _msg = Convert.ToInt32(msg);
  93. AnyStateTransition(_msg, func, _next, monitorFunc);
  94. }
  95. protected void EnterExitTransition<T, V>(T state, FsmFunc enter, Nullable<V> msg, FsmFunc exit) where V : struct
  96. {
  97. Debug.Assert(typeof(T).IsEnum && ((msg == null) || typeof(V).IsEnum));
  98. int _state = Convert.ToInt32(state);
  99. int _msg = msg == null ? (int)FSM_MSG.NONE : Convert.ToInt32(msg);
  100. EnterExitTransition(_state, enter, _msg, exit);
  101. }
  102. protected void EnterExitTransition(int state, FsmFunc enter, int msg, FsmFunc exit)
  103. {
  104. if (fsm != null)
  105. fsm.EnterExitTransition(state, enter, msg, exit);
  106. }
  107. public void PostMsg<T>(T msg, params object[] args) where T : struct
  108. {
  109. Debug.Assert(typeof(T).IsEnum);
  110. int id = Convert.ToInt32(msg);
  111. PostMsg(id, args);
  112. }
  113. public void PostMsg(int msg, params object[] args)
  114. {
  115. if (fsm == null)
  116. {
  117. //LOG.Error($"fsm is null, post msg {msg}");
  118. return;
  119. }
  120. fsm.PostMsg(msg, args);
  121. }
  122. public void MarkStateTime()
  123. {
  124. _dictStateTime[fsm.State] = fsm.ElapsedTime;
  125. }
  126. public virtual int TimeToReady
  127. {
  128. get
  129. {
  130. if (_dictStateTime.ContainsKey(fsm.State))
  131. {
  132. if ((_dictStateTime[fsm.State] - fsm.ElapsedTime) > 0)
  133. return _dictStateTime[fsm.State] - fsm.ElapsedTime;
  134. else
  135. return 0;
  136. }
  137. return int.MaxValue;
  138. }
  139. }
  140. }
  141. }