Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get the availiable states from a given state #2

Closed
Annakan opened this issue Jun 26, 2017 · 2 comments
Closed

Get the availiable states from a given state #2

Annakan opened this issue Jun 26, 2017 · 2 comments
Labels

Comments

@Annakan
Copy link

Annakan commented Jun 26, 2017

Hello,
Pysm seems to be the "most advanced and versatile" FSM python package but I am looking for a method to get the available transitions from a state.
Did I miss something ? or is there a way to build that without side effects ?

It is very very useful to be able to connect it to a graphical interface without duplication of the states in the GUI.
Available buttons and commands should be mapped/mappable to "current_subject_object.currentState.getPossibleNextStates()"

Thanks a lot for your time.

@pgularski
Copy link
Owner

pgularski commented Jun 27, 2017

Hi,

Currently there's no such functionality to get a list of next possible states. The quick and dirty solution to this would be something like below. You can adapt it to your own needs. Remember that even if there's a transition listed in the next_states it might be prevented by a condition callback.

def get_next_states(state):
    next_states = []
    # Machine has transitions, not a state
    machine = state.parent
    while machine:
        machine_transitions = machine._transitions._transitions
        transition_keys = [t for t in machine_transitions.keys() if t[0] is state]

        for key in transition_keys:
            if machine_transitions[key]:
                for transition in machine_transitions[key]:
                    to_state = transition["to_state"]
                    if to_state is None:
                        # It's a transition to the same state
                        to_state = state
                    next_states.append(to_state)
        machine = machine.parent

    return next_states

@pgularski
Copy link
Owner

Since I didn't hear from @Annakan anymore I close this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants