| 1 | package FSA::Engine::Transition; | ||
| 2 | # | ||
| 3 | # A transition taking place in FSA::Engine | ||
| 4 | # | ||
| 5 | use Moose; | ||
| 6 | |||
| 7 | has 'test' => ( | ||
| 8 | is => 'rw', | ||
| 9 | required => 1, | ||
| 10 | ); | ||
| 11 | |||
| 12 | has 'action' => ( | ||
| 13 | is => 'rw', | ||
| 14 | ); | ||
| 15 | |||
| 16 | has 'state' => ( | ||
| 17 | is => 'rw', | ||
| 18 | isa => 'Str', | ||
| 19 | required => 1, | ||
| 20 | ); | ||
| 21 | |||
| 22 | |||
| 23 | # Test against an input value | ||
| 24 | # | ||
| 25 | sub do_test { | ||
| 26 | my ($self, $input) = @_; | ||
| 27 | |||
| 28 | if ((ref $self->test) eq 'CODE') { | ||
| 29 | my $ret_val = &{$self->test}($input); | ||
| 30 | return $ret_val; | ||
| 31 | } | ||
| 32 | return $input eq $self->test; | ||
| 33 | } | ||
| 34 | |||
| 35 | 1; | ||
| 36 | |||
| 37 | |||
| 38 | =head1 NAME | ||
| 39 | |||
| 40 | FSA::Engine::Transition - Transition object for FSA::Engine objects. | ||
| 41 | |||
| 42 | =head1 SYNOPSIS | ||
| 43 | |||
| 44 | A Transition object is created as part of the definition of your FSA object. | ||
| 45 | |||
| 46 | sub _build_fsa_transitions { | ||
| 47 | my ($self) = @_; | ||
| 48 | |||
| 49 | my $transitions = { | ||
| 50 | open => { | ||
| 51 | slam_door => FSA::Engine::Transition->new({ | ||
| 52 | test => 'SHOVE DOOR', | ||
| 53 | action => sub {print "The door slams shut with a BANG\n";}, | ||
| 54 | state => 'closed', | ||
| 55 | }), | ||
| 56 | close_door => FSA::Engine::Transition->new({ | ||
| 57 | test => sub {$self->test_door_push(@_)}, | ||
| 58 | action => sub {print "There is a falling 'EEERRRrrrkkk' sound\n";}, | ||
| 59 | state => 'closed', | ||
| 60 | }), | ||
| 61 | }, | ||
| 62 | ... | ||
| 63 | |||
| 64 | =head1 DESCRIPTION | ||
| 65 | |||
| 66 | This class defines a transition for your FSA class which has included the Moose Role | ||
| 67 | L<FSA::Engine> | ||
| 68 | |||
| 69 | It defines the B<test> to carry out to determine if the transition is valid, the | ||
| 70 | B<state> that is to be moved to if the B<test> succeeds and the optional B<action> | ||
| 71 | to perform when the transition is valid. | ||
| 72 | |||
| 73 | =head1 METHODS | ||
| 74 | |||
| 75 | =head2 new | ||
| 76 | |||
| 77 | Create a new instance. Takes the following attributes. | ||
| 78 | |||
| 79 | =head3 test | ||
| 80 | |||
| 81 | The B<test> to carry out to determine if the transition is to be followed. | ||
| 82 | |||
| 83 | If B<test> is a scalar, it is compared with the current B<fsa_input> attribute | ||
| 84 | of your FSA and if they are identical then the test is deemed to have passed. | ||
| 85 | |||
| 86 | Otherwise B<test> should be an anonymous subroutine which is called to determine | ||
| 87 | if the transition should be followed. This subroutine should return B<true> if | ||
| 88 | the test succeeds, otherwise undef. | ||
| 89 | |||
| 90 | =head3 state | ||
| 91 | |||
| 92 | The B<state> is simply the name of the state to move to if the B<test> succeeds. | ||
| 93 | It should be defined as a 'Str' Moose attribute. | ||
| 94 | |||
| 95 | =head3 action | ||
| 96 | |||
| 97 | This should be an anonymous subroutine reference which will be called if the | ||
| 98 | transition is followed. The subroutine will be called after any exit action | ||
| 99 | is called from the state being left and before any entry action of the state | ||
| 100 | being moved to. | ||
| 101 | |||
| 102 | =head2 do_test | ||
| 103 | |||
| 104 | This is the method called to determine if the test succeeds. It takes one parameter, | ||
| 105 | the B<fsa_input> value from your FSA. | ||
| 106 | |||
| 107 | The method looks at the B<test> attribute, if it is a code reference it calls the | ||
| 108 | anonymous subroutine passing the B<fsa_input> value as the first parameter. The | ||
| 109 | subroutine should return true or false depending upon whether the test should | ||
| 110 | succeed or fail. | ||
| 111 | |||
| 112 | If it is not a code reference it carries out a comparison of the input value | ||
| 113 | with the B<test> attribute returning the result of that comparison. | ||
| 114 | |||
| 115 | =head1 SUPPORT | ||
| 116 | |||
| 117 | This module is stored on an open TBD GitHub repository. Feel free to fork and contribute. | ||
| 118 | |||
| 119 | Please file bug reports via TBD or by sending email to. | ||
| 120 | |||
| 121 | =head1 AUTHOR | ||
| 122 | |||
| 123 | Ian C. Docherty <pause@iandocherty.com> | ||
| 124 | |||
| 125 | =head1 COPYRIGHT AND LICENSE | ||
| 126 | |||
| 127 | Copyright(c) 2011 Ian C. Docherty | ||
| 128 | |||
| 129 | This module is free software; you can distribute it and/or modify it under the same | ||
| 130 | terms as Perl itself. |