Scigen/scigen.pm

filter: Statementstmt 70.1% 115 / 164 Branchbran 60.0% 48 / 80 Conditioncond 66.6% 4 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 100% 12 / 12 Podpod 0.0% 0 / 10 totaltotal 65.5% 181 / 276 CC 45 SCAR 33.2
CC
45
Cov
67%
expand
56.7
pretty_print
25.1
read_rules
20.6
CRAP 27.5
↑ Summary Scigen.pm »
1 package Text::Scigen::scigen;
2  
3 # This file is part of SCIgen.
4 #
5 # SCIgen is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # SCIgen is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with SCIgen; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  
19  
20 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use vars qw($SCIGEND_PORT);
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 #### daemon settings ####
24 1 $SCIGEND_PORT = 4724;
25  
26 9 ▶sub dup_name {
Subroutine dup_name: called CC=1 SCAR=0.0
Pod undocumented
27 9 my $name = shift;
28 9 return $name . "!!!";
29 }
30  
31 2 ▶sub file_name {
Subroutine file_name: called CC=1 SCAR=0.0
Pod undocumented
32 2 my $name = shift;
33 2 return $name . ".file";
34 }
35  
36 2 ▶sub read_rules {
Subroutine read_rules: called CC=19 SCAR=30.2
Pod undocumented
37 2 my ($fh, $rules, $RE, $debug, $self) = @_;
38 2 my $line;
39 2 ▶ while ($line = <$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($line = readline $fh)) 3979 2 3981
40 3979 ▶ next if $line =~ /^#/ ;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^#/ 129 3850 3979
41 3850 ▶ next if $line !~ /\S/ ;
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /\S/ 674 3176 3850
42  
43 3176 my @words = split /\s+/, $line;
44 3176 my $name = shift @words;
45 3176 my $rule = "";
46  
47 # non-duplicate rule
48 3176 ▶ if( $name =~ /([^\+]*)\!$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /([^\+]*)\!$/) 3 3173 3176
49 3 $name = $1;
50 3 push @{$rules->{dup_name("$name")}}, "";
51 3 next;
52 }
53  
54 # include rule
55 3173 ▶ if( $name =~ /\.include$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /\.include$/) 1 3172 3173
56 1 my $file = $words[0];
57 # make sure we haven't already included this file
58 # NOTE: this allows the main file to be included at most twice
59 1 ▶ if( defined $rules->{&file_name($file)} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $$rules{&file_name($file)}) { } 0 1 1
60 0 ▶ if( $debug > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug > 0) 0 0 0
61 0 print "Skipping duplicate included file $file\n";
62 }
63 0 next;
64 } else {
65 1 $rules->{&file_name($file)} = 1;
66 }
67 1 ▶ if( $debug > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug > 0) 0 1 1
68 0 print "Opening included file $file\n";
69 }
70 1 my $path = $self->_find( $file );
71 1 ▶ open(my $inc_fh, "<:utf8", $path)
Branch: 1 branch
BranchTrueFalseTotal
open my $inc_fh, "<:utf8", $path or die "Couldn't open included file $path" 1 0 1
72 or die( "Couldn't open included file $path" );
73 1 &read_rules( $inc_fh, $rules, undef, $debug );
74 1 next; # we don't want to have .include itself be a rule
75 }
76  
77 3172 ▶ if ($#words == 0 && $words[0] eq '{') {
Branch: 1 branch
BranchTrueFalseTotal
if ($#words == 0 and $words[0] eq '{') { } 124 3048 3172
Condition100%3/3
$#words == 0 and $words[0] eq "{"
!ll&&!rl&&r
12641784124
MC/DC100%2/2
$#words == 0 and $words[0] eq "{"
$#words == 0 $words[0] eq "{"
Truth table100%3/3
A and B
A= $#words == 0B= $words[0] eq "{"
ABresult
0X0
100
111
78 124 my $end = 0;
79 124 ▶ while ($line = <$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($line = readline $fh)) 1147 0 1147
80 1147 ▶ if ($line =~ /^}[\r\n]+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^}[\r\n]+$/) { } 124 1023 1147
81 124 $end = 1;
82 124 last;
83 } else {
84 1023 $rule .= $line;
85 }
86 }
87 124 ▶ if (! $end) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($end) 0 124 124
88 0 die "$name: EOF found before close rule\n";
89 }
90 } else {
91 3048 $line =~ s/^\S+\s+//;
92 3048 chomp ($line);
93 3048 $rule = $line;
94 }
95  
96 # look for the weight
97 3172 my $weight = 1;
98 3172 ▶ if( $name =~ /([^\+]*)\+(\d+)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /([^\+]*)\+(\d+)$/) 196 2976 3172
99 196 $name = $1;
100 196 $weight = $2;
101 196 ▶ if( $debug > 10 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug > 10) 0 196 196
102 0 warn "weighting rule by $weight: $name -> $rule\n";
103 }
104 }
105  
106 3172 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
--$weight > 0 and do { do { push @{$$rules{$name};}, $rule } } 1713 3172 4885
107 4885 push @{$rules->{$name}}, $rule;
108 } while( --$weight > 0 );
109 }
110  
111 2 ▶ if( defined $RE ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $RE) 1 1 2
112 1 compute_re( $rules, $RE );
113 }
114  
115 }
116  
117 1 ▶sub compute_re {
Subroutine compute_re: called CC=1 SCAR=0.0
Pod undocumented
118  
119 # must sort; order matters, and we want to make sure that we get
120 # the longest matches first
121 1 my ($rules, $RE) = @_;
122 1 my $in = join "|", sort { length ($b) <=> length ($a) } keys %$rules;
123 1 $$RE = qr/^(.*?)(${in})/s ;
124  
125 }
126  
127 1 ▶sub generate {
Subroutine generate: called CC=2 SCAR=7.0
Pod undocumented
128 1 my ($rules, $start, $RE, $debug, $pretty) = @_;
129  
130  
131 1 my $s = expand ($rules, $start, $RE, $debug);
132 1 ▶ if( $pretty ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pretty) 1 0 1
133 1 $s = pretty_print($s);
134 }
135 1 return $s;
136 }
137  
138 6 ▶sub pick_rand {
Subroutine pick_rand: called CC=1 SCAR=0.0
Pod undocumented
139 6 my ($set) = @_;
140 6 my $n = $#$set + 1;
141 6 my $v = @$set[int (rand () * $n)];
142 6 return $v;
143 }
144  
145 11 ▶sub pop_first_rule {
Subroutine pop_first_rule: called CC=2 SCAR=6.9
Pod undocumented
146 11 my ($rules, $preamble, $input, $rule, $RE) = @_;
147  
148 11 $$preamble = undef;
149 11 $$rule = undef;
150  
151 11 my $ret = undef;
152
153 11 ▶ if ($$input =~ s/$RE//s ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$input =~ s/$RE//s) 5 6 11
154 5 $$preamble = $1;
155 5 $$rule = $2;
156 5 return 1;
157 }
158
159 6 return 0;
160 }
161  
162 1 ▶sub pretty_print {
Subroutine pretty_print: called CC=9 SCAR=32.2
Pod undocumented
163  
164 1 my ($s) = shift;
165  
166 1 my $news = "";
167 1 my @lines = split( /\n/, $s );
168 1 foreach my $line (@lines) {
169  
170 1 my $newline = "";
171  
172 1 $line =~ s/(\s+)([\.\,\?\;\:])/$2/g;
173 1 $line =~ s/(\b)(a)\s+([aeiou])/$1$2n $3/gi;
174  
175 1 ▶ if( $line =~ /\\section(\*?){(.*)}/ ) {
Branch: 5 branches
BranchTrueFalseTotal
if ($line =~ /\\section(\*?){(.*)}/) { } 0 1 1
elsif ($line =~ /(\\subsection){(.*)}/ or $line =~ /(\\slideheading){(.*)}/) { } 0 1 1
elsif ($line =~ /\\title{(.*)}/) { } 0 1 1
elsif ($line =~ /(.*) = {(.*)}\,/) { } 0 1 1
elsif ($line =~ /\S/) { } 1 0 1
176 0 $newline = "\\section${1}{" .
177 Text::Autoformat::autoformat( $2, { case => 'highlight',
178 squeeze => 0 } );
179 0 chomp $newline;
180 0 chomp $newline;
181 0 $newline .= "}";
182 ▶ } elsif( $line =~ /(\\subsection){(.*)}/ or
Condition33%1/3
$line =~ /(\\subsection){(.*)}/ or $line =~ /(\\slideheading){(.*)}/
l!l&&r!l&&!r
001
MC/DC0%0/2
$line =~ /(\\subsection){(.*)}/ or $line =~ /(\\slideheading){(.*)}/
$line =~ /(\\subsection){(.*)}/ $line =~ /(\\slideheading){(.*)}/
Truth table33%1/3
A or B
A= $line =~ /(\\subsection){(.*)}/B= $line =~ /(\\slideheading){(.*)}/
ABresult
1X1
011
000
183 $line =~ /(\\slideheading){(.*)}/ ) {
184 0 $newline = $1 . "{" .
185 Text::Autoformat::autoformat( $2, { case => 'highlight',
186 squeeze => 0 } );
187 0 chomp $newline;
188 0 chomp $newline;
189 0 $newline .= "}";
190 } elsif( $line =~ /\\title{(.*)}/ ) {
191 0 $newline = "\\title{" .
192 Text::Autoformat::autoformat( $1, { case => 'highlight',
193 squeeze => 0 } );
194 0 chomp $newline;
195 0 chomp $newline;
196 0 $newline .= "}";
197 } elsif( $line =~ /(.*) = {(.*)}\,/ ) {
198 0 my $label = $1;
199 0 my $curr = $2;
200 # place brackets around any words containing capital letters
201 0 $curr =~ s/\b([^\s]*[A-Z]+[^\s\:]*)\b/\{$1\}/g;
202 0 $newline = "$label = {" .
203 Text::Autoformat::autoformat( $curr, { case => 'highlight',
204 squeeze => 0 } );
205 0 chomp $newline;
206 0 chomp $newline;
207 0 $newline .= "},";
208 } elsif( $line =~ /\S/ ) {
209 1 $newline =
210 Text::Autoformat::autoformat( $line, { case => 'sentence',
211 squeeze => 0,
212 break => break_latex(),
213 ignore => qr/^\\/ } );
214 }
215  
216 1 $newline =~ s/\\Em/\\em/g;
217  
218 1 ▶ if( $newline !~ /\n$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($newline =~ /\n$/) 0 1 1
219 0 $newline .= "\n";
220 }
221 1 $news .= $newline;
222  
223 }
224  
225 1 return $news;
226 }
227  
228 1 ▶sub break_latex($$$) {
Subroutine break_latex: called CC=2 SCAR=7.0
Pod undocumented
229 1 my ($text, $reqlen, $fldlen) = @_;
230 1 ▶ if( !defined $text ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $text) 1 0 1
231 1 $text = "";
232 }
233 1 return { $text, "" };
234 }
235  
236 6 ▶sub expand {
Subroutine expand: called CC=16 SCAR=40.4
Pod undocumented
237 6 my ($rules, $start, $RE, $debug) = @_;
238  
239 # check for special rules ending in + and #
240 # Rules ending in + generate a sequential integer
241 # The same rule ending in # chooses a random # from among preiously
242 # generated integers
243 6 ▶ if( $start =~ /(.*)\+$/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($start =~ /(.*)\+$/) { } 0 6 6
elsif ($start =~ /(.*)\#$/) { } 0 6 6
244 0 my $rule = $1;
245 0 my $i = $rules->{$rule};
246 0 ▶ if( !defined $i ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $i) { } 0 0 0
247 0 $i = 0;
248 0 $rules->{$rule} = 1;
249 } else {
250 0 $rules->{$rule} = $i+1;
251 }
252 0 return $i;
253 } elsif( $start =~ /(.*)\#$/ ) {
254 0 my $rule = $1;
255 0 my $i = $rules->{$rule};
256 0 ▶ if( !defined $i ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $i) { } 0 0 0
257 0 $i = 0;
258 } else {
259 0 $i = int rand $i;
260 }
261 0 return $i;
262 }
263 6 my $full_token;
264  
265 6 my $repeat = 0;
266 6 my $count = 0;
267 6 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$repeat and do { do { my $input = pick_rand($rules->{$start}); ++$count; if ($debug >= 5) { warn "$start -> $input\n"; } ; my($pre, $rule); my @components; $repeat = 0; while (pop_first_rule($rules, \$pre, \$input, \$rule, $RE)) { my $ex = expand($rules, $rule, $RE, $debug); push @components, $pre if length $pre; push @components, $ex if length $ex; } ; push @components, $input if length $input; $full_token = join("", @components); my $ref = $$rules{dup_name("$start")}; if (defined $ref) { my(@dups) = @{$ref;}; foreach my $d (@dups) { if ($d eq $full_token) { $repeat = 1; } ; } ; if (not $repeat) { push @{$$rules{dup_name("$start")};}, $full_token; } elsif ($count > 50) { $repeat = 0; } ; } } } 0 6 6
268  
269 6 my $input = pick_rand ($rules->{$start});
270 6 $count++;
271 6 ▶ if ($debug >= 5) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug >= 5) 0 6 6
272 0 warn "$start -> $input\n";
273 }
274  
275 6 my ($pre, $rule);
276 6 my @components;
277 6 $repeat = 0;
278  
279 6 ▶ while (pop_first_rule ($rules, \$pre, \$input, \$rule, $RE)) {
Branch: 1 branch
BranchTrueFalseTotal
if (pop_first_rule($rules, \$pre, \$input, \$rule, $RE)) 5 6 11
280 5 my $ex = expand ($rules, $rule, $RE, $debug);
281 5 ▶ push @components, $pre if length ($pre);
Branch: 1 branch
BranchTrueFalseTotal
if length $pre 4 1 5
282 5 ▶ push @components, $ex if length ($ex);
Branch: 1 branch
BranchTrueFalseTotal
if length $ex 5 0 5
283 }
284 6 ▶ push @components, $input if length ($input);
Branch: 1 branch
BranchTrueFalseTotal
if length $input 4 2 6
285 6 $full_token = join "", @components ;
286 6 my $ref = $rules->{dup_name("$start")};
287 6 ▶ if( defined $ref ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ref) 0 6 6
288 0 my @dups = @{$ref};
289 # make sure we haven't generated this exact token yet
290 0 foreach my $d (@dups) {
291 0 ▶ if( $d eq $full_token ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($d eq $full_token) 0 0 0
292 0 $repeat = 1;
293 }
294 }
295
296 0 ▶ if( !$repeat ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $repeat) { } 0 0 0
elsif ($count > 50) { } 0 0 0
297 0 push @{$rules->{dup_name("$start")}}, $full_token;
298 } elsif( $count > 50 ) {
299 0 $repeat = 0;
300 }
301
302 }
303  
304 } while( $repeat );
305  
306 6 return $full_token;
307
308 }
309  
310  
311 1 1;
↑ Summary Scigen.pm »