[Yugong Series] Case Study of .NET CORE Tools in August 2023 - XStateNet State Machine


foreword

A state machine is an abstract machine model used to describe the transition process of things between different states. It consists of a set of states, a set of events and a set of transition rules. In the state machine, each state represents a specific situation, each event triggers a state transition, and each transition rule describes the transition conditions and actions from one state to another. State machines can describe the behavioral models of many systems, such as computer programs, network protocols, and physical systems.

1. XStateNet state machine

1. Finite state machine

A finite state machine (Finite State Machine, FSM), also known as a finite state automaton (Finite State Automaton, FSA), is a common state machine model that contains a finite number of states and rules for transitioning between these states . FSM can describe the transition process of the system between a certain set of states and a limited set of inputs. Each transition rule describes which state the system should transition to when it is in a certain state and receives a certain input.

FSM can be divided into two types: Deterministic Finite State Machine (DFSM) and Nondeterministic Finite State Machine (NFSM). The transition rules of DFSM are unique, while the transition rules of NFSM may not only. FSM has a wide range of applications in computer science, electrical engineering, and control engineering.

FSM It is widely used in programming to model, design and implement various systems such as software applications, hardware control, game logic, etc.

The FSM finite state machine mainly consists of the following elements:

1. States: States are the different situations or stages that a system may be in. Each state represents a specific state or configuration of the system.

2. Transitions: Transitions define how a system in one state switches to another state based on an input, event, or condition. Transitions are usually associated with specific actions or behaviors.

3. Events: Events are signals that trigger state transitions. When the system receives an event, it may transition from the current state to a new state.

4. Actions: Actions are actions that are performed when a state transition occurs. These operations can include calculations, outputs, state updates, etc.

Any system is a combination of states and transitions, or at least can be represented as a state diagram. Each state of a state machine is an atomic execution context.

2. Introduction to XStateNet

XStateNet is a neural network structure based on finite state machine (FSM), which combines the advantages of FSM and deep learning, and can be applied to various tasks, such as sequence modeling, image processing and natural language processing.

XStateNet uses a finite state machine to describe the state transition of the model, where each state represents the hidden state of the neural network. At each time step, XStateNet takes the current input and the previous hidden state as input, calculates a new hidden state according to the current state transition function, and outputs the corresponding prediction at the same time.

The key strength of XStateNet is that it can handle long sequences and complex contextual dependencies well. Compared with traditional recurrent neural network (RNN) and long short-term memory network (LSTM), XStateNet has lower computational cost and memory overhead, and is also easier to train and adjust.

XStateNet is still in the research stage, but it has shown excellent performance on some tasks, such as language modeling, image description and semantic segmentation.

Project address: https://github.com/serge-sedelnikov/xstate.net

insert image description here

3. Use of XStateNet

3.1 Installation package

Install and reference XStateNet through Nuget

XStateNet

insert image description here

3.2 use

using XStateNet;
//1.定义四个状态,红灯,绿灯,黄灯,还有一个异常状态。
State redLight = new State("红灯");
State amberLight = new State("黄灯");
State greenLight = new State("绿灯");
State errorBlinkingAmber = new State("error");
bool isError = false;
int fixAttempt = 1;

//2.每个状态进行配置
redLight.WithInvoke(async (callback) => {
    
    

    Console.ForegroundColor = ConsoleColor.Red;
    Console.WriteLine("红灯亮!!! STOP!");
    await Task.Delay(1000);

    if (isError)
    {
    
    

        await callback("ERROR");
    }
    else
    {
    
    
        await callback("红灯结束");
    }
})

.WithTransition("ERROR", "error")
.WithTransition("红灯结束", "黄灯");


amberLight.WithInvoke(async (cancel) => {
    
    

    Console.ForegroundColor = ConsoleColor.Yellow;
    Console.WriteLine("黄灯亮! 注意!");


    await Task.Delay(3000, cancel);


}, "绿灯", "error");


greenLight.WithTimeout(5000, "红灯")
.WithActionOnEnter(() => {
    
    

    Console.ForegroundColor = ConsoleColor.Green;
    Console.WriteLine("绿灯亮! Go go go!");
})
.WithActionOnExit(() => {
    
    

    isError = new Random().NextDouble() > 0.5;

});


errorBlinkingAmber.WithInvoke(async (callback) => {
    
    
    Console.WriteLine("发生错误!");
    Console.WriteLine($"尝试修复... 第 {
      
      fixAttempt} 次");
    await Task.Delay(1000);
    isError = new Random().NextDouble() > 0.5;
    if (isError)
    {
    
    
        Interlocked.Increment(ref fixAttempt);
        await callback("未修复");
    }
    else
    {
    
    
        Interlocked.Exchange(ref fixAttempt, 1);
        await callback("已修复");
    }
})
.WithTransition("未修复", "error")
.WithTransition("已修复", "红灯");

//3.启动状态机
StateMachine machine = new StateMachine("红绿灯", "红绿灯状态机", redLight.Id);

machine.States = new[]
{
    
    
    redLight, amberLight, greenLight, errorBlinkingAmber
};
Interpreter interpreter = new Interpreter(machine);
await interpreter.StartStateMachineAsync();

insert image description here

Guess you like

Origin blog.csdn.net/aa2528877987/article/details/132562011