eglimi/cppfsm

Add entry and exit activities

Closed this issue · 1 comments

Hello,
I tried your fsm and I like the simplicity and the clean readable way of defining states and transitions. I was missing the entry/exit activities and tried to implement them.

...
  F::activityMap activities =
  {
    {States::A,
        {[]{std::cout << "State A entered" << std::endl;},  //entry
        []{std::cout << "State A exited" << std::endl;}}    //exit
    },
  };

  F fsm;
  fsm.add_transitions(transitions);
  fsm.add_activities(activities);
...

I am attaching the file with the implementation. If this is in line with your roadmap you could add the changes in the repo. This is kind of proof of concept so you can implement it in a different way.

fsm.zip

Hi @slavslavov, many thanks for your comment and the code you wrote. This is very much appreciated!

I thought about adding entry and exit actions before, and can understand that this is desirable in some situations.

Semantically, exit actions don't add anything new to this state machine, since they can be implemented in terms of transition actions. Therefore, they would just provide some syntactic sugar in order to not repeat the same function in several transitions. But this comes at the expense of a more complicated declaration.

Entry actions however do provide something new semantically, because they cannot be implemented in terms of transition actions. The reason is the time when a state change is performed. The state is changed after a transition action, but before an entry action. Or, in other words, when performing a transition action, the state is still the old one, whereas when performing an entry action, the state is already the new one.

This is the reason why I implemented entry actions myself at one time. After using it in a project in one state machine, I realised that I can / should simplify the state machine such that entry actions are no longer necessary.

I'm not saying that entry and exit actions are never useful, they are. But I prefer not to add them for this project. I rather keep the declaration as simple as possible. Because of this, also the implementation is simple, and it should be easy to implement entry and exit actions separately if needed, such as you did.

I would therefore close this issue. I hope this is ok and you still find the code useful as-is. That said, if many people disagree and feel that entry (and exit) actions should be added, I can certainly work on this.