File Coverage

blib/lib/Data/FSM.pm
Criterion Covered Total %
statement 24 24 100.0
branch n/a
condition n/a
subroutine 7 7 100.0
pod 0 1 0.0
total 31 32 96.8


line stmt bran cond sub pod time code
1             package Data::FSM;
2              
3 6     6   200398 use strict;
  6         11  
  6         215  
4 6     6   30 use warnings;
  6         60  
  6         355  
5              
6 6     6   2908 use Data::FSM::Utils qw(check_transition_objects);
  6         24  
  6         151  
7 6     6   3555 use Mo qw(build default is);
  6         4305  
  6         39  
8 6     6   15945 use Mo::utils::Array qw(check_array_object);
  6         14  
  6         346  
9 6     6   3038 use Mo::utils::Number qw(check_positive_natural);
  6         20649  
  6         171  
10              
11             our $VERSION = 0.01;
12              
13             has id => (
14             is => 'ro',
15             );
16              
17             has states => (
18             default => [],
19             is => 'ro',
20             );
21              
22             has transitions => (
23             default => [],
24             is => 'ro',
25             );
26              
27             sub BUILD {
28 17     17 0 1474841 my $self = shift;
29              
30             # Check 'id'.
31 17         94 check_positive_natural($self, 'id');
32              
33             # Check 'states'.
34 14         295 check_array_object($self, 'states', 'Data::FSM::State');
35              
36             # Check 'transitions'.
37 12         366 check_array_object($self, 'transitions', 'Data::FSM::Transition');
38 10         197 check_transition_objects($self, 'transitions', $self->states);
39              
40 8         24 return;
41             }
42              
43             1;
44              
45             __END__
46              
47             =pod
48              
49             =encoding utf8
50              
51             =head1 NAME
52              
53             Data::FSM - Data object for Finite State Machine.
54              
55             =head1 SYNOPSIS
56              
57             use Data::FSM;
58              
59             my $obj = Data::FSM->new(%params);
60             my $id = $obj->id;
61             my $states_ar = $obj->states;
62             my $transitions_ar = $obj->transitions;
63              
64             =head1 METHODS
65              
66             =head2 C<new>
67              
68             my $obj = Data::FSM->new(%params);
69              
70             Constructor.
71              
72             =over 8
73              
74             =item * C<id>
75              
76             FSM id.
77              
78             It's optional.
79              
80             Default value is undef.
81              
82             =item * C<states>
83              
84             Reference to array with L<Data::FSM::State> instances.
85              
86             Default value is [].
87              
88             =item * C<transitions>
89              
90             Reference to array with L<Data::FSM::Transition> instances.
91             The used states in transition must be a in C<states> values.
92              
93             Default value is [].
94              
95             =back
96              
97             Returns instance of object.
98              
99             =head2 C<id>
100              
101             my $id = $obj->id;
102              
103             Get FSM id.
104              
105             Returns number.
106              
107             =head2 C<states>
108              
109             my $states_ar = $obj->states;
110              
111             Get list of FSM states.
112              
113             Returns reference to array with L<Data::FSM::State> instances.
114              
115             =head2 C<transition>
116              
117             my $transitions_ar = $obj->transitions;
118              
119             Get list of FSM transitions.
120              
121             Returns reference to array with L<Data::FSM::Transition> instances.
122              
123             =head1 ERRORS
124              
125             new():
126             From Data::FSM::Utils::check_transition_objects():
127             Parameter 'transitions' check hasn't defined state objects.
128             Parameter 'transitions' contains object which has 'from' object which isn't in defined objects.
129             Reference: %s
130             Parameter 'transitions' contains object which has 'to' object which isn't in defined objects.
131             Reference: %s
132              
133             From Mo::utils::Array::check_array_object():
134             Parameter 'states' must be a array.
135             Value: %s
136             Reference: %s
137             Parameter 'states' with array must contain 'Data::FSM::State' objects.
138             Value: %s
139             Reference: %s
140             Parameter 'transitions' must be a array.
141             Value: %s
142             Reference: %s
143             Parameter 'transitions' with array must contain 'Data::FSM::Transition' objects.
144             Value: %s
145             Reference: %s
146              
147             From Mo::utils::Number::check_positive_natural():
148             Parameter 'id' must be a positive natural number.
149             Value: %s
150              
151             =head1 EXAMPLE
152              
153             =for comment filename=create_and_print_fsm.pl
154              
155             use strict;
156             use warnings;
157              
158             use Data::FSM;
159             use Data::FSM::State;
160             use Data::FSM::Transition;
161              
162             my $state1 = Data::FSM::State->new(
163             'name' => 'From',
164             );
165             my $state2 = Data::FSM::State->new(
166             'name' => 'To',
167             );
168             my $fsm = Data::FSM->new(
169             'id' => 7,
170             'states' => [
171             $state1,
172             $state2,
173             ],
174             'transitions' => [
175             Data::FSM::Transition->new(
176             'from' => $state1,
177             'to' => $state2,
178             ),
179             ],
180             );
181              
182             # Print out.
183             print 'Id: '.$fsm->id."\n";
184             print "States:\n";
185             foreach my $state (@{$fsm->states}) {
186             print '- '.$state->name."\n";
187             }
188             print "Transitions:\n";
189             foreach my $transition (@{$fsm->transitions}) {
190             print '- '.$transition->from->name.' -> '.$transition->to->name."\n";
191             }
192              
193             # Output:
194             # Id: 7
195             # States:
196             # - From
197             # - To
198             # Transitions:
199             # - From -> To
200              
201             =head1 DEPENDENCIES
202              
203             L<Data::FSM::Utils>,
204             L<Mo>,
205             L<Mo::utils::Array>,
206             L<Mo::utils::Number>.
207              
208             =head1 REPOSITORY
209              
210             L<https://github.com/michal-josef-spacek/Data-FSM>
211              
212             =head1 AUTHOR
213              
214             Michal Josef Špaček L<mailto:skim@cpan.org>
215              
216             L<http://skim.cz>
217              
218             =head1 LICENSE AND COPYRIGHT
219              
220             © 2025-2026 Michal Josef Špaček
221              
222             BSD 2-Clause License
223              
224             =head1 VERSION
225              
226             0.01
227              
228             =cut