Java demonstration of Finite State Machine (FSM)

Source: Internet
Author: User

Java demonstration of Finite State Machine (FSM)

This article begins with a simple example and gradually evolves into a very complex program.

In concise State mode (5.8), changes between states are controlled by the outside world, or multiple states are separated and irrelevant. The most interesting thing about the state mode is to discuss its state changes.

1. Introduction

Air-condition remote controlTwo buttons(More buttons are introduced in the following example ),Power/power key and cool/cooling key. Air conditioning operation presentationThree statuses: Stop/Off, only air supply/FanOnly, and cooling/Cool.The starting status is Off. The following figure shows the status change.

This is a Simplified Finite State Machine (Finite State Machine, FSM, or Finite State Automata) graph. It uses three elements of the State chart: ① bubbles, indicating the State (State); ② The arrow of the connection status indicates conversion (Transition); ③ Mark the former as an event on the arrow (Event).


Status conversion. Press the power key, then Off → FanOnly, Cool → Off, etc.; press cool, then Off → Off (if you don't draw it out, just draw it yourself if you like it all ).

Yqj2065 prefers this simple state conversion.Branch statement <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Vc3Ryb25nPqGjPGltZyBhbHQ9 "Smile" src = "http://www.bkjia.com/uploads/allimg/140921/0422522M9-2.gif">, concise and bright.

Example 4-5 simple and bright package property. state. stateMachine; import static tool. Print. *; // pln/*** Aircon. Simple Model: * The remote control has two buttons (more buttons are introduced in the example below), the power supply key and the cool cooling key. * Air conditioners run in three statuses: Stop/Off, air supply only/FanOnly, and cooling/Cool. * The starting status is Off * @ author (yqj2065) * @ version 0.1 */public class Aircon0 {// off, FanOnly, AC private int state = 0; // The initial status is Off public int getState () {return state;} // two actions public void power () {// press the power key if (state = 0) {// off state = 1; pln ("start Fan");} else if (state = 1) {// FanOnly state = 0; pln ("stop Fan");} else {state = 0; pln ("stop Cool");} public void cool () {// press the cooling key if (state = 0) {// off pln ("nothing");} else if (state = 1) {// FanOnly state = 2; pln ("start Cool") ;}else {state = 1; pln ("stop Cool") ;}} package property. state. stateMachine; public class ACCtrl {public static void test () {Aircon0 ac = new Aircon0 (); // power () cool () System. out. println ("Current State:" + ac. getState (); ac. cool (); ac. power (); ac. cool (); ac. cool (); ac. power (); ac. power ();}}
Test code output:

Current State: 0
Nothing
Start Fan
Start Cool
Stop Cool
Stop Fan
Start Fan

On this basis, you can spend 10 minutes practicing and modifying the code in the status mode. We use enum to write the state class hierarchy. Its structure is as follows:

Routine 4 6 enum Stateenum State0 {OFF {@ Override void power () {}@ Override void power () {}}, FANONLY {}, COOL {}; public abstract void power (); public abstract void cool ();}

(State1 should have been used as the internal class of aircon1. Outside, power () and so on need to add the parameter Aircon1, to power (Aircon1 ac )).

Now, we can enrich the details of finite state machines and add 4 actions, such as event actions and State actions.


Therefore, in enum State1, in addition to the interface for state mode extraction, various actions/action methode of the state machine are added.
Void entry (Aircon1 ac) {pln ("→" + ac. state. name ());}
Void exit (Aircon1 ac) {p (ac. state. name () + "→ ");}
Void startCool () {p ("start Cool ");}
Void stopCool () {p ("stop Cool ");}
Void startFan () {p ("start Fan ");}
Void stopFan () {p ("stop Fan ");}

The method bodies of each power (Aircon1 ac) and cool (Aircon1 ac) are:

This. exit (ac );
// Event actions, such as stopFan ();
Ac. state = OFF; // the next state
Ac. state. entry (ac );

Package property. state. stateMachine; import static tool. Print. *; // pln/*** should have State1 as the internal class of aircon1. Now put it outside, * power () and so on need to be changed to power (Aircon1 ac) */public enum State1 {OFF {@ Override void exit (Aircon1 ac) {super. exit (ac); startFan () ;}@ Override void power (Aircon1 ac) {this. exit (ac); ac. state = FANONLY; ac. state. entry (ac) ;}@ Override void cool (Aircon1 ac) {pln ("nothing") ;}, FANONLY {@ Override void power (Aircon1 ac) {this. exit (ac); stopFan (); ac. state = OFF; ac. state. entry (ac) ;}@ Override void cool (Aircon1 ac) {this. exit (ac); ac. state = COOL; ac. state. entry (ac) ;}, COOL {@ Override void exit (Aircon1 ac) {super. exit (ac); stopCool () ;}@ Override void entry (Aircon1 ac) {startCool (); super. entry (ac) ;}@ Override void power (Aircon1 ac) {this. exit (ac); stopFan (); ac. state = OFF; ac. state. entry (ac) ;}@ Override void cool (Aircon1 ac) {this. exit (ac); ac. state = FANONLY; ac. state. entry (ac) ;}}; // status mode extraction interface abstract void power (Aircon1 ac); abstract void cool (Aircon1 ac ); // action of the state machine methode void entry (Aircon1 ac) {pln ("→" + ac. state. name ();} void exit (Aircon1 ac) {p (ac. state. name () + "→");} void startCool () {p ("start Cool");} void stopCool () {p ("stop Cool ");} void startFan () {p ("start Fan") ;}void stopFan () {p ("stop Fan ");}}

The modified Aircon1 version.

Package property. state. stateMachine; import static tool. Print. *; // pln/*** Aircon1. Refactored Aircon0 in status mode, and used enum State1 to write the state class hierarchy. * @ Author (yqj2065) * @ version 0.1 */public class Aircon1 {State1 state = State1.OFF; // change the default value of private and delete getState (). // Two actions public void power () {// press the power key state. power (this);} public void cool () {// press the refrigeration key state. cool (this);}/*** ACCtrl code. */Public static void test () {Aircon1 ac = new Aircon1 (); System. out. println ("Current State:" + ac. state. name (); ac. cool (); ac. power (); ac. cool (); ac. cool (); ac. power (); ac. power (); ac. power ();}}
Output of the corresponding test operation: "OFF →" indicates that the test is out of the OFF state, while "→ FANONLY "...

Current State: OFF
Nothing
OFF → start Fan → FANONLY
FANONLY → start Cool → COOL
COOL → stop Cool → FANONLY
FANONLY → stop Fan → OFF
OFF → start Fan → FANONLY
FANONLY → stop Fan → OFF

2. For state machines with more states, the layered state machine usually combines states with many common features. Such as FANONLY and COOLRunning status.


Hierarchical states in the state machine. We can useCombination ModeProcessing. (No separate write combination mode yet ,). However, you may not be able to use the combination mode perfectly. For example, when Running to Off, the internal states of all Running are converted to OFF during PoverEvent, which is good. When OFF to Running, not all internal statuses of Running must call its entry. Use the responsibility chain when using enum (which is not suitable for class layers.


Drawing and considering more buttons ....


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.