Closed xiaohuihui1024 closed 4 years ago
Hi @xiaohuihui1024, State
is a rather simple class which is suitable for 'flat' state machines based on Machine
. However, as also stated in the readme:
HierarchicalMachine requires your custom state to be an instance of NestedState (State is not sufficient)
State instances passed to HierarchicalMachine
must be NestedState
or derived classes. The code above works fine with:
from transitions.extensions.factory import HierarchicalMachine
from transitions.extensions.nesting import NestedState
from transitions import Machine
class Matter(object):
def say_hello(self): print("hello, new state!")
def say_goodbye(self): print("goodbye, old state!")
lump = Matter()
states = [
NestedState(name='solid', on_exit=['say_goodbye']),
'liquid',
{ 'name': 'gas' }
]
m1 = HierarchicalMachine(lump, states=states)
But since you are not the first one attempting to use simple State
objects in HierarchicalMachine
I will try to improve the error message so that this issues becomes a bit clearer. I will also add this fact explicitely in the documentation.
Thank you for the quick response. I may not have noticed this detail in the 0.8.0 version of the document Your answer is perfect, it solves my problem, and also provides a friendly reminder for people who may encounter this problem in the future.
Yeah, it was definitely not prominent enough. This is one of those things that is hard to spot as a developer (or a user "too" familiar with the module). I will close this since this seems to be solved. Feel free to comment when there is more to add. I will reopen the issue if necessary. Thanks again for taking the time to bring this to our attention.
Hi @aleneum Using: Windows 10 64 bit Python 3.7.4 (Anaconda) transitions 0.8.1 Graphviz 2.38.0 (binary) graphviz 0.13.2 (python package)
I found a issue that HierarchicalMachine cannot add state of type State.
'solid'
state is an instance of the State classThe error output is as follows
Using HierarchicalGraphMachine will have the same issue
But using Machine can work normally