FLAT/PFA.pm

filter: Statementstmt 89.0% 138 / 155 Branchbran 85.7% 24 / 28 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 80.0% 16 / 20 Podpod 42.8% 6 / 14 totaltotal 84.7% 184 / 217 CC 29 SCAR 18.3
CC
29
Cov
89%
as_nfa
14.0
set_tied
6.0
unset_tied
6.0
CRAP 6.2
« FLAT/CMD.pm ↑ Summary FLAT/FA.pm »
1 package FLAT::PFA;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use parent qw(FLAT::NFA);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use FLAT::Transition;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 3 ▶use constant LAMBDA => '#lambda';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 # Note: in a PFA, states are made up of active nodes. In this implementation, we have
12 # decided to retain the functionality of the state functions in FA.pm, although the entities
13 # being manipulated are technically nodes, not states. States are only explicitly tracked
14 # once the PFA is serialized into an NFA. Therefore, the TRANS member of the PFA object is
15 # the nodal transition function, gamma. The state transition function, delta, is not used
16 # in anyway, but is derived out of the PFA->NFA conversion process.
17  
18 # The new way of doing things eliminated from PFA.pm of FLAT::Legacy is the
19 # need to explicitly track: start nodes, final nodes, symbols, and lambda & epsilon symbols,
20  
21 546 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
22 546 my $pkg = shift;
23 546 my $self = $pkg->SUPER::new(@_); # <-- SUPER is FLAT::NFA
24 546 return $self;
25 }
26  
27 # Singleton is no different than the NFA singleton
28 546 ▶sub singleton {
Subroutine singleton: called CC=3 SCAR=12.9
Pod undocumented
29 546 my ( $class, $char ) = @_;
30 546 my $pfa = $class->new;
31 546 ▶ if ( not defined $char ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $char) { } 0 546 546
elsif ($char eq '') { } 0 546 546
32 0 $pfa->add_states(1);
33 0 $pfa->set_starting(0);
34 }
35 elsif ( $char eq "" ) {
36 0 $pfa->add_states(1);
37 0 $pfa->set_starting(0);
38 0 $pfa->set_accepting(0);
39 }
40 else {
41 546 $pfa->add_states(2);
42 546 $pfa->set_starting(0);
43 546 $pfa->set_accepting(1);
44 546 $pfa->set_transition( 0, 1, $char );
45 }
46 546 return $pfa;
47 }
48  
49 # attack of the clones
50 656 ▶sub as_pfa { $_[0]->clone() }
Subroutine as_pfa: called CC=1 SCAR=0.0
Pod undocumented
51  
52 1469 ▶sub set_starting {
Subroutine set_starting: called CC=2 SCAR=6.9
Pod documented
53 1469 my ( $self, @states ) = @_;
54 1469 $self->_assert_states(@states);
55 1469 $self->{STATES}[$_]{starting} = 1 for @states;
56 }
57  
58 # Creates a single start state with epsilon transitions from
59 # the former start states;
60 # Creates a single final state with epsilon transitions from
61 # the former accepting states
62 84 ▶sub pinch {
Subroutine pinch: called CC=3 SCAR=11.0
Pod undocumented
63 84 my $self = shift;
64 84 my $symbol = shift;
65 84 my @starting = $self->get_starting;
66 84 ▶ if ( @starting > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@starting > 1) 84 0 84
67 84 my $newstart = $self->add_states(1);
68 84 map { $self->add_transition( $newstart, $_, $symbol ) } @starting;
69 84 $self->unset_starting(@starting);
70 84 $self->set_starting($newstart);
71 }
72 #
73 84 my @accepting = $self->get_accepting;
74 84 ▶ if ( @accepting > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@accepting > 1) 84 0 84
75 84 my $newfinal = $self->add_states(1);
76 84 map { $self->add_transition( $_, $newfinal, $symbol ) } @accepting;
77 84 $self->unset_accepting(@accepting);
78 84 $self->set_accepting($newfinal);
79 }
80 84 return;
81 }
82  
83 # Implement the joining of two PFAs with lambda transitions
84 # Note: using epsilon pinches for simplicity
85 48 ▶sub shuffle {
Subroutine shuffle: called CC=2 SCAR=6.9
Pod documented
86 48 my @pfas = map { $_->as_pfa } @_;
87 48 my $result = $pfas[0]->clone;
88 48 $result->_swallow($_) for @pfas[ 1 .. $#pfas ];
89 48 $result->pinch(LAMBDA);
90 48 $result;
91 }
92  
93 ##############
94  
95 0 ▶sub is_tied {
Subroutine is_tied: not called CC=1 SCAR=6.9
Pod undocumented
96 0 my ( $self, $state ) = @_;
97 0 $self->_assert_states($state);
98 0 return $self->{STATES}[$state]{tied};
99 }
100  
101 0 ▶sub set_tied {
Subroutine set_tied: not called CC=2 SCAR=17.9
Pod undocumented
102 0 my ( $self, @states ) = @_;
103 0 $self->_assert_states(@states);
104 0 $self->{STATES}[$_]{tied} = 1 for @states;
105 }
106  
107 0 ▶sub unset_tied {
Subroutine unset_tied: not called CC=2 SCAR=17.9
Pod undocumented
108 0 my ( $self, @states ) = @_;
109 0 $self->_assert_states(@states);
110 0 $self->{STATES}[$_]{tied} = 0 for @states;
111 }
112  
113 0 ▶sub get_tied {
Subroutine get_tied: not called CC=1 SCAR=6.9
Pod undocumented
114 0 my $self = shift;
115 0 return grep { $self->is_tied($_) } $self->get_states;
116 }
117  
118 ##############
119  
120 # joins two PFAs in a union (or) - no change from NFA
121 36 ▶sub union {
Subroutine union: called CC=2 SCAR=6.9
Pod documented
122 36 my @pfas = map { $_->as_pfa } @_;
123 36 my $result = $pfas[0]->clone;
124 36 $result->_swallow($_) for @pfas[ 1 .. $#pfas ];
125 36 $result->pinch('');
126 36 $result;
127 }
128  
129 # joins two PFAs via concatenation - no change from NFA
130 163 ▶sub concat {
Subroutine concat: called CC=5 SCAR=16.1
Pod documented
131 163 my @pfas = map { $_->as_pfa } @_;
132  
133 163 my $result = $pfas[0]->clone;
134 163 my @newstate = ( [ $result->get_states ] );
135 163 my @start = $result->get_starting;
136  
137 163 for ( 1 .. $#pfas ) {
138 314 push @newstate, [ $result->_swallow( $pfas[$_] ) ];
139 }
140  
141 163 $result->unset_accepting( $result->get_states );
142 163 $result->unset_starting( $result->get_states );
143 163 $result->set_starting(@start);
144  
145 163 for my $pfa_id ( 1 .. $#pfas ) {
146 314 for my $s1 ( $pfas[ $pfa_id - 1 ]->get_accepting ) {
147 314 for my $s2 ( $pfas[$pfa_id]->get_starting ) {
148 314 $result->set_transition( $newstate[ $pfa_id - 1 ][$s1], $newstate[$pfa_id][$s2], "" );
149 }
150 }
151 }
152  
153 163 $result->set_accepting( @{ $newstate[-1] }[ $pfas[-1]->get_accepting ] );
154  
155 163 $result;
156 }
157  
158 # forms closure around a the given PFA - no change from NFA
159 68 ▶sub kleene {
Subroutine kleene: called CC=3 SCAR=11.0
Pod documented
160 68 my $result = $_[0]->clone;
161  
162 68 my ( $newstart, $newfinal ) = $result->add_states(2);
163  
164 68 $result->set_transition( $newstart, $_, "" ) for $result->get_starting;
165 68 $result->unset_starting( $result->get_starting );
166 68 $result->set_starting($newstart);
167  
168 68 $result->set_transition( $_, $newfinal, "" ) for $result->get_accepting;
169 68 $result->unset_accepting( $result->get_accepting );
170 68 $result->set_accepting($newfinal);
171  
172 68 $result->set_transition( $newstart, $newfinal, "" );
173 68 $result->set_transition( $newfinal, $newstart, "" );
174  
175 68 $result;
176 }
177  
178 137 ▶sub as_nfa {
Subroutine as_nfa: called CC=14 SCAR=26.4
Pod documented
179 137 my $self = shift;
180 137 my $result = FLAT::NFA->new();
181  
182 # Dstates is initially populated with the start state, which
183 # is exactly the set of all nodes marked as a starting node
184 137 my @Dstates = [ sort( $self->get_starting() ) ]; # I suppose all start states are considered 'tied'
185 137 my %DONE = (); # |- what about all accepting states? I think so...
186 # the main while loop that ends when @Dstates becomes exhausted
187 137 my %NEW = ();
188 137 ▶ while (@Dstates) {
Branch: 1 branch
BranchTrueFalseTotal
if (@Dstates) 2396 137 2533
189 2396 my $current = pop(@Dstates);
190 2396 my $currentid = join( ',', @{$current} );
191 2396 $DONE{$currentid}++; # mark done
192 2396 foreach my $symbol ( $self->alphabet(), '' ) { # Sigma UNION epsilon
193 14648 ▶ if ( LAMBDA eq $symbol ) {
Branch: 1 branch
BranchTrueFalseTotal
if ('#lambda' eq $symbol) { } 1639 13009 14648
194 1639 my @NEXT = ();
195 1639 my @tmp = $self->successors( [ @{$current} ], $symbol );
196 1639 ▶ if (@tmp) {
Branch: 1 branch
BranchTrueFalseTotal
if (@tmp) 608 1031 1639
197 608 my @pred = $self->predecessors( [@tmp], LAMBDA );
198 608 ▶ if ( $self->array_is_subset( [@pred], [ @{$current} ] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->array_is_subset([@pred], [@{$current;}])) 98 510 608
199 98 push( @NEXT, @tmp, $self->array_complement( [ @{$current} ], [@pred] ) );
200 98 @NEXT = sort( $self->array_unique(@NEXT) );
201 98 my $nextid = join( ',', @NEXT );
202 98 ▶ push( @Dstates, [@NEXT] ) if ( !exists( $DONE{$nextid} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $DONE{$nextid} 96 2 98
203  
204 # make new states if none exist and track
205 98 ▶ if ( !exists( $NEW{$currentid} ) ) { $NEW{$currentid} = $result->add_states(1) }
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $NEW{$currentid}) 32 66 98
206 98 ▶ if ( !exists( $NEW{$nextid} ) ) { $NEW{$nextid} = $result->add_states(1) }
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $NEW{$nextid}) 96 2 98
207 98 $result->add_transition( $NEW{$currentid}, $NEW{$nextid}, '' );
208 }
209 }
210 }
211 else {
212 13009 foreach my $node ( @{$current} ) {
213 22218 my @tmp = $self->successors( [$node], $symbol );
214 22218 foreach my $new (@tmp) {
215 3936 my @NEXT = ();
216 3936 push( @NEXT, $new, $self->array_complement( [ @{$current} ], [$node] ) );
217 3936 @NEXT = sort( $self->array_unique(@NEXT) );
218 3936 my $nextid = join( ',', @NEXT );
219 3936 ▶ push( @Dstates, [@NEXT] ) if ( !exists( $DONE{$nextid} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $DONE{$nextid} 2163 1773 3936
220  
221 # make new states if none exist and track
222 3936 ▶ if ( !exists( $NEW{$currentid} ) ) { $NEW{$currentid} = $result->add_states(1) }
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $NEW{$currentid}) 105 3831 3936
223 3936 ▶ if ( !exists( $NEW{$nextid} ) ) { $NEW{$nextid} = $result->add_states(1) }
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $NEW{$nextid}) 1899 2037 3936
224 3936 $result->add_transition( $NEW{$currentid}, $NEW{$nextid}, $symbol );
225 }
226 }
227 }
228 }
229 }
230 137 $result->set_starting( $NEW{ join( ",", sort $self->get_starting() ) } );
231 137 $result->set_accepting( $NEW{ join( ",", sort $self->get_accepting() ) } );
232 137 return $result;
233 }
234  
235 3 1;
236  
237 __END__
« FLAT/CMD.pm ↑ Summary FLAT/FA.pm »