The State Machine

Tram Ho

The State Machine is defined here as a machine state architecture that processes messages and can have ordered states.

A state is a State object and enter / exit / getName option states. The enter / exit methods are the equivalent of construction and destruction in object-oriented programming and are used to perform the initialization and cleaning of respective states. The getName method returns the name of the state; the default implementation returns the class name. It may be desirable for the getName method to return an instance of state instead of a name in some separate cases if a separate state class has many instances.

When a state machine is created, addState is used to build the architecture and setInitialState is used to indicate this is the initialization state. After the initialization process, the programmer calls start which will initialize and start a state machine. SateMachine’s first action is to call enter for all architecture’s initialization states, starting at its largest parent. The enter call will be executed in the context of the StateMachine Handler, not in the context of where it was called to start, and they will be called before any messages processed. For example, in the simple state machine below, mP1.enter will be called and then mS1.enter. Finally, messages sent to the state machine will be processed by the current state; In our simple state machine below that could be the mS1.processMessage initialization.

After the state machine is created and run, messages are sent to a state machine using sendMessage and messages created using obtainMessage. When a state machine receives a message, the current state’s processMessage method is called. In the above example mS1.processMessage will be called first. The state can use transitionTo to change the current state to a new state.

Each state in the state machine can have no or one parent. If a child state cannot process a message, it may have to be handled by its parent by returning false or NOT_HANDLE. If a message is not processed by a child state or any of its ancestors, the unhandledMessage is called to send a last chance to the state machine to process that message.

When all processing completes a state machine can choose to call transitionToHaltingState. When the current processingMessage returns, the state machine moves to a local HaltingState and then halting. Any subsequent messages received by the sate machine will cause the haltedProcessMassage to be called.

If it is desirable to completely stop the state machine call quit or quitNow. This will call the current sate’s exit and its parents, call onQuitting, and then exit Thread / Loopers.

In addition to processMessage, each State has an enter method and an exit method that can be overridden.

Hence the states are sorted in a transition architecture (h for a new state caused by the current states to exit and enter a new state.) For the list of states entered / exited from parent state to find the current state We would then exit the current state and return to its parent state but exclude the parent state and then enter all the new states below the parent normal state goes down to the target state below If there is no parent normal state all states are exited, and then new states entered.

The other two methods that states can use are deferMessage and sendMessageAtFrontOfQueue. sendMessageAtFrontOfQueue sends a mesage but places it in front of the queue instead of behind. deferMessage caused by the message to stay in a list until a transition is made to reach a new state. At the moment all deferred messages will be pushed to the front of the state machine queue with the oldest message in front. This will be processed by the current new state before any other messages that are on the queue or may be added later. Both of these are protected and can only be invoked from the state machine.

To explain some of these properties we will use the state machine with 8 states architecture.

After mS5 start the list of valid states will be mP0, mP1, mS1 and mS5. So the order of process calls processMessage when a message is received is mS5, mS1, mP1, and mP0 process ensures each processMessage indicates it cannot process this message by returning false or NOT_HANDLE.

Now, to make sure mS5.processMessage receives a message it can handle, and during processing determines the machine should switch the states. It can call transitionTo (mS4) and return either true or HANDLE. Immediately after the process returns from processMessage the state machine runtime will find the normal state of the father, which is mP1. It will then call mS5.exit, mS2.exit, and mS2.enter and then mS4.enter. The list of new valid states are mP0, mP1, mS2, and mS4. So when the next message received mS4.processMessage will be called.

Now for the concrete examples, this is the traditional HelloWorld application as a State Machine. It returns “Hello World” which is printed to the log for each message.

One more interest in the state machine is the one with 4 states with two independent parent states.

Here is the description of this state machine using pseudo code:

The implementation is below also in StateMachineTest:

If this is done by sending two messages, CMD_1 and CMD_2 (Note that synchronization is only needed because we use hsm.wait () ).

And the result will be as follows:

State Machine source code in Android SDK: https://android.googlesource.com/platform/frameworks/base.git/+/master/core/java/com/android/internal/util/StateMachine.java

P / S

Posts on my viblo, if there is a Source section, then this is a translation from the source that is linked to the original post in this section. These are the articles I select + search + synthesized from Google in the process of handling issues when making real + projects useful and interesting for myself. => Translated as an article to rummage through when necessary. Therefore, when reading the article, everyone should note:

1. You can go to the source to read the original article (extremely recommend).

2. Translated article => Can not avoid misunderstanding, omission, confusion due to differences in language, context as well as understanding of the translator => We hope you can leave comments to complete the problem.

3. The translation is for reference only + has the true meaning of a translated article requested by the company.

4. Hope the article helps you a bit (I hope so!). =)))))))

Share the news now

Source : Viblo