在C#中简单的状态机的例子吗? [英] Simple state machine example in C#?

查看:264
本文介绍了在C#中简单的状态机的例子吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:

再次感谢您的例子,他们都非常有帮助,并与下面的我不是指
从他们那里带走什么。

Again thanks for the examples, they have been very helpful and with the following I don't mean to take anything away from them.

不是目前给出的例子,据我理解他们和放大器;状态机,只是我们通常用状态机理解一半?结果
在这个意义上,例子并改变状态,但这只是重新改变一个变量的值(并允许在不同的国家不同的值 - 改变)psented $ P $,而通常一个状态机也应该改变它的行为和行为的不(唯一的),允许一个变量不同的值的变化取决于状态的感觉,但在允许不同方法的意义不同状态来执行。

Aren't the currently given examples, as far as I understand them & state-machines, only half of what we usually understand by a state-machine?
In the sense that the examples do change state but that's only represented by changing the value of a variable (and allowing different value- changes in different states), while usually a state machine should also change it's behavior, and behavior not (only) in the sense of allowing different value changes for a variable depending on state, but in the sense of allowing different methods to be executed for different states.

或我的状态机的误解和他们共同使用?

Or do I have a misconception of state machines and their common use?

最好的问候

原题:

我发现了大约状态机和放大器的讨论;在C#中迭代器模块和工具来创建状态机并没有什么针对C#,所以我找了很多抽象的东西,但作为一个小白这一切的是一个有点混乱。

I found this discussion about state machines & iterator blocks in c# and tools to create state machines and what not for C#, so I found a lot of abstract stuff but as a noob all of this is a little confusing.

因此​​,这将是巨大的,如果有人可以提供C#源$ C ​​$ C-例子,实现了一个简单的状态机也许3,4状态,只是为了得到它的要点。

So it would be great if someone could provide a C# source code-example that realizes a simple state machine with perhaps 3,4 states, just to get the gist of it.

推荐答案

让我们从这个简单的状态图:

Let's start with this simple state diagram:

我们有:


  • 4种状态(无效,有效,暂停并退出)

  • 5种状态转换(begin命令,结束命令,暂停命令,恢复命令,退出命令)。

您可以用不同的方式,比如当前状态和命令执行switch语句,或在转换表中查找转换一把将其转换为C#。对于这个简单的状态机,我preFER过渡表,这是非常容易使用,重新present一个词典

You can convert this to C# in a handful of ways, such as performing a switch statement on the current state and command, or looking up transitions in a transition table. For this simple state machine, I prefer a transition table, which is very easy to represent using a Dictionary:

using System;
using System.Collections.Generic;

namespace Juliet
{
    public enum ProcessState
    {
        Inactive,
        Active,
        Paused,
        Terminated
    }

    public enum Command
    {
        Begin,
        End,
        Pause,
        Resume,
        Exit
    }

    public class Process
    {
        class StateTransition
        {
            readonly ProcessState CurrentState;
            readonly Command Command;

            public StateTransition(ProcessState currentState, Command command)
            {
                CurrentState = currentState;
                Command = command;
            }

            public override int GetHashCode()
            {
                return 17 + 31 * CurrentState.GetHashCode() + 31 * Command.GetHashCode();
            }

            public override bool Equals(object obj)
            {
                StateTransition other = obj as StateTransition;
                return other != null && this.CurrentState == other.CurrentState && this.Command == other.Command;
            }
        }

        Dictionary<StateTransition, ProcessState> transitions;
        public ProcessState CurrentState { get; private set; }

        public Process()
        {
            CurrentState = ProcessState.Inactive;
            transitions = new Dictionary<StateTransition, ProcessState>
            {
                { new StateTransition(ProcessState.Inactive, Command.Exit), ProcessState.Terminated },
                { new StateTransition(ProcessState.Inactive, Command.Begin), ProcessState.Active },
                { new StateTransition(ProcessState.Active, Command.End), ProcessState.Inactive },
                { new StateTransition(ProcessState.Active, Command.Pause), ProcessState.Paused },
                { new StateTransition(ProcessState.Paused, Command.End), ProcessState.Inactive },
                { new StateTransition(ProcessState.Paused, Command.Resume), ProcessState.Active }
            };
        }

        public ProcessState GetNext(Command command)
        {
            StateTransition transition = new StateTransition(CurrentState, command);
            ProcessState nextState;
            if (!transitions.TryGetValue(transition, out nextState))
                throw new Exception("Invalid transition: " + CurrentState + " -> " + command);
            return nextState;
        }

        public ProcessState MoveNext(Command command)
        {
            CurrentState = GetNext(command);
            return CurrentState;
        }
    }


    public class Program
    {
        static void Main(string[] args)
        {
            Process p = new Process();
            Console.WriteLine("Current State = " + p.CurrentState);
            Console.WriteLine("Command.Begin: Current State = " + p.MoveNext(Command.Begin));
            Console.WriteLine("Command.Pause: Current State = " + p.MoveNext(Command.Pause));
            Console.WriteLine("Command.End: Current State = " + p.MoveNext(Command.End));
            Console.WriteLine("Command.Exit: Current State = " + p.MoveNext(Command.Exit));
            Console.ReadLine();
        }
    }
}

随着个人preference的事,我喜欢设计我的状态机以的GetNext 函数返回一个状态的deterministically 的MoveNext 功能变异状态机。

As a matter of personal preference, I like to design my state machines with a GetNext function to return the next state deterministically, and a MoveNext function to mutate the state machine.

这篇关于在C#中简单的状态机的例子吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