stateMachine 相关知识

一个state的基本构造,processMessage 以及可选的enter exit 和getName。

processMessager是用于处理数据。 enter 和exit 则是类似于 面向编程的构造和析构方法。

* <p>A state is a <code>State</code> object and must implement
* <code>processMessage</code> and optionally <code>enter/exit/getName</code>.
* The enter/exit methods are equivalent to the construction and destruction
* in Object Oriented programming and are used to perform initialization and
* cleanup of the state respectively


什么时候会调用exit这个方法。

当从一个state transitionTo 另一个State 的时候是否会调用exit方法?

如果按照面向对象的思想对应过去, 之后在这个state消失的时候才会调用exit

那么transitionTo 是否会导致state的销毁?理论上应该是会的, 因为StateMachine只能处于一个state,所以在转移到别的state的同时会销毁当前state

也就是会调用exit方法。

以上仅仅是自己的逻辑推理结构, 不保证正确, 有空的时候自己做个实验验证下。


构建hsm,利用setInitialState来设定初始状态。

When a state machine is created <code>addState</code> is used to build the
* hierarchy and <code>setInitialState</code> is used to identify which of these
* is the initial state.

 


使用obtainMessage 来创建消息, 使用sendMessage发送给StateMachine,  当StateMachine收到消息时会调用当前状态的processMessage。

After the state machine is created and started, messages are sent to a state
* machine using <code>sendMessage</code> and the messages are created using
* <code>obtainMessage</code>. When the state machine receives a message the
* current state‘s <code>processMessage</code> is invoked. In the above example
* mS1.processMessage will be invoked first. The state may use <code>transitionTo</code>
* to change the current state to a new state

 


如果子状态不能处理某个消息时,可以通过返回false或者NOT_HANDLED交给其父状态处理

* a child state is unable to handle a message it may have the message processed
* by its parent by returning false or NOT_HANDLED.

这个消息转为父类处理是自动的还是手动的?理论上来说应该有一个transitionTo的过程。

如果说不需要的话,那么说说明构造hsm的时候addState会建立其子状态和父状态之间的特殊关系。

那么有个deferMessage这个是处理什么消息的呢?

 


当消息处理完了之后, 会转移到Halting的状态

* <p>When all processing is completed a state machine may choose to call
* <code>transitionToHaltingState</code>. When the current <code>processingMessage</code>
* returns the state machine will transfer to an internal <code>HaltingState</code>
* and invoke <code>halting</code>. Any message subsequently received by the state
* machine will cause <code>haltedProcessMessage</code> to be invoked.</p>


停止stateMachine的方法,quit

* <p>If it is desirable to completely stop the state machine call <code>quit</code> or
* <code>quitNow</code>. These will call <code>exit</code> of the current state and its parents,
* call <code>onQuiting</code> and then exit Thread/Loopers.</p>

这个地方明确的说明了会调用当前State和其父类state的exit方法,有两个疑问:

1、其父类是指的其直系父类还是会迭代调用

2、这个地方直接说明了exit的调用条件,是否说明之前的推断是错误的:|

 

 

exit 方法是为了保证StateMachine的环境正确性,感觉其主要工作应该是清楚当前状态对于整个环境的改变,相当于复位功能。

这个机制就保证了各个状态之间的独立性, 也说明了为什么在退出的时候会迭代的调用父状态的exit方法。


介绍了状态之间的切换规则, 主要的标准是沿着HSM这条线进行切换。

 

* <p>Since the states are arranged in a hierarchy transitioning to a new state
* causes current states to be exited and new states to be entered. To determine
* the list of states to be entered/exited the common parent closest to
* the current state is found. We then exit from the current state and its
* parent‘s up to but not including the common parent state and then enter all
* of the new states below the common parent down to the destination state.
* If there is no common parent all states are exited and then the new states
* are entered.</p>

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。