Classifier.pm

filter: Statementstmt 79.4% 81 / 102 Branchbran 72.2% 26 / 36 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 84.6% 11 / 13 Podpod 62.5% 5 / 8 totaltotal 78.0% 128 / 164 CC 22 SCAR 19.2
CC
22
Cov
78%
recursive_search
12.1
check_match
6.9
tree_error
6.0
CRAP 6.8
↑ Summary Classifier/Result.pm »
1 package Data::Classifier;
2  
3 1 our $VERSION = '0.01';
4  
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use YAML qw(LoadFile Load);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use Data::Classifier::Result;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 #public interface
14 1 ▶sub new {
Subroutine new: called CC=4 SCAR=16.8
Pod undocumented
15 1 my ($class, %opts) = @_;
16 1 my $self = {};
17  
18 1 bless($self, $class);
19  
20 1 ▶ if (defined($opts{file})) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined $opts{'file'}) { } 0 1 1
elsif (defined $opts{'yaml'}) { } 1 0 1
elsif (defined $opts{'tree'}) { } 0 0 0
21 0 $self->{TREE} = LoadFile($opts{file});
22 } elsif (defined($opts{yaml})) {
23 1 $self->{TREE} = Load($opts{yaml});
24 } elsif (defined($opts{tree})) {
25 0 $self->{TREE} = $opts{tree};
26 } else {
27 0 croak "You must specify one of file, yaml, or tree";
28 }
29  
30 1 $self->{DEBUG} = $opts{debug};
31  
32 1 return $self;
33 }
34  
35 3 ▶sub process {
Subroutine process: called CC=1 SCAR=0.0
Pod documented
36 3 my ($self, $attributes) = @_;
37 3 my ($result, $result_class);
38  
39 3 $self->debug("starting classification");
40  
41 3 $self->{CLASS_STACK} = [];
42  
43 3 $self->recursive_search($attributes, $self->{TREE});
44  
45 3 $result = $self->{CLASS_STACK};
46  
47 3 $self->{CLASS_STACK} = undef;
48  
49 3 $self->debug("classification done\n\n");
50  
51 3 return $self->return_result($result);
52 }
53  
54 #this method should be overloaded by base classes to change the class
55 #that is returned by process
56 3 ▶sub return_result {
Subroutine return_result: called CC=1 SCAR=0.0
Pod documented
57 3 my ($self, $result) = @_;
58 3 return Data::Classifier::Result->new($result);
59 }
60  
61 0 ▶sub dump {
Subroutine dump: not called CC=1 SCAR=6.9
Pod documented
62 0 my ($self) = @_;
63  
64 0 return Dumper($self->{TREE});
65 }
66  
67 #private interface
68 13 ▶sub recursive_search {
Subroutine recursive_search: called CC=12 SCAR=24.9
Pod documented
69 13 my ($self, $attributes, $node) = @_;
70 13 my $name = $node->{name};
71 13 my $matchmap = $node->{match};
72 13 my $children = $node->{children};
73 13 my $class_stack = $self->{CLASS_STACK};
74 13 my $node_match = 0;
75 13 my $recurse_match = 0;
76 13 my $generic_match = 0;
77  
78 13 $self->debug("testing $name");
79  
80 13 push(@$class_stack, $node);
81
82 13 ▶ if (! defined($matchmap)) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $matchmap) { } 6 7 13
elsif (ref $matchmap ne 'HASH') { } 0 7 7
83 #no rules for this class, so we will be a member of it only if a lower
84 #rule applies
85 6 $node_match = 1;
86 6 $generic_match = 1;
87 } elsif (ref($matchmap) ne 'HASH') {
88 0 $self->tree_error("match was not a map");
89 } else {
90 7 ▶ if ($self->check_match($matchmap, $attributes)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->check_match($matchmap, $attributes)) 2 5 7
91 2 $node_match = 1;
92 }
93 }
94  
95 13 ▶ if ($node_match) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node_match) 8 5 13
96 #check the children for a more specific class
97 8 $self->debug("looking at child classes");
98  
99 8 ▶ if (defined($children)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $children) 6 2 8
100 6 ▶ if (ref($children) ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $children ne "ARRAY") 0 6 6
101 0 $self->tree_error("children must be a sequence");
102 }
103  
104 6 foreach my $child (@$children) {
105 10 ▶ if ($self->recursive_search($attributes, $child)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->recursive_search($attributes, $child)) 4 6 10
106 4 $recurse_match = 1;
107 4 last;
108 }
109 }
110 }
111 }
112  
113 13 ▶ if ($generic_match && ! $recurse_match) {
Branch: 2 branches
BranchTrueFalseTotal
if ($generic_match and not $recurse_match) { } 2 11 13
elsif (not $node_match) { } 5 6 11
Condition100%3/3
$generic_match and not $recurse_match
!ll&&!rl&&r
742
MC/DC100%2/2
$generic_match and not $recurse_match
$generic_match not $recurse_match
Truth table100%3/3
A and not(B)
A= $generic_matchB= not $recurse_match
ABresult
0X0
100
111
114 #didn't match a lower level class after a generic match, so this is really no match
115 2 pop(@$class_stack);
116 2 return 0;
117 } elsif (! $node_match) {
118 5 pop(@$class_stack);
119 5 return 0;
120 }
121  
122 6 return 1;
123 }
124  
125 #only return true if everything in $matchlist matches the stuff in $attributes
126 7 ▶sub check_match {
Subroutine check_match: called CC=6 SCAR=19.3
Pod documented
127 7 my ($self, $matchlist, $attributes) = @_;
128 7 my $match = 0;
129  
130 #no idea why this has to be here, but with out it, matches fail
131 #for very odd reasons - not sure if it's my bug or a perl bug, but it's
132 #very strange
133 7 keys(%$matchlist);
134  
135 7 ▶ while(my ($attribute, $regex) = each(%$matchlist)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$matchlist) 8 2 10
136 8 my $to_test = $attributes->{$attribute};
137  
138 8 ▶ if (! defined($to_test)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $to_test) 0 8 8
139 0 $self->debug("nothing to test");
140 0 return 0;
141 }
142  
143 8 ▶ if (! defined($regex)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $regex) 0 8 8
144 0 $self->debug("no regex");
145 0 die "regex";
146 }
147  
148 8 ▶ if ($to_test !~ m/$regex/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($to_test =~ /$regex/) 5 3 8
149 5 $self->debug("match failure");
150 5 return 0;
151 }
152  
153 3 $self->debug("testing data $attribute $regex '$to_test'");
154  
155 3 $match = 1;
156 }
157  
158 2 ▶ if ($match) {
Branch: 1 branch
BranchTrueFalseTotal
if ($match) 2 0 2
159 2 $self->debug("success");
160  
161 2 return 1;
162 }
163  
164 0 $self->debug("fell through with no matches");
165  
166 0 return 0;
167 }
168  
169 0 ▶sub tree_error {
Subroutine tree_error: not called CC=2 SCAR=17.9
Pod undocumented
170 0 my ($self, $msg) = @_;
171 0 my $class_stack = $self->{CLASS_STACK};
172 0 my @names;
173  
174 0 foreach my $one (@$class_stack) {
175 0 push(@names, $one->{name});
176 }
177  
178 0 $self->debug("ERROR: Class tree was not consistent: $msg\n");
179 0 $self->debug("\tClass path: ", join('::', @names), "\n");
180  
181 0 die "can not continue after class tree error";
182 }
183  
184 37 ▶sub debug {
Subroutine debug: called CC=2 SCAR=7.2
Pod undocumented
185 37 my ($self, $msg) = @_;
186 37 ▶ print STDERR "DEBUG: $msg\n" if $self->{DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"DEBUG"} 0 37 37
187 }
188  
189 1 1;
190  
191 __END__
↑ Summary Classifier/Result.pm »