StateML/Machine.pm

filter: Statementstmt 34.5% 78 / 226 Branchbran 12.1% 9 / 74 Conditioncond 6.5% 3 / 46 MC/DCmcdc 0.0% 0 / 26 Subroutinesub 41.6% 15 / 36 Podpod 96.1% 25 / 26 totaltotal 29.9% 130 / 434 CC 66 SCAR 42.6
CC
66
Cov
26%
matching_events
210.0
arcs_for_event
182.0
assert_valid
76.3
CRAP 70.5
« StateML/SAXHandler.pm ↑ Summary StateML/Object.pm »
1 package StateML::Machine;
2  
3 =head1 NAME
4  
5 StateML::Machine - a StateML state machine data structure
6  
7 =head1 DESCRIPTION
8  
9 Contains all events, arcs and states for a state machine.
10  
11 =head1 METHODS
12  
13 =over
14  
15 =cut
16  
17 3 ▶use strict ;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 ▶use Carp ;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 3 ▶use StateML::Action ;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 3 ▶use StateML::Arc ;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 3 ▶use StateML::Class ;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 3 ▶use StateML::Event ;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 3 ▶use StateML::State ;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 3 ▶use StateML::Utils qw( empty as_str );
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 3 ▶use base qw( StateML::Object ) ;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 #use SelfTest ;
29  
30 =for testing
31 use Test ;
32 use StateML::Machine ;
33 plan tests => 0 ;
34  
35 =item new
36  
37 my $m = StateML::Machine->new ;
38  
39 =for testing
40 my $m = StateML::Machine->new(
41 EVENTS => [1,2],
42 ARCS => [1,2,3],
43 STATES => [1,2,3,4],
44 ) ;
45 ok( ref $m ) ;
46  
47 =cut
48  
49 3 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
50 3 my $proto = shift ;
51  
52 3 my $self = $proto->SUPER::new(
53 ACTIONS => [],
54 ARCS => [],
55 CLASSES => [],
56 EVENTS => [],
57 STATES => [],
58 OBJECTS => {}, ## All objects, indexed on id.
59 MODES => [],
60 ALL_STATE => StateML::State->new( ID => "#ALL", ORDER => -1 ),
61 @_, ## TODO: Error check the args.
62 ) ;
63  
64 ## Note that the #ALL state is not put in STATES.
65 3 my $all = $self->{ALL_STATE} ;
66 3 $all->machine( $self->{ID} ) ;
67 3 $all->_set_number( -1 ) ;
68 3 $all->{PARENT_ID} = $self->{ID} ;
69 3 $self->{OBJECTS}->{"#ALL"} = $all ;
70 3 $self->assert_valid ;
71 3 return $self ;
72 }
73  
74  
75 =item autogenerated_message
76  
77 Sets/gets a suitable warning message that can be placed in a template file.
78  
79 Use only [\w :./!,-] in this warning message and no newlines, tabs, or other
80 control codes.
81  
82 =cut
83  
84 0 ▶sub autogenerated_message {
Subroutine autogenerated_message: not called CC=5 SCAR=34.0
Pod documented
85 0 my $self = shift ;
86 0 ▶ if ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
87 0 my ( $message ) = @_ ;
88 0 ▶Carp::confess unless defined $message;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $message 0 0 0
89 0 ▶ if ( $message =~ /([^\w\t :.\\\/!,-])/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($message =~ m[([^\w\t :.\\/!,-])]) 0 0 0
90 0 croak "Illegal characters ('$1') in message '$message'\n"
91 }
92 0 $self->{AUTOGENERATED_MESSAGE} = $message ;
93 }
94 0 ▶ return $self->{AUTOGENERATED_MESSAGE} || "AUTOGENERATED, DO NOT EDIT!!" ;
Condition0%0/2
$self->{'AUTOGENERATED_MESSAGE'} || 'AUTOGENERATED, DO NOT EDIT!!'
l!l
00
MC/DC0%0/1
$self->{'AUTOGENERATED_MESSAGE'} || 'AUTOGENERATED, DO NOT EDIT!!'
$self->{'AUTOGENERATED_MESSAGE'}
Truth table0%0/2
A
A= $self->{'AUTOGENERATED_MESSAGE'}
Aresult
11
00
95 }
96  
97
98  
99  
100 0 ▶sub _number_states {
Subroutine _number_states: not called CC=2 SCAR=17.9
101 ## We do this lazily so that changes to a state's id or to the
102 ## list of states are always reflected in the numbers. That might
103 ## be overdesign, time will tell.
104 0 my $self = shift ;
105 0 my $i = 0 ;
106  
107 ## Number states startign at 1 so that 0 is left available for
108 ## initting or "unknown". #ALL is always -1.
109 $_->_set_number( ++$i )
110 0 for @{$self->{STATES}} ;
111 }
112  
113  
114 =item modes
115  
116 Set/get the list of modes that will be used to control what portions of
117 the document get parsed. This is used to conditionally control
118 inclusion of things like optional states or language-specific APIs.
119  
120 =cut
121  
122 0 ▶sub modes {
Subroutine modes: not called CC=2 SCAR=17.9
Pod documented
123 0 my $self = shift ;
124 0 ▶ $self->{MODES} = [ @_ ] if @_ ;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
125 0 return @{$self->{MODES}};
126 }
127  
128  
129 =item all_state
130  
131 Returns the "#ALL" state.
132  
133 =cut
134  
135 0 ▶sub all_state { return shift()->{ALL_STATE} }
Subroutine all_state: not called CC=1 SCAR=6.9
Pod documented
136  
137 =item states
138  
139 Returns a list of all states other than state #ALL.
140  
141 =for testing
142 ok( scalar $m->states, 4, "number of states" ) ;
143  
144 =cut
145  
146 0 ▶sub states {
Subroutine states: not called CC=1 SCAR=6.9
Pod documented
147 0 my $self = shift ;
148 0 $self->_number_states ;
149 return sort {
150 0 $a->number <=> $b->number
151 0 } @{$self->{STATES}} ;
152 }
153  
154  
155 =item raw_states
156  
157 Returns a list of all states including #ALL.
158  
159 =for testing
160 ok( scalar $m->states, 4, "number of states" ) ;
161  
162 =cut
163  
164 0 ▶sub raw_states {
Subroutine raw_states: not called CC=1 SCAR=6.9
Pod documented
165 0 my $self = shift ;
166 0 $self->_number_states ;
167 return sort {
168 0 $a->number <=> $b->number
169 0 } @{$self->{STATES}}, $self->{ALL_STATE} ;
170 }
171  
172  
173 =item description
174  
175 Sets or gets a textual description of the machine
176  
177 =cut
178  
179 0 ▶sub description {
Subroutine description: not called CC=2 SCAR=17.9
Pod documented
180 0 my $self = shift ;
181 0 ▶ $self->{DESCRIPTION} = shift if @_ ;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
182 0 return $self->{DESCRIPTION};
183 }
184  
185  
186 =item actions
187  
188 Returns a list of all actions.
189  
190 =for testing
191 ok( scalar $m->actions, 2, "number of actions" ) ;
192  
193 =cut
194  
195 0 ▶sub actions {
Subroutine actions: not called CC=1 SCAR=6.9
Pod documented
196 0 my $self = shift ;
197 0 return @{$self->{ACTIONS}} ;
198 }
199  
200  
201 =item classes
202  
203 Returns a list of all classes
204  
205 =for testing
206 ok( scalar $m->classes, 2, "number of classes" ) ;
207  
208 =cut
209  
210 0 ▶sub classes {
Subroutine classes: not called CC=1 SCAR=6.9
Pod documented
211 0 my $self = shift ;
212 0 return @{$self->{CLASSES}} ;
213 }
214  
215  
216 =item events
217  
218 Returns a list of all events.
219  
220 =for testing
221 ok( scalar $m->events, 2, "number of events" ) ;
222  
223 =cut
224  
225 0 ▶sub events {
Subroutine events: not called CC=1 SCAR=6.9
Pod documented
226 0 my $self = shift ;
227 0 return @{$self->{EVENTS}} ;
228 }
229  
230  
231 =item arcs
232  
233 Returns a list of all arcs.
234  
235 =for testing
236 ok( scalar $m->arcs, 3, "number of arcs" ) ;
237  
238 =cut
239  
240 2 ▶sub arcs {
Subroutine arcs: called CC=1 SCAR=0.0
Pod documented
241 2 my $self = shift ;
242 2 return @{$self->{ARCS}} ;
243 }
244  
245  
246 =item preamble
247  
248 Returns the preamble code.
249  
250 =cut
251  
252 0 ▶sub preamble {
Subroutine preamble: not called CC=1 SCAR=6.9
Pod documented
253 0 my $self = shift ;
254 0 return $self->{PREAMBLE}->[0] ;
255 }
256  
257  
258 =item postamble
259  
260 Returns the postamble code.
261  
262 =cut
263  
264 0 ▶sub postamble {
Subroutine postamble: not called CC=1 SCAR=6.9
Pod documented
265 0 my $self = shift ;
266 0 return $self->{POSTAMBLE}->[0] ;
267 }
268  
269  
270 =item object_by_id
271  
272 my $object = $m->object_by_id( $id ) ;
273 my $object = $m->object_by_id( $id, $require_type ) ;
274  
275 Returns the state, event, or arc labelled $id or undef if one isn't found.
276  
277 If present, $required_type is used to make sure that the object requested
278 if of the indicated type.
279  
280 =cut
281  
282 5 ▶sub object_by_id {
Subroutine object_by_id: called CC=5 SCAR=16.3
Pod documented
283 5 my $self = shift ;
284 5 my ( $id, $type ) = @_ ;
285  
286 5 ▶ return undef unless defined $id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id 0 5 5
287  
288 5 my $obj ;
289 5 ▶ if ( exists $self->{OBJECTS}->{$id} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"OBJECTS"}{$id}) 4 1 5
290 4 $obj = $self->{OBJECTS}->{$id} ;
291 4 ▶ die "$id is not a $type"
Branch: 1 branch
BranchTrueFalseTotal
if $type and not $obj->isa($type) 0 4 4
Condition66%2/3
$type and not $obj->isa($type)
!ll&&!rl&&r
310
MC/DC0%0/2
$type and not $obj->isa($type)
$type not $obj->isa($type)
Truth table66%2/3
A and not(B)
A= $typeB= not $obj->isa($type)
ABresult
0X0
100
111
292 if $type && ! $obj->isa( $type ) ;
293 4 return $obj ;
294 }
295 1 return undef ;
296 }
297  
298  
299 =item action_by_id
300  
301 Returns an action given it's id. Dies if $id refers to a non-state.
302  
303 =cut
304  
305 0 ▶sub action_by_id {
Subroutine action_by_id: not called CC=1 SCAR=6.9
Pod documented
306 0 my $self = shift ;
307 0 return $self->object_by_id( shift, "StateML::Action" ) ;
308 }
309  
310  
311 =item class_by_id
312  
313 Returns a class given it's id. Dies if $id refers to a non-class.
314  
315 In general this is not used because inheritance works across
316 all objects.
317  
318 =cut
319  
320 0 ▶sub class_by_id {
Subroutine class_by_id: not called CC=1 SCAR=6.9
Pod documented
321 0 my $self = shift ;
322 0 return $self->object_by_id( shift, "StateML::Class" ) ;
323 }
324  
325  
326 =item event_by_id
327  
328 Returns a event given it's id. Dies if $id refers to a non-event.
329  
330 =cut
331  
332 2 ▶sub event_by_id {
Subroutine event_by_id: called CC=1 SCAR=0.0
Pod documented
333 2 my $self = shift ;
334 2 return $self->object_by_id( shift, "StateML::Event" ) ;
335 }
336  
337  
338 =item state_by_id
339  
340 Returns a state given it's id. Dies if $id refers to a non-state.
341  
342 =cut
343  
344 0 ▶sub state_by_id {
Subroutine state_by_id: not called CC=1 SCAR=6.9
Pod documented
345 0 my $self = shift ;
346 0 return $self->object_by_id( shift, "StateML::State" ) ;
347 }
348  
349  
350 =item add
351  
352 $m->add( $arc ) ;
353 $m->add( $class ) ;
354 $m->add( $event ) ;
355 $m->add( $state ) ;
356  
357 =cut
358  
359 10 ▶sub add {
Subroutine add: called CC=6 SCAR=22.0
Pod documented
360 10 my $self = shift ;
361 10 for ( @_ ) {
362 10 my $id = $_->id ;
363 10 ▶ if ( exists $self->{OBJECTS}->{$id} || $id eq $self->{ID} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"OBJECTS"}{$id} or $id eq $self->{"ID"}) 0 10 10
Condition33%1/3
exists $self->{"OBJECTS"}{$id} or $id eq $self->{"ID"}
l!l&&r!l&&!r
0010
MC/DC0%0/2
exists $self->{"OBJECTS"}{$id} or $id eq $self->{"ID"}
exists $self->{"OBJECTS"}{$id} $id eq $self->{"ID"}
Truth table33%1/3
A or B
A= exists $self->{"OBJECTS"}{$id}B= $id eq $self->{"ID"}
ABresult
1X1
011
000
364 0 my $new_type = ref $_ ;
365 0 my $old_type = ref $self->{OBJECTS}->{$id} ;
366 0 $old_type =~ s/^StateML::// ;
367 0 $new_type =~ s/^StateML::// ;
368 0 ▶ $new_type = $old_type eq $new_type ? "" : " (held by $new_type)" ;
Branch: 1 branch
BranchTrueFalseTotal
$old_type eq $new_type ? : 0 0 0
369 0 croak "Can't add $old_type with duplicate ID '$id'$new_type.\n"
370 }
371 10 $_->machine( $self ) ;
372 10 $self->{OBJECTS}->{$_->id} = $_ ;
373 10 my $t = $_->type;
374 10 ▶ my $type = $t eq "CLASS" ? "${t}ES": "${t}S" ;
Branch: 1 branch
BranchTrueFalseTotal
$t eq 'CLASS' ? : 2 8 10
375 10 push @{$self->{$type}}, $_ ;
376 }
377 }
378  
379  
380 =item extract_output_machine
381  
382 my $om = $m->extract_output_machine( \@types ) ;
383  
384 Returns an output machine comprised of the events, arcs, and states
385 that match the \@types specified.
386  
387 =cut
388  
389 0 ▶sub extract_output_machine {
Subroutine extract_output_machine: not called CC=4 SCAR=30.0
Pod documented
390 0 my $self = shift ;
391 0 my $options = {@_} ;
392  
393 0 $options->{raw} = 1 ;
394  
395 0 my @events = $self->matching_events( $options ) ;
396 0 ▶ warn "no events found\n" unless @events ;
Branch: 1 branch
BranchTrueFalseTotal
unless @events 0 0 0
397  
398 0 $self->_number_states ;
399 0 my @arcs = map $self->arcs_for_event( $_, $options ), @events ;
400 0 ▶ warn "no arcs found\n" unless @arcs ;
Branch: 1 branch
BranchTrueFalseTotal
unless @arcs 0 0 0
401  
402 0 my @states = map $self->states_for_arc( $_, $options ), @arcs ;
403 0 ▶ warn "no states found\n" unless @states ;
Branch: 1 branch
BranchTrueFalseTotal
unless @states 0 0 0
404  
405 ## Remove #ALL and duplicate states.
406 0 @states = values %{{ map {
407 0 ( $_ => $_ )
408 } grep $_->id ne "#ALL", @states }} ;
409  
410 0 @states = sort { $a->number <=> $b->number } @states ;
411  
412 my $clone = $self->new(
413 ID => $self->{ID},
414 LOCATION => $self->{LOCATION},
415 ALL_STATE => $self->{ALL_STATE},
416 PREAMBLE => $self->{PREAMBLE},
417 POSTAMBLE => $self->{POSTAMBLE},
418 DESCRIPTION => $self->{DESCRIPTION},
419 ATTRS => $self->{ATTRS},
420 0 MODES => [ @{$self->{MODES}} ],
421 AUTOGENERATED_MESSAGE => $self->{AUTOGENERATED_MESSAGE},
422 0 ) ;
423  
424 0 $clone->add( @events, @states, @arcs, $self->classes, $self->actions ) ;
425  
426 0 return $clone ;
427 }
428  
429 =item matching_events
430  
431 my @events = $m->matching_events( types=>\@types ) ;
432 my @events = $m->matching_events( types=>[ "ui", "io" ] ) ;
433  
434 Gets all events that have type= attributes that match an entry in @types.
435 If no parameters are passed, all events are returned.
436  
437 Events with a type of "#ANY" or "#ALL" (case insensitive) will show up
438 in all filter settings. Passing "all", "any", "#all", or "#any" in the
439 typelist will cause all events to be returned.
440  
441 =cut
442  
443 0 ▶sub matching_events {
Subroutine matching_events: not called CC=14 SCAR=53.5
Pod documented
444 0 my $self = shift ;
445 0 ▶ my $options = @_ && ref $_[-1] eq "HASH" ? pop : {} ;
Branch: 1 branch
BranchTrueFalseTotal
@_ && ref $_[-1] eq 'HASH' ? : 0 0 0
Condition0%0/3
@_ and ref $_[-1] eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
@_ and ref $_[-1] eq "HASH"
@_ ref $_[-1] eq "HASH"
Truth table0%0/3
A and B
A= @_B= ref $_[-1] eq "HASH"
ABresult
0X0
100
111
446  
447 0 my $types = $options->{types} ;
448  
449 0 ▶ return @{$self->{EVENTS}} unless $types && @$types ;
Branch: 1 branch
BranchTrueFalseTotal
unless $types and @$types 0 0 0
Condition0%0/3
$types and @$types
!ll&&!rl&&r
000
MC/DC0%0/2
$types and @$types
$types @$types
Truth table0%0/3
A and B
A= $typesB= @$types
ABresult
0X0
100
111
450  
451 0 my %events ;
452 my @specs ;
453 0 my @not_specs ;
454  
455 0 for ( @$types ) {
456 0 ▶ if ( substr( $_, 0, 1 ) eq "!" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($_, 0, 1) eq '!') { } 0 0 0
457 0 push @not_specs, uc substr $_, 1 ;
458 }
459 else {
460 0 push @specs, uc $_ ;
461 }
462 }
463  
464 0 for ( @{$self->{EVENTS}} ) {
465 0 my $type_re = qr/^($_->{TYPE})$/i ;
466 0 ▶ $events{$_} = $_
Branch: 1 branch
BranchTrueFalseTotal
if "#ALL" =~ /$type_re/ or "#ANY" =~ /$type_re/ or !@specs && @not_specs || grep(($_ =~ /$type_re/), @specs) and not grep(($_ =~ /$type_re/), @not_specs) 0 0 0
Condition0%0/15
"#ALL" =~ /$type_re/ or "#ANY" =~ /$type_re/ or !@specs && @not_specs || grep(($_ =~ /$type_re/), @specs) and not grep(($_ =~ /$type_re/), @not_specs)
l!l&&r!l&&!r
000
"#ALL" =~ /$type_re/ or "#ANY" =~ /$type_re/
l!l&&r!l&&!r
000
!@specs && @not_specs || grep(($_ =~ /$type_re/), @specs) and not grep(($_ =~ /$type_re/), @not_specs)
!ll&&!rl&&r
000
not @specs and @not_specs or grep(($_ =~ /$type_re/), @specs)
l!l&&r!l&&!r
000
not @specs and @not_specs
!ll&&!rl&&r
000
MC/DC0%0/6
"#ALL" =~ /$type_re/ or "#ANY" =~ /$type_re/ or !@specs && @not_specs || grep(($_ =~ /$type_re/), @specs) and not grep(($_ =~ /$type_re/), @not_specs)
"#ALL" =~ /$type_re/ "#ANY" =~ /$type_re/ not @specs @not_specs grep(($_ =~ /$type_re/), @specs) not grep(($_ =~ /$type_re/), @not_specs)
Truth table0%0/10
A or B or not(C) and D or E and not(F)
A= "#ALL" =~ /$type_re/B= "#ANY" =~ /$type_re/C= not @specsD= @not_specsE= grep(($_ =~ /$type_re/), @specs)F= not grep(($_ =~ /$type_re/), @not_specs)
ABCDEFresult
1XXXXX1
01XXXX1
0011X11
000X111
0010111
000X0X0
00100X0
0011X00
000X100
0010100
467 if "#ALL" =~ $type_re
468 || "#ANY" =~ $type_re
469 || ( ( ! @specs && @not_specs ) || grep $_ =~ $type_re, @specs )
470 && ! grep( $_ =~ $type_re, @not_specs ) ;
471 }
472  
473 0 return values %events ;
474 }
475  
476  
477 =item arcs_for_event
478  
479 my @arcs = $m->arcs_for_event( $event ) ;
480  
481 Returns all arcs in the state machine for event $event.
482  
483 A arc is an edge in the state machine diagram.
484  
485 Unfolds arcs in state #ALL to be for all states.
486  
487 =cut
488  
489 0 ▶sub arcs_for_event {
Subroutine arcs_for_event: not called CC=13 SCAR=52.0
Pod documented
490 0 my $self = shift ;
491 0 ▶ my $options = @_ && ref $_[-1] eq "HASH" ? pop : {} ;
Branch: 1 branch
BranchTrueFalseTotal
@_ && ref $_[-1] eq 'HASH' ? : 0 0 0
Condition0%0/3
@_ and ref $_[-1] eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
@_ and ref $_[-1] eq "HASH"
@_ ref $_[-1] eq "HASH"
Truth table0%0/3
A and B
A= @_B= ref $_[-1] eq "HASH"
ABresult
0X0
100
111
492 0 my ( $event ) = @_ ;
493  
494 0 my %arcs ;
495 my @arcs_for_all ;
496  
497 ## First, get all explicit ARCs, then inherit ARCs from #ALL if no
498 ## explicit ARCS.
499 0 my $uc_event_id = uc $event->id ;
500 0 for my $arc ( $self->arcs ) {
501 0 ▶ next unless uc $arc->event_id eq $uc_event_id ;
Branch: 1 branch
BranchTrueFalseTotal
unless uc $arc->event_id eq $uc_event_id 0 0 0
502 0 ▶ if ( uc $arc->from eq "#ALL" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (uc $arc->from eq "#ALL") 0 0 0
503 0 push @arcs_for_all, $arc ;
504 }
505  
506 0 ▶ $arcs{uc $arc->from . ($arc->guard || "" )} = $arc ;
Condition0%0/2
$arc->guard || ''
l!l
00
MC/DC0%0/1
$arc->guard || ''
$arc->guard
Truth table0%0/2
A
A= $arc->guard
Aresult
11
00
507 }
508  
509 0 ▶ if ( exists $arcs{"#ALL"} && ! $options->{raw} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $arcs{"#ALL"} and not $options->{"raw"}) 0 0 0
Condition0%0/3
exists $arcs{"#ALL"} and not $options->{"raw"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $arcs{"#ALL"} and not $options->{"raw"}
exists $arcs{"#ALL"} not $options->{"raw"}
Truth table0%0/3
A and not(B)
A= exists $arcs{"#ALL"}B= not $options->{"raw"}
ABresult
0X0
100
111
510 0 delete $arcs{"#ALL"} ;
511 0 for my $arc ( @arcs_for_all ) {
512 0 for my $from_state ( $self->states ) {
513 ## #ALL arcs don't replace explicit arcs. TODO: We may add
514 ## a merge_with_overrides = "before" or "after" attr on #ALL
515 ## arcs to allow handlers from both arcs to be run.
516 0 ▶ next if exists $arcs{uc $from_state->id} ;
Branch: 1 branch
BranchTrueFalseTotal
if exists $arcs{uc $from_state->id} 0 0 0
517 0 ▶ $arcs{uc $from_state->id} = StateML::Arc->new(
Branch: 1 branch
BranchTrueFalseTotal
uc $arc->to eq '#ALL' ? : 0 0 0
518 %$arc,
519 ID => $arc->id . "_" . $from_state->id,
520 FROM => $from_state->id,
521 TO => uc $arc->to eq "#ALL"
522 ? $from_state->id
523 : $arc->to,
524 DESCRIPTION => $arc->description,
525 ) ;
526 }
527 }
528 }
529  
530 ## Return results in a stable order, one that agrees with the state enum
531 ## and which perhaps is more likely to be easily optimizable by compilers.
532 map warn( $_->id ), grep( ! defined $_->number,
533 0 map( { ( $_->from_state, $_->to_state ) } values %arcs ) ) ;
534  
535 my @arcs = sort {
536 0 $a->from_state->number <=> $b->from_state->number
537 } values %arcs ;
538  
539 0 return @arcs ;
540 }
541  
542  
543 =item all_state_arcs_for_event
544  
545 my @arcs = $m->all_state_arcs_for_event( $event ) ;
546  
547 Returns all arcs in the state machine for event $event.
548  
549 A arc is an edge in the state machine diagram.
550  
551 =cut
552  
553 0 ▶sub all_state_arc_for_event {
Subroutine all_state_arc_for_event: not called CC=6 SCAR=37.4
Pod undocumented
554 0 my $self = shift ;
555 0 ▶ my $options = @_ && ref $_[-1] eq "HASH" ? pop : {} ;
Branch: 1 branch
BranchTrueFalseTotal
@_ && ref $_[-1] eq 'HASH' ? : 0 0 0
Condition0%0/3
@_ and ref $_[-1] eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
@_ and ref $_[-1] eq "HASH"
@_ ref $_[-1] eq "HASH"
Truth table0%0/3
A and B
A= @_B= ref $_[-1] eq "HASH"
ABresult
0X0
100
111
556 0 my ( $event ) = @_ ;
557  
558 0 my $uc_event_id = uc $event->id ;
559 ## Note that there can be only one arc for a given even in the #ALL state.
560 0 for my $arc ( $self->arcs ) {
561 0 ▶ next unless uc $arc->event_id eq $uc_event_id
Branch: 1 branch
BranchTrueFalseTotal
unless uc $arc->event_id eq $uc_event_id and uc $arc->from eq "#ALL" 0 0 0
Condition0%0/3
uc $arc->event_id eq $uc_event_id and uc $arc->from eq "#ALL"
!ll&&!rl&&r
000
MC/DC0%0/2
uc $arc->event_id eq $uc_event_id and uc $arc->from eq "#ALL"
uc $arc->event_id eq $uc_event_id uc $arc->from eq "#ALL"
Truth table0%0/3
A and B
A= uc $arc->event_id eq $uc_event_idB= uc $arc->from eq "#ALL"
ABresult
0X0
100
111
562 && uc $arc->from eq "#ALL" ;
563 0 return $arc ;
564 }
565 0 return undef ;
566 }
567  
568  
569 =item states_by_id
570  
571 my %states_by_id = $m->states_by_id ;
572  
573 Returns a HASH ref of all states indexed by their id= attributes.
574  
575 =cut
576  
577 0 ▶sub states_by_id {
Subroutine states_by_id: not called CC=1 SCAR=6.9
Pod documented
578 0 my $self = shift ;
579  
580 0 $self->_number_states ;
581  
582 return {
583 0 map { ( $_->{ID} => $_ ) } @{$self->{STATES}}
584 } ;
585  
586 }
587  
588  
589 =item states_for_arc
590  
591 my @states = $m->states_for_arc( $arc ) ;
592  
593 Returns all states that appear as starting or ending points for $arc
594 other than the "#ALL" state. Will only return one state for loopbacks.
595  
596 =cut
597  
598 0 ▶sub states_for_arc {
Subroutine states_for_arc: not called CC=3 SCAR=24.8
Pod documented
599 0 my $self = shift ;
600 0 ▶ my $options = @_ && ref $_[-1] eq "HASH" ? pop : {} ;
Branch: 1 branch
BranchTrueFalseTotal
@_ && ref $_[-1] eq 'HASH' ? : 0 0 0
Condition0%0/3
@_ and ref $_[-1] eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
@_ and ref $_[-1] eq "HASH"
@_ ref $_[-1] eq "HASH"
Truth table0%0/3
A and B
A= @_B= ref $_[-1] eq "HASH"
ABresult
0X0
100
111
601 0 my ( $arc ) = @_ ;
602  
603 0 my %states ;
604  
605 0 $states{$arc->from} = $arc->from_state ;
606 0 $states{$arc->to} = $arc->to_state ;
607  
608 0 return values %states ;
609 }
610  
611  
612 =item assert_valid
613  
614 $m->assert_valid ;
615  
616 Dies if there are dangling references. The error message contains all
617 undefined states, events, etc.
618  
619 =cut
620  
621 3 ▶sub assert_valid {
Subroutine assert_valid: called CC=15 SCAR=43.3
Pod documented
622 3 my $self = shift ;
623  
624 3 my @errors ;
625  
626 my %from_state_via_event ;
627 3 my %states_with_mult_arcs_same_event ;
628  
629 3 for my $arc ( @{$self->{ARCS}} ) {
630 0 my $unique_id = $arc->event_id;
631 0 ▶ $unique_id .= "[" . $arc->guard . "]" if defined $arc->guard;
Branch: 1 branch
BranchTrueFalseTotal
if defined $arc->guard 0 0 0
632  
633 0 $DB::single = 1;
634 0 ▶ if ( empty $arc->from ) {
Branch: 2 branches
BranchTrueFalseTotal
if (empty($arc->from)) { } 0 0 0
elsif (not $self->state_by_id($arc->from)) { } 0 0 0
635 0 push @errors,
636 "no from state (",
637 as_str( $arc->from ),
638 ") in arc$arc->{LOCATION}\n"
639 }
640 elsif ( ! $self->state_by_id( $arc->from ) ) {
641 0 push @errors,
642 "unknown from state ",
643 as_str( $arc->from ),
644 " in arc$arc->{LOCATION}\n";
645 }
646 else {
647 $states_with_mult_arcs_same_event{$arc->from} = $unique_id
648 0 ▶ if $from_state_via_event{$arc->from,$unique_id};
Branch: 1 branch
BranchTrueFalseTotal
if $from_state_via_event{join $;, $arc->from, $unique_id} 0 0 0
649 0 $from_state_via_event{$arc->from,$unique_id} = $arc ;
650 }
651  
652 0 ▶ if ( empty $arc->to ) {
Branch: 2 branches
BranchTrueFalseTotal
if (empty($arc->to)) { } 0 0 0
elsif (not $self->state_by_id($arc->to)) { } 0 0 0
653 0 push @errors,
654 "no to state (", as_str( $arc->to ), ") in arc$arc->{LOCATION}\n";
655 }
656 elsif ( ! $self->state_by_id( $arc->to ) ) {
657 0 push @errors,
658 "unknown to state ",
659 as_str( $arc->to ),
660 " in arc$arc->{LOCATION}\n";
661 }
662  
663 0 ▶ if ( empty $arc->event_id ) {
Branch: 2 branches
BranchTrueFalseTotal
if (empty($arc->event_id)) { } 0 0 0
elsif (not $self->event_by_id($arc->event_id)) { } 0 0 0
664 0 push @errors,
665 "no event-id ",
666 as_str( $arc->event_id ),
667 " in arc$arc->{LOCATION}\n"
668 }
669 elsif ( ! $self->event_by_id( $arc->event_id ) ) {
670 0 push @errors,
671 "unknown event-id ",
672 as_str( $arc->event_id ),
673 " in arc$arc->{LOCATION}\n";
674 }
675 }
676  
677 ## TODO: Make this optional.
678 3 for ( sort keys %states_with_mult_arcs_same_event ) {
679 push @errors,
680 "multiple arcs exit from state $_ by event ",
681 0 $states_with_mult_arcs_same_event{$_},
682 "\n" ;
683 }
684  
685 3 my %dup_enum_ids ;
686 {
687 3 my %enum_ids ;
688 3 for ( values %{$self->{OBJECTS}} ) {
689 $dup_enum_ids{$_->enum_id} = $enum_ids{$_->enum_id}
690 3 ▶ if exists $enum_ids{$_->enum_id} ;
Branch: 1 branch
BranchTrueFalseTotal
if exists $enum_ids{$_->enum_id} 0 3 3
691 3 push @{$enum_ids{$_->enum_id}}, $_ ;
692 }
693 }
694  
695 3 for ( keys %dup_enum_ids ) {
696 warn
697 "multiple objects with the enum_id '$_': ",
698 0 join( " ", @{$dup_enum_ids{$_}} ),
699 "\n" ;
700 }
701  
702 3 ▶ die @errors if @errors ;
Branch: 1 branch
BranchTrueFalseTotal
if @errors 0 3 3
703  
704 3 return ;
705 }
706  
707 =back
708  
709 =head1 LIMITATIONS
710  
711 Alpha code. Ok test suite, but we may need to change things in
712 non-backward compatible ways.
713  
714 =head1 COPYRIGHT
715  
716 Copyright 2003, R. Barrie Slaymaker, Jr., All Rights Reserved
717  
718 =head1 LICENSE
719  
720 You may use this module under the terms of the BSD, Artistic, or GPL licenses,
721 any version.
722  
723 =head1 AUTHOR
724  
725 Barrie Slaymaker <barries@slaysys.com>
726  
727 =cut
728  
729  
730 3 1 ;
« StateML/SAXHandler.pm ↑ Summary StateML/Object.pm »