Code Monkey home page Code Monkey logo

Comments (3)

aleneum avatar aleneum commented on June 9, 2024 1

If it is the auto transitions that you would not expect (which is the case according to your post), you can disable them by passing "auto_transitions=False" to the constructor:

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()

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' }
]


states = ['solid', 'liquid', 'gas', 'plasma']
m = Machine(lump, states, initial='liquid', transitions=transitions, auto_transitions=False)
print(m.get_triggers('plasma'))
# >>> []

They are enabled by default and are valid triggers. That's why get_triggers will return them when they are enabled.
The relevant part from the documentation:


In addition to any transitions added explicitly, a to_«state»() method is created automatically whenever a state is added to a Machine instance. This method transitions to the target state no matter which state the machine is currently in:

lump.to_liquid()
lump.state
>>> 'liquid'
lump.to_solid()
lump.state
>>> 'solid'

If you desire, you can disable this behavior by setting auto_transitions=False in the Machine initializer.


from transitions.

aleneum avatar aleneum commented on June 9, 2024

Hello @jonra1993,

triggers are only available on the source state. In your code example:

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' }
]

you can trigger "melt" from state "solid", "evaporate" from "liquid" and so on. Consequently, "melt" is not a trigger for "solid" since it cannot be triggered when in that state. As far as I can tell, "plasma" is not a source state for any of your transitions. Except auto transitions to_<state> there are no valid triggers. When you query triggers for "solid", you get "melt" and "sublimate":

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()

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' }
]


states = ['solid', 'liquid', 'gas', 'plasma']
m = Machine(lump, states, initial='liquid', transitions=transitions)
print(m.get_triggers('solid'))
# >>> ['to_solid', 'to_liquid', 'to_gas', 'to_plasma', 'melt', 'sublimate']

from transitions.

jonra1993 avatar jonra1993 commented on June 9, 2024

Hello @aleneum after using auto_transitions=False it worked as I expected thanks for your help. I have not noticed that before.

from transitions.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.