| 1 | # $Revision: #4 $$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::Subrule; | ||||||||||||||||||||||||||||
| 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 strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | 4 | ▶ | use vars qw(@ISA $Debug); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | 4 | @ISA = ('Parse::RandGen::Condition'); | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 3 | ▶ | sub _newDerived { } # Nothing to do | ||||||||||||||||||||||||||
|
Subroutine
_newDerived: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 0 | ▶ | sub isSubrule { return 1; } | ||||||||||||||||||||||||||
|
Subroutine
isSubrule: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 32 | 0 | ▶ | sub isTerminal { return 0; } | ||||||||||||||||||||||||||
|
Subroutine
isTerminal: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 33 | 104 | ▶ | sub isQuantSupported { return 1; } | ||||||||||||||||||||||||||
|
Subroutine
isQuantSupported: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 404 | ▶ | sub subrule { | ||||||||||||||||||||||||||
|
Subroutine
subrule: called CC=5 SCAR=20.2
Pod
documented
| |||||||||||||||||||||||||||||
| 36 | 404 | ▶ | my $self = shift or confess ("%Error: Cannot call without a valid object!"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | 404 | my $subrule = $self->element(); | |||||||||||||||||||||||||||
| 38 | 404 | ▶ | if (ref($subrule)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | 404 | ▶ | UNIVERSAL::isa($subrule, "Parse::RandGen::Rule") or confess("subrule() contains a reference ($subrule), but it is not a Rule!"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | 404 | return($subrule); | |||||||||||||||||||||||||||
| 41 | } else { | ||||||||||||||||||||||||||||
| 42 | 0 | ▶ | defined($self->grammar()) or confess ("%Error: Parse::RandGen::Subrule::subrule() called, but the \"$subrule\" rule cannot be found, because grammar() is undef!"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | 0 | return($self->grammar()->rule($subrule)); | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 0 | ▶ | sub dump { | ||||||||||||||||||||||||||
|
Subroutine
dump: not called CC=7 SCAR=40.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 48 | 0 | ▶ | my $self = shift or confess ("%Error: Cannot call without a valid object!"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | 0 | my $subrule = $self->element(); | |||||||||||||||||||||||||||
| 50 | 0 | ▶ | if (ref($subrule)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | 0 | return ($subrule->dumpHeir() . $self->quant()); | |||||||||||||||||||||||||||
| 52 | } else { | ||||||||||||||||||||||||||||
| 53 | # Named non-reference rule | ||||||||||||||||||||||||||||
| 54 | 0 | my $output = $subrule; | |||||||||||||||||||||||||||
| 55 | 0 | ▶ | if (defined($self->max()) && ($self->min() == $self->max())) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $self->max and $self->min == $self->max
MC/DC0%0/2
defined $self->max and $self->min == $self->max
defined $self->max
$self->min == $self->max
Truth table0%0/3
A and B
A= defined $self->maxB= $self->min == $self->max
| |||||||||||||||||||||||||||||
| 56 | 0 | ▶ | $output .= "(" . $self->min() . ")" unless ($self->min() == 1); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | } else { | ||||||||||||||||||||||||||||
| 58 | 0 | ▶ | my $max = defined($self->max()) ? $self->max() : ""; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | 0 | $output .= "(" . $self->min() . ".." . $max . ")"; | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | 0 | return $output; | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 202 | ▶ | sub pick { | ||||||||||||||||||||||||||
|
Subroutine
pick: called CC=10 SCAR=23.0
Pod
documented
| |||||||||||||||||||||||||||||
| 66 | 202 | ▶ | my $self = shift or confess ("%Error: Cannot call without a valid object!"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 202 | my %args = ( match=>1, # Default is to pick matching data | |||||||||||||||||||||||||||
| 68 | @_ ); | ||||||||||||||||||||||||||||
| 69 | 202 | my $rule = $self->subrule(); | |||||||||||||||||||||||||||
| 70 | 202 | my $ruleName = $self->element(); | |||||||||||||||||||||||||||
| 71 | 202 | ▶ | defined($rule) or confess("Subrule::pick(): $ruleName subrule cannot be found in the grammar!\n"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 202 | my %result = $self->pickRepetitions(%args); | |||||||||||||||||||||||||||
| 74 | 202 | my $matchCnt = $result{matchCnt}; | |||||||||||||||||||||||||||
| 75 | 202 | my $badOne = $result{badOne}; | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 202 | my $val = ""; | |||||||||||||||||||||||||||
| 78 | 202 | ▶ | for (my $i=0; $i<$matchCnt; $i++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 612 | ▶ | my $matchThis = (defined($badOne) && ($i==$badOne))?0:1; # Only don't match for corrupted data | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $badOne and $i == $badOne
MC/DC100%2/2
defined $badOne and $i == $badOne
defined $badOne
$i == $badOne
Truth table100%3/3
A and B
A= defined $badOneB= $i == $badOne
| |||||||||||||||||||||||||||||
| 80 | 612 | ▶ | my $specifiedVals = ($matchThis && ($i==($matchCnt-1))) ? $args{vals} : { }; # Only specify rules for last capture value | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$matchThis and $i == $matchCnt - 1
MC/DC100%2/2
$matchThis and $i == $matchCnt - 1
$matchThis
$i == $matchCnt - 1
Truth table100%3/3
A and B
A= $matchThisB= $i == $matchCnt - 1
| |||||||||||||||||||||||||||||
| 81 | 612 | $val .= $rule->pick(%args, match=>$matchThis, vals => $specifiedVals); | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | 202 | ▶ | if ($Debug) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 200 | ▶ | print("Parse::RandGen::Subrule::pick(match=>$args{match}, matchCnt=>$matchCnt, badOne=>".(defined($badOne)?$badOne:"undef") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | .") on the rule \"".$rule->dumpHeir()."\" has a value of ".$self->dumpVal($val)."\n"); | ||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | 202 | return ($val); | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | # Returns true (1) if this subrule contains any of the rules specified by the "vals" argument | ||||||||||||||||||||||||||||
| 91 | 202 | ▶ | sub containsVals { | ||||||||||||||||||||||||||
|
Subroutine
containsVals: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 92 | 202 | ▶ | my $self = shift or confess("%Error: Cannot call without a valid object!"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | 202 | my %args = ( vals => { }, # Hash of values of various hard-coded sub-rules (by name) | |||||||||||||||||||||||||||
| 94 | @_ ); | ||||||||||||||||||||||||||||
| 95 | 202 | return $self->subrule()->containsVals(%args); | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | ###################################################################### | ||||||||||||||||||||||||||||
| 99 | #### Package return | ||||||||||||||||||||||||||||
| 100 | 4 | 1; | |||||||||||||||||||||||||||
| 101 | __END__ | ||||||||||||||||||||||||||||