RandGen/Rule.pm

filter: Statementstmt 73.4% 83 / 113 Branchbran 42.1% 27 / 64 Conditioncond 29.1% 7 / 24 MC/DCmcdc 7.1% 1 / 14 Subroutinesub 82.3% 14 / 17 Podpod 33.3% 4 / 12 totaltotal 55.7% 136 / 244 CC 48 SCAR 27.8
CC
48
Cov
58%
production
72.0
check
20.0
dump
20.0
CRAP 16.1
« RandGen/Subrule.pm ↑ Summary RandGen/Literal.pm »
1 # $Revision: #3 $$Date: 2005/08/31 $$Author: jd150722 $
2 ######################################################################
3 #
4 # This program is Copyright 2003-2005 by Jeff Dutton.
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of either the GNU General Public License or the
8 # Perl Artistic License.
9 #
10 # This program 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 # If you do not have a copy of the GNU General Public License write to
16 # the Free Software Foundation, Inc., 675 Mass Ave, Cambridge,
17 # MA 02139, USA.
18 ######################################################################
19  
20 package Parse::RandGen::Rule;
21  
22 4 require 5.006_001;
23 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 4 ▶use Parse::RandGen qw($Debug);
Subroutine BEGIN: called CC=1 SCAR=0.0
25 4 ▶use Data::Dumper; # FIX - debug only
Subroutine BEGIN: called CC=1 SCAR=0.0
26 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 4 ▶use vars qw($Debug);
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 ######################################################################
30 #### Creators
31  
32 9 ▶sub new {
Subroutine new: called CC=4 SCAR=14.8
Pod documented
33 9 my $class = shift;
34 9 my $self = {
35 _name => undef, # Name of the rule
36 _grammar => undef, # Reference to the parent Grammar object
37 _productions => [ ], # Productions for the rule
38 };
39 9 ▶ bless $self, ref($class)||$class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
090
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
40  
41 9 $self->{_name} = shift;
42 !defined($self->{_name}) or ($self->{_name} =~ /^[a-z_]\w*$/i)
43 9 ▶ or confess("The specified rule name must be exclusively alphanumeric characters (", $self->{_name}, ")!");
Branch: 1 branch
BranchTrueFalseTotal
not defined $self->{"_name"} or $self->{"_name"} =~ /^[a-z_]\w*$/i or confess("The specified rule name must be exclusively alphanumeric characters (", $self->{"_name"}, ")!") 9 0 9
Condition66%2/3
not defined $self->{"_name"} or $self->{"_name"} =~ /^[a-z_]\w*$/i
l!l&&r!l&&!r
810
MC/DC0%0/2
not defined $self->{"_name"} or $self->{"_name"} =~ /^[a-z_]\w*$/i
not defined $self->{"_name"} $self->{"_name"} =~ /^[a-z_]\w*$/i
Truth table66%2/3
not(A) or B
A= not defined $self->{"_name"}B= $self->{"_name"} =~ /^[a-z_]\w*$/i
ABresult
1X1
011
000
44 9 return($self);
45 }
46  
47 ######################################################################
48 #### Methods
49  
50 1 ▶sub set {
Subroutine set: called CC=6 SCAR=19.2
Pod undocumented
51 1 ▶ my $self = shift or confess("%Error: Cannot call without a valid object!");
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or confess("%Error: Cannot call without a valid object!") 1 0 1
52 1 my @args = @_;
53 1 my $prodType = "Parse::RandGen::Production";
54
55 1 my $numArgs = $#args + 1;
56  
57 1 ▶ ($numArgs) or confess("%Error: Rule::set() called with no arguments!");
Branch: 1 branch
BranchTrueFalseTotal
$numArgs or confess("%Error: Rule::set() called with no arguments!") 1 0 1
58  
59 1 ▶ ($numArgs % 2) and confess("%Error: Rule::set() called with an odd number of arguments ($numArgs); arguments must be in named pairs!");
Branch: 1 branch
BranchTrueFalseTotal
$numArgs % 2 and confess("%Error: Rule::set() called with an odd number of arguments ($numArgs); arguments must be in named pairs!") 0 1 1
60 1 ▶ while ($#args >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($#args >= 0) 1 0 1
61 1 my ($arg, $val) = (shift(@args), shift(@args));
62 1 ▶ if ($arg eq "prod") {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg eq 'prod') { } 1 0 1
63 1 $self->addProd($val);
64 } else {
65 # Unknown arguments become data members
66 0 $self->{$arg} = $val;
67 }
68 }
69 }
70  
71 11 ▶sub addProd {
Subroutine addProd: called CC=7 SCAR=19.9
Pod undocumented
72 11 ▶ my $self = shift or confess("%Error: Cannot call without a valid object!");
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or confess("%Error: Cannot call without a valid object!") 11 0 11
73 11 my @args = @_;
74 11 my $prodType = "Parse::RandGen::Production";
75
76 11 foreach my $arg (@args) {
77 11 my $prod; # Production object
78 11 my $type = ref($arg);
79 11 ▶ if ($type && UNIVERSAL::isa($arg, $prodType)) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type and UNIVERSAL::isa($arg, $prodType)) { } 10 1 11
elsif ($type eq 'ARRAY') { } 1 0 1
Condition66%2/3
$type and UNIVERSAL::isa($arg, $prodType)
!ll&&!rl&&r
0110
MC/DC50%1/2
$type and UNIVERSAL::isa($arg, $prodType)
$type UNIVERSAL::isa($arg, $prodType)
Truth table66%2/3
A and B
A= $typeB= UNIVERSAL::isa($arg, $prodType)
ABresult
0X0
100
111
80 10 $prod = $arg;
81 } elsif ($type eq "ARRAY") {
82 # [ 'http://' host path(?) ]
83 1 $prod = Parse::RandGen::Production->new(@$arg);
84 } else {
85 0 confess("%Error: Passed a $type argument instead of a $prodType or ARRAY reference argument!");
86 }
87 10 ▶ defined($prod->{_rule}) and confess("%Error: Adding a Production that already belongs to another Rule!");
Branch: 1 branch
BranchTrueFalseTotal
defined $prod->{"_rule"} and confess("%Error: Adding a Production that already belongs to another Rule!") 0 10 10
88 10 $prod->{_rule} = $self;
89 10 push @{$self->{_productions}}, $prod; # Add the production to the end of the _productions list
90 10 $prod->{_number} = $#{$self->{_productions}};
91 }
92 }
93  
94 0 ▶sub check {
Subroutine check: not called CC=4 SCAR=30.0
Pod undocumented
95 0 ▶ my $self = shift or confess("%Error: Cannot call without a valid object!");
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or confess("%Error: Cannot call without a valid object!") 0 0 0
96 0 ▶ return "%Error: Production has no Grammar object!" unless $self->grammar();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->grammar 0 0 0
97 0 my $grammarName = $self->grammar()->name();
98  
99 0 my $err = "";
100 0 foreach my $prod (@{$self->{_productions}}) {
101 0 $err .= $prod->check();
102 }
103 0 return $err;
104 }
105  
106 0 ▶sub dump {
Subroutine dump: not called CC=4 SCAR=30.0
Pod undocumented
107 0 ▶ my $self = shift or confess("%Error: Cannot call without a valid object!");
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or confess("%Error: Cannot call without a valid object!") 0 0 0
108 0 my $output = $self->name() . ":\n";
109 0 my $firstProd = 1;
110 0 foreach my $prod (@{$self->{_productions}}) {
111 0 ▶ if ($firstProd) {
Branch: 1 branch
BranchTrueFalseTotal
if ($firstProd) { } 0 0 0
112 0 $output .= "\t\t ";
113 0 $firstProd = 0;
114 } else {
115 0 $output .= "\t\t| ";
116 }
117 0 $output .= $prod->dump() . "\n";
118 }
119 0 $output .= "\n";
120 }
121  
122 200 ▶sub dumpHeir {
Subroutine dumpHeir: called CC=5 SCAR=16.2
Pod undocumented
123 200 ▶ my $self = shift or confess("%Error: Cannot call without a valid object!");
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or confess("%Error: Cannot call without a valid object!") 200 0 200
124 200 ▶ my $name = defined($self->name()) ? ($self->name() . ":") : "";
Branch: 1 branch
BranchTrueFalseTotal
defined $self->name ? : 0 200 200
125 200 my $output = "($name ";
126 200 my $firstProd = 1;
127 200 foreach my $prod (@{$self->{_productions}}) {
128 400 ▶ if ($firstProd) {
Branch: 1 branch
BranchTrueFalseTotal
if ($firstProd) { } 200 200 400
129 200 $firstProd = 0;
130 } else {
131 200 $output .= " | ";
132 }
133 400 $output .= $prod->dumpHeir();
134 }
135 200 $output .= " )";
136 }
137  
138 814 ▶sub pick {
Subroutine pick: called CC=8 SCAR=22.8
Pod documented
139 814 ▶ my $self = shift or confess("%Error: Cannot call without a valid object!");
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or confess("%Error: Cannot call without a valid object!") 814 0 814
140 814 my %args = ( match=>1, # Default is to pick matching data
141 vals => { }, # Hash of values of various hard-coded sub-rules (by name)
142 @_ );
143  
144 # Return explicitly specified value (if specified by name or reference to $self)
145 814 ▶ return $args{vals}{$self->name()} if (defined($self->name()) && defined($args{vals}{$self->name()}));
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->name and defined $args{"vals"}{$self->name} 0 814 814
Condition33%1/3
defined $self->name and defined $args{"vals"}{$self->name}
!ll&&!rl&&r
81400
MC/DC0%0/2
defined $self->name and defined $args{"vals"}{$self->name}
defined $self->name defined $args{"vals"}{$self->name}
Truth table33%1/3
A and B
A= defined $self->nameB= defined $args{"vals"}{$self->name}
ABresult
0X0
100
111
146 814 ▶ return $args{vals}{$self} if defined($args{vals}{$self});
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"vals"}{$self} 0 814 814
147  
148 814 my @prods;
149 814 foreach my $prod ($self->productions()) {
150 1426 ▶ push(@prods, $prod) if $prod->containsVals(%args);
Branch: 1 branch
BranchTrueFalseTotal
if $prod->containsVals(%args) 0 1426 1426
151 }
152 814 ▶ @prods = $self->productions() unless(@prods); # If {vals} does not specify any production of this rule, pick from all productions
Branch: 1 branch
BranchTrueFalseTotal
unless @prods 814 0 814
153  
154 814 my $prodNum = int(rand($#prods+1));
155 814 return( $prods[$prodNum]->pick(%args) );
156 }
157  
158 # Returns true (1) if this rule is or has any explicitly specified values in the "vals" argument
159 202 ▶sub containsVals {
Subroutine containsVals: called CC=7 SCAR=21.8
Pod undocumented
160 202 ▶ my $self = shift or confess("%Error: Cannot call without a valid object!");
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or confess("%Error: Cannot call without a valid object!") 202 0 202
161 202 my %args = ( vals => { }, # Hash of values of various hard-coded sub-rules (by name)
162 @_ );
163  
164 # Return true if this rule is explicitly specified (if specified by name or reference to $self)
165 202 ▶ return 1 if (defined($self->name()) && defined($args{vals}{$self->name()}));
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->name and defined $args{"vals"}{$self->name} 0 202 202
Condition33%1/3
defined $self->name and defined $args{"vals"}{$self->name}
!ll&&!rl&&r
20200
MC/DC0%0/2
defined $self->name and defined $args{"vals"}{$self->name}
defined $self->name defined $args{"vals"}{$self->name}
Truth table33%1/3
A and B
A= defined $self->nameB= defined $args{"vals"}{$self->name}
ABresult
0X0
100
111
166 202 ▶ return 1 if defined($args{vals}{$self});
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"vals"}{$self} 0 202 202
167  
168 202 foreach my $prod ($self->productions()) {
169 404 ▶ return 1 if $prod->containsVals(%args);
Branch: 1 branch
BranchTrueFalseTotal
if $prod->containsVals(%args) 0 404 404
170 }
171 202 return 0;
172 }
173  
174 ######################################################################
175 #### Accessors
176  
177 1218 ▶sub name {
Subroutine name: called CC=2 SCAR=7.2
Pod documented
178 1218 ▶ my $self = shift or confess("%Error: Cannot call name() without a valid object!");
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or confess("%Error: Cannot call name() without a valid object!") 1218 0 1218
179 1218 return $self->{_name};
180 }
181  
182 1 ▶sub grammar {
Subroutine grammar: called CC=2 SCAR=7.2
Pod documented
183 1 ▶ my $self = shift or confess("%Error: Cannot call grammar() without a valid object!");
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or confess("%Error: Cannot call grammar() without a valid object!") 1 0 1
184 1 return $self->{_grammar};
185 }
186  
187 1830 ▶sub productions {
Subroutine productions: called CC=2 SCAR=7.1
Pod undocumented
188 1830 ▶ my $self = shift or confess("%Error: Cannot call productions() without a valid object!");
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or confess("%Error: Cannot call productions() without a valid object!") 1830 0 1830
189 1830 return (@{$self->{_productions}});
190 }
191  
192 0 ▶sub production {
Subroutine production: not called CC=8 SCAR=42.8
Pod undocumented
193 0 ▶ my $self = shift or confess("%Error: Cannot call productions() without a valid object!");
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or confess("%Error: Cannot call productions() without a valid object!") 0 0 0
194 0 my $prodIdent = shift;
195 0 ▶ defined($prodIdent) or confess("%Error: Must specify either the name or the number of the production to be found!");
Branch: 1 branch
BranchTrueFalseTotal
defined $prodIdent or confess("%Error: Must specify either the name or the number of the production to be found!") 0 0 0
196 0 my $isNumber = ($prodIdent !~ m/[^\d]/);
197 0 foreach my $prod (@{$self->{_productions}}) {
198 0 ▶ if ( ($isNumber && ($prod->{_number} == $prodIdent))
Branch: 1 branch
BranchTrueFalseTotal
if ($isNumber and $prod->{"_number"} == $prodIdent or defined $prod->{"_name"} and $prod->{"_name"} eq $prodIdent) 0 0 0
Condition0%0/9
$isNumber and $prod->{"_number"} == $prodIdent or defined $prod->{"_name"} and $prod->{"_name"} eq $prodIdent
l!l&&r!l&&!r
000
$isNumber and $prod->{"_number"} == $prodIdent
!ll&&!rl&&r
000
defined $prod->{"_name"} and $prod->{"_name"} eq $prodIdent
!ll&&!rl&&r
000
MC/DC0%0/4
$isNumber and $prod->{"_number"} == $prodIdent or defined $prod->{"_name"} and $prod->{"_name"} eq $prodIdent
$isNumber $prod->{"_number"} == $prodIdent defined $prod->{"_name"} $prod->{"_name"} eq $prodIdent
Truth table0%0/7
A and B or C and D
A= $isNumberB= $prod->{"_number"} == $prodIdentC= defined $prod->{"_name"}D= $prod->{"_name"} eq $prodIdent
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
199 || (defined($prod->{_name}) && ($prod->{_name} eq $prodIdent)) ) {
200 0 return $prod;
201 }
202 }
203 0 return undef; # Not found
204 }
205  
206 ######################################################################
207 #### Package return
208 4 1;
209 __END__
« RandGen/Subrule.pm ↑ Summary RandGen/Literal.pm »