This state machine is inherently unsafe for concurrent use, because the CurrentState and Transitions fields aren't completely protected by a mutex. You already have an unexported mutex that you lock when mutating those fields, but then consumers trying to read those exported fields are not able to lock the same mutex to prevent concurrent read/write operations.
You should not export those fields, and instead make them available via methods where the reads are protected by the mutex. You'll probably also need to make a copy of the map since it's a reference type, OR make the accessor take the key name and fetch the value directly from the map and return it. I learned this when I wrote a similar simple state machine in Go ~7 years ago. :)
I'd also make sure to return `T` from the CurrentState accessor method and not `*T`, just to make it easier for consumers to do comparison operations with the returned result.
Nice job. In my own usage of state machine libraries over the years I have found that for complex use cases, it's definitely helpful to have event-based transition dispatching be part of the library. The great benefit of FSMs is that you can express in data a lot of aspects that you would normally express in code. Not knowing what event needs to happen to transition to one state vs another leaves out a lot of the benefits that you get from designing your code around FSMs.
That being said, I appreciate the simplicity and it's a totally fine choice to leave out event based dispatch for less complex use cases!
One thing that has been mentioned here already: it's super helpful to have your library output a diagram file to visualize the FSM. This is a really great way to keep code and documentation in sync always.
Thank you. The focus was on simplicity and handling less complex scenarios. In my current projects the business logic lends itself to sitting outside the FSM/State Trooper. The logic dictates the transitions at arms length to the FSM. The FSM does not carry on any particular business logic, it just enforces the transition rules from one state to any number of possible states - hence why I've included the metadata aspect to embed info about the reason for a transition.
Re the visualization, I think that would be cool. I might give that a shot.
A state machine should have more than states. It also needs actions which cause transitions. The API should be about writing out which new state an action causes given a base state. Eg you have a modal with a button and it can be clicked or dismissed. In the open state, click and dismiss cause close, and in the closed state, click causes open. Anyway, the whole thing is only valuable once you have actions.
The README has an example of shipment, so let’s think about that. It is a bad example though because it just has packed → shipped → delivered, which is not a real business process.
Let’s say you’re a seller, as opposed to UPS or FedEx. The first thing that happens is a customer makes an order. What happens next? Well, the customer can cancel their order. Or the order can be sent and then the customer can ask for a refund. Let’s try to model it:
Ordered + cancelled → cancelled
Ordered + warehouse packing → packed
Packed + cancelled → cancelled
Packed + warehouse handed package to shipper → shipped
Shipped + cancelled → too late, it’s still in the shipped state
Shipped + return request → awaiting return
Awaiting return + received return → refund
Shipped + received return → no RMA, so no refund. No state change.
Shipped + 30 days → archived
Etc. I am too lazy to do the full model, but you start to get the idea. The important thing is that actions happen from outside the system and then they cause state transitions based on what the current state is. Sometimes the system just stays in the state it is already is in, and sometimes the system moves backwards. Some actions can’t happen in certain states (can’t get something back before it goes out, can’t dismiss a closed modal dialog), but that’s not because of business logic but because of the real world. The business logic only enforces itself, not the world.
The README has an example of shipment, so let’s think about that. It is a bad example though because it just has packed → shipped → delivered, which is not a real business process.
Let’s say you’re a seller, as opposed to UPS or FedEx. The first thing that happens is a customer makes an order. What happens next? Well, the customer can cancel their order. Or the order can be sent and then the customer can ask for a refund. Let’s try to model it:
Ordered + cancelled → cancelled
Ordered + warehouse packing → packed
Packed + cancelled → cancelled
Packed + warehouse handed package to shipper → shipped
Shipped + cancelled → too late, it’s still in the shipped state
Shipped + return request → awaiting return
Awaiting return + received return → refund
Shipped + received return → no RMA, so no refund. No state change.
Shipped + 30 days → archived
Etc. I am too lazy to do the full model, but you start to get the idea. The important thing is that actions happen from outside the system and then they cause state transitions based on what the current state is. Sometimes the system just stays in the state it is already is in, and sometimes the system moves backwards. Some actions can’t happen in certain states (can’t get something back before it goes out, can’t dismiss a closed modal dialog), but that’s not because of business logic but because of the real world. The business logic only enforces itself, not the world.
IMO, AddValidTransition would be way better. I think I would go for AddTransition, though. It’s not as if there’s also a way to add invalid transitions.
Commenters like you are why people don't share thing. Why make something available for free when someone like you is just going to come along and shit on it?
If your only reasoning behind releasing an open source project is to receive uncritical accolades just for producing open source, then I think you need to examine your motives and seriously question why you expected this deference in the first place.
Commenters like you are why people don’t give honest feedback. Why review something for free when someone like you is unable to accept honest feedback even if it is critical.
If you post your project on a public forum with a comment section, you should expect some people to be critical and suggest things, no matter what it is or how much it costs.
Perhaps the real issue is commenters like GP is why YOU don't share things?
Interesting. I wrote almost the same code for work a couple of weeks ago.
Not sore, but it looks like the Transition function has a race condition. It calls CanTansition() before acquiring the mutex lock. I think this could lead to illegal state transitions.
I saw the benchmark which seemed crazy (3us per transition and allocations). So looked quickly at the code.
Recommend putting the tracking of previous states in a separate optional debugging type so you don’t have to pay the cost in general. Oh and using time stamps as a key is kinda weird, but even weirder in Go where maps are non-deterministically enumerable.
Otherwise, seems like a good use of generics, given Golangs particular take on it.
A state machine (specifically a FSM) class is something I end up having to reinvent in every new language I've adopted. Such a useful pattern whose need comes up repeatedly. Especially in games/sims or in anything with a GUI. Since I've been making both for decades I have a lot of homegrown FSM classes sitting around. :-p
This program waits until thread(s) is true in another thread until thread(r) is true, everything left of the equals symbol needs to be true before it passes to the next group of facts after the = symbol. Then it waits for "fact(variable)" to be fired, then when all those atoms are true, the state transitions past the pipe symbol and does a send(message) while the other thread does a receive(message) and then the process is repeated in the opposite direction. I've not shown it here, but you can wait for multiple facts to be true too.
Here's a state machine for an (echo) server that is intended to be mapped to epoll or libiouring:
That is a description of flow-based programming, minus the generalization to "arbitrary packets of data in bounded buffers" - that is, your only data type is true/false and your only buffer size is 1, which lets you get some very clean syntax on it. It's a good invention!
FBP adapts the physical concept of unit record machines that operate over punchcard stacks, which predisposes it to thinking in terms of processing and transforming richer data like "characters in a string" or "records in an array", but it basically works for truth-signalling logic too - let truth packets wait in a buffer, and then when the node signals that the packets are able to move, that is the transition.
The emphasis does differ in that if we are thinking "FSM", the rest of the program and the data it handles have been abstracted, while if we are thinking "FBP" we're engaged with designing specific machines to connect together in terms of I/O, which is more helpful when you have a library of data operations to reuse.
You're right about the buffers with a truth value in a buffer. I actually use a style of promises or my own count down latch.
I am inspired by Prolog and Drools, a rule engine. I want to "fire" and "retract" facts and the state machine shall work out what to do next.
Here's something that I find especially useful about the formulation: the state machine only moves in one direction, from left to right, but there are multiple transition signals available (facts/atoms) that can be fired to cause the state machine to move forwards.
If you define multiple independent state machines it should be possible to detect "stuck states" where it is impossible for the state machine to continue progressing! This is useful for liveness analysis.
I want the state machine formulation to be useful in designing distributed systems such as Raft.
Thanks for everyone's feedback. I've pushed out a few changes:
1- Regular slice instead of timestamp-keyed map. That didn't make sense in retrospect.
2- Better benchmarks.
3- Non-exported current state and transitions. Mutexed getters to avoid concurrency issues.
4- Variadic rule parameters.
5- Better example.
Nice work!! I have always appreciated the clarity of FSMs since first learning about them in college. They are a great way to think about a system's state and given X input, clearly define the next state. This makes them very easy to test as well.
you define Transition over T comparable, but there is no guarantee that comparable implements json.Marshaler, so FSM's MarshalJSON and UnmarshalJSON don't really work
I've written 2 in C++ for different projects at companies State Farm and State of Decay. I'm trying to write one that kind of inverts things and calling it Enemy of the State.
But yes, his name pisses me off because I didnt think of it either.
what do you think would happen if there was a comment about go on the internet that you somehow didn't see, and to which you weren't able to reply with a sarcastic and sneering dismissal?
so, i had a look because i wondered if you were joking or not, but algol 60 doesn't seem to have enums (putting it here just in case someone else may read this conversation)
dang sorry bro but the site guidelines are bad. People regularly endorse straight up atrocities, racism, eugenics, holocaust denial in here and that's all fine if they stay calm and don't say the slurs. I will never not be mad at people defending atrocities and that is the superior transgression. You value civility over human dignity. It's cowardly.
Let me express anger. The people commenting this stuff here expect never to be challenged on it. Expect that they have the right to spew this stuff, carefully, without being told their actions are shameful. It's good for them.
Words like "human dignity" and "challenging" ae too grandiose to apply to internet attacks, putdowns and so on. If you want to do something for human dignity, there are plenty of other ways, some of which might even have a positive effect, unlike calling names and whatnot. We know what all that leads to, there's no moral value in it (regardless of how righteous the people doing it feel, or how they justify it) and we're trying to avoid that.
If other users are posting bad comments, the thing to do is to downvote them and/or flag them and/or let us know about them at hn@ycombinator.com - as the site guidelines request. Posting flamewar attacks in response only evokes worse from others, draws more attention to the thing you're attacking (which energizes it, which contradicts your intention),
and ultimately plunges the ecosystem into internet hell. If you don't want to help HN avoid that outcome, please don't post here.
We don't come close to seeing everything that gets posted to HN. If you see a post that ought to have been moderated but hasn't been, the likeliest explanation is that we didn't see it. You can help by flagging it or emailing us at hn@ycombinator.com. https://hn.algolia.com/?dateRange=all&page=0&prefix=false&qu...
Re "superior transgression": everyone always feels like the other person started it and did worse.
I haven't used the word civility in many years except to explain why we don't use that word.
You should not export those fields, and instead make them available via methods where the reads are protected by the mutex. You'll probably also need to make a copy of the map since it's a reference type, OR make the accessor take the key name and fetch the value directly from the map and return it. I learned this when I wrote a similar simple state machine in Go ~7 years ago. :)
I'd also make sure to return `T` from the CurrentState accessor method and not `*T`, just to make it easier for consumers to do comparison operations with the returned result.
Reference on Go memory safety: https://go.dev/ref/mem