blib/lib/Set/Cluster.pm

filter: Statementstmt 99.2% 136 / 137 Branchbran 86.3% 19 / 22 Conditioncond 72.2% 13 / 18 MC/DCmcdc 30.0% 3 / 10 Subroutinesub 100% 20 / 20 Podpod 60.0% 6 / 10 totaltotal 90.7% 197 / 217 CC 26 SCAR 12.5
CC
26
Cov
95%
setup
7.2
items
4.0
takeover
4.0
CRAP 3.5
↑ Summary
1 package Set::Cluster::Result;
2  
3 1 ▶use 5.004;
Subroutine BEGIN: called CC=1 SCAR=0.0
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 1 ▶use Storable qw(dclone);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 34 ▶sub copy {
Subroutine copy: called CC=2 SCAR=7.0
11 34 my $self = shift;
12 34 ▶ croak "can't copy class $self" unless (ref $self);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 34 34
13 34 my $copy = Storable::dclone($self);
14 34 return $copy;
15 }
16  
17 8 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
18 8 my $class = shift;
19 8 my $self = {};
20 8 return bless $self, $class;
21 }
22  
23  
24 package Set::Cluster;
25  
26 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 1 our $VERSION = '0.02';
32  
33 7 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
34 7 my $class = shift;
35 7 my $self = { results => {}, node_name_lookup => {}, item_lookup => {}, item_type => "" };
36 7 return bless $self, $class;
37 }
38  
39 4 ▶sub setup {
Subroutine setup: called CC=7 SCAR=19.8
Pod documented
40 4 my $self = shift;
41 4 my %args = @_;
42 4 ▶ $self->{nodes} = $args{nodes} || croak "No nodes specified";
Condition50%1/2
$args{'nodes'} || croak('No nodes specified')
l!l
40
MC/DC0%0/1
$args{'nodes'} || croak('No nodes specified')
$args{'nodes'}
Truth table50%1/2
A
A= $args{'nodes'}
Aresult
11
00
43 4 ▶ $args{items} || croak "No items specified";
Branch: 1 branch
BranchTrueFalseTotal
$args{"items"} or croak("No items specified") 4 0 4
44 4 ▶ if (ref $args{items} eq "ARRAY") {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $args{'items'} eq 'ARRAY') { } 2 2 4
elsif (ref $args{'items'} eq 'HASH') { } 2 0 2
45 2 $self->{item_type} = "ARRAY";
46 2 foreach my $object (@{$args{items}}) {
47 6 $self->{item_lookup}->{"$object"} = $object;
48 6 ▶ $object->can("weight") or croak "Object $object does not have a weight method";
Branch: 1 branch
BranchTrueFalseTotal
$object->can("weight") or croak("Object $object does not have a weight method") 5 1 6
49 5 $self->{items}->{"$object"} = $object->weight;
50 }
51 } elsif (ref $args{items} eq "HASH") {
52 2 $self->{item_type} = "HASH";
53 2 $self->{items} = $args{items};
54 } else {
55 0 croak "items is not correct type: ".ref $args{items};
56 }
57 }
58  
59 27 ▶sub results { shift->{results}; }
Subroutine results: called CC=1 SCAR=0.0
Pod documented
60  
61 7 ▶sub calculate {
Subroutine calculate: called CC=4 SCAR=13.9
Pod documented
62 7 my $self = shift;
63 7 ▶ my $levels = shift || 0;
Condition100%2/2
shift() || 0
l!l
61
MC/DC100%1/1
shift() || 0
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
64  
65 7 ▶ $levels = scalar @{$self->{nodes}} - 1 if ($levels >= scalar @{$self->{nodes}});
Branch: 1 branch
BranchTrueFalseTotal
if $levels >= scalar @{$self->{"nodes"};} 2 5 7
66  
67 # Setup nodes and distribute first time
68 7 my $result = Set::Cluster::Result->new;
69 7 foreach my $n (@{$self->{nodes}}) {
70 19 my $stringified = "$n";
71 19 $self->{node_name_lookup}->{$stringified} = $n;
72 19 $result->{$stringified} = [];
73 }
74 7 $self->distribute($result, [keys %{$self->{items}}]);
75  
76 7 $self->process("", $result, $levels);
77 }
78  
79 41 ▶sub process {
Subroutine process: called CC=3 SCAR=11.0
Pod undocumented
80 41 my ($self, $scenario, $result, $levels) = @_;
81 41 $self->{results}->{$scenario} = $result;
82 41 ▶ if ($levels > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($levels > 0) 15 26 41
83 15 foreach my $failed_node (keys %$result) {
84 34 my $new_state = $result->copy;
85 34 my $scenario = join(",", split(",",$scenario), $failed_node);
86 34 my @items = @{$new_state->{$failed_node}};
87 34 delete $new_state->{$failed_node};
88 34 $self->distribute($new_state, [@items]);
89 34 $self->process($scenario, $new_state, $levels-1);
90 }
91 }
92 }
93  
94 # Logic - sort items by weight. From highest, add item to
95 # node with lowest total weight
96 41 ▶sub distribute {
Subroutine distribute: called CC=3 SCAR=11.0
Pod undocumented
97 41 my ($self, $state, $items) = @_;
98  
99 41 my @nodes = keys %$state;
100 41 ▶ my @items = sort { $self->{items}->{$a} <=> $self->{items}->{$b} || $a cmp $b } @$items;
Condition66%2/3
$self->{'items'}{$a} <=> $self->{'items'}{$b} || $a cmp $b
l!l&&r!l&&!r
187220
MC/DC0%0/2
$self->{'items'}{$a} <=> $self->{'items'}{$b} || $a cmp $b
$self->{'items'}{$a} <=> $self->{'items'}{$b} $a cmp $b
Truth table66%2/3
A || B
A= $self->{'items'}{$a} <=> $self->{'items'}{$b}B= $a cmp $b
ABresult
1X1
011
000
101  
102 41 ▶ while (my $i = pop @items) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $i = pop @items) 166 41 207
103 166 my $node = $self->lowest($state);
104 166 push @{$state->{$node}}, $i;
105 }
106 41 return $state;
107 }
108  
109 # TODO: Should keep total with the node, to save recalculating each time
110 168 ▶sub lowest {
Subroutine lowest: called CC=3 SCAR=11.0
Pod undocumented
111 168 my $self = shift;
112 168 my $state = shift;
113 168 my %totals;
114 168 foreach my $n (keys %$state) {
115 295 $totals{$n} = 0;
116 295 map { $totals{$n} += $self->{items}->{$_} } @{$state->{$n}};
117 }
118 168 ▶ my @a = sort { $totals{$a} <=> $totals{$b} || $a cmp $b } keys %totals;
Condition66%2/3
$totals{$a} <=> $totals{$b} || $a cmp $b
l!l&&r!l&&!r
131200
MC/DC0%0/2
$totals{$a} <=> $totals{$b} || $a cmp $b
$totals{$a} <=> $totals{$b} $a cmp $b
Truth table66%2/3
A || B
A= $totals{$a} <=> $totals{$b}B= $a cmp $b
ABresult
1X1
011
000
119 168 return shift @a;
120 }
121  
122 4 ▶sub items {
Subroutine items: called CC=4 SCAR=13.9
Pod documented
123 4 my $self = shift;
124 4 my %args = @_;
125 4 ▶ $args{fail} = $args{fail} || "";
Condition100%2/2
$args{'fail'} || ''
l!l
13
MC/DC100%1/1
$args{'fail'} || ''
$args{'fail'}
Truth table100%2/2
A
A= $args{'fail'}
Aresult
11
00
126 4 my $scenario = "$args{fail}";
127 4 ▶ my $node = "$args{node}" || croak "Must specify node";
Condition50%1/2
"$args{'node'}" || croak('Must specify node')
l!l
40
MC/DC0%0/1
"$args{'node'}" || croak('Must specify node')
"$args{'node'}"
Truth table50%1/2
A
A= "$args{'node'}"
Aresult
11
00
128 4 ▶ if ($self->{item_type} eq "HASH") {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'item_type'} eq 'HASH') { } 2 2 4
129 2 return @{$self->results->{$scenario}->{$node}};
130 } else {
131 2 return map {$_ = $self->{item_lookup}->{$_}} @{$self->results->{$scenario}->{$node}};
132 }
133 }
134  
135 3 ▶sub takeover {
Subroutine takeover: called CC=4 SCAR=13.9
Pod documented
136 3 my $self = shift;
137 3 my %args = @_;
138 3 ▶ my $node = "$args{node}" || croak "Must specify node";
Condition50%1/2
"$args{'node'}" || croak('Must specify node')
l!l
30
MC/DC0%0/1
"$args{'node'}" || croak('Must specify node')
"$args{'node'}"
Truth table50%1/2
A
A= "$args{'node'}"
Aresult
11
00
139 3 my @fail = split (",", "$args{fail}");
140 3 pop @fail;
141 3 my $fail = join(",", @fail);
142 3 my $prior = $self->results->{$fail}->{$node};
143 3 my $now = $self->results->{$args{fail}}->{$node};
144 3 my %seen;
145 3 map {$seen{$_}++} @$now;
146 3 map {$seen{$_}--} @$prior;
147 3 my @result = ();
148 3 ▶ map { push @result, $_ if $seen{$_}==1 } keys %seen;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$_} == 1 7 8 15
149 3 ▶ if ($self->{item_type} eq "HASH") {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'item_type'} eq 'HASH') { } 2 1 3
150 2 return @result;
151 } else {
152 1 return map { $_ = $self->{item_lookup}->{$_}} @result;
153 }
154 }
155  
156 4 ▶sub hash_by_item {
Subroutine hash_by_item: called CC=4 SCAR=13.9
Pod documented
157 4 my $self = shift;
158 4 my %args = @_;
159 4 ▶ my $fail = $args{fail} || "";
Condition100%2/2
$args{'fail'} || ''
l!l
22
MC/DC100%1/1
$args{'fail'} || ''
$args{'fail'}
Truth table100%2/2
A
A= $args{'fail'}
Aresult
11
00
160 4 my $hash = {};
161 4 foreach my $node (keys %{$self->results->{$fail}}) {
162 8 my $items = $self->results->{$fail}->{$node};
163 8 foreach my $i (@{$items}) {
164 27 $hash->{$i} = $self->{node_name_lookup}->{$node};
165 }
166 }
167 4 return $hash;
168 }
169  
170 1 1;
171 __END__
↑ Summary