Nunnifsmgen-Automatic Code Generator for Finite State Machine programs

Source: Internet
Author: User
Tags fsm
Nunnifsmgen-Automatic Code Generator for Finite State Machine programs

This is a good thing I know from my colleagues, nunni FSM generator. It can automatically generate the program code of the state machine according to the configuration file you entered. For detailed data, please refer to the software instructions, however, I want to focus on "automatically generated program code". Because the automatically generated code is encapsulated beautifully, I want to introduce the code.

Something that used to write state machines should not be separated from if, else, Case, switch... When these syntaxes are used together, the written code is "not easy to understand". For example, I have a state machine today. Suppose there is a table with only one bottle on it, if I take the bottle away, I cannot take it away from the table. If there is already a bottle on the table, then there will be no more bottles on the table.

If you use the IF-else syntax to write and read data, it may be as follows:

  1. # Include
  2. Int bottles = 1;
  3. Void getbottleapi ()
  4. {
  5. If (Bottles = 1)
  6. {
  7. Printf ("You got bottle/N ");
  8. Bottles --;
  9. }
  10. Else printf ("No Bootle to get/N ");
  11. }
  12. Void putbottleapi ()
  13. {
  14. If (Bottles = 0)
  15. {
  16. Printf ("You put a bottle/N ");
  17. Bottles ++;
  18. }
  19. Else printf ("too much bootles/N ");
  20. }
  21. Int main ()
  22. {
  23. Getbottleapi ();
  24. Getbottleapi ();
  25. Putbottleapi ();
  26. Putbottleapi ();
  27. Getbottleapi ();
  28. }

Int bottles is used when the global variable is used to determine whether the get or put action is valid. If this method is used in a small state machine, it is OK. If the state machine is large and complex, we can imagine that the program code will write several more judgment variables for these states.

Try the following program code. I only need to define the trigger mechanism for each State and external action, and then I can write a pretty code.

  1. # Include
  2. Struct tablestate;
  3. Struct tablefsm
  4. {
  5. INT (* getbottlestatus) (struct tablefsm * FSM, void * O );
  6. INT (* putbottlestatus) (struct tablefsm * FSM, void * O );
  7. Void (* changestate) (struct tablefsm * FSM, struct tablestate * nextstate );
  8. Struct tablestate * m_state;
  9. };
  10. Struct tablestate
  11. {
  12. INT (* getbottlestatus) (struct tablefsm * FSM, void * O );
  13. INT (* putbottlestatus) (struct tablefsm * FSM, void * O );
  14. };
  15. Static struct tablestate tablefull;
  16. Static struct tablestate tableempty;
  17. Void getbottle ()
  18. {
  19. Printf ("You get a bottle/N ");
  20. }
  21. Void givebottle ()
  22. {
  23. Printf ("You put bottle on the table/N ");
  24. }
  25. Void Nobottle ()
  26. {
  27. Printf ("there is no bottle on the table/N ");
  28. }
  29. Void bottlefull ()
  30. {
  31. Printf ("there is too much bottle on the table/N ");
  32. }
  33. Int fsmgetbottlestat (struct tablefsm * FSM, void * O)
  34. {
  35. Return FSM-> m_state-> getbottlestatus (FSM, O );
  36. }
  37. Int fsmputbottlestat (struct tablefsm * FSM, void * O)
  38. {
  39. Return FSM-> m_state-> putbottlestatus (FSM, O );
  40. }
  41. Void fsmchangestate (struct tablefsm * FSM, struct bottlestate * newstate)
  42. {
  43. FSM-> m_state = newstate;
  44. }
  45. Int getbottleapi (struct tablefsm * FSM, void * O)
  46. {
  47. Getbottle ();
  48. FSM-> changestate (FSM, & tableempty );
  49. }
  50. Int givebottleapi (struct tablefsm * FSM, void * O)
  51. {
  52. Givebottle ();
  53. FSM-> changestate (FSM, & tablefull );
  54. }
  55. Int toofewbottleapi (struct tablefsm * FSM, void * O)
  56. {
  57. Nobottle ();
  58. FSM-> changestate (FSM, & tableempty );
  59. }
  60. Int toomuchbottleapi (struct tablefsm * FSM, void * O)
  61. {
  62. Bottlefull ();
  63. FSM-> changestate (FSM, & tablefull );
  64. }
  65. Int main ()
  66. {
  67. Static struct tablefsm BF;
  68. Tablefull. getbottlestatus = getbottleapi;
  69. Tablefull. putbottlestatus = toomuchbottleapi;
  70. Tableempty. getbottlestatus = toofewbottleapi;
  71. Tableempty. putbottlestatus = givebottleapi;
  72. BF. getbottlestatus = fsmgetbottlestat;
  73. BF. putbottlestatus = fsmputbottlestat;
  74. BF. changestate = fsmchangestate;
  75. BF. m_state = & tablefull;
  76. Fsmgetbottlestat (& BF, 0 );
  77. Fsmgetbottlestat (& BF, 0 );
  78. Fsmputbottlestat (& BF, 0 );
  79. Fsmputbottlestat (& BF, 0 );
  80. }

The program code above is quite concise. When an external action triggers an internal state change, the function specified by FSM is specified to the State callback after the transfer, therefore, programmer can fully focus on the implementation of functions such as getbottleapi, without considering the complexity of the State Conversion Program.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.