FLAT/FA.pm

filter: Statementstmt 82.0% 192 / 234 Branchbran 75.0% 36 / 48 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 87.8% 36 / 41 Podpod 75.8% 22 / 29 totaltotal 81.2% 286 / 352 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 sub new {
28 976 ▶ my $pkg = shift;
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
29 976 bless {
30 STATES => [],
31 TRANS => [],
32 ALPHA => {},
33 ALPHA_BLESSED => {},
34 }, $pkg;
35 }
36  
37 sub get_states {
38 217724 ▶ my $self = shift;
Subroutine get_states: called CC=1 SCAR=0.0
Pod documented
39 217724 return 0 .. ( $self->num_states - 1 );
40 }
41  
42 sub num_states {
43 224079 ▶ my $self = shift;
Subroutine num_states: called CC=1 SCAR=0.0
Pod documented
44 224079 return scalar @{ $self->{STATES} };
45 }
46  
47 sub is_state {
48 365576 ▶ my ( $self, $state ) = @_;
Subroutine is_state: called CC=1 SCAR=0.0
Pod documented
49 365576 exists $self->{STATES}->[$state];
50 }
51  
52 sub _assert_states {
53 207278 ▶ my ( $self, @states ) = @_;
Subroutine _assert_states: called CC=3 SCAR=11.2
54 207278 for (@states) {
55 365434 ▶ croak "'$_' is not a state" if not $self->is_state($_);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_state($_) 0 365434 365434
56 }
57 }
58  
59 sub _assert_non_states {
60 0 ▶ my ( $self, @states ) = @_;
Subroutine _assert_non_states: not called CC=3 SCAR=24.8
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 sub delete_states {
67 134 ▶ my ( $self, @states ) = @_;
Subroutine delete_states: called CC=4 SCAR=13.9
Pod documented
68  
69 134 $self->_assert_states(@states);
70  
71 134 for my $s ( sort { $b <=> $a } @states ) {
72 134 $self->_decr_alphabet($_) for @{ splice @{ $self->{TRANS} }, $s, 1 };
73  
74 134 $self->_decr_alphabet( splice @$_, $s, 1 ) for @{ $self->{TRANS} };
75  
76 134 splice @{ $self->{STATES} }, $s, 1;
77 }
78 }
79  
80 sub add_states {
81 4830 ▶ my ( $self, $num ) = @_;
Subroutine add_states: called CC=4 SCAR=13.9
Pod documented
82 4830 my $id = $self->num_states;
83  
84 4830 for my $s ( $id .. ( $id + $num - 1 ) ) {
85 5444 push @$_, undef for @{ $self->{TRANS} };
86 5444 push @{ $self->{TRANS} }, [ (undef) x ( $s + 1 ) ];
87 5444 push @{ $self->{STATES} },
88 {
89 starting => 0,
90 accepting => 0
91 };
92 }
93  
94 return wantarray
95 4830 ▶ ? ( $id .. ( $id + $num - 1 ) )
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 845 3985 4830
96 : $id + $num - 1;
97 }
98  
99 ##############
100  
101 sub is_starting {
102 36642 ▶ my ( $self, $state ) = @_;
Subroutine is_starting: called CC=1 SCAR=0.0
Pod documented
103 36642 $self->_assert_states($state);
104 36642 return $self->{STATES}[$state]{starting};
105 }
106  
107 sub set_starting {
108 430 ▶ my ( $self, @states ) = @_;
Subroutine set_starting: called CC=2 SCAR=6.9
Pod documented
109 430 $self->_assert_states(@states);
110 430 $self->{STATES}[$_]{starting} = 1 for @states;
111 }
112  
113 sub unset_starting {
114 858 ▶ my ( $self, @states ) = @_;
Subroutine unset_starting: called CC=2 SCAR=6.9
Pod documented
115 858 $self->_assert_states(@states);
116 858 $self->{STATES}[$_]{starting} = 0 for @states;
117 }
118  
119 sub get_starting {
120 3885 ▶ my $self = shift;
Subroutine get_starting: called CC=1 SCAR=0.0
Pod documented
121 3885 return grep { $self->is_starting($_) } $self->get_states;
122 }
123  
124 ##############
125  
126 sub is_accepting {
127 33922 ▶ my ( $self, $state ) = @_;
Subroutine is_accepting: called CC=1 SCAR=0.0
Pod documented
128 33922 $self->_assert_states($state);
129 33922 return $self->{STATES}[$state]{accepting};
130 }
131  
132 sub set_accepting {
133 2126 ▶ my ( $self, @states ) = @_;
Subroutine set_accepting: called CC=2 SCAR=6.9
Pod documented
134 2126 $self->_assert_states(@states);
135 2126 $self->{STATES}[$_]{accepting} = 1 for @states;
136 }
137  
138 sub unset_accepting {
139 864 ▶ my ( $self, @states ) = @_;
Subroutine unset_accepting: called CC=2 SCAR=6.9
Pod documented
140 864 $self->_assert_states(@states);
141 864 $self->{STATES}[$_]{accepting} = 0 for @states;
142 }
143  
144 sub get_accepting {
145 2188 ▶ my $self = shift;
Subroutine get_accepting: called CC=1 SCAR=0.0
Pod documented
146 2188 return grep { $self->is_accepting($_) } $self->get_states;
147 }
148  
149 ###############
150  
151 sub _decr_alphabet {
152 15038 ▶ my ( $self, $t ) = @_;
Subroutine _decr_alphabet: called CC=5 SCAR=16.1
153  
154 15038 ▶ return if not defined $t;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $t 9387 5651 15038
155 5651 for ( $t->alphabet ) {
156 10561 ▶ delete $self->{ALPHA}{$_} if not --$self->{ALPHA}{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless --$self->{"ALPHA"}{$_} 524 10037 10561
157  
158 # supposed to delete key when _decrement_count returns 0, so need to test this
159 10561 ▶ delete $self->{ALPHA_BLESSED}{$_} if not $self->{ALPHA_BLESSED}{$_}->_decrement_count;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"ALPHA_BLESSED"}{$_}->_decrement_count 524 10037 10561
160 }
161 }
162  
163 sub _incr_alphabet {
164 13700 ▶ my ( $self, $t ) = @_;
Subroutine _incr_alphabet: called CC=4 SCAR=13.9
165  
166 13700 ▶ return if not defined $t;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $t 0 13700 13700
167 13700 for ( $t->alphabet ) {
168 21705 $self->{ALPHA}{$_}++;
169  
170 21705 ▶ if ( !exists( $self->{ALPHA_BLESSED}{$_} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $self->{'ALPHA_BLESSED'}{$_}) { } 2868 18837 21705
171 2868 $self->{ALPHA_BLESSED}{$_} = $self->{ALPHA_CLASS}->new($_);
172 }
173 else {
174 18837 $self->{ALPHA_BLESSED}{$_}->_increment_count;
175 }
176 }
177 }
178  
179 sub set_transition {
180 1131 ▶ my ( $self, $state1, $state2, @label ) = @_;
Subroutine set_transition: called CC=2 SCAR=7.0
Pod documented
181 1131 $self->remove_transition( $state1, $state2 );
182  
183 1131 @label = grep defined, @label;
184 1131 ▶ return if not @label;
Branch: 1 branch
BranchTrueFalseTotal
unless @label 0 1131 1131
185  
186 1131 my $t = $self->{TRANS_CLASS}->new(@label);
187 1131 $self->{TRANS}[$state1][$state2] = $t;
188 1131 $self->_incr_alphabet($t);
189 }
190  
191 sub add_transition {
192 12569 ▶ my ( $self, $state1, $state2, @label ) = @_;
Subroutine add_transition: called CC=3 SCAR=11.0
Pod documented
193  
194 12569 @label = grep defined, @label;
195 12569 ▶ return if not @label;
Branch: 1 branch
BranchTrueFalseTotal
unless @label 0 12569 12569
196  
197 12569 my $t = $self->get_transition( $state1, $state2 );
198 12569 $self->_decr_alphabet($t);
199  
200 12569 ▶ if ( !$t ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($t) 7585 4984 12569
201 7585 $t = $self->{TRANS}[$state1][$state2] = $self->{TRANS_CLASS}->new;
202 }
203  
204 12569 $t->add(@label);
205 12569 $self->_incr_alphabet($t);
206 }
207  
208 sub get_transition {
209 18391 ▶ my ( $self, $state1, $state2 ) = @_;
Subroutine get_transition: called CC=1 SCAR=0.0
Pod documented
210 18391 $self->_assert_states( $state1, $state2 );
211  
212 18391 $self->{TRANS}[$state1][$state2];
213 }
214  
215 sub remove_transition {
216 1131 ▶ my ( $self, $state1, $state2 ) = @_;
Subroutine remove_transition: called CC=1 SCAR=0.0
Pod documented
217  
218 1131 $self->_decr_alphabet( $self->{TRANS}[$state1][$state2] );
219 1131 $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 sub successors {
228 112507 ▶ my ( $self, $state, $symb ) = @_;
Subroutine successors: called CC=8 SCAR=20.8
Pod documented
229  
230 112507 ▶ my @states = ref $state eq 'ARRAY' ? @$state : ($state);
Branch: 1 branch
BranchTrueFalseTotal
ref $state eq 'ARRAY' ? : 98219 14288 112507
231 112507 ▶ my @symbs =
Branch: 2 branches
BranchTrueFalseTotal
defined $symb ? : 112489 18 112507
ref $symb eq 'ARRAY' ? : 555 111934 112489
232 defined $symb
233 ? ( ref $symb eq 'ARRAY' ? @$symb : ($symb) )
234 : ();
235  
236 112507 $self->_assert_states(@states);
237  
238 112507 my %succ;
239 112507 for my $s (@states) {
240 208147 $succ{$_}++ for grep {
241 15847602 my $t = $self->{TRANS}[$s][$_];
242 15847602 ▶ defined $t && ( @symbs ? $t->does(@symbs) : 1 )
Branch: 2 branches
BranchTrueFalseTotal
defined $t and @symbs ? $t->does(@symbs) : 1 476933 15370669 15847602
@symbs ? : 476727 206 476933
243 } $self->get_states;
244 }
245  
246 112507 return keys %succ;
247 }
248  
249 sub predecessors {
250 561 ▶ my $self = shift;
Subroutine predecessors: called CC=1 SCAR=0.0
Pod undocumented
251 561 $self->clone->reverse->successors(@_);
252 }
253  
254 # reverse - no change from NFA
255 sub reverse {
256 0 ▶ my $self = $_[0]->clone;
Subroutine reverse: not called CC=1 SCAR=6.9
Pod documented
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 sub alphabet {
273 4225 ▶ my $self = shift;
Subroutine alphabet: called CC=1 SCAR=0.0
Pod documented
274 4225 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 sub prune {
286 0 ▶ my $self = shift;
Subroutine prune: not called CC=2 SCAR=17.9
Pod documented
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 sub clone {
306 4294 ▶ dclone( $_[0] );
Subroutine clone: called CC=1 SCAR=0.0
Pod documented
307 }
308  
309 sub _transpose {
310 561 ▶ my $self = shift;
Subroutine _transpose: called CC=1 SCAR=0.0
311 561 my $N = $self->num_states - 1;
312  
313 $self->{TRANS} = [
314 map {
315 561 my $row = $_;
316 21304 [ map { $_->[$row] } @{ $self->{TRANS} } ]
317 } 0 .. $N
318 ];
319 }
320  
321 # tests to see if set1 is a subset of set2
322 sub array_is_subset {
323 3354 ▶ my $self = shift;
Subroutine array_is_subset: called CC=6 SCAR=17.9
Pod undocumented
324 3354 my $set1 = shift;
325 3354 my $set2 = shift;
326 3354 ▶ $set1 = [$set1] if not ref $set1;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $set1 0 3354 3354
327 3354 ▶ $set2 = [$set2] if not ref $set2;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $set2 8 3346 3354
328 3354 my $ok = 1;
329 3354 my %setcount = ();
330 3354 foreach ( $self->array_unique( @{$set1} ), $self->array_unique( @{$set2} ) ) {
331 7949 $setcount{$_}++;
332 }
333 3354 foreach ( $self->array_unique( @{$set1} ) ) {
334 3628 ▶ if ( $setcount{$_} != 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($setcount{$_} != 2) 1741 1887 3628
335 1741 $ok = 0;
336 1741 last;
337 }
338 }
339 3354 return $ok;
340 }
341  
342 sub array_unique {
343 14032 ▶ my $self = shift;
Subroutine array_unique: called CC=2 SCAR=6.9
Pod undocumented
344 14032 my %ret = ();
345 14032 foreach (@_) {
346 19439 $ret{$_}++;
347 }
348 14032 return keys(%ret);
349 }
350  
351 sub array_complement {
352 4095 ▶ my $self = shift;
Subroutine array_complement: called CC=5 SCAR=16.1
Pod undocumented
353 4095 my $set1 = shift;
354 4095 my $set2 = shift;
355 4095 my @ret = ();
356  
357 # convert set1 to a hash
358 4095 my %set1hash = map { $_ => 1 } @{$set1};
359  
360 # iterate of set2 and test if $set1
361 4095 foreach ( @{$set2} ) {
362 4148 ▶ if ( !defined $set1hash{$_} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $set1hash{$_}) 0 4148 4148
363 0 push( @ret, $_ );
364 }
365 }
366 ## Now do the same using $set2
367 # convert set2 to a hash
368 4095 my %set2hash = map { $_ => 1 } @{$set2};
369  
370 # iterate of set1 and test if $set1
371 4095 foreach ( @{$set1} ) {
372 8203 ▶ if ( !defined $set2hash{$_} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $set2hash{$_}) 4055 4148 8203
373 4055 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 4095 return @ret;
380 }
381  
382 # returns all items that 2 arrays have in common
383 sub array_intersect {
384 0 ▶ my $self = shift;
Subroutine array_intersect: not called CC=4 SCAR=30.0
Pod undocumented
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 sub get_valid_symbols {
401 0 ▶ my $self = shift;
Subroutine get_valid_symbols: not called CC=1 SCAR=6.9
Pod undocumented
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 sub _swallow {
408 412 ▶ my ( $self, $other ) = @_;
Subroutine _swallow: called CC=5 SCAR=16.1
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 707 $self->{ALPHA}{$_} += $other->{ALPHA}{$_};
420  
421 # towards objects as symbols
422 707 ▶ if ( !exists( $self->{ALPHA_BLESSED}{$_} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $self->{'ALPHA_BLESSED'}{$_}) { } 546 161 707
423 546 $self->{ALPHA_BLESSED}{$_} = $other->{ALPHA_BLESSED}{$_};
424 }
425 else {
426 161 $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 »