When and why does transition assign methods to models #727
Replies: 4 comments
-
|
Hello @schaefer01,
Could you provide a minimal reproducible example where this does not work? Here is an example that shows how it can be used: from transitions import Machine
class Matter(object):
pass
lump = Matter()
states = ["solid", "liquid", "gas", "plasma"]
transitions = [
{"trigger": "melt", "source": "solid", "dest": "liquid"},
{"trigger": "evaporate", "source": "liquid", "dest": "gas"},
{"trigger": "sublimate", "source": "solid", "dest": "gas"},
{"trigger": "ionize", "source": "gas", "dest": "plasma"},
]
machine = Machine(model=lump, states=states, transitions=transitions, initial="gas")
lump.trigger("ionize")
assert lump.is_plasma() |
Beta Was this translation helpful? Give feedback.
-
|
I will add the exception today. My organization's plans for this tool are to use transitions as a state machine triggering processes running an atmospheric science radar that many scientists depend on. rps@C02D81GJMD6W ~ % python3
During handling of the above exception, another exception occurred: Traceback (most recent call last): |
Beta Was this translation helpful? Give feedback.
-
|
Thank you, the problem here is that you pass When you do I hope this example makes this a bit more obvious. import logging
from transitions import Machine
class Matter(object):
pass
logging.basicConfig()
lump = Matter()
machine = Machine(
model=lump, states=["solid", "liquid", "gas", "plasma"], initial="solid"
)
# add a unique transition on machine A
machine.add_transition("gasify", "*", "gas")
assert lump.state == "solid"
ref = lump.trigger
try:
# this is not (yet) defined
lump.evaporate()
assert False
except AttributeError:
pass
states = ["solid", "liquid", "gas", "plasma"]
transitions = [
{"trigger": "melt", "source": "solid", "dest": "liquid"},
{"trigger": "evaporate", "source": "liquid", "dest": "gas"},
{"trigger": "sublimate", "source": "solid", "dest": "gas"},
{"trigger": "ionize", "source": "gas", "dest": "plasma"},
]
machine = Machine(lump, states=states, transitions=transitions, initial="liquid") # this will trigger logging warnings
assert lump.evaporate()
assert ref == lump.trigger # <-- this has not changed
assert lump.trigger("gasify") # <-- this
assert lump.is_gas()Furthermore, if you use a logger as shown in this example, |
Beta Was this translation helpful? Give feedback.
-
|
Thank you. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Thank you for taking the time to report a bug! Your support is essential for the maintenance of this project. Please fill out the following fields to ease bug hunting and resolving this issue as soon as possible:
On the page URL: https://github.com/pytransitions/transitions?tab=readme-ov-file#diagrams
in the section that begins "Let's try again."
The example statement shows: lump.trigger('ionize') as this raises an exception
The correct example statement should be: lump.ionize()
Beta Was this translation helpful? Give feedback.
All reactions