DemoEntity.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace Aitex.Core.RT.Fsm
  6. {
  7. public class DemoEntity : Entity
  8. {
  9. public enum STATE
  10. {
  11. IDLE,
  12. RUN,
  13. ERROR,
  14. }
  15. public enum MSG
  16. {
  17. INIT,
  18. RUN,
  19. STOP,
  20. ERROR,
  21. }
  22. public static void Testcase()
  23. {
  24. DemoEntity demo = new DemoEntity();
  25. demo.Initialize();
  26. Console.WriteLine("DemoEntity Unit Test.");
  27. Console.WriteLine("Input \"exit\" for terminate.");
  28. string cmd;
  29. while ((cmd = Console.ReadLine()) != "exit")
  30. {
  31. switch (cmd)
  32. {
  33. case "run":
  34. demo.PostMsg<DemoEntity.MSG>(DemoEntity.MSG.RUN, null);
  35. break;
  36. case "init":
  37. demo.PostMsg<DemoEntity.MSG>(DemoEntity.MSG.INIT, null);
  38. break;
  39. case "stop":
  40. demo.PostMsg<DemoEntity.MSG>(DemoEntity.MSG.STOP, null);
  41. break;
  42. case "error":
  43. demo.PostMsg<DemoEntity.MSG>(DemoEntity.MSG.ERROR, null);
  44. break;
  45. }
  46. }
  47. demo.Terminate();
  48. Console.WriteLine("Input any key for quite.");
  49. Console.Read();
  50. Console.WriteLine("DemoEntity Unit Test end");
  51. }
  52. public DemoEntity()
  53. {
  54. fsm = new StateMachine<DemoEntity>("Demo", (int)STATE.IDLE, 3000);
  55. EnterExitTransition<STATE, MSG>(STATE.IDLE, EnterIdel, null, null);
  56. AnyStateTransition(MSG.ERROR, Error, STATE.ERROR);
  57. Transition(STATE.IDLE, MSG.RUN, Run, STATE.RUN);
  58. Transition(STATE.ERROR, MSG.INIT, Init, STATE.IDLE);
  59. Transition(STATE.RUN, MSG.STOP, null, STATE.IDLE);
  60. Transition(STATE.RUN, FSM_MSG.TIMER, OnTimeout, STATE.RUN);
  61. }
  62. private bool Run(object[] objs)
  63. {
  64. Console.WriteLine("Demo Run");
  65. return true;
  66. }
  67. private bool Init(object[] objs)
  68. {
  69. Console.WriteLine("Demo Init");
  70. return true;
  71. }
  72. private bool Error(object[] objs)
  73. {
  74. Console.WriteLine("Demo Error");
  75. return true;
  76. }
  77. private bool EnterIdel(object[] objs)
  78. {
  79. Console.WriteLine("Enter idle state");
  80. return true;
  81. }
  82. private bool OnTimeout(object[] objs)
  83. {
  84. if (fsm.ElapsedTime > 10000)
  85. {
  86. Console.WriteLine("Demo run is timeout");
  87. PostMsg<MSG>(MSG.ERROR);
  88. }
  89. else
  90. {
  91. Console.WriteLine("Demo run recive timer msg");
  92. }
  93. return true;
  94. }
  95. }
  96. }