lundi 18 septembre 2017

What is the best data structure to implement Finite state machine?

What is the best data structure to implement FSM in C++. The FSM should have below attributes:-

  1. Capability to register new states and transition at run time
  2. Capability to register callbacks for transitions.
  3. State and Input must be generic i.e. template based.

transition example :- Transition(‘State-A’, ‘State-B’, “input-1”, callback-1); Machine will move from State-A to State-B if input-1 has been passed and callback-1 will get called.

My Approach :-

  1. Create a base class CState and have a pure virtual function in that i.e. GetHashCode() = 0;
  2. Create a base class CInput and have a pure virtual function in that i.e. GetHashCode() = 0;
  3. Maintain a hashtable for keeping states. key will be Hashcode returned from CState::GetHashCode function, and value will be the entire object of CState class.
  4. Create another hashtable for maintaining transactions. It will have composite key made up of hascode of CState and CInput class, and value will be HasCode of new state for the machine and callback function pointer.

pl advise if there is any flaw with the above design.

Aucun commentaire:

Enregistrer un commentaire