FLAT/FA.pm

filter: Statementstmt 82.0% 192 / 234 Branchbran 73.9% 34 / 46 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 87.8% 36 / 41 Podpod 75.8% 22 / 29 totaltotal 81.4% 289 / 355 CC 50 SCAR 15.3
CC
50
Cov
81%
array_intersect
20.0
_assert_non_states
12.0
successors
8.0
CRAP 4.6
« FLAT/PFA.pm ↑ Summary FLAT/CMD/AcyclicStrings.pm »
1 package FLAT::FA;
2  
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use parent qw(FLAT);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 6 ▶use FLAT::Transition::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 FLAT::FA - Base class for regular finite automata
13  
14 =head1 SYNOPSIS
15  
16 A FLAT::FA object is a collection of states and transitions. Each state
17 may be labeled as starting or accepting. Each transition between states
18 is labeled with a transition object.
19  
20 =head1 USAGE
21  
22 FLAT::FA is a superclass that is not intended to be used directly. However,
23 it does provide the following methods:
24  
25 =cut
26  
27 976 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
28 976 my $pkg = shift;
29 976 bless {
30 STATES => [],
31 TRANS => [],
32 ALPHA => {},
33 ALPHA_BLESSED => {},
34 }, $pkg;
35 }
36  
37 224655 ▶sub get_states {
Subroutine get_states: called CC=1 SCAR=0.0
Pod documented
38 224655 my $self = shift;
39 224655 return 0 .. ( $self->num_states - 1 );
40 }
41  
42 231191 ▶sub num_states {
Subroutine num_states: called CC=1 SCAR=0.0
Pod documented
43 231191 my $self = shift;
44 231191 return scalar @{ $self->{STATES} };
45 }
46  
47 374369 ▶sub is_state {
Subroutine is_state: called CC=1 SCAR=0.0
Pod documented
48 374369 my ( $self, $state ) = @_;
49 374369 exists $self->{STATES}->[$state];
50 }
51  
52 213765 ▶sub _assert_states {
Subroutine _assert_states: called CC=3 SCAR=11.2
53 213765 my ( $self, @states ) = @_;
54 213765 for (@states) {
55 374232 ▶ croak "'$_' is not a state" if not $self->is_state($_);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_state($_) 0 374232 374232
56 }
57 }
58  
59 0 ▶sub _assert_non_states {
Subroutine _assert_non_states: not called CC=3 SCAR=24.8
60 0 my ( $self, @states ) = @_;
61 0 for (@states) {
62 0 ▶ croak "There is already a state called '$_'" if $self->is_state($_);
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_state($_) 0 0 0
63 }
64 }
65  
66 131 ▶sub delete_states {
Subroutine delete_states: called CC=4 SCAR=13.9
Pod documented
67 131 my ( $self, @states ) = @_;
68  
69 131 $self->_assert_states(@states);
70  
71 0 for my $s ( sort { $b <=> $a } @states ) {
72 131 $self->_decr_alphabet($_) for @{ splice @{ $self->{TRANS} }, $s, 1 };
73  
74 131 $self->_decr_alphabet( splice @$_, $s, 1 ) for @{ $self->{TRANS} };
75  
76 131 splice @{ $self->{STATES} }, $s, 1;
77 }
78 }
79  
80 4964 ▶sub add_states {
Subroutine add_states: called CC=4 SCAR=13.9
Pod documented
81 4964 my ( $self, $num ) = @_;
82 4964 my $id = $self->num_states;
83  
84 4964 for my $s ( $id .. ( $id + $num - 1 ) ) {
85 5582 push @$_, undef for @{ $self->{TRANS} };
86 5582 push @{ $self->{TRANS} }, [ (undef) x ( $s + 1 ) ];
87 5582 push @{ $self->{STATES} },
88 {
89 starting => 0,
90 accepting => 0
91 };
92 }
93  
94 return wantarray
95 4964 ▶ ? ( $id .. ( $id + $num - 1 ) )
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 867 4097 4964
96 : $id + $num - 1;
97 }
98  
99 ##############
100  
101 37678 ▶sub is_starting {
Subroutine is_starting: called CC=1 SCAR=0.0
Pod documented
102 37678 my ( $self, $state ) = @_;
103 37678 $self->_assert_states($state);
104 37678 return $self->{STATES}[$state]{starting};
105 }
106  
107 430 ▶sub set_starting {
Subroutine set_starting: called CC=2 SCAR=6.9
Pod documented
108 430 my ( $self, @states ) = @_;
109 430 $self->_assert_states(@states);
110 430 $self->{STATES}[$_]{starting} = 1 for @states;
111 }
112  
113 923 ▶sub unset_starting {
Subroutine unset_starting: called CC=2 SCAR=6.9
Pod documented
114 923 my ( $self, @states ) = @_;
115 923 $self->_assert_states(@states);
116 923 $self->{STATES}[$_]{starting} = 0 for @states;
117 }
118  
119 4151 ▶sub get_starting {
Subroutine get_starting: called CC=1 SCAR=0.0
Pod documented
120 4151 my $self = shift;
121 4151 return grep { $self->is_starting($_) } $self->get_states;
122 }
123  
124 ##############
125  
126 34083 ▶sub is_accepting {
Subroutine is_accepting: called CC=1 SCAR=0.0
Pod documented
127 34083 my ( $self, $state ) = @_;
128 34083 $self->_assert_states($state);
129 34083 return $self->{STATES}[$state]{accepting};
130 }
131  
132 2199 ▶sub set_accepting {
Subroutine set_accepting: called CC=2 SCAR=6.9
Pod documented
133 2199 my ( $self, @states ) = @_;
134 2199 $self->_assert_states(@states);
135 2199 $self->{STATES}[$_]{accepting} = 1 for @states;
136 }
137  
138 929 ▶sub unset_accepting {
Subroutine unset_accepting: called CC=2 SCAR=6.9
Pod documented
139 929 my ( $self, @states ) = @_;
140 929 $self->_assert_states(@states);
141 929 $self->{STATES}[$_]{accepting} = 0 for @states;
142 }
143  
144 2242 ▶sub get_accepting {
Subroutine get_accepting: called CC=1 SCAR=0.0
Pod documented
145 2242 my $self = shift;
146 2242 return grep { $self->is_accepting($_) } $self->get_states;
147 }
148  
149 ###############
150  
151 15316 ▶sub _decr_alphabet {
Subroutine _decr_alphabet: called CC=5 SCAR=16.1
152 15316 my ( $self, $t ) = @_;
153  
154 15316 ▶ return if not defined $t;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $t 9722 5594 15316
155 5594 for ( $t->alphabet ) {
156 10486 ▶ delete $self->{ALPHA}{$_} if not --$self->{ALPHA}{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless --$self->{"ALPHA"}{$_} 470 10016 10486
157  
158 # supposed to delete key when _decrement_count returns 0, so need to test this
159 10486 ▶ delete $self->{ALPHA_BLESSED}{$_} if not $self->{ALPHA_BLESSED}{$_}->_decrement_count;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"ALPHA_BLESSED"}{$_}->_decrement_count 470 10016 10486
160 }
161 }
162  
163 13967 ▶sub _incr_alphabet {
Subroutine _incr_alphabet: called CC=4 SCAR=13.9
164 13967 my ( $self, $t ) = @_;
165  
166 13967 ▶ return if not defined $t;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $t 0 13967 13967
167 13967 for ( $t->alphabet ) {
168 21944 $self->{ALPHA}{$_}++;
169  
170 21944 ▶ if ( !exists( $self->{ALPHA_BLESSED}{$_} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $self->{'ALPHA_BLESSED'}{$_}) { } 2823 19121 21944
171 2823 $self->{ALPHA_BLESSED}{$_} = $self->{ALPHA_CLASS}->new($_);
172 }
173 else {
174 19121 $self->{ALPHA_BLESSED}{$_}->_increment_count;
175 }
176 }
177 }
178  
179 1132 ▶sub set_transition {
Subroutine set_transition: called CC=2 SCAR=7.0
Pod documented
180 1132 my ( $self, $state1, $state2, @label ) = @_;
181 1132 $self->remove_transition( $state1, $state2 );
182  
183 1132 @label = grep defined, @label;
184 1132 ▶ return if not @label;
Branch: 1 branch
BranchTrueFalseTotal
unless @label 0 1132 1132
185  
186 1132 my $t = $self->{TRANS_CLASS}->new(@label);
187 1132 $self->{TRANS}[$state1][$state2] = $t;
188 1132 $self->_incr_alphabet($t);
189 }
190  
191 12835 ▶sub add_transition {
Subroutine add_transition: called CC=3 SCAR=11.0
Pod documented
192 12835 my ( $self, $state1, $state2, @label ) = @_;
193  
194 12835 @label = grep defined, @label;
195 12835 ▶ return if not @label;
Branch: 1 branch
BranchTrueFalseTotal
unless @label 0 12835 12835
196  
197 12835 my $t = $self->get_transition( $state1, $state2 );
198 12835 $self->_decr_alphabet($t);
199  
200 12835 ▶ if ( !$t ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($t) 7924 4911 12835
201 7924 $t = $self->{TRANS}[$state1][$state2] = $self->{TRANS_CLASS}->new;
202 }
203  
204 12835 $t->add(@label);
205 12835 $self->_incr_alphabet($t);
206 }
207  
208 19251 ▶sub get_transition {
Subroutine get_transition: called CC=1 SCAR=0.0
Pod documented
209 19251 my ( $self, $state1, $state2 ) = @_;
210 19251 $self->_assert_states( $state1, $state2 );
211  
212 19251 $self->{TRANS}[$state1][$state2];
213 }
214  
215 1132 ▶sub remove_transition {
Subroutine remove_transition: called CC=1 SCAR=0.0
Pod documented
216 1132 my ( $self, $state1, $state2 ) = @_;
217  
218 1132 $self->_decr_alphabet( $self->{TRANS}[$state1][$state2] );
219 1132 $self->{TRANS}[$state1][$state2] = undef;
220 }
221  
222 # given a state and a symbol, it tells you
223 # what the next state(s) are; do get successors
224 # for find the successors for a set of symbols,
225 # use array refs. For example:
226 # @NEXT=$self->successors([@nodes],[@symbols]);
227 116672 ▶sub successors {
Subroutine successors: called CC=8 SCAR=20.8
Pod documented
228 116672 my ( $self, $state, $symb ) = @_;
229  
230 116672 ▶ my @states = ref $state eq 'ARRAY' ? @$state : ($state);
Branch: 1 branch
BranchTrueFalseTotal
ref $state eq 'ARRAY' ? : 100953 15719 116672
231 116672 ▶ my @symbs =
Branch: 2 branches
BranchTrueFalseTotal
defined $symb ? : 116654 18 116672
ref $symb eq 'ARRAY' ? : 575 116079 116654
232 defined $symb
233 ? ( ref $symb eq 'ARRAY' ? @$symb : ($symb) )
234 : ();
235  
236 116672 $self->_assert_states(@states);
237  
238 116672 my %succ;
239 116672 for my $s (@states) {
240 214613 $succ{$_}++ for grep {
241 16004559 my $t = $self->{TRANS}[$s][$_];
242 16004559 ▶ defined $t && ( @symbs ? $t->does(@symbs) : 1 )
Branch: 1 branch
BranchTrueFalseTotal
@symbs ? : 492742 206 492948
Condition100%3/3
defined $t && (@symbs ? $t->does(@symbs) : 1)
!ll&&!rl&&r
15511611316989175959
MC/DC100%2/2
defined $t && (@symbs ? $t->does(@symbs) : 1)
defined $t (@symbs ? $t->does(@symbs) : 1)
Truth table100%3/3
A && B
A= defined $tB= (@symbs ? $t->does(@symbs) : 1)
ABresult
0X0
100
111
243 } $self->get_states;
244 }
245  
246 116672 return keys %succ;
247 }
248  
249 608 ▶sub predecessors {
Subroutine predecessors: called CC=1 SCAR=0.0
Pod undocumented
250 608 my $self = shift;
251 608 $self->clone->reverse->successors(@_);
252 }
253  
254 # reverse - no change from NFA
255 0 ▶sub reverse {
Subroutine reverse: not called CC=1 SCAR=6.9
Pod documented
256 0 my $self = $_[0]->clone;
257 0 $self->_transpose;
258  
259 0 my @start = $self->get_starting;
260 0 my @final = $self->get_accepting;
261  
262 0 $self->unset_accepting( $self->get_states );
263 0 $self->unset_starting( $self->get_states );
264  
265 0 $self->set_accepting(@start);
266 0 $self->set_starting(@final);
267  
268 0 $self;
269 }
270  
271 # get an array of all symbols
272 4303 ▶sub alphabet {
Subroutine alphabet: called CC=1 SCAR=0.0
Pod documented
273 4303 my $self = shift;
274 4303 grep length, keys %{ $self->{ALPHA} };
275 }
276  
277 # give an array of symbols, return the symbols that
278 # are in the alphabet
279 #sub is_in_alphabet {
280 # my $self = shift;
281 # my $
282 #}
283  
284 ############
285 0 ▶sub prune {
Subroutine prune: not called CC=2 SCAR=17.9
Pod documented
286 0 my $self = shift;
287  
288 0 my @queue = $self->get_starting;
289 0 my %seen = map { $_ => 1 } @queue;
290  
291 0 ▶ while (@queue) {
Branch: 1 branch
BranchTrueFalseTotal
if (@queue) 0 0 0
292 0 @queue = grep { !$seen{$_}++ } $self->successors( \@queue );
293 }
294  
295 0 my @useless = grep { !$seen{$_} } $self->get_states;
296 0 $self->delete_states(@useless);
297  
298 0 return @useless;
299 }
300  
301 ############
302  
303 6 ▶use Storable 'dclone';
Subroutine BEGIN: called CC=1 SCAR=0.0
304  
305 4412 ▶sub clone {
Subroutine clone: called CC=1 SCAR=0.0
Pod documented
306 4412 dclone( $_[0] );
307 }
308  
309 608 ▶sub _transpose {
Subroutine _transpose: called CC=1 SCAR=0.0
310 608 my $self = shift;
311 608 my $N = $self->num_states - 1;
312  
313 $self->{TRANS} = [
314 map {
315 608 my $row = $_;
316 20930 [ map { $_->[$row] } @{ $self->{TRANS} } ]
317 } 0 .. $N
318 ];
319 }
320  
321 # tests to see if set1 is a subset of set2
322 3795 ▶sub array_is_subset {
Subroutine array_is_subset: called CC=6 SCAR=17.9
Pod undocumented
323 3795 my $self = shift;
324 3795 my $set1 = shift;
325 3795 my $set2 = shift;
326 3795 ▶ $set1 = [$set1] if not ref $set1;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $set1 0 3795 3795
327 3795 ▶ $set2 = [$set2] if not ref $set2;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $set2 2 3793 3795
328 3795 my $ok = 1;
329 3795 my %setcount = ();
330 3795 foreach ( $self->array_unique( @{$set1} ), $self->array_unique( @{$set2} ) ) {
331 9016 $setcount{$_}++;
332 }
333 3795 foreach ( $self->array_unique( @{$set1} ) ) {
334 4116 ▶ if ( $setcount{$_} != 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($setcount{$_} != 2) 1960 2156 4116
335 1960 $ok = 0;
336 1960 last;
337 }
338 }
339 3795 return $ok;
340 }
341  
342 15419 ▶sub array_unique {
Subroutine array_unique: called CC=2 SCAR=6.9
Pod undocumented
343 15419 my $self = shift;
344 15419 my %ret = ();
345 15419 foreach (@_) {
346 21204 $ret{$_}++;
347 }
348 15419 return keys(%ret);
349 }
350  
351 4159 ▶sub array_complement {
Subroutine array_complement: called CC=5 SCAR=16.1
Pod undocumented
352 4159 my $self = shift;
353 4159 my $set1 = shift;
354 4159 my $set2 = shift;
355 4159 my @ret = ();
356  
357 # convert set1 to a hash
358 4159 my %set1hash = map { $_ => 1 } @{$set1};
359  
360 # iterate of set2 and test if $set1
361 4159 foreach ( @{$set2} ) {
362 4218 ▶ if ( !defined $set1hash{$_} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $set1hash{$_}) 0 4218 4218
363 0 push( @ret, $_ );
364 }
365 }
366 ## Now do the same using $set2
367 # convert set2 to a hash
368 4159 my %set2hash = map { $_ => 1 } @{$set2};
369  
370 # iterate of set1 and test if $set1
371 4159 foreach ( @{$set1} ) {
372 8414 ▶ if ( !defined $set2hash{$_} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $set2hash{$_}) 4196 4218 8414
373 4196 push( @ret, $_ );
374 }
375 }
376  
377 # now @ret contains all items in $set1 not in $set 2 and all
378 # items in $set2 not in $set1
379 4159 return @ret;
380 }
381  
382 # returns all items that 2 arrays have in common
383 0 ▶sub array_intersect {
Subroutine array_intersect: not called CC=4 SCAR=30.0
Pod undocumented
384 0 my $self = shift;
385 0 my $set1 = shift;
386 0 my $set2 = shift;
387 0 my %setcount = ();
388 0 my @ret = ();
389 0 foreach ( $self->array_unique( @{$set1} ) ) {
390 0 $setcount{$_}++;
391 }
392 0 foreach ( $self->array_unique( @{$set2} ) ) {
393 0 $setcount{$_}++;
394 0 ▶ push( @ret, $_ ) if ( $setcount{$_} > 1 );
Branch: 1 branch
BranchTrueFalseTotal
if $setcount{$_} > 1 0 0 0
395 }
396 0 return @ret;
397 }
398  
399 # given a set of symbols, returns only the valid ones
400 0 ▶sub get_valid_symbols {
Subroutine get_valid_symbols: not called CC=1 SCAR=6.9
Pod undocumented
401 0 my $self = shift;
402 0 my $symbols = shift;
403 0 return $self->array_intersect( [ $self->alphabet() ], [ @{$symbols} ] );
404 }
405  
406 ## add an FA's states & transitions to this FA (as disjoint union)
407 412 ▶sub _swallow {
Subroutine _swallow: called CC=5 SCAR=16.1
408 412 my ( $self, $other ) = @_;
409 412 my $N1 = $self->num_states;
410 412 my $N2 = $other->num_states;
411  
412 412 push @$_, (undef) x $N2 for @{ $self->{TRANS} };
413  
414 412 push @{ $self->{TRANS} }, [ (undef) x $N1, @{ clone $_ } ] for @{ $other->{TRANS} };
415  
416 412 push @{ $self->{STATES} }, @{ clone $other->{STATES} };
417  
418 412 for ( keys %{ $other->{ALPHA} } ) {
419 709 $self->{ALPHA}{$_} += $other->{ALPHA}{$_};
420  
421 # towards objects as symbols
422 709 ▶ if ( !exists( $self->{ALPHA_BLESSED}{$_} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $self->{'ALPHA_BLESSED'}{$_}) { } 547 162 709
423 547 $self->{ALPHA_BLESSED}{$_} = $other->{ALPHA_BLESSED}{$_};
424 }
425 else {
426 162 $self->{ALPHA_BLESSED}{$_}->_increment_count( $other->{ALPHA_BLESSED}{$_}->get_count );
427 }
428 }
429  
430 412 return map { $_ + $N1 } $other->get_states;
431 }
432  
433 6 1;
434  
435 __END__
« FLAT/PFA.pm ↑ Summary FLAT/CMD/AcyclicStrings.pm »