blib/lib/Grammar/Graph/Simplify.pm

filter: Statementstmt 27.4% 39 / 142 Branchbran 0.0% 0 / 60 Conditioncond 0.0% 0 / 13 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 50.0% 11 / 22 Podpod 0.0% 0 / 1 totaltotal 20.3% 50 / 246 CC 48 SCAR 55.5
CC
48
Cov
18%
fa_merge_equivalent_vertices
650.0
__ANON__
110.0
__ANON__
42.0
CRAP 258.1
↑ Summary
1 #####################################################################
2 # Grammar::Graph
3 #####################################################################
4 package Grammar::Graph::Simplify;
5 2 ▶use 5.012000;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Grammar::Graph;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Grammar::Formal;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Algorithm::ConstructDFA::XS 0.13;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use List::UtilsBy qw/partition_by/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use List::MoreUtils qw/uniq/;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use List::Util qw/shuffle sum max/;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Storable qw/freeze thaw/;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use Graph::SomeUtils qw/:all/;
Subroutine BEGIN: called CC=1 SCAR=0.0
16
17 2 local $Storable::canonical = 1;
18
19 2 our $VERSION = '0.02';
20
21 2 our %EXPORT_TAGS = ( 'all' => [ qw(
22
23 ) ] );
24
25 2 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
26
27 2 our @EXPORT = qw(
28 );
29
30 0 ▶sub Grammar::Graph::fa_merge_equivalent_vertices {
Subroutine fa_merge_equivalent_vertices: not called CC=25 SCAR=64.8
Pod undocumented
31 0 my ($g, $start_vertex, $final_vertex) = @_;
32
33 0 ▶ die if $g->get_graph_attribute('ran_fa_merge_equivalent_vertices');
Branch: 1 branch
BranchTrueFalseTotal
if $g->get_graph_attribute("ran_fa_merge_equivalent_vertices") 0 0 0
34 0 $g->set_graph_attribute('ran_fa_merge_equivalent_vertices', 'yes');
35
36 0 ▶ my $signature = sub {
Subroutine __ANON__: not called CC=10 SCAR=47.0
37 0 my ($g, $label) = @_;
38
39 0 my $symbols = $g->get_graph_attribute('symbol_table');
40
41 0 ▶ if (not defined $label) {
Branch: 8 branches
BranchTrueFalseTotal
if (not defined $label) { } 0 0 0
elsif ($label->isa('Grammar::Graph::StartOf')) { } 0 0 0
elsif ($label->isa('Grammar::Graph::FinalOf')) { } 0 0 0
elsif ($label->isa('Grammar::Graph::Prefix')) { } 0 0 0
elsif ($label->isa('Grammar::Graph::Suffix')) { } 0 0 0
elsif ($label->isa('Grammar::Formal::CharClass')) { } 0 0 0
elsif ($label->isa('Grammar::Formal::Reference')) { } 0 0 0
elsif ($label->isa('Grammar::Formal::Empty')) { } 0 0 0
42 0 return;
43 } elsif ($label->isa('Grammar::Graph::StartOf')) {
44 0 return "start " . $symbols->{ $label->of }{shortname};
45 } elsif ($label->isa('Grammar::Graph::FinalOf')) {
46 0 return "final " . $symbols->{ $label->of }{shortname};
47 } elsif ($label->isa('Grammar::Graph::Prefix')) {
48 0 return "prefix " . $label->link;
49 } elsif ($label->isa('Grammar::Graph::Suffix')) {
50 0 return "suffix " . $label->link;
51 } elsif ($label->isa('Grammar::Formal::CharClass')) {
52 0 ▶ die "Bad label" if $label->spans->empty;
Branch: 1 branch
BranchTrueFalseTotal
if $label->spans->empty 0 0 0
53 0 return '' . $label->spans;
54 } elsif ($label->isa('Grammar::Formal::Reference')) {
55 ...
56 0 } elsif ($label->isa('Grammar::Formal::Empty')) {
57 0 return;
58 } else {
59 ...
60 0 }
61 0 };
62
63 0 ▶ my $get_classes = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
64 0 my ($start_vertex, $final_vertex, $sub) = @_;
65
66 my $dfa = Algorithm::ConstructDFA::XS::construct_dfa_xs(
67 start => [ $start_vertex ],
68 0 ▶ is_accepting => sub { grep { $_ eq $final_vertex } @_ },
Subroutine __ANON__: not called CC=1 SCAR=6.9
69 0 ▶ is_nullable => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
70 0 my $label = $g->get_vertex_attribute($_[0], 'label');
71 0 ▶ return 1 unless defined $label;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $label 0 0 0
72 0 ▶ return 1 if ref $label eq 'Grammar::Formal::Empty';
Branch: 1 branch
BranchTrueFalseTotal
if ref $label eq "Grammar::Formal::Empty" 0 0 0
73 0 return 0;
74 },
75 successors => $sub,
76 0 ▶ get_label => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
77 0 my $label = $g->get_vertex_attribute($_[0], 'label');
78 0 ▶ return unless defined $label;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $label 0 0 0
79 0 ▶ return if ref $label eq 'Grammar::Formal::Empty';
Branch: 1 branch
BranchTrueFalseTotal
if ref $label eq "Grammar::Formal::Empty" 0 0 0
80 0 return $signature->($g, $label);
81 0 return;
82 },
83 0 );
84
85 0 my %delta;
86 0 for my $s (keys %$dfa) {
87 0 $delta{$_}->{$s}++ for @{ $dfa->{$s}{Combines} };
88 }
89
90 0 ▶ my %h = partition_by {
Subroutine __ANON__: not called CC=1 SCAR=6.9
91 0 join ' ', sort keys %{ $delta{$_} }
92 0 } keys %delta;
93
94 0 return values %h;
95 0 };
96
97 0 while (1) {
98 0 my $changed = 0;
99 0 ▶ my @fwd = $get_classes->($start_vertex, $final_vertex, sub { $g->successors($_[0]); });
Subroutine __ANON__: not called CC=1 SCAR=6.9
100 0 ▶ my @bck = $get_classes->($final_vertex, $start_vertex, sub { $g->predecessors($_[0]); });
Subroutine __ANON__: not called CC=1 SCAR=6.9
101 0 my @eq;
102
103 0 for my $x (@fwd, @bck) {
104 0 ▶ push @eq, grep { @$_ > 1 } values %{{ partition_by {
Subroutine __ANON__: not called CC=4 SCAR=30.0
105 0 my $label = $g->get_vertex_attribute($_, 'label');
106 0 ▶ return 'prefix' if $label->isa('Grammar::Graph::Prefix');
Branch: 1 branch
BranchTrueFalseTotal
if $label->isa("Grammar::Graph::Prefix") 0 0 0
107 0 ▶ return 'suffix' if $label->isa('Grammar::Graph::Suffix');
Branch: 1 branch
BranchTrueFalseTotal
if $label->isa("Grammar::Graph::Suffix") 0 0 0
108 0 ▶ return $signature->($g, $label) // '';
Condition0%0/2
&$signature($g, $label) // ""
l!l
00
MC/DC0%0/1
&$signature($g, $label) // ""
&$signature($g, $label)
Truth table0%0/2
A
A= &$signature($g, $label)
Aresult
11
00
109 0 } @$x }};
110 }
111
112 0 my %cappa;
113
114 0 for my $group (@eq) {
115 0 my $label0 = $g->get_vertex_attribute($group->[0], 'label');
116 0 ▶ next unless $label0->isa('Grammar::Graph::Prefix')
Branch: 1 branch
BranchTrueFalseTotal
unless $label0->isa("Grammar::Graph::Prefix") or $label0->isa("Grammar::Graph::Suffix") 0 0 0
Condition0%0/3
$label0->isa("Grammar::Graph::Prefix") or $label0->isa("Grammar::Graph::Suffix")
l!l&&r!l&&!r
000
MC/DC0%0/2
$label0->isa("Grammar::Graph::Prefix") or $label0->isa("Grammar::Graph::Suffix")
$label0->isa("Grammar::Graph::Prefix") $label0->isa("Grammar::Graph::Suffix")
Truth table0%0/3
A or B
A= $label0->isa("Grammar::Graph::Prefix")B= $label0->isa("Grammar::Graph::Suffix")
ABresult
1X1
011
000
117 or $label0->isa('Grammar::Graph::Suffix');
118
119 0 for my $v1 (@$group) {
120 0 for my $v2 (@$group) {
121 0 $cappa{$v1}->{$v2}++;
122 }
123 }
124 }
125
126 0 my %ren;
127 0 for my $v1 (sort keys %cappa) {
128 0 for my $v2 (sort keys %{$cappa{$v1}}) {
129 0 my $label1 = $g->get_vertex_attribute($v1, 'label');
130 0 my $label2 = $g->get_vertex_attribute($v2, 'label');
131 0 my ($p1, $s1) = split/ # /, $label1->link, 2;
132 0 my ($p2, $s2) = split/ # /, $label2->link, 2;
133 0 ▶ next if $p1 eq $p2;
Branch: 1 branch
BranchTrueFalseTotal
if $p1 eq $p2 0 0 0
134 0 ▶ next if $s1 eq $s2;
Branch: 1 branch
BranchTrueFalseTotal
if $s1 eq $s2 0 0 0
135 next unless $cappa{$p1}->{$p2} and
136 0 ▶ $cappa{$s1}->{$s2};
Branch: 1 branch
BranchTrueFalseTotal
unless $cappa{$p1}{$p2} and $cappa{$s1}{$s2} 0 0 0
Condition0%0/3
$cappa{$p1}{$p2} and $cappa{$s1}{$s2}
!ll&&!rl&&r
000
MC/DC0%0/2
$cappa{$p1}{$p2} and $cappa{$s1}{$s2}
$cappa{$p1}{$p2} $cappa{$s1}{$s2}
Truth table0%0/3
A and B
A= $cappa{$p1}{$p2}B= $cappa{$s1}{$s2}
ABresult
0X0
100
111
137
138 0 ▶ $ren{"$p1 $s1"} = $ren{"$p2 $s2"} // [$p2, $s2];
Condition0%0/2
$ren{"$p2 $s2"} // [$p2, $s2]
l!l
00
MC/DC0%0/1
$ren{"$p2 $s2"} // [$p2, $s2]
$ren{"$p2 $s2"}
Truth table0%0/2
A
A= $ren{"$p2 $s2"}
Aresult
11
00
139 }
140 }
141
142 0 ▶ my $replace = sub {
Subroutine __ANON__: not called CC=6 SCAR=37.4
143 0 my ($g, $goes, $stays) = @_;
144
145 0 ▶ return if $goes eq $stays;
Branch: 1 branch
BranchTrueFalseTotal
if $goes eq $stays 0 0 0
146
147 0 ▶ unless ($g->has_vertex($goes)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($g->has_vertex($goes)) 0 0 0
148 # warn $goes . " not in graph";
149 0 return;
150 }
151
152 0 ▶ unless ($g->has_vertex($stays)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($g->has_vertex($stays)) 0 0 0
153 # warn "Cannot replace $goes by $stays because $stays does not exist";
154 0 return;
155 }
156
157 # warn "replacing $goes by $stays\n";
158
159 0 for my $p ($g->predecessors($goes)) {
160 0 $g->add_edge($p, $stays);
161 }
162 0 for my $s ($g->successors($goes)) {
163 0 $g->add_edge($stays, $s);
164 }
165
166 0 graph_delete_vertex_fast($g, $goes);
167
168 ###############################################################
169 # Note that this makes no effort to adjust the ->link attribute
170 # of the vertex, so while above we rely on the contents of it
171 # to be accurate, it cannot be relied upon after running this
172 # step. Ideally that should be fixed to maintain invariants.
173 ###############################################################
174 0 };
175
176 0 for my $k (sort keys %ren) {
177 0 my ($p1, $s1) = split/ /, $k, 2;
178 0 my ($p2, $s2) = @{ $ren{$k} };
179 0 $replace->($g, $p1, $p2);
180 0 $replace->($g, $s1, $s2);
181 0 ▶ next unless $p1 ne $p2 or $s1 ne $s2;
Branch: 1 branch
BranchTrueFalseTotal
unless $p1 ne $p2 or $s1 ne $s2 0 0 0
Condition0%0/3
$p1 ne $p2 or $s1 ne $s2
l!l&&r!l&&!r
000
MC/DC0%0/2
$p1 ne $p2 or $s1 ne $s2
$p1 ne $p2 $s1 ne $s2
Truth table0%0/3
A or B
A= $p1 ne $p2B= $s1 ne $s2
ABresult
1X1
011
000
182 0 $changed += 1;
183 }
184
185 0 ▶ next if $changed;
Branch: 1 branch
BranchTrueFalseTotal
if $changed 0 0 0
186
187 0 for my $group (@eq) {
188 0 my $label = $g->get_vertex_attribute($group->[0], 'label');
189 0 ▶ next unless $label;
Branch: 1 branch
BranchTrueFalseTotal
unless $label 0 0 0
190 0 ▶ next if $label->isa('Grammar::Graph::Prefix');
Branch: 1 branch
BranchTrueFalseTotal
if $label->isa("Grammar::Graph::Prefix") 0 0 0
191 0 ▶ next if $label->isa('Grammar::Graph::Suffix');
Branch: 1 branch
BranchTrueFalseTotal
if $label->isa("Grammar::Graph::Suffix") 0 0 0
192
193 0 ▶ for (my $ix = 1; $ix < @$group; ++$ix) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ix < @$group) 0 0 0
194 0 $replace->($g, $group->[$ix], $group->[0]);
195 0 $changed += 1;
196 }
197 }
198
199 0 ▶ last unless $changed;
Branch: 1 branch
BranchTrueFalseTotal
unless $changed 0 0 0
200 }
201 }
202
203
204 2 1;
205
206 __END__
↑ Summary