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 SCAR 42.1
CC
50
Cov
81%
array_intersect
20.0
_assert_non_states
12.0
successors
8.0
CRAP 67.6
« FLAT/DFA.pm ↑ Summary FLAT/Regex/Op.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 219411 ▶ my $self = shift;
Subroutine get_states: called CC=1 SCAR=0.0
Pod documented
39 219411 return 0 .. ( $self->num_states - 1 );
40 }
41  
42 sub num_states {
43 225852 ▶ my $self = shift;
Subroutine num_states: called CC=1 SCAR=0.0
Pod documented
44 225852 return scalar @{ $self->{STATES} };
45 }
46  
47 sub is_state {
48 370076 ▶ my ( $self, $state ) = @_;
Subroutine is_state: called CC=1 SCAR=0.0
Pod documented
49 370076 exists $self->{STATES}->[$state];
50 }
51  
52 sub _assert_states {
53 209946 ▶ my ( $self, @states ) = @_;
Subroutine _assert_states: called CC=3 SCAR=11.2
54 209946 for (@states) {
55 369930 ▶ croak "'$_' is not a state" if not $self->is_state($_);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_state($_) 0 369930 369930
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 139 ▶ my ( $self, @states ) = @_;
Subroutine delete_states: called CC=4 SCAR=13.9
Pod documented
68  
69 139 $self->_assert_states(@states);
70  
71 139 for my $s ( sort { $b <=> $a } @states ) {
72 139 $self->_decr_alphabet($_) for @{ splice @{ $self->{TRANS} }, $s, 1 };
73  
74 139 $self->_decr_alphabet( splice @$_, $s, 1 ) for @{ $self->{TRANS} };
75  
76 139 splice @{ $self->{STATES} }, $s, 1;
77 }
78 }
79  
80 sub add_states {
81 4882 ▶ my ( $self, $num ) = @_;
Subroutine add_states: called CC=4 SCAR=13.9
Pod documented
82 4882 my $id = $self->num_states;
83  
84 4882 for my $s ( $id .. ( $id + $num - 1 ) ) {
85 5508 push @$_, undef for @{ $self->{TRANS} };
86 5508 push @{ $self->{TRANS} }, [ (undef) x ( $s + 1 ) ];
87 5508 push @{ $self->{STATES} },
88 {
89 starting => 0,
90 accepting => 0
91 };
92 }
93  
94 return wantarray
95 4882 ▶ ? ( $id .. ( $id + $num - 1 ) )
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 849 4033 4882
96 : $id + $num - 1;
97 }
98  
99 ##############
100  
101 sub is_starting {
102 37509 ▶ my ( $self, $state ) = @_;
Subroutine is_starting: called CC=1 SCAR=0.0
Pod documented
103 37509 $self->_assert_states($state);
104 37509 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 909 ▶ my ( $self, @states ) = @_;
Subroutine unset_starting: called CC=2 SCAR=6.9
Pod documented
115 909 $self->_assert_states(@states);
116 909 $self->{STATES}[$_]{starting} = 0 for @states;
117 }
118  
119 sub get_starting {
120 3960 ▶ my $self = shift;
Subroutine get_starting: called CC=1 SCAR=0.0
Pod documented
121 3960 return grep { $self->is_starting($_) } $self->get_states;
122 }
123  
124 ##############
125  
126 sub is_accepting {
127 34823 ▶ my ( $self, $state ) = @_;
Subroutine is_accepting: called CC=1 SCAR=0.0
Pod documented
128 34823 $self->_assert_states($state);
129 34823 return $self->{STATES}[$state]{accepting};
130 }
131  
132 sub set_accepting {
133 2179 ▶ my ( $self, @states ) = @_;
Subroutine set_accepting: called CC=2 SCAR=6.9
Pod documented
134 2179 $self->_assert_states(@states);
135 2179 $self->{STATES}[$_]{accepting} = 1 for @states;
136 }
137  
138 sub unset_accepting {
139 915 ▶ my ( $self, @states ) = @_;
Subroutine unset_accepting: called CC=2 SCAR=6.9
Pod documented
140 915 $self->_assert_states(@states);
141 915 $self->{STATES}[$_]{accepting} = 0 for @states;
142 }
143  
144 sub get_accepting {
145 2245 ▶ my $self = shift;
Subroutine get_accepting: called CC=1 SCAR=0.0
Pod documented
146 2245 return grep { $self->is_accepting($_) } $self->get_states;
147 }
148  
149 ###############
150  
151 sub _decr_alphabet {
152 15254 ▶ my ( $self, $t ) = @_;
Subroutine _decr_alphabet: called CC=5 SCAR=16.1
153  
154 15254 ▶ return if not defined $t;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $t 9560 5694 15254
155 5694 for ( $t->alphabet ) {
156 10687 ▶ delete $self->{ALPHA}{$_} if not --$self->{ALPHA}{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless --$self->{"ALPHA"}{$_} 505 10182 10687
157  
158 # supposed to delete key when _decrement_count returns 0, so need to test this
159 10687 ▶ delete $self->{ALPHA_BLESSED}{$_} if not $self->{ALPHA_BLESSED}{$_}->_decrement_count;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"ALPHA_BLESSED"}{$_}->_decrement_count 505 10182 10687
160 }
161 }
162  
163 sub _incr_alphabet {
164 13903 ▶ my ( $self, $t ) = @_;
Subroutine _incr_alphabet: called CC=4 SCAR=13.9
165  
166 13903 ▶ return if not defined $t;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $t 0 13903 13903
167 13903 for ( $t->alphabet ) {
168 22049 $self->{ALPHA}{$_}++;
169  
170 22049 ▶ if ( !exists( $self->{ALPHA_BLESSED}{$_} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $self->{'ALPHA_BLESSED'}{$_}) { } 2871 19178 22049
171 2871 $self->{ALPHA_BLESSED}{$_} = $self->{ALPHA_CLASS}->new($_);
172 }
173 else {
174 19178 $self->{ALPHA_BLESSED}{$_}->_increment_count;
175 }
176 }
177 }
178  
179 sub set_transition {
180 1173 ▶ my ( $self, $state1, $state2, @label ) = @_;
Subroutine set_transition: called CC=2 SCAR=7.0
Pod documented
181 1173 $self->remove_transition( $state1, $state2 );
182  
183 1173 @label = grep defined, @label;
184 1173 ▶ return if not @label;
Branch: 1 branch
BranchTrueFalseTotal
unless @label 0 1173 1173
185  
186 1173 my $t = $self->{TRANS_CLASS}->new(@label);
187 1173 $self->{TRANS}[$state1][$state2] = $t;
188 1173 $self->_incr_alphabet($t);
189 }
190  
191 sub add_transition {
192 12730 ▶ my ( $self, $state1, $state2, @label ) = @_;
Subroutine add_transition: called CC=3 SCAR=11.0
Pod documented
193  
194 12730 @label = grep defined, @label;
195 12730 ▶ return if not @label;
Branch: 1 branch
BranchTrueFalseTotal
unless @label 0 12730 12730
196  
197 12730 my $t = $self->get_transition( $state1, $state2 );
198 12730 $self->_decr_alphabet($t);
199  
200 12730 ▶ if ( !$t ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($t) 7698 5032 12730
201 7698 $t = $self->{TRANS}[$state1][$state2] = $self->{TRANS_CLASS}->new;
202 }
203  
204 12730 $t->add(@label);
205 12730 $self->_incr_alphabet($t);
206 }
207  
208 sub get_transition {
209 18234 ▶ my ( $self, $state1, $state2 ) = @_;
Subroutine get_transition: called CC=1 SCAR=0.0
Pod documented
210 18234 $self->_assert_states( $state1, $state2 );
211  
212 18234 $self->{TRANS}[$state1][$state2];
213 }
214  
215 sub remove_transition {
216 1173 ▶ my ( $self, $state1, $state2 ) = @_;
Subroutine remove_transition: called CC=1 SCAR=0.0
Pod documented
217  
218 1173 $self->_decr_alphabet( $self->{TRANS}[$state1][$state2] );
219 1173 $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 113353 ▶ my ( $self, $state, $symb ) = @_;
Subroutine successors: called CC=8 SCAR=20.8
Pod documented
229  
230 113353 ▶ my @states = ref $state eq 'ARRAY' ? @$state : ($state);
Branch: 1 branch
BranchTrueFalseTotal
ref $state eq 'ARRAY' ? : 99055 14298 113353
231 113353 ▶ my @symbs =
Branch: 2 branches
BranchTrueFalseTotal
defined $symb ? : 113335 18 113353
ref $symb eq 'ARRAY' ? : 549 112786 113335
232 defined $symb
233 ? ( ref $symb eq 'ARRAY' ? @$symb : ($symb) )
234 : ();
235  
236 113353 $self->_assert_states(@states);
237  
238 113353 my %succ;
239 113353 for my $s (@states) {
240 209666 $succ{$_}++ for grep {
241 15888139 my $t = $self->{TRANS}[$s][$_];
242 15888139 ▶ defined $t && ( @symbs ? $t->does(@symbs) : 1 )
Branch: 2 branches
BranchTrueFalseTotal
defined $t and @symbs ? $t->does(@symbs) : 1 481362 15406777 15888139
@symbs ? : 481156 206 481362
243 } $self->get_states;
244 }
245  
246 113353 return keys %succ;
247 }
248  
249 sub predecessors {
250 595 ▶ my $self = shift;
Subroutine predecessors: called CC=1 SCAR=0.0
Pod undocumented
251 595 $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 4249 ▶ my $self = shift;
Subroutine alphabet: called CC=1 SCAR=0.0
Pod documented
274 4249 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 4356 ▶ dclone( $_[0] );
Subroutine clone: called CC=1 SCAR=0.0
Pod documented
307 }
308  
309 sub _transpose {
310 595 ▶ my $self = shift;
Subroutine _transpose: called CC=1 SCAR=0.0
311 595 my $N = $self->num_states - 1;
312  
313 $self->{TRANS} = [
314 map {
315 595 my $row = $_;
316 21978 [ 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 3332 ▶ my $self = shift;
Subroutine array_is_subset: called CC=6 SCAR=17.9
Pod undocumented
324 3332 my $set1 = shift;
325 3332 my $set2 = shift;
326 3332 ▶ $set1 = [$set1] if not ref $set1;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $set1 0 3332 3332
327 3332 ▶ $set2 = [$set2] if not ref $set2;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $set2 14 3318 3332
328 3332 my $ok = 1;
329 3332 my %setcount = ();
330 3332 foreach ( $self->array_unique( @{$set1} ), $self->array_unique( @{$set2} ) ) {
331 7962 $setcount{$_}++;
332 }
333 3332 foreach ( $self->array_unique( @{$set1} ) ) {
334 3632 ▶ if ( $setcount{$_} != 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($setcount{$_} != 2) 1695 1937 3632
335 1695 $ok = 0;
336 1695 last;
337 }
338 }
339 3332 return $ok;
340 }
341  
342 sub array_unique {
343 14004 ▶ my $self = shift;
Subroutine array_unique: called CC=2 SCAR=6.9
Pod undocumented
344 14004 my %ret = ();
345 14004 foreach (@_) {
346 19495 $ret{$_}++;
347 }
348 14004 return keys(%ret);
349 }
350  
351 sub array_complement {
352 4133 ▶ my $self = shift;
Subroutine array_complement: called CC=5 SCAR=16.1
Pod undocumented
353 4133 my $set1 = shift;
354 4133 my $set2 = shift;
355 4133 my @ret = ();
356  
357 # convert set1 to a hash
358 4133 my %set1hash = map { $_ => 1 } @{$set1};
359  
360 # iterate of set2 and test if $set1
361 4133 foreach ( @{$set2} ) {
362 4188 ▶ if ( !defined $set1hash{$_} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $set1hash{$_}) 0 4188 4188
363 0 push( @ret, $_ );
364 }
365 }
366 ## Now do the same using $set2
367 # convert set2 to a hash
368 4133 my %set2hash = map { $_ => 1 } @{$set2};
369  
370 # iterate of set1 and test if $set1
371 4133 foreach ( @{$set1} ) {
372 8231 ▶ if ( !defined $set2hash{$_} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $set2hash{$_}) 4043 4188 8231
373 4043 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 4133 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 691 $self->{ALPHA}{$_} += $other->{ALPHA}{$_};
420  
421 # towards objects as symbols
422 691 ▶ if ( !exists( $self->{ALPHA_BLESSED}{$_} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $self->{'ALPHA_BLESSED'}{$_}) { } 536 155 691
423 536 $self->{ALPHA_BLESSED}{$_} = $other->{ALPHA_BLESSED}{$_};
424 }
425 else {
426 155 $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/DFA.pm ↑ Summary FLAT/Regex/Op.pm »