kiishor / UML-State-Machine-in-C

A minimalist UML State machine framework for finite state machine and hierarchical state machine in C
MIT License
170 stars 47 forks source link

Help: hierarchical state machine #31

Open KammutierSpule opened 4 years ago

KammutierSpule commented 4 years ago

Hello! Thanks for this nice project and well documented! I didn't tried it yet, I'm learning how could I use it. I am trying to implement the following hierarchical state machine, that I will try explain with ascii code:

A)L0S0->B)L0S1->C)L0S2->...
A)L1S0  B)L1S0  C)L1S0
A)L1S1  B)L1S1  C)L1S1
A)L1S2  B)L1S2  C)L1S2

So I have at L0 State0 that can switch to L0 S1 (or L0 State N-1), .. but then on each L0 State, I can switch to a new L1 state for each On L1 I will have L1 S0 ->S1->S2->S0.. and then can return to L0 from any point

From this library, there are Entry and Exit callbacks for each state. But I would like to implement an Entry/Exit for each Level So when it enter or exit a Level it will call a function to prepare things for that level

How can I implement an Entry/Exit for a Level?

kiishor commented 4 years ago

Hi Mario Luzeiro,

The UML state machine explained in Wikipedia or other resources don't have a concept of entry and exit callback for level. The entry and exit actions are for the State.

I couldn't understand the complete hierarchy of your states from your description.
Does the below diagram matches with your description? If yes you can explain your specific requirement based on the below diagram. However, if you post your state diagram it will be useful to understand your requirement.

State Diagram

As shown in the diagram, the state transition from L1S1to L1S4 will be L1S1->L0S0->L1S1->L1S4. And their entry-exit actions flow will be,

L1S1->Exit()
L0S0->Entry()
L0S0->Exit()
L1S1->Entry()
L1S1->Exit()
L1S4->Entry()
  1. Do you need a common entry and exit callback for Level? e.g. a single entry and exit callback for L1S0 to L1S8. Also a common entry and exit callback for L0S0, L0S1, and L0S2. OR
  2. Do you need an additional entry action for each state? This entry action will be triggered whenever there a is state transition between different levels. In both cases, I don't think it conforms to the UML hierarchical state machine specification. It is possible to fork and modify the framework to suit your requirement. However, I would recommend you to stick to the standard state machine philosophy.

Will it be possible to implement your requirement by adding logic inside the entry action of each state? OR Can we solve your problem by rearranging the states and their hierarchy?

KammutierSpule commented 4 years ago

Thanks for your answer! I'm not familiar with the UML notation. I made a small change on your diagram: image

Each L1 is specific for each L0Sx, there wont be transitions between L1 for different L0Sx states

Will it be possible to implement your requirement by adding logic inside the entry action of each state? OR Can we solve your problem by rearranging the states and their hierarchy?

I have that in mind A possibility would be to implement some internal logic (bool flags etc) that I can use to enter/exit of L0 Another possibility I think would be to use separate and additional State Machines.. like one state machine with just 1 state so I can add the Entry/Exit from Level. So each Level is implemented with a 1 single state State Machine