python库 transitions 状态机

from transitions import Machine

class Matter(object):
    pass

lump = Matter()

# 状态定义
states=['solid', 'liquid', 'gas', 'plasma']

# 初始化状态机
machine = Machine(model=lump, states=states, initial='solid')

# 添加转换
machine.add_transition(trigger='melt', source='solid', dest='liquid')
machine.add_transition(trigger='evaporate', source='liquid', dest='gas')
machine.add_transition(trigger='ionize', source='gas', dest='plasma')

# 状态转换
lump.melt()
print(lump.state)  # 输出: liquid

作者:spike

分类: Python

创作时间:2024-05-14

更新时间:2024-05-14

联系方式放在中括号之中例如[[email protected]],回复评论在开头加上标号例如:#1