digitalmars.D.learn - Generic state machines
- MJ (34/34) May 18 2019 Anyone have any nice, generic and very simple state machine
Anyone have any nice, generic and very simple state machine design patterns in D? Basically one someone specifies the state transition matrix and callbacks in a very simple and direct way. Using a struct itself is rather straightforward but requires boilerplate code. This could be removed by making it more generic. I think the idea would be to specify an enum that represents the states, then an nxn matrix that maps the current state to the future state with each entry holding a delegate would be auto generated. One then just passes the new state to a function that does the rest... Not to hard to code but curious if there already exists something that works well and is very easy to use. Also, ideally, I'd like to be able to compose state machines which is a state machine itself but it allows multiple state machines to be active. For example, I might have a state machine for the mouse, which is 3 state machines(or more), one for each button. Another state machine for the keyboard. Generally the states machines are independent. When certain combinations of states occur, the state machines interact and create new state transitions. For example, if ctrl is pressed without any key, a state in the keyboard SM, and the left mouse is pressed, a state in the mouse SM, then one has a higher order combinational state that is transitioned too. The problem is that this gets very complex to deal with quickly since combinations grow exponentially. Hence a very regimented and efficient way is required. When we program, we are usually working in these state machine patterns but without the pattern but it usually is efficient since we only handle the transitions we care about. E.g., we might not want to store a transition for every keyboard combination, might be 10's of thousands of combinations possible. Therefore, rather than a N-D matrix it would be a list.
May 18 2019