Questions tagged [state-machine]

A State Machine is a computation model used to design both computer programs and sequential logic circuits. It determines that an entity in a program can be in one of a finite number of states. The state it is in at any given time is called the current state. It can change from one state to another when initiated by a triggering event or condition; this is called a transition.

A state machine is an abstract machine, which possesses

  • State a condition that when tested will be either true or false
  • Transitions the result of true/false decision to another State
  • Entry actions (what happens when a state is entered)
  • Exit actions (analogous to entry actions)

As a result, a system can dynamically change its behavior based upon what it is currently doing and what input it receives - this allows state machines to model many types of systems. (An familiar example is Regex - Perl, Python, Tcl, .NET, and most other implementations use a state machine internally).

For example, consider a printer, which can be either waiting for a job, or printing it (states), and can start printing when a job is received and start waiting when it is done with the current job (transitions). Send notification is an exit action for the "Printing" state, to notify the print queue that it is done.

SDL description of a printer

(The above diagram is in the SDL format - UML is also popular for representing state diagrams).

#Related tags - The Coyote open source asynchronous programming framework

1708 questions
49
votes
6 answers

C++ code for state machine

This was an interview question to be coded in C++: Write code for a vending machine: Start with a simple one where it just vends one type of item. So two state variables: money and inventory, would do. My answer: I would use a state machine…
Romonov
  • 8,145
  • 14
  • 43
  • 55
36
votes
2 answers

Workflow engine in Javascript

Does anybody know a workflow engine (such as Spring WebFlow) for Javascript? We have a home-made framework that allows us to guide site navigation using JSON, but its quality is far from good. Edit based on given answers: the engine must run on the…
eabait
  • 1,196
  • 2
  • 10
  • 16
35
votes
20 answers

Code Golf: Finite-state machine!

Finite state machine A deterministic finite state machine is a simple computation model, widely used as an introduction to automata theory in basic CS courses. It is a simple model, equivalent to regular expression, which determines of a certain…
Adam Matan
  • 128,757
  • 147
  • 397
  • 562
34
votes
9 answers

Java enum-based state machine (FSM): Passing in events

I'm using several enum-based state machines in my Android application. While these work very well, what I am looking for is a suggestion for how to elegantly receive events, typically from registered callbacks or from eventbus messages, into the…
Trevor
  • 10,903
  • 5
  • 61
  • 84
34
votes
15 answers

Is there a programming language with built-in state machine construct?

I am just curious if there is a programming language which has state machines (similar to boost::statechart) as primary language construct. Analogies - c# has delegates where java uses the observer pattern and C has callbacks. Perl and python have…
danatel
  • 4,844
  • 11
  • 48
  • 62
34
votes
10 answers

JavaScript Event State Machine

Does anybody know of any javascript implementations of a state machine? My goal is to setup a state machine implementation that would bind events to state transitions. So, if a user clicks on a button then the state will be changed, and this state…
jab
  • 5,673
  • 9
  • 53
  • 84
32
votes
1 answer

Comparison between Stateless (on google code) and Windows Workflow

I'm starting to think that I should ditch Windows WF in favor of something simpler. I don't necessarily need to pause workflow execution for extended periods of time and restore them later. I would like a simple state machine framework that does…
Dave
  • 14,618
  • 13
  • 91
  • 145
32
votes
3 answers

Algorithm for implementing C# yield statement

I'd love to figure it out myself but I was wondering roughly what's the algorithm for converting a function with yield statements into a state machine for an enumerator? For example how does C# turn this: IEnumerator
Mark Cidade
  • 98,437
  • 31
  • 224
  • 236
31
votes
4 answers

What is the difference between state machine and workflow?

I want to learn what is the difference between "State machine" and "workflow", and how is it any different from "State machine workflow"?
31
votes
5 answers

Is there a difference between a "finite state machine" and a "state machine"?

I'm not sure I understand if there is a difference between a finite state machine and a state machine? Am I thinking about this too hard?
Carson
  • 17,073
  • 19
  • 66
  • 87
31
votes
5 answers

How to Make a Basic Finite State Machine in Objective-C

I am attempting to build an FSM to control a timer in (iphone sdk) objective c. I felt it was a necessary step, because I was otherwise ending up with nasty spaghetti code containing pages of if-then statements. The complexity, non-readability, and…
mwt
  • 523
  • 1
  • 5
  • 11
30
votes
4 answers

How to implement a RESTful resource for a state machine or finite automata

I'm a Rails and REST newbie and I'm trying to figure how best to expose a resource that is backed by a domain object that has a state machine (in other words is a finite automata). I've seen a number of gems for making a model class a state…
Galex
  • 303
  • 3
  • 5
27
votes
4 answers

implementing a state machine using the "yield" keyword

Is it feasible to use the yield keyword to implement a simple state machine as shown here. To me it looks like the C# compiler has done the hard work for you as it internally implements a state machine to make the yield statement work. Can you…
Matt Warren
  • 10,279
  • 7
  • 48
  • 63
25
votes
7 answers

State Machine Frameworks for .NET

We have a system at my work that is basically a message-driven state machine. It takes in a variety of types of messages, looks up some context/state based on the message, then decides what to do, based on the message and the current state. …
Andy White
  • 86,444
  • 48
  • 176
  • 211
23
votes
6 answers

Short example of regular expression converted to a state machine?

In the Stack Overflow podcast #36 (https://blog.stackoverflow.com/2009/01/podcast-36/), this opinion was expressed: Once you understand how easy it is to set up a state machine, you’ll never try to use a regular expression inappropriately ever…
slothbear
  • 2,016
  • 3
  • 21
  • 38