[英]Pytransitions: How can we accomplish a statemachine which posses the features of Hierarchical Machine and AsyncMachine in a single state machine?
I want to build a stat machine such that it can posses features of both HierarchicalMachine and AsyncMachine.我想构建一个统计机器,使其可以同时拥有 HierarchicalMachine 和 AsyncMachine 的功能。 I tried this code but Hierarchical and Async are not working simultaneously.
我试过这段代码,但分层和异步没有同时工作。
` `
from transitions.extensions.markup import MarkupMachine
from transitions.extensions.factory import HierarchicalMachine
from transitions.extensions.asyncio import AsyncMachine
QUEUED = False
class Unhealthy(HierarchicalMachine, AsyncMachine):
def __init__(self):
states = [{"name":'aborted', "on_enter":[]},
{"name":'clearancetimeouterr', "on_enter":[]},
{"name":"awaitingclearanceerr", 'on_enter':[]},
{"name":"cleared", 'on_enter':[]}
]
transitions = [{"trigger":"abort", "source":"aborted", "dest":"awaitingclearanceerr"},
{"trigger":"awaitingclearanceerr", "source":"clearancetimeout", "dest":"awaitingclearanceerr"},
{"trigger":"cleared", "source":"awaitingclearanceerr", "dest":"cleared"}]
super().__init__(states=states, transitions=transitions, initial="awaitingclearanceerr", queued=QUEUED)
class Healthy(HierarchicalMachine, AsyncMachine):
def __init__(self):
unhealthy = Unhealthy()
states = [{"name":'idle', 'on_enter':[]},
{"name":"busy", 'on_enter':[]},
{"name":"done", 'on_enter':[]}]
transitions = [{'trigger':'start', 'source':'idle', 'dest':'busy'},
{"trigger":"done", "source":"busy", "dest":"done"},
{"trigger":"idle", "source":"awaiting_clearance", "dest":"idle"}]
super().__init__(states=states, transitions=transitions, initial="idle", queued=QUEUED)
class StateMachine(HierarchicalMachine, MarkupMachine, AsyncMachine):
def __init__(self):
unhealthy= Unhealthy()
healthy = Healthy()
states = [{'name':"idle"}, {"name":'healthy', 'children':healthy}, {"name":"unhealthy", "children":unhealthy}]
super().__init__(states=states, initial="idle", queued=QUEUED)
self.add_transition("start_machine", "idle", "healthy")
self.add_transition('abort', 'healthy', 'unhealthy')
I want something like that but HierarchicalMachine and AsyncMachine are not working together. And giving the following error:
I want something like that but HierarchicalMachine and AsyncMachine are not working together. And giving the following error:
RuntimeError: AsyncMachine should not call Machine._process
. I want something like that but HierarchicalMachine and AsyncMachine are not working together. And giving the following error:
RuntimeError: AsyncMachine should not call Machine._process
。 Use Machine._process_async
instead.'请改用
Machine._process_async
。
The extensions section of transitions
documentation mentions the following: transitions
文档的扩展部分提到了以下内容:
There are two mechanisms to retrieve a state machine instance with the desired features enabled.
有两种机制可以检索启用了所需功能的 state 机器实例。 The first approach makes use of the convenience factory with the four parameters graph, nested, locked or asyncio set to
True
if the feature is required.第一种方法使用便利工厂,如果需要该功能,则将四个参数 graph、nested、locked 或 asyncio 设置为
True
。
from transitions.extensions import MachineFactory
AsyncHSM = MachineFactory.get_predefined(nested=True, asyncio=True)
machine = AsyncHSM(states=['A', 'B'], ...)
class MyHSM(ASyncHSM):
...
This approach targets experimental use since in this case the underlying classes do not have to be known.
这种方法针对实验用途,因为在这种情况下,底层类不必为人所知。 However, classes can also be directly imported from
transitions.extensions
.但是,也可以直接从
transitions.extensions
导入类。 The naming scheme is as follows:命名方案如下:
Diagrams![]() |
Nested![]() |
Locked![]() |
Asyncio![]() |
|
---|---|---|---|---|
Machine![]() |
✘ ![]() |
✘ ![]() |
✘ ![]() |
✘ ![]() |
GraphMachine![]() |
✓ ![]() |
✘ ![]() |
✘ ![]() |
✘ ![]() |
HierarchicalMachine![]() |
✘ ![]() |
✓ ![]() |
✘ ![]() |
✘ ![]() |
LockedMachine![]() |
✘ ![]() |
✘ ![]() |
✓ ![]() |
✘ ![]() |
HierarchicalGraphMachine![]() |
✓ ![]() |
✓ ![]() |
✘ ![]() |
✘ ![]() |
LockedGraphMachine![]() |
✓ ![]() |
✘ ![]() |
✓ ![]() |
✘ ![]() |
LockedHierarchicalMachine![]() |
✘ ![]() |
✓ ![]() |
✓ ![]() |
✘ ![]() |
LockedHierarchicalGraphMachine![]() |
✓ ![]() |
✓ ![]() |
✓ ![]() |
✘ ![]() |
AsyncMachine![]() |
✘ ![]() |
✘ ![]() |
✘ ![]() |
✓ ![]() |
AsyncGraphMachine![]() |
✓ ![]() |
✘ ![]() |
✘ ![]() |
✓ ![]() |
HierarchicalAsyncMachine![]() |
✘ ![]() |
✓ ![]() |
✘ ![]() |
✓ ![]() |
HierarchicalAsyncGraphMachine![]() |
✓ ![]() |
✓ ![]() |
✘ ![]() |
✓ ![]() |
So I guess this is what you are looking for:所以我想这就是您要找的东西:
from transitions.extensions import HierarchicalAsyncMachine
# in case you want markup/graphviz support
from transitions.extensions import HierarchicalAsyncGraphMachine
In case you want to 'mix' your own machine class, have a look at factory.py to get an idea about how the subclasses are defined.如果您想“混合”自己的机器 class,请查看factory.py以了解如何定义子类。 Pay attention to the order of inheritance. Mixing this up might cause undesired effects.
注意inheritance的顺序,混用可能会造成不良影响。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.