Atitit. FSM state mode for finite state machines

Source: Internet
Author: User
Tags fsm string format

Atitit. FSM state mode for finite state machines

1. Finite state machine 1

2. "Status table" and "State rotation table" 1

3. Finite state machine concept (state) event conversion (Transition) Action (action) 2

4. Application scenario for state Machine 2

4.1., the "finite state machine" is very useful in the AI aspect of the game. 2

4.2. Eliminate complex if else logic with the state machine mode 2

4.3. Source Text Processing state Machine 2

4.4. Regular expressions (regexp), to determine the string format and parsing the contents of the string basically all depend on her. 3

4.5. Game programming AI material, feel the AI in the game, the first thing to say is the finite state machine to implement the Elf AI, 3

4.6. Lexical Analysis 3

5. How the FSM is implemented 3

5.1.: 1) switch/case or If/else 3

5.2.2) Status Table 4

5.3.3) using the State Pattern 4

5.4. Use the macro definition to describe the state machine 4

6. State machine mode of design mode 5

1. Finite state machine

is a very important sequential logic circuit module. It plays a very important role in the design of digital system. A finite state machine is a sequential logic circuit that outputs dependent on past input and current input parts. In general, in addition to the input and output parts, the finite state machine also contains a set of "memory" function registers, these registers function is the memory finite state machine's internal state, they are often called the State Register. In a finite state machine, the next state of the State register is not only related to the input signal, but also to the current state of the register, so the finite state machine can also be considered as a combination of combinatorial logic and register logic. The function of the register logic is to store the internal state of the finite state machine, and the combinational logic can be divided into two parts: the second state logic and the output logic, and the function of the sub-State is to determine the next condition of the finite states machine, and the function of the output logic is to determine the output of the finite state machine.

The state machine is not a term in software and program, in the digital logic the finite state machine refers to the sequential logic circuit which depends on the input part and the current input part of the output. There is even no need to emphasize the finite state machine, you can simply understand the state machine as a black box, into which the command can be manipulated and swapped state, it has a final state, when the final state, you can complete the task.

Do not limit the state machine to software, in fact, the hardware is really a lot of use of state machine place.
The sequential circuit is the embodiment of the state machine

Author:: Old Wow's paw Attilax Ayron, email:1466519819@qq.com

Reprint please indicate source: Http://blog.csdn.net/attilax

2. " State table" and "State rotation table"

3. finite state machine concept (state event) transform (Transition) Action (action)

Figure 1 State machine for controlling the gate

States, events, transitions, and actions are some of the basic concepts that are often encountered when describing finite state machines.

State refers to a condition in which an object is in its life cycle, and an object in a particular state is bound to satisfy certain conditions, perform certain actions, or wait for certain events.

Event refers to the things that occupy a certain place in time and space, and which are meaningful to the state machine. Events often cause changes in state, prompting the state machine to switch from one state to another.

Conversion (Transition) refers to a relationship between two states, indicating that an object will perform a certain action in the first state and will enter a second state when an event occurs while a particular condition is satisfied.

Action refers to those atomic operations that can be performed in a state machine, which means that they cannot be interrupted by other messages while they are running, and must be executed all the time.

4. Application Scenarios for state machines

is too extensive, such as the control of various memory, ad control of external devices, also including internal circuit control,

4.1., the "finite state machine" is very useful in the AI aspect of the game. 4.2. Eliminate complex if else logic with the state machine mode

4.3. Source Text processing state machine

In other common text processing problems, the input file is very "state". The meaning of each piece of data depends on the string preceding it (perhaps the string following it). Reports, mainframe data entry, readable text, programming source files, and other kinds of text files are stateful.

A simple example is a line of code that might appear in a Python source file:

MyObject = SomeClass (this, so, other)

This line indicates that if there are just a few lines around this line, some of the content is different:

"How to use Someclass:myobject = SomeClass" "" "

We should know that we are in a "block reference" state to make sure that this line of code is part of a comment rather than a Python operation.

4.4. Regular Expressions (regexp), to determine the string format and parsing the contents of the string basically all depend on her.

In fact, the regular expression is a finite state machine. It's just a different form of expression. After the regular expression is written, you can "compile" the State conversion table by the program, which is the kind of state conversion diagram that we often see.

Interpreter mode has two most typical application JSON and regular expressions in JS 4.5. Game programming AI material, feel the AI in the game, the first thing to say is the finite state machine to implement the Elf AI,

NPCs in the game, regardless of the premise of artificial intelligence, NPC can only be based on pre-set conditions and user feedback to respond, that is to say, NPC has n states, with each response is a "rising edge" (a trigger), NPC jumps from the current K state to the M state according to the user's choice, although the range of state jumps is within N. What developers have to do is get the player to do something in a state such as acquiring treasures, triggering tasks, upgrading, and so on. The basic framework for achieving this structure may be switch...case ... 4.6. Lexical analysis (for example, the implementation of forward slash escaping)

Lexical analysis finite state machine task It is simple to read a single character from the input stream, and when the input character is recognized as a separate syntactic unit (token), the token is placed in the stream of words to be analyzed.

The implementation of a forward slash escape. Usually the string escape is implemented as a backslash \ If there is a string, now we want to use the forward slash as the escape character for some special purposes, and the other characters are placed as is. Then the forward slash/and the character behind it must be considered as a whole, and each of the other characters is a whole.

This state machine has only two states the first State is read into the normal character state the second state is read into the forward slash after the state state diagram is as follows

5. how to implement FSM 5.1.:
1) switch/case or if/else


This is not intended to be the most intuitive way, using a bunch of conditions to judge, can be programmed to do, for the simple small state machine is the most suitable, but no doubt, such a way relatively primitive, the large state machine is difficult to maintain.

But the two functions of Checkstatechange () and Performstatechange () themselves are still in the face of very complex state, the internal logic becomes abnormally bloated, even may be difficult to implement.

For a long time, the use of switch statements has been the only way to implement finite state machines, and even a complex software system such as compilers, most of which are directly implemented by this method. However, with the gradual deepening of the application of state machine, the state machine is more and more complex, this method also began to face a variety of severe tests, the most troubling is if the state machine state is very much, or the transition between States is unusually complex, A state machine that is constructed simply by using the switch statement will be non-maintainable.

5.2.
2) Status Table


Maintains a two-dimensional state table, the horizontal axis represents the current state, the ordinate represents the input, and one element in the table stores the next state and the corresponding operation. This is easy to maintain, but the cost of running time and storage space is higher.
5.3.
3) Use state Pattern


Using the state pattern makes the code more maintainable than Switch/case, and does not have a lot of impact on performance, but it is not 100% perfect. But Robert C. Martin made two tools for automatically generating FSM code, one for Java and for C + +, and a free download on Http://www.objectmentor.com/resources/index. The input to this tool is a plain text state machine description that automatically generates code that conforms to the state pattern, so that developer's work only needs to maintain the text description of the status machine, each of which takes the risk of introducing a bug to maintain code.

4) 5.4.
describe a state machine using a macro definition


In general, C + + programming should avoid the use of # define, but this is mainly because if you use a macro to define functions, it is easy to create such a problem, but clever use, still can produce wonderful effect. MFC is the use of macro definitions to implement large architectures.
In the implementation of the FSM, you can put some cumbersome if/else and curly braces in the macro, so that in the code can be 3) state machine description text as written, through the compiler pre-compilation processing generated 1) The same effect, I have seen the generation of C code macro, if you want to generate C + + code, The software MFC can, then theoretically also feasible
6. Design mode state machine mode

Reference

Cute Python: Using the state machine. htm

Eliminate complex if else logic with state machine mode-Technical channel _ it168.htm

State machine-xgbing-Blog channel-CSDN.NET.htm

Lexical analysis and realization of state machine _ the dream of the Sina blog. htm

Lexical analysis and realization of state machine _ the dream of the Sina blog. htm

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.