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 SCAR 34.1
CC
29
Cov
89%
as_nfa
14.0
set_tied
6.0
unset_tied
6.0
CRAP 30.3
« FLAT/Regex/Op.pm ↑ Summary FLAT/CMD/AcyclicStrings.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 sub new {
22 546 ▶ my $pkg = shift;
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
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 sub singleton {
29 546 ▶ my ( $class, $char ) = @_;
Subroutine singleton: called CC=3 SCAR=12.9
Pod undocumented
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 647 ▶sub as_pfa { $_[0]->clone() }
Subroutine as_pfa: called CC=1 SCAR=0.0
Pod undocumented
51  
52 sub set_starting {
53 1455 ▶ my ( $self, @states ) = @_;
Subroutine set_starting: called CC=2 SCAR=6.9
Pod documented
54 1455 $self->_assert_states(@states);
55 1455 $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 sub pinch {
63 80 ▶ my $self = shift;
Subroutine pinch: called CC=3 SCAR=11.0
Pod undocumented
64 80 my $symbol = shift;
65 80 my @starting = $self->get_starting;
66 80 ▶ if ( @starting > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@starting > 1) 80 0 80
67 80 my $newstart = $self->add_states(1);
68 80 map { $self->add_transition( $newstart, $_, $symbol ) } @starting;
69 80 $self->unset_starting(@starting);
70 80 $self->set_starting($newstart);
71 }
72 #
73 80 my @accepting = $self->get_accepting;
74 80 ▶ if ( @accepting > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@accepting > 1) 80 0 80
75 80 my $newfinal = $self->add_states(1);
76 80 map { $self->add_transition( $_, $newfinal, $symbol ) } @accepting;
77 80 $self->unset_accepting(@accepting);
78 80 $self->set_accepting($newfinal);
79 }
80 80 return;
81 }
82  
83 # Implement the joining of two PFAs with lambda transitions
84 # Note: using epsilon pinches for simplicity
85 sub shuffle {
86 45 ▶ my @pfas = map { $_->as_pfa } @_;
Subroutine shuffle: called CC=2 SCAR=6.9
Pod documented
87 45 my $result = $pfas[0]->clone;
88 45 $result->_swallow($_) for @pfas[ 1 .. $#pfas ];
89 45 $result->pinch(LAMBDA);
90 45 $result;
91 }
92  
93 ##############
94  
95 sub is_tied {
96 0 ▶ my ( $self, $state ) = @_;
Subroutine is_tied: not called CC=1 SCAR=6.9
Pod undocumented
97 0 $self->_assert_states($state);
98 0 return $self->{STATES}[$state]{tied};
99 }
100  
101 sub set_tied {
102 0 ▶ my ( $self, @states ) = @_;
Subroutine set_tied: not called CC=2 SCAR=17.9
Pod undocumented
103 0 $self->_assert_states(@states);
104 0 $self->{STATES}[$_]{tied} = 1 for @states;
105 }
106  
107 sub unset_tied {
108 0 ▶ my ( $self, @states ) = @_;
Subroutine unset_tied: not called CC=2 SCAR=17.9
Pod undocumented
109 0 $self->_assert_states(@states);
110 0 $self->{STATES}[$_]{tied} = 0 for @states;
111 }
112  
113 sub get_tied {
114 0 ▶ my $self = shift;
Subroutine get_tied: not called CC=1 SCAR=6.9
Pod undocumented
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 sub union {
122 35 ▶ my @pfas = map { $_->as_pfa } @_;
Subroutine union: called CC=2 SCAR=6.9
Pod documented
123 35 my $result = $pfas[0]->clone;
124 35 $result->_swallow($_) for @pfas[ 1 .. $#pfas ];
125 35 $result->pinch('');
126 35 $result;
127 }
128  
129 # joins two PFAs via concatenation - no change from NFA
130 sub concat {
131 158 ▶ my @pfas = map { $_->as_pfa } @_;
Subroutine concat: called CC=5 SCAR=16.1
Pod documented
132  
133 158 my $result = $pfas[0]->clone;
134 158 my @newstate = ( [ $result->get_states ] );
135 158 my @start = $result->get_starting;
136  
137 158 for ( 1 .. $#pfas ) {
138 323 push @newstate, [ $result->_swallow( $pfas[$_] ) ];
139 }
140  
141 158 $result->unset_accepting( $result->get_states );
142 158 $result->unset_starting( $result->get_states );
143 158 $result->set_starting(@start);
144  
145 158 for my $pfa_id ( 1 .. $#pfas ) {
146 323 for my $s1 ( $pfas[ $pfa_id - 1 ]->get_accepting ) {
147 323 for my $s2 ( $pfas[$pfa_id]->get_starting ) {
148 323 $result->set_transition( $newstate[ $pfa_id - 1 ][$s1], $newstate[$pfa_id][$s2], "" );
149 }
150 }
151 }
152  
153 158 $result->set_accepting( @{ $newstate[-1] }[ $pfas[-1]->get_accepting ] );
154  
155 158 $result;
156 }
157  
158 # forms closure around a the given PFA - no change from NFA
159 sub kleene {
160 76 ▶ my $result = $_[0]->clone;
Subroutine kleene: called CC=3 SCAR=11.0
Pod documented
161  
162 76 my ( $newstart, $newfinal ) = $result->add_states(2);
163  
164 76 $result->set_transition( $newstart, $_, "" ) for $result->get_starting;
165 76 $result->unset_starting( $result->get_starting );
166 76 $result->set_starting($newstart);
167  
168 76 $result->set_transition( $_, $newfinal, "" ) for $result->get_accepting;
169 76 $result->unset_accepting( $result->get_accepting );
170 76 $result->set_accepting($newfinal);
171  
172 76 $result->set_transition( $newstart, $newfinal, "" );
173 76 $result->set_transition( $newfinal, $newstart, "" );
174  
175 76 $result;
176 }
177  
178 sub as_nfa {
179 137 ▶ my $self = shift;
Subroutine as_nfa: called CC=14 SCAR=26.4
Pod documented
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) 2390 137 2527
189 2390 my $current = pop(@Dstates);
190 2390 my $currentid = join( ',', @{$current} );
191 2390 $DONE{$currentid}++; # mark done
192 2390 foreach my $symbol ( $self->alphabet(), '' ) { # Sigma UNION epsilon
193 14705 ▶ if ( LAMBDA eq $symbol ) {
Branch: 1 branch
BranchTrueFalseTotal
if ('#lambda' eq $symbol) { } 1595 13110 14705
194 1595 my @NEXT = ();
195 1595 my @tmp = $self->successors( [ @{$current} ], $symbol );
196 1595 ▶ if (@tmp) {
Branch: 1 branch
BranchTrueFalseTotal
if (@tmp) 595 1000 1595
197 595 my @pred = $self->predecessors( [@tmp], LAMBDA );
198 595 ▶ if ( $self->array_is_subset( [@pred], [ @{$current} ] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->array_is_subset([@pred], [@{$current;}])) 94 501 595
199 94 push( @NEXT, @tmp, $self->array_complement( [ @{$current} ], [@pred] ) );
200 94 @NEXT = sort( $self->array_unique(@NEXT) );
201 94 my $nextid = join( ',', @NEXT );
202 94 ▶ push( @Dstates, [@NEXT] ) if ( !exists( $DONE{$nextid} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $DONE{$nextid} 90 4 94
203  
204 # make new states if none exist and track
205 94 ▶ if ( !exists( $NEW{$currentid} ) ) { $NEW{$currentid} = $result->add_states(1) }
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $NEW{$currentid}) 27 67 94
206 94 ▶ if ( !exists( $NEW{$nextid} ) ) { $NEW{$nextid} = $result->add_states(1) }
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $NEW{$nextid}) 90 4 94
207 94 $result->add_transition( $NEW{$currentid}, $NEW{$nextid}, '' );
208 }
209 }
210 }
211 else {
212 13110 foreach my $node ( @{$current} ) {
213 22064 my @tmp = $self->successors( [$node], $symbol );
214 22064 foreach my $new (@tmp) {
215 3914 my @NEXT = ();
216 3914 push( @NEXT, $new, $self->array_complement( [ @{$current} ], [$node] ) );
217 3914 @NEXT = sort( $self->array_unique(@NEXT) );
218 3914 my $nextid = join( ',', @NEXT );
219 3914 ▶ push( @Dstates, [@NEXT] ) if ( !exists( $DONE{$nextid} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $DONE{$nextid} 2163 1751 3914
220  
221 # make new states if none exist and track
222 3914 ▶ if ( !exists( $NEW{$currentid} ) ) { $NEW{$currentid} = $result->add_states(1) }
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $NEW{$currentid}) 110 3804 3914
223 3914 ▶ if ( !exists( $NEW{$nextid} ) ) { $NEW{$nextid} = $result->add_states(1) }
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $NEW{$nextid}) 1871 2043 3914
224 3914 $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/Regex/Op.pm ↑ Summary FLAT/CMD/AcyclicStrings.pm »