| 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
| |||||||||||||||||||||
| 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')
MC/DC0%0/1
$args{'nodes'} || croak('No nodes specified')
$args{'nodes'}
Truth table50%1/2
A
A= $args{'nodes'}
| |||||||||||||||||||||
| 43 | 4 | ▶ | $args{items} || croak "No items specified"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 44 | 4 | ▶ | if (ref $args{items} eq "ARRAY") { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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
MC/DC100%1/1
shift() || 0
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | 7 | ▶ | $levels = scalar @{$self->{nodes}} - 1 if ($levels >= scalar @{$self->{nodes}}); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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
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
| |||||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | 41 | ▶ | while (my $i = pop @items) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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
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
| |||||||||||||||||||||
| 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'} || ''
MC/DC100%1/1
$args{'fail'} || ''
$args{'fail'}
Truth table100%2/2
A
A= $args{'fail'}
| |||||||||||||||||||||
| 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')
MC/DC0%0/1
"$args{'node'}" || croak('Must specify node')
"$args{'node'}"
Truth table50%1/2
A
A= "$args{'node'}"
| |||||||||||||||||||||
| 128 | 4 | ▶ | if ($self->{item_type} eq "HASH") { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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')
MC/DC0%0/1
"$args{'node'}" || croak('Must specify node')
"$args{'node'}"
Truth table50%1/2
A
A= "$args{'node'}"
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 149 | 3 | ▶ | if ($self->{item_type} eq "HASH") { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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'} || ''
MC/DC100%1/1
$args{'fail'} || ''
$args{'fail'}
Truth table100%2/2
A
A= $args{'fail'}
| |||||||||||||||||||||
| 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__ | ||||||||||||||||||||