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

Query valid inputs? #115

Open
jackgeek opened this issue Sep 28, 2019 · 4 comments
Open

Query valid inputs? #115

jackgeek opened this issue Sep 28, 2019 · 4 comments

Comments

@jackgeek
Copy link

Hi there. I'm new to automa and interested in your library. Thank you for the effort you have put in and for making it open source.

Can you recommend an approach for knowing which inputs are valid at any given time? It seems to me that the FSM has this information and so I should be able to query it. Is there a way I can know what inputs are presently valid/subscribe to changes?

My use case right now is a simple calculator app. I want to bind the input buttons to inputs and auto disable them if the input is not valid in the current state.

How would you approach this?

Thank you for your time and consideration.

@jackgeek
Copy link
Author

jackgeek commented Sep 28, 2019

Here is the hacky work around that I have come up with... But there must be a better way?

from automat._core import Transitioner

def get_transitioner(fsm):
    attributes = fsm.__dict__
    for attribute in attributes.values():
        if isinstance(attribute, Transitioner):
            return attribute

def print_valid_inputs(fsm):
    transitioner = get_transitioner(fsm)
    automaton = transitioner.__dict__["_automaton"]
    current_state = transitioner.__dict__["_state"]
    transitions = automaton.__dict__["_transitions"]

    def for_current_state(t):
        return t[0] == current_state

    for t in filter(for_current_state, transitions):
        f = t[1].method.__name__
        print(f)

@jackgeek
Copy link
Author

Interestingly, the above hack only works after the first input has been executed. Beforehand the _automaton attribute is not in the transitioner dict

@glyph
Copy link
Owner

glyph commented Nov 11, 2019

It would be great to have a patch to support this.

@glyph
Copy link
Owner

glyph commented Nov 11, 2019

I don’t have much time to look into this myself, but have you used the visualizer? It’s the closest thing to an inspection API we have yet.

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

No branches or pull requests

2 participants