blib/lib/Algorithm/ConstructDFA.pm

filter: Statementstmt 98.6% 147 / 149 Branchbran 87.5% 49 / 56 Conditioncond 71.4% 15 / 21 MC/DCmcdc 50.0% 6 / 12 Subroutinesub 100% 21 / 21 Podpod 100% 1 / 1 totaltotal 91.9% 239 / 260 CC 38 SCAR 24.4
CC
38
Cov
93%
_get_graph
22.0
construct_dfa
13.1
__ANON__
4.0
CRAP 11.5
↑ Summary
1 package Algorithm::ConstructDFA;
2
3 3 ▶use 5.012000;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Storable qw/freeze thaw/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use List::UtilsBy qw/partition_by/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use List::MoreUtils qw/uniq/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Data::AutoBimap;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Memoize;
Subroutine BEGIN: called CC=1 SCAR=0.0
12
13 3 our $VERSION = '0.03';
14
15 3 our %EXPORT_TAGS = ( 'all' => [ qw(
16 construct_dfa
17 ) ] );
18
19 3 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
20
21 3 our @EXPORT = qw(
22 construct_dfa
23 );
24
25 3 local $Storable::canonical = 1;
26
27 84 ▶sub _memoizess {
Subroutine _memoizess: called CC=1 SCAR=0.0
28 84 my ($sub) = @_;
29 84 my %cache;
30 19872 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
31 19872 my ($s) = @_;
32 19872 ▶ if (not exists $cache{$s}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $cache{$s}) 694 19178 19872
33 694 $cache{$s} = $sub->($s);
34 }
35 19872 return $cache{$s};
36 84 };
37 }
38
39 28 ▶sub _get_graph {
Subroutine _get_graph: called CC=22 SCAR=30.9
40 28 my ($roots, $labelf, $nullablef, $successorsf, $acceptingf) = @_;
41
42 28 my $m = Data::AutoBimap->new();
43
44 28 ▶ my $label = _memoizess(sub { $labelf->($m->n2s($_[0])) });
Subroutine __ANON__: called CC=1 SCAR=0.0
45
46 342 ▶ my $successors = memoize(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
47 342 map { $m->s2n($_) } $successorsf->($m->n2s($_[0]))
48 28 });
49
50 952 ▶ my $accepting = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
51 952 !!$acceptingf->(map { $m->n2s($_) } @_)
52 28 };
53
54 28 my %nullable;
55
56 342 ▶ my $nullable = _memoizess(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
57 342 !!$nullablef->($m->n2s($_[0]));
58 28 });
59
60 10 ▶ my $all_reachable_and_self = _memoizess(sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
61 10 my ($v) = @_;
62 10 my %seen;
63 10 my @todo = ($v);
64 10 ▶ while (@todo) {
Branch: 1 branch
BranchTrueFalseTotal
if (@todo) 59 10 69
65 59 my $c = pop @todo;
66 59 ▶ next if $seen{$c}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$c}++ 8 51 59
67 51 ▶ push @todo, $successors->($c) if $nullable->($c);
Branch: 1 branch
BranchTrueFalseTotal
if &$nullable($c) 26 25 51
68 }
69 10 [keys %seen];
70 28 });
71
72 1409 ▶ my $all_reachable_and_self_many = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
73 1409 my %seen;
74 1409 my @todo = (@_);
75 1409 ▶ while (@todo) {
Branch: 1 branch
BranchTrueFalseTotal
if (@todo) 16602 1409 18011
76 16602 my $c = pop @todo;
77 16602 ▶ next if $seen{$c}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$c}++ 4359 12243 16602
78 12243 ▶ push @todo, $successors->($c) if $nullable->($c);
Branch: 1 branch
BranchTrueFalseTotal
if &$nullable($c) 3046 9197 12243
79 }
80 1409 keys %seen;
81 28 };
82
83 my $start = [
84 84 sort { $a cmp $b }
85 uniq map {
86 28 ▶ $nullable->($_) ? @{ $all_reachable_and_self->($_) } : $_
Branch: 1 branch
BranchTrueFalseTotal
&$nullable($_) ? : 10 18 28
87 }
88 28 map { $m->s2n($_) }
89 @$roots
90 ];
91
92 28 my $start_s = join ' ', @$start;
93
94 28 my @todo = ($start);
95 28 my %seen;
96 my $dfa;
97
98 28 my @accepting_dfa_states;
99 28 my %predecessors;
100
101 28 ▶ while (@todo) {
Branch: 1 branch
BranchTrueFalseTotal
if (@todo) 1437 28 1465
102 1437 my $src = pop @todo;
103 1437 my @src = @{ $src };
104 1437 my $src_s = join ' ', @src;
105 1437 ▶ next if $seen{$src_s}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$src_s}++ 953 484 1437
106
107 484 my $src_accepts = $accepting->(@src);
108 484 ▶ push @accepting_dfa_states, $src_s if $src_accepts;
Branch: 1 branch
BranchTrueFalseTotal
if $src_accepts 204 280 484
109
110 3770 ▶ my %p = partition_by { $label->($_) }
Subroutine __ANON__: called CC=1 SCAR=0.0
111 484 grep { defined $label->($_) } @src;
112
113 484 ▶ while (my ($k, $v) = each %p) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %p) 1409 484 1893
114 30978 my @dst = sort { $a cmp $b } uniq
115 1409 $all_reachable_and_self_many->(map { $successors->($_) } @$v);
116
117 1409 push @todo, \@dst;
118 1409 my $dst_s = join ' ', @dst;
119 1409 $dfa->{$src_s}->{$k} = $dst_s;
120 1409 $predecessors{$dst_s}->{$src_s}++;
121 }
122 }
123
124 28 my %reachable = do {
125 28 my %seen;
126 28 my @todo = @accepting_dfa_states;
127 28 ▶ while (@todo) {
Branch: 1 branch
BranchTrueFalseTotal
if (@todo) 1481 28 1509
128 1481 my $c = pop @todo;
129 1481 ▶ next if $seen{$c}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$c}++ 1041 440 1481
130 440 push @todo, keys %{ $predecessors{$c} };
131 }
132 28 map { $_ => 1 } keys %seen;
133 };
134
135 28 my $o = Data::AutoBimap->new(start => 0);
136
137 # Ensure that DFA state 0 is the one that corresponds to no
138 # vertices in the input graph. This is an API convention and
139 # does not have significance beyond that.
140 28 my $r = { $o->s2n('') => {
141 Combines => [],
142 Accepts => $accepting->()
143 } };
144
145 # Ensure start state is 1 also as a convention
146 28 $o->s2n($start_s);
147
148 28 ▶ while (my ($src, $x) = each %$dfa) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$dfa) 459 28 487
149
150 # Merge dead states
151 459 ▶ $src = '' unless $reachable{$src};
Branch: 1 branch
BranchTrueFalseTotal
unless $reachable{$src} 19 440 459
152
153 459 my @src_combines = map { $m->n2s($_) } split/ /, $src;
154 459 ▶ $r->{$o->s2n($src)}{Combines} //= \@src_combines;
Condition100%2/2
$$r{$o->s2n($src)}{"Combines"} //= \@src_combines
l!l
296163
MC/DC100%1/1
$$r{$o->s2n($src)}{"Combines"} //= \@src_combines
$$r{$o->s2n($src)}{"Combines"}
Truth table100%2/2
A
A= $$r{$o->s2n($src)}{"Combines"}
Aresult
11
00
155 0 $r->{$o->s2n($src)}{Combines} = [ sort { $a cmp $b }
156 459 ▶ uniq (@{$r->{$o->s2n($src)}{Combines} // []}, @src_combines) ]
Branch: 1 branch
BranchTrueFalseTotal
if $src eq "" 19 440 459
Condition50%1/2
$$r{$o->s2n($src)}{"Combines"} // []
l!l
190
MC/DC0%0/1
$$r{$o->s2n($src)}{"Combines"} // []
$$r{$o->s2n($src)}{"Combines"}
Truth table50%1/2
A
A= $$r{$o->s2n($src)}{"Combines"}
Aresult
11
00
157 if $src eq '';
158
159 $r->{$o->s2n($src)}{Accepts} //=
160 459 ▶ 0 + $accepting->(split/ /, $src);
Condition100%2/2
$$r{$o->s2n($src)}{"Accepts"} //= 0 + &$accepting(split(/ /u, $src, 0))
l!l
296163
MC/DC100%1/1
$$r{$o->s2n($src)}{"Accepts"} //= 0 + &$accepting(split(/ /u, $src, 0))
$$r{$o->s2n($src)}{"Accepts"}
Truth table100%2/2
A
A= $$r{$o->s2n($src)}{"Accepts"}
Aresult
11
00
161
162 459 ▶ while (my ($k, $dst) = each %{$x}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$x;}) 1409 459 1868
163
164 1409 ▶ $dst = '' unless $reachable{$dst};
Branch: 1 branch
BranchTrueFalseTotal
unless $reachable{$dst} 131 1278 1409
165
166 1409 $r->{$o->s2n($src)}{NextOver}{$k} = $o->s2n($dst);
167
168 1409 ▶ if ((not defined $r->{$o->s2n($dst)}{Combines}) or $dst eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $$r{$o->s2n($dst)}{"Combines"} or $dst eq "") 408 1001 1409
Condition100%3/3
not defined $$r{$o->s2n($dst)}{"Combines"} or $dst eq ""
l!l&&r!l&&!r
2771311001
MC/DC100%2/2
not defined $$r{$o->s2n($dst)}{"Combines"} or $dst eq ""
not defined $$r{$o->s2n($dst)}{"Combines"} $dst eq ""
Truth table100%3/3
not(A) or B
A= not defined $$r{$o->s2n($dst)}{"Combines"}B= $dst eq ""
ABresult
1X1
011
000
169 408 my @dst_combines = map { $m->n2s($_) } split/ /, $dst;
170 408 ▶ $r->{$o->s2n($dst)}{Combines} //= \@dst_combines;
Condition100%2/2
$$r{$o->s2n($dst)}{"Combines"} //= \@dst_combines
l!l
131277
MC/DC100%1/1
$$r{$o->s2n($dst)}{"Combines"} //= \@dst_combines
$$r{$o->s2n($dst)}{"Combines"}
Truth table100%2/2
A
A= $$r{$o->s2n($dst)}{"Combines"}
Aresult
11
00
171 0 $r->{$o->s2n($dst)}{Combines} = [ sort { $a cmp $b }
172 408 ▶ uniq (@{$r->{$o->s2n($dst)}{Combines} // []}, @dst_combines) ]
Branch: 1 branch
BranchTrueFalseTotal
if $dst eq "" 131 277 408
Condition50%1/2
$$r{$o->s2n($dst)}{"Combines"} // []
l!l
1310
MC/DC0%0/1
$$r{$o->s2n($dst)}{"Combines"} // []
$$r{$o->s2n($dst)}{"Combines"}
Truth table50%1/2
A
A= $$r{$o->s2n($dst)}{"Combines"}
Aresult
11
00
173 if $dst eq '';
174 }
175
176 $r->{$o->s2n($dst)}{Accepts} //=
177 1409 ▶ 0 + $accepting->(split/ /, $dst);
Condition100%2/2
$$r{$o->s2n($dst)}{"Accepts"} //= 0 + &$accepting(split(/ /u, $dst, 0))
l!l
1132277
MC/DC100%1/1
$$r{$o->s2n($dst)}{"Accepts"} //= 0 + &$accepting(split(/ /u, $dst, 0))
$$r{$o->s2n($dst)}{"Accepts"}
Truth table100%2/2
A
A= $$r{$o->s2n($dst)}{"Accepts"}
Aresult
11
00
178 }
179 }
180
181 28 return $r;
182 }
183
184 28 ▶sub construct_dfa {
Subroutine construct_dfa: called CC=10 SCAR=25.7
Pod documented
185 28 my (%o) = @_;
186
187 28 ▶ die unless ref $o{is_nullable};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $o{"is_nullable"} 0 28 28
188 28 ▶ die unless ref $o{is_accepting} or exists $o{final};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $o{"is_accepting"} or exists $o{"final"} 0 28 28
Condition33%1/3
ref $o{"is_accepting"} or exists $o{"final"}
l!l&&r!l&&!r
0280
MC/DC0%0/2
ref $o{"is_accepting"} or exists $o{"final"}
ref $o{"is_accepting"} exists $o{"final"}
Truth table33%1/3
A or B
A= ref $o{"is_accepting"}B= exists $o{"final"}
ABresult
1X1
011
000
189 28 ▶ die unless ref $o{successors};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $o{"successors"} 0 28 28
190 28 ▶ die unless ref $o{get_label};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $o{"get_label"} 0 28 28
191 28 ▶ die unless exists $o{start};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $o{"start"} 0 28 28
192 28 ▶ die if ref $o{is_accepting} and exists $o{final};
Branch: 1 branch
BranchTrueFalseTotal
if ref $o{"is_accepting"} and exists $o{"final"} 0 28 28
Condition33%1/3
ref $o{"is_accepting"} and exists $o{"final"}
!ll&&!rl&&r
2800
MC/DC0%0/2
ref $o{"is_accepting"} and exists $o{"final"}
ref $o{"is_accepting"} exists $o{"final"}
Truth table33%1/3
A and B
A= ref $o{"is_accepting"}B= exists $o{"final"}
ABresult
0X0
100
111
193
194 28 ▶ if (exists $o{final}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $o{"final"}) 28 0 28
195 28 my %in_final = map { $_ => 1 } @{ $o{final} };
196 952 ▶ $o{is_accepting} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
197 952 grep { $in_final{$_} } @_
198 28 };
199 }
200
201 _get_graph($o{start}, $o{get_label}, $o{is_nullable},
202 28 $o{successors}, $o{is_accepting});
203
204 }
205
206
207 3 1;
208
209 __END__
↑ Summary