jakesgordon / javascript-state-machine

A javascript finite state machine library
MIT License
8.69k stars 964 forks source link

Determine if a state can switch to another state #131

Open Michael-Leonardo-Cantina opened 7 years ago

Michael-Leonardo-Cantina commented 7 years ago

I have a unique use-case for these FSMs that I hope you can help me with!

I am using the FSM to track a state machine from another source. It's possible that the source could send invalid state switches... I am forcing my FSM to change to any state (using that "goto" wildcard example) but I also defined what I know to be the correct possible transitions. I would like to force my FSM switch to new state (done already), but also report that the transition was in-error because there was no defined transition that went from the beforeState to this afterState.

I can use fsm.transitions to get all possible transitions (great)... but I need to be able to see what all possible endStates are for each possible transition. Is there any way to do that?

Something like: fsm.getAllPossibleNextStates() or fsm.getToState(transition)

Any help is greatly appreciated!

adami commented 7 years ago

from the documentation:

fsm.is(s) - return true if state s is the current state fsm.can(t) - return true if transition t can occur from the current state fsm.cannot(t) - return true if transition t cannot occur from the current state fsm.transitions() - return list of transitions that are allowed from the current state fsm.allTransitions() - return list of all possible transitions fsm.allStates() - return list of all possible states

when getting transitions() - aren't you getting the state each transition leads you to?

Michael-Leonardo-Cantina commented 7 years ago

Yes, I've seen those functions - none of them address the issue of needing to know what valid states you could be switching to based on what state you are in.

fsm.transitions() seems to only return the names of the possible transitions, but doesn't include which end-states these transitions go to. That why I would need something like fsm.getToState(transition) or something like that to then look up what states these possible transitions represent.

I appreciate you looking into this - any other ideas?

adami commented 7 years ago

from a quick look there's no quick way to do it, so either:

stayko commented 4 years ago

Is this still not possible?