Machine::State - State::Machine Implementation à la Moose
version 0.05
use Machine::State; use Machine::State::State; use Machine::State::Transition; # light-switch circular-state example my $is_on = Machine::State::State->new(name => 'is_on'); my $is_off = Machine::State::State->new(name => 'is_off'); my $turn_on = Machine::State::Transition->new( name => 'turn_on', result => $is_on ); my $turn_off = Machine::State::Transition->new( name => 'turn_off', result => $is_off ); $is_on->add_transition($turn_off); # on -> turn off $is_off->add_transition($turn_on); # off -> turn on my $lightswitch = Machine::State->new( topic => 'typical light switch', state => $is_off ); $lightswitch->apply('turn_on'); $lightswitch->status; # is_on
A finite-state machine (FSM) or finite-state automaton (plural: automata), or simply a state machine, is an abstract machine that can be in one of a finite number of states. The machine is in only one state at a time. It can change from one state to another when initiated by a triggering event or condition; this is called a transition. Machine::State is a system for creating state machines and managing their transitions; It is also a great mechanism for enforcing and tracking workflow, especially in distributed computing. This library is a Moose-based implementation of the Machine::State library.
State machines are useful for modeling systems with perform a predetermined sequence of event and result in deterministic state. Machine::State, as you might expect, allows for the definition of events, states, state transitions and user defined actions that can be executed before or after transitions. All features of the state machine itself can be configured via a DSL, Machine::State::Simple. Note: This is an early release available for testing and feedback and as such is subject to change.
my $state = $machine->state; $state = $machine->state(Machine::State::State->new(...));
The current state of the state machine. The value should be a Machine::State::State object.
my $topic = $machine->topic; $topic = $machine->topic('Take over the world');
The topic or purpose of the state machine. The value can be any arbitrary string describing intent.
my $state = $machine->apply('transition_name'); $state = $machine->apply; # apply known next transition
The apply method transitions the state machine from the current state into the resulting state. If the apply method is called without a transition name, the machine will transition into the next known state of the current state.
my $transition_name = $machine->next;
The next method returns the name of the next known transition of the current state if exists, otherwise it will return undefined.
my $state_name = $machine->status;
The status method returns the name of the current state.
Al Newkirk <anewkirk@ana.io>
Сергей Романов <sromanov-dev@yandex.ru>
This software is copyright (c) 2014 by Al Newkirk.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
To install Machine::State, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Machine::State
CPAN shell
perl -MCPAN -e shell install Machine::State
For more information on module installation, please visit the detailed CPAN module installation guide.