blib/lib/FSM/Simple.pm

filter: Statementstmt 100% 105 / 105 Branchbran 97.7% 43 / 44 Conditioncond 91.6% 11 / 12 MC/DCmcdc 85.7% 6 / 7 Subroutinesub 100% 17 / 17 Podpod 92.3% 12 / 13 totaltotal 97.9% 194 / 198 CC 30 SCAR 14.7
CC
30
Cov
99%
run
7.0
add_trans
7.0
add_state
6.0
CRAP 4.3
↑ Summary
1 package FSM::Simple;
2  
3 # h2xs -XA -n FSM::Simple
4  
5 5 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 #use Data::Dumper;
10  
11 5 require Exporter;
12  
13 5 our @ISA = qw(Exporter);
14  
15 # Items to export into callers namespace by default. Note: do not export
16 # names by default without a very good reason. Use EXPORT_OK instead.
17 # Do not simply export all your public functions/methods/constants.
18  
19 # This allows declaration use FSM::Simple ':all';
20 # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
21 # will save memory.
22 5 our %EXPORT_TAGS = ( 'all' => [ qw(
23
24 ) ] );
25  
26 5 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
27  
28 #our @EXPORT = qw(
29 #
30 #);
31  
32 5 our $VERSION = '0.02';
33  
34  
35 ##########################################################################################
36 ### Public interface.
37 ##########################################################################################
38  
39 7 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
40 7 my ($class, %opts) = @_;
41
42 7 ▶ my $trans_history = (exists $opts{trans_history} and $opts{trans_history} == 1) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $opts{'trans_history'} && $opts{'trans_history'} == 1 ? : 1 6 7
Condition66%2/3
exists $opts{"trans_history"} and $opts{"trans_history"} == 1
!ll&&!rl&&r
601
MC/DC50%1/2
exists $opts{"trans_history"} and $opts{"trans_history"} == 1
exists $opts{"trans_history"} $opts{"trans_history"} == 1
Truth table66%2/3
A and B
A= exists $opts{"trans_history"}B= $opts{"trans_history"} == 1
ABresult
0X0
100
111
43
44 7 my $self = {
45 states => {}, # Defined states
46 init_state => undef, # Name of initial state
47 trans_history => [],
48 trans_history_enabled => $trans_history,
49 };
50 7 bless($self, $class);
51 7 return $self;
52 }
53  
54  
55 20 ▶sub add_state {
Subroutine add_state: called CC=6 SCAR=17.9
Pod documented
56 20 my ($self, %args) = @_;
57 20 my $state_name = $args{name};
58 20 my $sub_ref = $args{sub};
59
60 20 ▶ croak "State name and subroutine reference required" unless defined $state_name and defined $sub_ref;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $state_name and defined $sub_ref 3 17 20
Condition100%3/3
defined $state_name and defined $sub_ref
!ll&&!rl&&r
2117
MC/DC100%2/2
defined $state_name and defined $sub_ref
defined $state_name defined $sub_ref
Truth table100%3/3
A and B
A= defined $state_nameB= defined $sub_ref
ABresult
0X0
100
111
61 17 ▶ croak "'sub' must be subroutine reference" unless ref $sub_ref eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $sub_ref eq "CODE" 1 16 17
62 16 ▶ croak "'name' must be SCALAR" unless ref $state_name eq '';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $state_name eq "" 1 15 16
63
64 15 $self->{states}{$state_name}{sub_to_run} = $sub_ref;
65 15 $self->{states}{$state_name}{counter} = 0;
66  
67 15 ▶ $self->{init_state} = $state_name unless defined $self->{init_state}; # You don't need to know which state was added first.
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"init_state"} 5 10 15
68
69 15 return;
70 }
71  
72  
73 7 ▶sub init_state {
Subroutine init_state: called CC=4 SCAR=13.9
Pod documented
74 7 my ($self, @states) = @_;
75 7 ▶ return $self->{init_state} unless @states;
Branch: 1 branch
BranchTrueFalseTotal
unless @states 2 5 7
76
77 5 my $init_state = shift @states;
78 5 ▶ croak "Wrong initial state name" unless defined $init_state;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $init_state 1 4 5
79 4 ▶ croak "Initial state '$init_state' must be exists in list of states" unless exists $self->{states}{$init_state};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"states"}{$init_state} 1 3 4
80
81 3 $self->{init_state} = $init_state;
82
83 3 return;
84 }
85  
86 # This transition will be triggered when the subroutine returns this value.
87 15 ▶sub add_trans {
Subroutine add_trans: called CC=7 SCAR=19.5
Pod documented
88 15 my ($self, %args) = @_;
89 15 my $from_state = $args{from};
90 15 my $to_state = $args{to};
91 15 my $expected_value = $args{exp_val};
92
93 15 ▶ croak "'from' required in add_transition" unless defined $from_state;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $from_state 1 14 15
94 14 ▶ croak "'to' required in add_transition" unless defined $to_state;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $to_state 1 13 14
95 13 ▶ croak "'exp_val' required in add_transition" unless defined $expected_value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $expected_value 1 12 13
96
97 12 ▶ croak "A name of start state '$from_state' must be defined" unless exists $self->{states}{$from_state};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"states"}{$from_state} 1 11 12
98 11 ▶ croak "A name of target state '$to_state' must be defined" unless exists $self->{states}{$to_state};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"states"}{$to_state} 1 10 11
99 10 ▶ croak "A value must be a SCALAR" if ref $expected_value ne '';
Branch: 1 branch
BranchTrueFalseTotal
if ref $expected_value ne "" 1 9 10
100  
101 9 $self->{states}{$from_state}{transitions}{$expected_value} = $to_state;
102
103 9 return;
104 }
105  
106  
107 7 ▶sub run {
Subroutine run: called CC=7 SCAR=19.5
Pod documented
108 7 my ($self) = @_;
109 7 ▶ return unless defined $self->{init_state};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"init_state"} 1 6 7
110
111 6 my $current_state = $self->{init_state}; # for convenience.
112 6 my $rh_inout = {};
113
114 6 while(1) {
115 20 ▶ croak "Undefined state $current_state" unless exists $self->{states}{$current_state};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"states"}{$current_state} 0 20 20
116
117 20 $rh_inout = $self->{states}{$current_state}{sub_to_run}($rh_inout);
118 20 $self->{states}{$current_state}{counter}++;
119 20 ▶ push @{ $self->{trans_history} }, $current_state if $self->{trans_history_enabled};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"trans_history_enabled"} 7 13 20
120
121 20 ▶ my $result = $rh_inout->{returned_value} // last;
Condition100%2/2
$rh_inout->{"returned_value"} // (last)
l!l
164
MC/DC100%1/1
$rh_inout->{"returned_value"} // (last)
$rh_inout->{"returned_value"}
Truth table100%2/2
A
A= $rh_inout->{"returned_value"}
Aresult
11
00
122 16 ▶ croak "Returned value must be a SCALAR" if ref $result ne '';
Branch: 1 branch
BranchTrueFalseTotal
if ref $result ne "" 1 15 16
123 15 ▶ croak "Next state is not defined for value '$result'" unless exists $self->{states}{$current_state}{transitions}{$result};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"states"}{$current_state}{"transitions"}{$result} 1 14 15
124
125 14 $current_state = $self->{states}{$current_state}{transitions}{$result};
126 }
127
128 4 return;
129 }
130  
131  
132 ##########################################################################################
133 ### Additional methods.
134 ##########################################################################################
135  
136  
137 6 ▶sub trans_history {
Subroutine trans_history: called CC=1 SCAR=0.0
Pod documented
138 6 my ($self) = @_;
139 6 return $self->{trans_history};
140 }
141  
142  
143 2 ▶sub trans_stats {
Subroutine trans_stats: called CC=2 SCAR=6.9
Pod documented
144 2 my ($self) = @_;
145
146 2 my $rh_states_stats = {};
147 2 ▶ while (my ($state, $rh_value) = each %{ $self->{states} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self{"states"};}) 10 2 12
148 10 $rh_states_stats->{$state} = $rh_value->{counter};
149 }
150  
151 2 return $rh_states_stats;
152 }
153  
154  
155 5 ▶sub trans_array {
Subroutine trans_array: called CC=3 SCAR=11.0
Pod documented
156 5 my ($self) = @_;
157  
158 5 my %transitions;
159 5 ▶ while (my ($state, $rh_value) = each %{ $self->{states} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self{"states"};}) 20 5 25
160 20 ▶ while (my ($returned_val, $target_state) = each %{ $rh_value->{transitions} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$rh_value{"transitions"};}) 24 20 44
161 24 $transitions{"${state}->${target_state}"} = {from => $state, to => $target_state, returned_value => $returned_val};
162 }
163 }
164  
165 5 return values %transitions;
166 }
167  
168  
169 2 ▶sub clear_trans_history {
Subroutine clear_trans_history: called CC=1 SCAR=0.0
Pod undocumented
170 2 my ($self) = @_;
171
172 2 $self->{trans_history} = [];
173
174 2 return;
175 }
176  
177  
178 2 ▶sub clear_trans_stats {
Subroutine clear_trans_stats: called CC=2 SCAR=6.9
Pod documented
179 2 my ($self) = @_;
180
181 2 foreach my $rh_state_desc ( values %{ $self->{states} }) {
182 10 $rh_state_desc->{counter} = 0;
183 }
184
185 2 return;
186 }
187  
188  
189 1 ▶sub trans_history_off {
Subroutine trans_history_off: called CC=1 SCAR=0.0
Pod documented
190 1 my ($self) = @_;
191
192 1 $self->{trans_history_enabled} = 0;
193
194 1 return;
195 }
196  
197  
198 1 ▶sub trans_history_on {
Subroutine trans_history_on: called CC=1 SCAR=0.0
Pod documented
199 1 my ($self) = @_;
200
201 1 $self->{trans_history_enabled} = 1;
202
203 1 return;
204 }
205  
206  
207 3 ▶sub generate_graphviz_code {
Subroutine generate_graphviz_code: called CC=4 SCAR=13.9
Pod documented
208 3 my ($self, %args) = @_;
209 3 ▶ my $graph_name = $args{name} || 'finite_state_machine';
Condition100%2/2
$args{'name'} || 'finite_state_machine'
l!l
21
MC/DC100%1/1
$args{'name'} || 'finite_state_machine'
$args{'name'}
Truth table100%2/2
A
A= $args{'name'}
Aresult
11
00
210 3 ▶ my $size = $args{size} || '8';
Condition100%2/2
$args{'size'} || '8'
l!l
12
MC/DC100%1/1
$args{'size'} || '8'
$args{'size'}
Truth table100%2/2
A
A= $args{'size'}
Aresult
11
00
211
212 3 my $transitions = '';
213 3 foreach my $rh_trans ($self->trans_array) {
214 15 $transitions .= sprintf " %s -> %s [ label = \"%s\" ];\n", $rh_trans->{from}, $rh_trans->{to}, $rh_trans->{returned_value};
215 }
216
217 3 my $graphviz_code = qq|
218 digraph $graph_name {
219 rankdir=LR;
220 size="$size";
221 node [shape = doublecircle]; $self->{init_state};
222 node [shape = circle];
223 $transitions
224 // Install graphviz if required
225 // Write this code to (e.g.) fsm.dot and run:
226 // dot -T png fsm.dot -o fsm.png
227 }
228 |;
229  
230 3 return $graphviz_code;
231 }
232  
233  
234 5 1;
235 __END__
↑ Summary