将嵌套状态与 pytransitions 一起使用的正确方法是什么?

What is the right way to use Nested states with pytransitions?

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

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

I would like to know if it's still possible to create such a machine with pytransition.

创建和管理 HSM 的方式在 0.8 中发生了变化,但您当然可以使用(深度)嵌套状态。对于具有子状态的状态,您需要将 states(或 children)参数与您想要嵌套的状态 definitions/objects 一起传递。此外,您可以为该特定范围传递转换。我正在使用 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 的输出: