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
23
votes
6 answers

Mealy v/s. Moore

What is the difference between Mealy & Moore type of finite state machines?
user191776
22
votes
6 answers

Design Pattern problem involving N states and transitions between them

I have a problem at hand and I am not getting which design pattern to use. The problem goes as such: I have to build a system which has 'N' states and my system has to do a transition from any state to any other state depending on some conditions.…
Amit
  • 435
  • 2
  • 8
  • 16
21
votes
9 answers

What are some strategies for testing large state machines?

I inherited a large and fairly complex state machine. It has 31 possible states, all are really needed (big business process). It has the following inputs: Enum: Current State (so 0 -> 30) Enum: source (currently only 2 entries) Boolean:…
Pondidum
  • 11,457
  • 8
  • 50
  • 69
21
votes
6 answers

Validation before persistance on state_machine gem

What is the correct syntax for performing a validation on before a transition in the state_machine gem? I've tried the following, before_transition :apple => :orange do validate :validate_core end def validate_core if core.things.blank? …
James McMahon
  • 48,506
  • 64
  • 207
  • 283
20
votes
1 answer

Basic State Machine setup using Stateless

I have some fairly simple state needs (for now). I think I would like to model these using the Stateless api. (But I don't really know much about state machines, so I could be wrong.) But I am getting caught up in the terminology (Specifically…
Vaccano
  • 78,325
  • 149
  • 468
  • 850
20
votes
8 answers

PHP state machine framework

I doubt that is there any state machine framework like https://github.com/pluginaweek/state_machine for PHP. I've had to define many if-else logical clauses, and I would like something to help make it more fun by just defining: Condition required…
noomz
  • 1,955
  • 4
  • 18
  • 20
20
votes
5 answers

Passing variables to Rails StateMachine gem transitions

Is it possible to send variables in the the transition? i.e. @car.crash!(:crashed_by => current_user) I have callbacks in my model but I need to send them the user who instigated the transition after_crash do |car, transition| # Log the car…
Kevin Monk
  • 1,434
  • 1
  • 16
  • 23
20
votes
5 answers

.NET Workflow Engine Suggestions

I came across stateless, a hierarchical state machine framework based on Simple State Machine for Boo, but configured using C# 3.0 It is easy to configure and use, I will probably use it soon. But I was just wondering if Anyone used stateless for…
Leyu
  • 2,687
  • 2
  • 23
  • 27
20
votes
2 answers

How to write state machines with c#?

I need to write state machines that run fast in c#. I like the Windows Workflow Foundation library, but it's too slow and over crowded with features (i.e. heavy). I need something faster, ideally with a graphical utility to design the diagrams, and…
Nestor
  • 13,706
  • 11
  • 78
  • 119
19
votes
3 answers

Finite State Machine Pattern - The One True Pattern?

Could all Code ever written be improved by applying the State Machine Pattern? I was working on a project that was a mass of horrendous awful, buggy, broken spaghetti code. I copied Martin Fowler's example State Machine code from this blog and…
hooleyhoop
  • 9,128
  • 5
  • 37
  • 58
19
votes
3 answers

What is a Finite State Machine and What is it Used For?

Recently, I've begun doing some research into Finite State Machines in JavaScript and I even found a library that makes them easier to implement. While I think I've grasped the idea that a state machine is used for tracking and changing the "state"…
Levi Hackwith
  • 9,232
  • 18
  • 64
  • 115
19
votes
2 answers

Finite automaton in Haskell

What is a good way to represent finite automaton in Haskell? How would the data type of it look like? In our college, automata were defined as a 5-tuple (Q, X, delta, q_0, F) where Q is the set of automaton's states, X is the alphabet (is this…
mathemage
  • 356
  • 3
  • 11
17
votes
1 answer

What is the purpose of IAsyncStateMachine.SetStateMachine?

Interface IAsyncStateMachine can be used only by compiler, and is used in generating state machine for async methods. Interface has SetMachineState - configures the state machine with a heap-allocated replica(from msdn). I used ILSpy to decompile…
Vasyl Senko
  • 1,779
  • 20
  • 33
17
votes
4 answers

Rails: How to test state_machine?

Please, help me. I'm confused. I know how to write state-driven behavior of model, but I don't know what should I write in specs... My model.rb file look class Ratification < ActiveRecord::Base belongs_to :user attr_protected :status_events …
petRUShka
  • 9,812
  • 12
  • 61
  • 95
17
votes
10 answers

State machines in C

What is the best way to write a state machine in C? I usually write a big switch-case statement in a for(;;), with callbacks to re-enter the state machine when an external operation is finished. Do you know a more efficient way?
Maurizio Reginelli
  • 3,152
  • 2
  • 27
  • 41