简体   繁体   English

Pytransitions:我们如何在单个 state 机器中实现具有分层机器和异步机器功能的状态机?

[英]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.

相关问题 如何按它们实际拥有数据点的列对行进行分组? - How can I groupby rows by the columns in which they actually posses a data point? 如何将特征的不确定性纳入机器学习算法? - How to incorporate uncertainty of features into machine learning algorithms? 我们可以在一台机器上产生多少个 celery 工人? - How many celery workers we can spawn on a machine? Python 机器学习标签和功能 - Python Machine learning labels and features 我怎样才能完成这个正则表达式? - How can I accomplish this regex? 将 DMXPY 与全视线 state 机器一起使用 - Use DMXPY with a Transitions state machine 如何在单次训练期间保存和快照机器学习模型? - How to save and snapshot machine learning model during a single training? 在python中将函数指定为有限状态机的变量 - Assigning functions as variables for a finite state machine in python 我们可以在目标机器上没有安装 Python 的情况下创建一个使用 Python 调用的 .exe 文件吗? - Can we create an .exe file that uses Python calls without having Python installed in the target machine? 与协程的双向通信(asyncio 中的状态机) - Two way communication with coroutine (state machine in asyncio)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM