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

Self Transitions #19

Closed
theathibm opened this issue Dec 20, 2016 · 11 comments
Closed

Self Transitions #19

theathibm opened this issue Dec 20, 2016 · 11 comments

Comments

@theathibm
Copy link

Self transitions don't seem to be supported. The following code snippet from fsm.go seems to consider self transitions as errors.

if f.current == dst {
	f.afterEventCallbacks(e)
	return &NoTransitionError{e.Err}
}

Perhaps I'm not using the fsm appropriately. any thoughts?

@maxekman
Copy link
Member

If you expect a self transition just check for the error. If you want to treat it as an error or not is up to you.

@maxekman
Copy link
Member

Or did you want to do something more with a self transition? Callbacks etc.

@theathibm
Copy link
Author

theathibm commented Dec 20, 2016

a self transition is completely valid in state machines, FSM should really support it and not consider it an error. Perhaps it's ok to not leave the state and then renter the same state, but the transition should still be valid and not be considered and error.
image

@maxekman
Copy link
Member

Yes, an error could be misleading in this case, I definitely see your point. Let's discuss this further. What changes do you suggest? Does the fact that no transition happened need to be fed back into one of the callbacks or something similar?

@maxekman maxekman reopened this Dec 20, 2016
@theathibm
Copy link
Author

I think the transition should happen, but the leave state and then enter state may not happen since it's just a self transition.

The transition needs to be a first class citizen. Currently beforeEvent and afterEvent hides the actual transition logic that needs to occur.

Adding one more callback called "doEvent" might be needed.

@JalfResi
Copy link

JalfResi commented Sep 21, 2017

I've just been bitten by this issue. Any progress on this?

I'd also suggest that leave and enter state actually do fire, as the state machine is transitioning out of the state and back into the state - its more that the transition has occurred and resulted in the same state, rather than the event itself has fired.

Example, think of a simple game with two players. You may want the game to transition between player turns:

{Name: "PlayerEndsTurn", Src: []string{"player_turn"}, Dst: "player_turn"},
{Name: "AllPlayerPiecesHaveBeenMoved", Src: []string{"player_turn"}, Dst: "player_turn"},

and have a callback for enter_ player_turn that setups up the state for each player on their turn. It's not the event that is important here, as there may be other events that trigger the transition to a new player_turn (such as the last possible piece movable by the player being moved).

@ppwfx
Copy link

ppwfx commented Jan 1, 2018

This is a deterministic state machine implementation. That's why every state can only have one dst.
There is a really simple solution tho, just add a second state machine that is created when a state is reached that has state on its own.

@maxekman
Copy link
Member

I'll close this, referring to the last comment.

@pajacobs17
Copy link

pajacobs17 commented Aug 25, 2020

I still think this should be looked at. The comment from @ppwfx isn't correct or I'm misunderstanding what he's saying. While the implementation of this library is based on a determinstic fsm, a determinstic fsm allows for multiple transitions out of a state, and even back into the same state, for different input. We're not talking about allowing multiple transition from a state with the same input. That would make it a NFA. What this issue is about does not fall outside of a DFA.

So in my case, we have an fsm where you have to upload at least one document, but you may need to upload multiple

{Name: "upload", Src: []string{"need-upload", "uploaded"}, Dst: "uploaded"}, {Name: "start", Src: []string{"uploaded"}, Dst: "started"},

You transition to uploaded once you have uploaded one piece, but then you should be able to upload multiple pieces of proof. This is why a self transition makes sense. You should still trigger the before and after functions, as the transition is still occurring.

@lebck
Copy link

lebck commented Oct 31, 2022

any comments/updates on this? i think @pajacobs17 is right. currently we need to implement all self transitions ourselves (by handling the transition error) since we the lib is not supporting it which is a bit annoying.

Compare: http://www.phon.ox.ac.uk/jcoleman/new_SLP/Lecture_1/FSMs.html chapter "self loops"

@Ashrit263
Copy link

Hi Team, The state machine is not supporting transition from and to if its same state. There is a business requirement to allow the state transition and trigger some events in case of the from and to state is same. Any recommendation here would be appreciated

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

No branches or pull requests

7 participants