While I understand the importance of state machines and its direct usage in complex Business Logic Workflows (and indirectly almost everywhere), there is nothing special in this post. It is a generic comment on State Machines. Am I missing something here?
Designing state machines
31–40 of 58 posts
Re: Designing state machines
#32While I understand the importance of state machines and its direct usage in complex Business Logic Workflows (and indirectly almost everywhere), there is nothing special in this post. It is a generic comment on State Machines. Am I missing something here?
Googled looks like it is. That was a cool homework/project.
Re: Designing state machines
#33In case you're using Python, I can't recommend this library enough - https://github.com/pytransitions/transitions . An absolute pleasure to work with.
I replaced it with just this:
class StateMachine(object):
'''
A very simple FSM class.
Params:
initial: Initial state
table: A dict (current, event) -> target
'''
def __init__(self, initial, table):
self.current_state = initial
self.state_table = table
def __call__(self, event):
'''Trigger one state transition.'''
self.current_state = self.state_table[self.current_state, event]
class Foo(object):
STATE_TABLE = {
(current_state, event): next_state,
...
}
def __init__(self, xid, token, config):
self.fsm = StateMachine('start', self.STATE_TABLE)
def begin(self):
while self.fsm.current_state not in {'success', 'error'}:
method = getattr(self, self.fsm.current_state)
self.fsm(method())
if self.fsm.current_state == 'error':
self.report_error()
If you're using Python anything more involved than a dict mapping (current, transition) -> next is just overkill. Use it, don't abuse it. ;-)Re: Designing state machines
#34Earlier quoted context omitted.
I wanted to like Mermaid, then I realized it was just a neutered version of GraphViz. I had wanted to write a Mermaid-to-Graphviz compiler but never got around to it. It produces pretty ugly graphs that can't be exported to any other format. Meanwhile just about ever graph manipulation program (e.g. Gephi) can handle GraphViz DOT format.
I didn't know GraphViz. I have to try it. DOT language looks very similar to Mermaid. It was too hard to make that compiler or you have encountered another problem?
It's still on my list of short projects. I have a lot of learning to do, knowing nothing about writing parsers and such. Yes, I know I could probably hack it together with a bunch of garbage Python. But that just does not sound fun to write, let alone maintain.
Re: Designing state machines
#35In case you're using Python, I can't recommend this library enough - https://github.com/pytransitions/transitions . An absolute pleasure to work with.
Re: Designing state machines
#36Statecharts (also called hierarchical state machines) are essentially generalized state machines which allow for nesting and parallel composition of states. The 'nesting' part is my favourite, since it allows one to delegate event handling logic shared by multiple states to a 'parent' state, reducing code duplication.
The great thing about this paper is that you can glean most of its key ideas by just looking at the diagrams.
Re: Designing state machines
#37There was an article posted here a while back on how to implement a state machine in Rust, purely using its type system to facilitate state transitions. It's a neat concept, even if it's a bit more verbose than what you'll find in other languages. https://hoverbear.org/2016/10/12/rust-state-machine-pattern/
Re: Designing state machines
#38While I understand the importance of state machines and its direct usage in complex Business Logic Workflows (and indirectly almost everywhere), there is nothing special in this post. It is a generic comment on State Machines. Am I missing something here?
Is "state machines" a global term that includes finite state machines like how vending machines deal with varying coin amounts that go in to trigger the price and dispense proper change? Googled looks like it is. That was a cool homework/project.
Re: Designing state machines
#39A paper that changed my approach to designing state machines is "Statecharts: A Visual Formalism for Complex Systems" by Harel ( http://www.wisdom.weizmann.ac.il/~harel/papers/Statecharts.p... ). Statecharts (also called hierarchical state machines) are essentially generalized state machines which allow for nesting and parallel composition of states. The 'nesting' part is my favourite, since it allows one to delegate…
Re: Designing state machines
#40While I understand the importance of state machines and its direct usage in complex Business Logic Workflows (and indirectly almost everywhere), there is nothing special in this post. It is a generic comment on State Machines. Am I missing something here?
A surprising number of entrepreneurs and developers are unfamiliar with the application of state machines to business logic workflows, so it's useful to see the topic presented in a succinct way. Good learning content tends to get upvoted on HN, especially when the discussion around the concept (for example, the discussion of State Machines and state machine libraries in this thread) is interesting as well.
the blog post wasn't particularly deep, but the (short) discussion here was worth perusing, as i'm considering employing a state machine for my rails app and have been generally looking for options and best practices (in a mental background thread). but it looks like most (all?) existing gems assume a fixed set of states and transitions, and i was hoping to find something designed primarily for user-defined states and transitions.