0

所以我一直在 pytransitions github 和 SO 上四处寻找,似乎在 0.8 之后你可以使用宏状态(或其中包含子状态的超级状态)的方式发生了变化。我想知道是否仍然可以使用 pytransition 创建这样的机器(蓝色方块假设是一个宏状态,其中有 2 个状态,其中一个是绿色,是另一个宏):

在此处输入图像描述

还是我必须遵循此处建议的工作流程:https ://github.com/pytransitions/transitions/issues/332 ?非常感谢任何信息!

4

1 回答 1

1

我想知道是否仍然可以使用 pytransition 创建这样的机器。

创建和管理 HSM 的方式在 0.8 中发生了变化,但您当然可以使用(深度)嵌套状态。对于具有子状态的状态,您需要将states(or children) 参数与您想要嵌套的状态定义/对象一起传递。此外,您可以传递该特定范围的转换。我正在使用HierarchicalGraphMachine,因为这使我可以立即创建图表。

from transitions.extensions.factory import HierarchicalGraphMachine

states = [
    # create a state named A
    {"name": "A",
     # with the following children
     "states":
        # a state named '1' which will be accessible as 'A_1' 
        ["1", {
        # and a state '2' with its own children ...
            "name": "2",
            #  ... 'a' and 'b'
            "states": ["a", "b"],
            "transitions": [["go", "a", "b"],["go", "b", "a"]],
            # when '2' is entered, 'a' should be entered automatically.
            "initial": "a"
        }],
     # we could also pass [["go", "A_1", "A_2"]] to the machine constructor
     "transitions": [["go", "1", "2"]],
     "initial": "1"
     }]

m = HierarchicalGraphMachine(states=states, initial="A")
m.go()
m.get_graph().draw("foo.png", prog="dot")  # [1]

输出1

在此处输入图像描述

于 2021-08-26T15:22:34.663 回答