使用分层状态机简化平面状态机

时间:2019-01-22 11:39:50

标签: c++ fsm

我是有限状态机的新手,我试图了解是否应该使用分层状态机,还是坚持使用平面结构,我必须以最简单的方式对问题进行建模。

我有一个“分析器”,可以停止它,分析输入,分析输出,分析输入和输出或清除。我在enum的{​​{1}}中有一些分析器状态,在analyzer_states中有状态之间的转换。

问题是:我可以使用分层状态机更简单地对此建模吗?也许FSM不能解决这个问题?

analyzer_transitions

输出为:

#include <iostream>
#include <cstdlib>
#include <string>
#include "fsm.h"

enum class analyzer_transitions
{
    start_input,
    start_output,
    clear,
    stop_input,
    stop_output,
    resume
};

enum class analyzer_states
{
    analyzing_both,
    analyzing_input,
    analyzing_output,
    clearing_both,
    clearing_input,
    clearing_output,
    stopped,
};

inline auto output(std::string s)
{
    return [s]()
    {
        std::cout << s << std::endl;
    };
}

int main()
{
    fsm<analyzer_states, analyzer_transitions> machine{ analyzer_states::stopped };

    machine.add(
        analyzer_states::stopped, analyzer_transitions::start_input, analyzer_states::analyzing_input,
        output("-> analyzing_input"));

    machine.add(
        analyzer_states::stopped, analyzer_transitions::start_output, analyzer_states::analyzing_output,
        output("-> analyzing_output"));

    machine.add(
        analyzer_states::analyzing_input, analyzer_transitions::stop_input, analyzer_states::stopped, 
        output("-> stopped"));

    machine.add(
        analyzer_states::analyzing_output, analyzer_transitions::stop_output, analyzer_states::stopped, 
        output("-> stopped"));

    machine.add(
        analyzer_states::analyzing_both, analyzer_transitions::stop_input, analyzer_states::analyzing_output, 
        output("-> analyzing_output"));

    machine.add(
        analyzer_states::analyzing_both, analyzer_transitions::stop_output, analyzer_states::analyzing_input,
        output("-> analyzing_input"));

    machine.add(
        analyzer_states::analyzing_input, analyzer_transitions::start_output, analyzer_states::analyzing_both, 
        output("-> analyzing_both"));

    machine.add(
        analyzer_states::analyzing_output, analyzer_transitions::start_input, analyzer_states::analyzing_both,
        output("-> analyzing_both"));

    machine.add(
        analyzer_states::analyzing_input, analyzer_transitions::clear, analyzer_states::clearing_input,
        output("-> clearing_input"));

    machine.add(
        analyzer_states::analyzing_output, analyzer_transitions::clear, analyzer_states::clearing_output,
        output("-> clearing_output"));

    machine.add(
        analyzer_states::analyzing_both, analyzer_transitions::clear, analyzer_states::clearing_both,
        output("-> clearing_both"));

    machine.add(
        analyzer_states::clearing_input, analyzer_transitions::resume, analyzer_states::analyzing_input,
        output("-> analyzing_input"));

    machine.add(
        analyzer_states::clearing_output, analyzer_transitions::resume, analyzer_states::analyzing_output,
        output("-> analyzing_output"));

    machine.add(
        analyzer_states::clearing_both, analyzer_transitions::resume, analyzer_states::analyzing_both,
        output("-> analyzing_both"));

    machine.to(analyzer_transitions::start_input);
    machine.to(analyzer_transitions::start_output);
    machine.to(analyzer_transitions::stop_input);
    machine.to(analyzer_transitions::clear);
    machine.to(analyzer_transitions::resume);

    std::cout << "Done...";
    std::getchar();

    return EXIT_SUCCESS;
}

fsm.h:

-> analyzing_input
-> analyzing_both
-> analyzing_output
-> clearing_output
-> analyzing_output

0 个答案:

没有答案