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

Access all possible events from a certain state? #8

Open
Rob117 opened this issue Dec 14, 2018 · 2 comments
Open

Access all possible events from a certain state? #8

Rob117 opened this issue Dec 14, 2018 · 2 comments

Comments

@Rob117
Copy link

Rob117 commented Dec 14, 2018

If this is confusing, please let me know.

Given the test code:

val stateMachine = StateMachine.create<State, Event, SideEffect> {
    initialState(State.Solid)
    state<State.Solid> {
        on<Event.OnMelted> {
            transitionTo(State.Liquid, SideEffect.LogMelted)
        }
    }
    state<State.Liquid> {
        on<Event.OnFroze> {
            transitionTo(State.Solid, SideEffect.LogFrozen)
        }
        on<Event.OnVaporized> {
            transitionTo(State.Gas, SideEffect.LogVaporized)
        }
    }
    state<State.Gas> {
        on<Event.OnCondensed> {
            transitionTo(State.Liquid, SideEffect.LogCondensed)
        }
    }
    onTransition {
        val validTransition = it as? StateMachine.Transition.Valid ?: return@onTransition
        when (validTransition.sideEffect) {
            SideEffect.LogMelted -> logger.log(ON_MELTED_MESSAGE)
            SideEffect.LogFrozen -> logger.log(ON_FROZEN_MESSAGE)
            SideEffect.LogVaporized -> logger.log(ON_VAPORIZED_MESSAGE)
            SideEffect.LogCondensed -> logger.log(ON_CONDENSED_MESSAGE)
        }
    }
}

I want to know how I could, for example, do
stateMachine.validEventsFor(State.Liquid) // -> list(Event.OnFroze, event.OnVaporized)

Is there a way to do this, even with reflection?

@zhxnlai
Copy link
Collaborator

zhxnlai commented Jan 17, 2019

@Rob117 operations like stateMachine.validEventsFor(State.Liquid) // -> list(Event.OnFroze, event.OnVaporized) are not well supported because of the way state machine is implemented.

What state<State.Solid>, on<Event.OnCondensed>, on("A State"), and on("An Event") create are matchers, which are defined by a list of closures stored here.

Because the relationship between states and events is computed at runtime using closures, I don't think it is possible to extract all valid events for a state.

@Rob117
Copy link
Author

Rob117 commented Jan 18, 2019

Got it. I statically defined an event list, and it worked for my use case. Thank you for your response though!

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