Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [finite-state-machine]

The tag has no usage guidance.

119
votes
9answers
56k views

Are exceptions as control flow considered a serious antipattern? If so, Why?

Back in the late 90's I worked quite a bit with a code base that used exceptions as flow control. It implemented a finite state machine to drive telephony applications. Lately I am reminded of those ...
25
votes
10answers
41k views

Examples of Finite State Machines [closed]

I'm looking for good examples of finite state machines; language isn't particularly important, just good examples. Code implementations are useful (generalized pseudo-code), but it's also very useful ...
23
votes
10answers
17k views

State machines vs threads

Alan Cox once said "A Computer is a state machine. Threads are for people who can't program state machines". Since asking Alan directly is not an option for humble me, I'd rather ask here: how does ...
16
votes
4answers
3k views

Conceptually, how does one make a rules/constraints (not graphics/physics) engine for a game

I want to make a simple game similar to the choose-your-own-adventure books. The player is presented with a narrative text and gets to choose his action from a list of possibilities. This, in turn, ...
13
votes
6answers
3k views

How to recover from finite-state-machine breakdown?

My question may seems very scientific but I think it's a common problem and seasoned developers and programmers hopefully will have some advice to avoid the problem I mention in title. Btw., what I ...
13
votes
5answers
4k views

Can an object oriented program be seen as a Finite State Machine?

This might be a philosophical/fundamental question, but I just want to clarify it. In my understanding a Finite State Machine is a way of modeling a system in which the system's output will not only ...
13
votes
4answers
8k views

Definition of “state”

What is a good way to define "state", as in state variable or state machine, to a new (previously non) programmer? What are some good ways to explain why this concept is useful for writing software? ...
9
votes
4answers
1k views

How can a child state machine relinquish control back to the parent state machine?

My top level state machine has some states and edges. I will call this the parent state machine. A ----> B ----> C Any state within the parent state machine can be a state machine too. I will ...
7
votes
4answers
1k views

Is a finite state machine an appropriate solution for this situation?

I was writing some test code as an example where I had a washing machine class. It has a door state that should be open or closed, and also a running state, either on or off. I want to prevent the ...
7
votes
4answers
2k views

How is a state machine different from any other computer program?

I have seen several implementations of "State Machines" on github. As far as I understand, a state machine takes input that may or may not transform its state into one of a finite set of other states. ...
7
votes
1answer
803 views

Finite state machine menu design

I'm creating an FSM in python (it's a step sequencer and sample pad based on a Raspberry Pi 2). Right now there are two states and the third is the Menu. This is handled by a class System which ...
7
votes
2answers
690 views

How to store and validate data between state transitions?

I'm developing a largeish application that will have custom finite state machines. That is, the admin users of the application will be able to create their own state machines, limited by little pieces ...
6
votes
3answers
786 views

Can a generic piece of C code be converted to an FSM?

I understand that any language worth its salt can encode a Finite state machine. My question is the opposite, is it possible to convert an arbitrary piece of code ( say in C ) to a functionally ...
6
votes
1answer
3k views

Passing data between concrete states?

I have a state machine pattern implementation that's pretty straightforward, but I've got a design problem I don't know an elegant solution to. Here's some partly pseudo code to illustrate. class ...
5
votes
3answers
11k views

Advantages/Disadvantages of NFA over DFA and vice versa

What are the relative pro's and con's of both DFA's and NFA's when compared to each other? I know that DFA's are easier to implement than NFA's and that NFA's are slower to arrive at the accept state ...
5
votes
1answer
5k views

How to Implement a Parallel Workflow

I'm trying to implement a parallel split task using a workflow system. I'm using .NET but my process is very simple and I don't want to use WF or anything heavy like that. I've tried using Stateless. ...
5
votes
2answers
692 views

Good ways to jump to a particular state in a yielding stateful function?

