Code Monkey home page Code Monkey logo

Comments (2)

tyarkoni avatar tyarkoni commented on May 18, 2024

Hmm... that's an interesting idea, but I'm not sure it's a common enough use case to warrant adding. I think there are two alternative approaches that might work better. One is to just fall back on the standard approach and add all transitions explicitly rather than using the ordered_transitions shorthand. I.e., instead of

machine.add_ordered_transitions(['A', 'C', 'B'], trigger='next_state')

...one can always write:

transitions = [['next_state', 'A', 'C', 'this_must_pass'], ['next_state', 'C', 'B']]

...which will usually not be very verbose. I think for the few cases where an individual condition is needed for an ordered chain, and there aren't very many states, that's probably a reasonable approach.

When there are a lot of states, adding all the transitions explicitly might become a pain. In that case I think a reasonable way to handle this would be to add some kind of update_transition or replace_transition method--or maybe add a boolean 'replace' argument to Machine.add_transition() that overwrites any existing matching transition instead of always appending. Then your problem could be solved by doing one of the following:

# Create the chain
machine.add_ordered_transitions(['A', 'C', 'B'], trigger='next_state')

# Option A: get transition and manually update state 
t = machine.get_transition('next_state', 'A', 'C')
t.add_condition('this_must_pass')

# Option B: replace transition in place
machine.add_transition('next_state', 'A', 'C', conditions='this_must_pass', replace=True)

I'm okay with either approach, but I think the first one might be a better way to go as it would probably be generally useful to let users easily retrieve existing transitions for inspection/modification. Thoughts?

from transitions.

xdmiodz avatar xdmiodz commented on May 18, 2024

Oh, I completely missed the possibility to use a 'next_state' trigger. For me it will do the job. Thanks!

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.