我目前动态创建使用tyarkoni/transitions为Python状态机。但是,我已经到了一个我想重新命名一个状态并且是转换的地步。我想知道是否有办法做到这一点,而不用删除并重新添加一个不同的名称状态。重命名状态和转移已应用于计算机
更具体地说,我做的是我创建的状态和转换,同时通过一个未知的系统做一个BF-搜索与一组子系统和实际名称/状态的IDS /系统有时可在搜索的后期阶段被“发现”,而不是在状态被找到并被添加到状态机时(然后被保存在临时名称/ ID下)。然后我想在程序的其他部分重新命名它们以便于处理等。
有没有简单的方法来做到这一点?
如何,我认为是上面是在Python执行你就可以重命名状态
from transitions import Machine
s = ['A','B','C'] # States
t = [['ab','A','B'],['bc','B','C'],['ca','C','A']] # Transitions
m = Machine(states=s,transitions=t,initial='A')
m.state # Current state ('A')
m.get_state('A') # Gets state 'A' (<State('A')@140486642630440>)
m.get_state('A').name # Gets state name 'A' ('A')
m.get_state('A').name = 'D' # Renames state name 'A' to 'D'
m.get_state('A') # Gets state 'A' (<State('D')@140486642630440>)
m.get_state('A').name # Gets state name 'A' ('D')
# The following generates an error, since the state isn't renamed in the machine
m.get_state('D') # (ValueError: State 'D' is not a registered state.)
结果实例
>>> from transitions import Machine
>>> s = ['A','B','C'] # States
>>> t = [['ab','A','B'],['bc','B','C'],['ca','C','A']] # Transitions
>>> m = Machine(states=s,transitions=t,initial='A')
>>> m.state # Current state ('A')
'A'
>>> m.get_state('A') # Gets state 'A' (<State('A')@140486642630440>)
<State('A')@140329230661784>
>>> m.get_state('A').name # Gets state name 'A' ('A')
'A'
>>> m.get_state('A').name = 'D' # Renames state name 'A' to 'D'
>>> m.get_state('A') # Gets state 'A' (<State('D')@140486642630440>)
<State('D')@140329230661784>
>>> m.get_state('A').name # Gets state name 'A' ('D')
'D'
>>> # The following generates an error, since the state isn't renamed in the machine
>>> m.get_state('D') # (ValueError: State 'D' is not a registered state.)
ValueError: State 'D' is not a registered state.
这不工作,我只是重新命名的名称字符串,而不是实际的状态。 我想要的结果是('A')
改为('D')
到国家的所有引用,因此,例如机器m
的过渡将是[['ab','D','B'],['bc','B','C'],['ca','C','D']]
这是可能的,还是我要坚持添加/删除的状态和所有它是单独的转换引用?如果是这种情况,那么可行的方法是什么?