I'm working on some embedded code using C. Various pieces of functionality need non-blocking stateful functions, which are mostly implemented using a switch on various states. For example, a modem ...
5
votes
2answers
507 views

A simple GUI app: states

I'm making a Windows Forms app: It is client-server. Authorization, then load some data from server, then send back some statistics from time to time. You should go through a bootstrapping procedure, ...
5
votes
1answer
161 views

How to Recover from Inconsistent Job State without Database Polling

I'm working on scaling an application which is currently polling a mySQL database to send async jobs to a background processing system. A state machine is being used to keep track of the entities ...
4
votes
4answers
2k views

How should state machines handle exceptions in actions?

State machines, for example UML state machines, statecharts or other finite state machines, allow actions to be executed when a state machine takes a transition between states or is within a state. ...
4
votes
2answers
345 views

Why do I need a game loop with states, instead of init the next state in current state directly?

For example, it is common to see games with game loop and states: stateChanged(){ switch(state){ STATE.PLAYER_SELECT_CHARACTER: this.currentController=new PlayerSelectCharacterController()...
4
votes
2answers
634 views

Can all business logic be decomposed as a (declarative) state diagram on simple crud operations and conditionals?

This seems an extreme question probably but I'm wondering anyhow: Can all business logic be decomposed as a (declarative) state diagram on simple crud operations and conditionals? Already a lot of ...
4
votes
2answers
385 views

Why does F. Wagner consider “NOT (AI_LARGER_THAN_8.1)” to be ambiguous?

In his article on Virtual Environments (a part of his VFSM specification method) Ferdinand Wagner describes some new ways of thinking about Boolean Algebra as a software design tool. On page 4 of ...
4
votes
2answers
568 views

Trouble with circular dependency in state machine design

I am trying to develop the structure for a basic state machine that can also take in input and produce output. I've hit a bit of a mental block in trying to figure out how to model the relationship ...
4
votes
1answer
1k views

Should I use the State Design pattern for only two states? Also, what if one object's state is affected by another state?

If you only have two states for an object, is it worth it to still use the State Design pattern? The context is a multi-player, turn-based game environment where there are Game "Tables" that a player ...
3
votes
1answer
236 views

UML “direct substate” vs “transitively nested substate”

I am reading through Wikipedia's page on UML state machines. I don't understand where it says: A nested state is called a direct substate when it is not contained by any other state; otherwise, ...
3
votes
1answer
2k views

Constructing a Deterministic Finite State Automaton for a given Regex

I have a couple of exam questions for my compilers class and wanted to check if my solutions are correct. The first question is: Consider a language in which numbers start with an optional minus ...
3
votes
1answer
903 views

How does one combine the state pattern and the component pattern?

Suppose we have a class Player with classes PhysicsComponent, InputComponent and StandingState, DuckingState. The class Player itself does not have an input function, the InputComponent does. But ...
3
votes
2answers
2k views

Language to describe Finite State Machine

I'm doing a lot of code analysis using lexer and finite state machine. For time being I'm using table to describe FSM: | token | current state | target state | +-------+---------------+--------------+...
3
votes
2answers
2k views

Should I use multiple state machines for a layered protocol?

When implementing a layered communications protocol are layers commonly implemented as state machines? I have an implementation of PMBus I am currently working on for an embedded device. I have a ...
3
votes
1answer
785 views

Model of simple traffic simulation [closed]

I want to write a simple traffic simulation with one road intersection. First I thought of a cellular automata in the model but it should not be limited by an underlying discrete grid system so the ...
3
votes
2answers
215 views

Is there a good way (pattern) for adding a state to a finite state machine

So I'm working on a roguelike, and planning to represent the monsters using a finite state machine spot PC wander------------------->move ^ ^ | | \ ...
3
votes
1answer
844 views

Application protocols and state machines: designing a complicated server

I'm designing and prototyping a server that has the following characteristics: during the daemon's initialization it spawns nine "manager" servers, each of which solves one part of the overall ...
3
votes
1answer
150 views

In OOP which object is common to update the state of an object when working with State Machines?

I'm about to implement my first State Machine and I'm left with one question, which is the acceptable coding form on who should change the state of the object/entity? Should the StateMachine change ...
3
votes
1answer
961 views

State machine with additional variable

When implementing a state machine and it comes to the fact that a transition depends on an external boolean a, or an enum value b which can be one out of three (which are set somewhere else in the ...
3
votes
1answer
2k views

What is the alternative design pattern of a state machine that may have recursive state change?

I have a state machine like code, which has state A,B and C, with a function changeState(char i) which can change the state of a system, and each state can also call changeState(char i) to switch the ...
3
votes
1answer
480 views

How to process document state transition?

Imagine there is an application (ASP.NET MVC) that processes some documents. The document must be revised several times by different group of users. state/role rules: simple user can only publish ...
3
votes
2answers
894 views

Communication between state machines with hidden transitions

The question emerged for me in embedded programming but I think it can be applied to quite a number of general networking situations e.g. when a communication partner fails. Assume we have an ...
3
votes
2answers
580 views

How do I reuse a state machine in a slightly different way?

Problem I have a big state machine. The design requirements of the project have changed such that I need to re-use this state machine in another place. All the states remain the same in this new ...
2
votes
4answers
115 views

How to explain to a vendor that when erroneous data is deleted/deactivated the application future state should be as if the data was never entered?

A major application we use has a bug that the vendor is calling (a swear word in our business) "working as designed". As a programmer / system analyst this is a bug - and against all I have been ...
2
votes
2answers
74 views

Hierarchical State Machine (HSM) superstate and substate sharing event

I am learning HSMs and I can see a problem that I don’t know how to address. The problem is shown in the image bellow. Inside S1, substates S11 and S12 are continuously changing in reaction to ...
2
votes
2answers
113 views

How to model a statemachine in this case

I have a system where lot of messages come and go over network, each message to be sent or received goes through several states, state change occurs based on external events for example if the linked ...
2
votes
1answer
311 views

Is state pattern a wise architecture for a diagram application?

I'm designing a vector drawing app. I need to implemente copy and paste, shapes insertion, deletion and transforming while still being capable of including new tools with easy in the future. The way ...
2
votes
2answers
2k views

Dependency injection with Finite State Machines and the Interface Segregation Principle

I am trying to figure out what is the best way to setup a FSM and also keep my interfaces clean and use an IOC container to use DI. Let's just visit the Head First design pattern textbook example of ...
2
votes
2answers
2k views

How to present asynchronous state change in chart or diagram?

I started to study about state transition chart. As I see, it assumes all state transition is done instantly with no time consuming. But in most of my case, I'm heavily depending on asynchronous I/O, ...
2
votes
1answer
65 views

Using state machines when managing remote resources

So I’ve got a service I’m trying to clean up. It responds to requests by starting a stopped server on Amazon EC2, waiting for it to come online, then configures a 3rd party piece of software on the ...
2
votes
2answers
175 views

Is there a well defined notion of 'before' and 'after' in finite state machines?

When working with finite state machines, is there or can there be a well defined concept of 'before' and 'after'. The ordering would, for example, tell me that one state is always considered to be '...
2
votes
2answers
276 views

Constructs for wrapping a hardware state machine

I am using a piece of hardware with a well defined C API. The hardware is stateful, with the relevant API calls needing to be in the correct order for the hardware to work properly. The API calls ...
2
votes
1answer
601 views

How do you diagram global or shared state?

Say you're building an FSM for something like a game and you've got states like: MainMenu Options SinglePlayer MultiPlayer Your state diagram might look something like this: Now say you have a ...
2
votes
0answers
318 views

Finite State Machine user input tree

I'm drawing a Finite State Machine (which I'll have to implement later on) which is semi-automatic, i.e. some transitions require user input, some are decided on internal value. The problem is that ...