https://github.com/pytransitions/transitions
我正在尝试使用batman = NarcolepticSuperhero("Batman")文档中的示例。但是假设我们有 100 个蝙蝠侠要处理,我的方法是创建每个蝙蝠侠对象的列表,并使用循环单独跟踪它们的状态。IMO 这是非常幼稚的。
有人可以提出一些有效的解决方案。
谢谢
https://github.com/pytransitions/transitions
我正在尝试使用batman = NarcolepticSuperhero("Batman")文档中的示例。但是假设我们有 100 个蝙蝠侠要处理,我的方法是创建每个蝙蝠侠对象的列表,并使用循环单独跟踪它们的状态。IMO 这是非常幼稚的。
有人可以提出一些有效的解决方案。
谢谢
你的建议对我来说听起来很合理。
heros = [NarcolepticSuperhero(f"Batman{i:02d}")
for i in range(100)]
然后要么循环所有这些,要么使用一个while True:循环睡眠片刻然后随机选择一个英雄。无论哪种方式,您都将拥有一个可以触发一些新过渡的英雄。
假设所有NarcolepticSuperheros行为都应该相同,我建议Machine对所有“蝙蝠侠”使用一个,而不是实例化NarcolepticSuperhero一百次。这样,您可以Machine.dispatch用来触发所有模型上的事件,而不是循环遍历超级英雄列表(Machine虽然在引擎盖下也只是循环遍历模型列表)。我稍微压缩了提到的示例(一个列表中的所有转换),删除了绑定到的机器NarcolepticSuperheros并引入了SuperheroDen. 关于状态检查,您可以使用字典来跟踪当前有多少英雄处于什么状态。我添加了一个hero_logto NarcolepticSuperhero,每次英雄更改其状态时,它将首先退出当前状态,然后以新状态登录。
from transitions import Machine
from collections import defaultdict
import random
class NarcolepticSuperhero(object):
hero_log = defaultdict(list)
def __init__(self, name):
self.name = name
self.kittens_rescued = 0
def update_journal(self):
self.kittens_rescued += 1
@property
def is_exhausted(self):
return random.random() < 0.5
@staticmethod
def change_into_super_secret_costume():
print("Beauty, eh?")
def log_out(self):
self.hero_log[self.state].remove(self)
def log_in(self):
self.hero_log[self.state].append(self)
class SuperheroDen(Machine):
states = ['asleep', 'hanging out', 'hungry', 'sweaty', 'saving the world']
transitions = [
['wake_up', 'asleep', 'hanging out'],
['work_out', 'hanging out', 'hungry'],
['eat', 'hungry', 'hanging out'],
['nap', '*', 'asleep'],
dict(trigger='distress_call', source='*', dest='saving the world', before='change_into_super_secret_costume'),
dict(trigger='complete_mission', source='saving the world', dest='sweaty', after='update_journal'),
dict(trigger='clean_up', source='sweaty', dest='asleep', conditions=['is_exhausted']),
['clean_up', 'sweaty', 'hanging out'],
]
def __init__(self):
super().__init__(model=[NarcolepticSuperhero('Clone warrior') for i in range(100)],
states=self.states,
transitions=self.transitions,
# tell each model to 'log_out' right before state change
before_state_change='log_out',
# and to 'log_in' right after
after_state_change='log_in',
initial='asleep')
# since our super heroes are asleep (and 'spawn' in their state), we have to log them in the first time
for model in self.models:
NarcolepticSuperhero.hero_log[self.initial].append(model)
machine = SuperheroDen()
# trigger event 'wake_up' on all models
machine.dispatch('wake_up')
assert len(NarcolepticSuperhero.hero_log['asleep']) == 0
assert len(NarcolepticSuperhero.hero_log['hanging out']) == 100
for i in range(10):
machine.models[i].work_out()
assert len(NarcolepticSuperhero.hero_log['hanging out']) == 90
assert len(NarcolepticSuperhero.hero_log['hungry']) == 10
assert machine.models[0] in NarcolepticSuperhero.hero_log['hungry']