lib/BalanceOfPower/Relations/RelPack.pm

filter: Statementstmt 79.2% 191 / 241 Branchbran 65.1% 43 / 66 Conditioncond 37.5% 3 / 8 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 84.3% 27 / 32 Podpod 0.0% 0 / 25 totaltotal 70.2% 264 / 376 CC 50 SCAR 17.0
CC
50
Cov
75%
load_pack
20.0
link_destinations_for_node
12.0
dump
12.0
CRAP 5.5
« lib/BalanceOfPower/Role/Broker.pm ↑ Summary blib/lib/BalanceOfPower/Executive.pm »
1 package BalanceOfPower::Relations::RelPack;
2 11 $BalanceOfPower::Relations::RelPack::VERSION = '0.200000';
3 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 11 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 11 ▶use Module::Load;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 has links => (
10 is => 'rw',
11 112 ▶ default => sub { [] }
Subroutine __ANON__: called CC=1 SCAR=0.0
12 11 );
13 has links_grid => (
14 is => 'rw',
15 112 ▶ default => sub { {} }
Subroutine __ANON__: called CC=1 SCAR=0.0
16 11 );
17 has distance_cache => (
18 is => 'rw',
19 112 ▶ default => sub { {} }
Subroutine __ANON__: called CC=1 SCAR=0.0
20 11 );
21  
22  
23 sub all
24 {
25 287 ▶ my $self = shift;
Subroutine all: called CC=1 SCAR=0.0
Pod undocumented
26 287 return @{$self->links};
27 }
28 sub reset
29 {
30 1 ▶ my $self = shift;
Subroutine reset: called CC=1 SCAR=0.0
Pod undocumented
31 1 $self->links([]);
32 1 $self->links_grid({});
33 1 $self->distance_cache({});
34 }
35  
36 sub exists_link
37 {
38 2865 ▶ my $self = shift;
Subroutine exists_link: called CC=2 SCAR=6.9
Pod undocumented
39 2865 my $node1 = shift;
40 2865 my $node2 = shift;
41 2865 ▶ if(exists $self->links_grid->{$node1}->{$node2})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->links_grid->{$node1}{$node2}) { } 1582 1283 2865
42 {
43 1582 return $self->links_grid->{$node1}->{$node2}
44 }
45 else
46 {
47 1283 return undef;
48 }
49 }
50  
51 sub add_link
52 {
53 216 ▶ my $self = shift;
Subroutine add_link: called CC=2 SCAR=6.9
Pod undocumented
54 216 my $link = shift;
55 216 my $node1 = $link->node1;
56 216 my $node2 = $link->node2;
57 216 ▶ if(! $self->exists_link($node1, $node2))
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->exists_link($node1, $node2)) { } 213 3 216
58 {
59 213 push @{$self->links}, $link;
60 213 $self->links_grid->{$node1}->{$node2} = $link;
61 213 $self->links_grid->{$node2}->{$node1} = $link;
62 213 return 1;
63 }
64 else
65 {
66 3 return 0;
67 }
68 }
69 sub update_link
70 {
71 0 ▶ my $self = shift;
Subroutine update_link: not called CC=1 SCAR=6.9
Pod undocumented
72 0 my $link = shift;
73 0 $self->delete_link($link->node1, $link->node2);
74 0 $self->add_link($link);
75 }
76 sub delete_references
77 {
78 9 ▶ my $self = shift;
Subroutine delete_references: called CC=1 SCAR=0.0
Pod undocumented
79 9 my $node1 = shift;
80 9 my $node2 = shift;
81 9 $self->links_grid->{$node1}->{$node2} = undef;
82 9 $self->links_grid->{$node2}->{$node1} = undef;
83 }
84 sub delete_references_for_node
85 {
86 18 ▶ my $self = shift;
Subroutine delete_references_for_node: called CC=3 SCAR=11.0
Pod undocumented
87 18 my $node1 = shift;
88 18 foreach my $k (%{$self->links_grid->{$node1}})
89 {
90 4 ▶ if($k)
Branch: 1 branch
BranchTrueFalseTotal
if ($k) 4 0 4
91 {
92 4 $self->links_grid->{$k}->{$node1} = undef;
93 }
94 }
95 18 $self->links_grid->{$node1} = undef;
96 }
97 sub delete_link
98 {
99 9 ▶ my $self = shift;
Subroutine delete_link: called CC=1 SCAR=0.0
Pod undocumented
100 9 my $node1 = shift;
101 9 my $node2 = shift;
102 9 @{$self->links} = grep { ! $_->involve($node1, $node2) } @{$self->links};
103 9 $self->delete_references($node1, $node2);
104 }
105  
106 sub delete_link_for_node
107 {
108 18 ▶ my $self = shift;
Subroutine delete_link_for_node: called CC=1 SCAR=0.0
Pod undocumented
109 18 my $n1 = shift;
110 18 @{$self->links} = grep { ! $_->has_node($n1) } @{$self->links};
111 18 $self->delete_references_for_node($n1);
112 }
113 sub garbage_collector
114 {
115 74 ▶ my $self = shift;
Subroutine garbage_collector: called CC=3 SCAR=11.1
Pod undocumented
116 74 my $query = shift;
117 74 my @new = ();
118 74 for(@{$self->links})
119 {
120 15 ▶ if(! $query->($_))
Branch: 1 branch
BranchTrueFalseTotal
if (not &$query($_)) { } 15 0 15
121 {
122 15 push @new, $_;
123 }
124 else
125 {
126 0 $self->delete_references($_->node1, $_->node2);
127 }
128 }
129 74 @{$self->links} = @new;
130 }
131 sub links_for_node
132 {
133 537 ▶ my $self = shift;
Subroutine links_for_node: called CC=5 SCAR=16.2
Pod undocumented
134 537 my $node = shift;
135 537 ▶ return $self->all() if(! $node);
Branch: 1 branch
BranchTrueFalseTotal
unless $node 0 537 537
136 537 my @out = ();
137 537 foreach my $k (keys %{$self->links_grid->{$node}})
138 {
139 475 ▶ if($k)
Branch: 1 branch
BranchTrueFalseTotal
if ($k) 475 0 475
140 {
141 475 my $r = $self->links_grid->{$node}->{$k};
142 475 ▶ push @out, $r if($r);
Branch: 1 branch
BranchTrueFalseTotal
if $r 461 14 475
143 }
144 }
145 537 return @out;
146 }
147 sub links_for_node1
148 {
149 309 ▶ my $self = shift;
Subroutine links_for_node1: called CC=5 SCAR=16.4
Pod undocumented
150 309 my $node = shift;
151 309 ▶ return $self->all() if(! $node);
Branch: 1 branch
BranchTrueFalseTotal
unless $node 0 309 309
152 309 my @out = ();
153 309 foreach my $r (@{$self->links})
154 {
155 70 ▶ if($r->bidirectional)
Branch: 1 branch
BranchTrueFalseTotal
if ($r->bidirectional) 0 70 70
156 {
157 0 return $self->links_for_node($node);
158 }
159 70 ▶ if($r->node1 eq $node)
Branch: 1 branch
BranchTrueFalseTotal
if ($r->node1 eq $node) 15 55 70
160 {
161 15 push @out, $r;
162 }
163 }
164 309 return @out;
165 }
166 sub links_for_node2
167 {
168 153 ▶ my $self = shift;
Subroutine links_for_node2: called CC=5 SCAR=16.4
Pod undocumented
169 153 my $node = shift;
170 153 ▶ return $self->all() if(! $node);
Branch: 1 branch
BranchTrueFalseTotal
unless $node 0 153 153
171 153 my @out = ();
172 153 foreach my $r (@{$self->links})
173 {
174 49 ▶ if($r->bidirectional)
Branch: 1 branch
BranchTrueFalseTotal
if ($r->bidirectional) 0 49 49
175 {
176 0 return $self->links_for_node($node);
177 }
178 49 ▶ if($r->node2 eq $node)
Branch: 1 branch
BranchTrueFalseTotal
if ($r->node2 eq $node) 18 31 49
179 {
180 18 push @out, $r;
181 }
182 }
183 153 return @out;
184 }
185 sub first_link_for_node
186 {
187 603 ▶ my $self = shift;
Subroutine first_link_for_node: called CC=3 SCAR=11.0
Pod undocumented
188 603 my $node = shift;
189 603 foreach my $r (@{$self->links})
190 {
191 99 ▶ if($r->has_node($node))
Branch: 1 branch
BranchTrueFalseTotal
if ($r->has_node($node)) 38 61 99
192 {
193 38 return $r;
194 }
195 }
196 565 return undef;
197 }
198 sub first_link_for_node1
199 {
200 0 ▶ my $self = shift;
Subroutine first_link_for_node1: not called CC=2 SCAR=17.9
Pod undocumented
201 0 my $node = shift;
202 0 my @links = $self->links_for_node1($node);
203 0 ▶ if(@links)
Branch: 1 branch
BranchTrueFalseTotal
if (@links) { } 0 0 0
204 {
205 0 return $links[0]
206 }
207 else
208 {
209 0 return undef;
210 }
211 }
212 sub first_link_for_node2
213 {
214 153 ▶ my $self = shift;
Subroutine first_link_for_node2: called CC=2 SCAR=6.9
Pod undocumented
215 153 my $node = shift;
216 153 my @links = $self->links_for_node2($node);
217 153 ▶ if(@links)
Branch: 1 branch
BranchTrueFalseTotal
if (@links) { } 18 135 153
218 {
219 18 return $links[0]
220 }
221 else
222 {
223 135 return undef;
224 }
225 }
226  
227 sub link_destinations_for_node
228 {
229 0 ▶ my $self = shift;
Subroutine link_destinations_for_node: not called CC=3 SCAR=24.8
Pod undocumented
230 0 my $node1 = shift;
231 0 my @out = ();
232 0 for(keys %{$self->links_grid->{$node1}})
233 {
234 0 my $r = $self->links_grid->{$node1}->{$_};
235 0 ▶ if($r)
Branch: 1 branch
BranchTrueFalseTotal
if ($r) 0 0 0
236 {
237 0 push @out, $r->destination($node1);
238 }
239 }
240 0 return @out;
241 }
242  
243 sub query
244 {
245 724 ▶ my $self = shift;
Subroutine query: called CC=5 SCAR=16.1
Pod undocumented
246 724 my $query = shift;
247 724 my $node1 = shift;
248 724 my @out = ();
249 724 for(@{$self->links})
250 {
251 432 ▶ if($query->($_))
Branch: 1 branch
BranchTrueFalseTotal
if (&$query($_)) 119 313 432
252 {
253 119 ▶ if($node1)
Branch: 1 branch
BranchTrueFalseTotal
if ($node1) { } 20 99 119
254 {
255 20 ▶ if($_->has_node($node1))
Branch: 1 branch
BranchTrueFalseTotal
if ($_->has_node($node1)) 20 0 20
256 {
257 20 push @out, $_;
258 }
259 }
260 else
261 {
262 99 push @out, $_;
263 }
264 }
265 }
266 724 return @out;
267 }
268  
269 sub print_links
270 {
271 1 ▶ my $self = shift;
Subroutine print_links: called CC=4 SCAR=19.4
Pod undocumented
272 1 my $n = shift;
273 1 my $out = "";
274 1 foreach my $b (@{$self->links})
275 {
276 0 ▶ if($n)
Branch: 1 branch
BranchTrueFalseTotal
if ($n) { } 0 0 0
277 {
278 0 ▶ if($b->has_node($n))
Branch: 1 branch
BranchTrueFalseTotal
if ($b->has_node($n)) 0 0 0
279 {
280 0 $out .= $b->print($n) . "\n";
281 }
282 }
283 else
284 {
285 0 $out .= $b->print($n) . "\n";
286 }
287 }
288 1 return $out;
289 }
290  
291 #BFS implementation
292 sub distance
293 {
294 136 ▶ my $self = shift;
Subroutine distance: called CC=8 SCAR=20.9
Pod undocumented
295 136 my $node1 = shift;
296 136 my $node2 = shift;
297 136 my $nodes_list = shift;
298 136 my %nodes = $self->get_cached_nodes($node1, $nodes_list);
299 136 my $log;
300 136 ▶ if($nodes{$node2}->{distance} != -1)
Branch: 1 branch
BranchTrueFalseTotal
if ($nodes{$node2}{"distance"} != -1) 45 91 136
301 {
302 45 return $nodes{$node2}->{distance};
303 }
304 91 ▶ if(my $cached_distance = $self->get_cached_distance($node2, $node1))
Branch: 1 branch
BranchTrueFalseTotal
if (my $cached_distance = $self->get_cached_distance($node2, $node1)) 0 91 91
305 {
306 0 $nodes{$node2}->{distance} = $cached_distance;
307 0 $self->distance_cache->{$node1}->{nodes} = \%nodes;
308 0 return $cached_distance;
309 }
310  
311 91 my @queue = ( $node1 );
312 91 ▶ if(exists $self->distance_cache->{$node1}->{queue})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->distance_cache->{$node1}{"queue"}) 39 52 91
313 {
314 39 @queue = @{$self->distance_cache->{$node1}->{queue}};
315 }
316 91 ▶ while(@queue)
Branch: 1 branch
BranchTrueFalseTotal
if (@queue) 145 49 194
317 {
318
319 145 my $n = shift @queue;
320 145 foreach my $near ($self->near($n, $nodes_list))
321 {
322 284 ▶ if($nodes{$near}->{distance} == -1)
Branch: 1 branch
BranchTrueFalseTotal
if ($nodes{$near}{"distance"} == -1) 124 160 284
323 {
324 124 my $d = $nodes{$n}->{distance} + 1;
325 124 $nodes{$near}->{distance} = $nodes{$n}->{distance} + 1;
326 124 push @queue, $near;
327 }
328 }
329 145 ▶ if($nodes{$node2}->{distance} != -1)
Branch: 1 branch
BranchTrueFalseTotal
if ($nodes{$node2}{"distance"} != -1) 42 103 145
330 {
331 42 $self->distance_cache->{$node1}->{nodes} = \%nodes;
332 42 $self->distance_cache->{$node1}->{queue} = \@queue;
333 42 return $nodes{$node2}->{distance};
334 }
335 }
336 49 $nodes{$node2}->{distance} = 100;
337 49 $self->distance_cache->{$node1}->{nodes} = \%nodes;
338 49 $self->distance_cache->{$node1}->{queue} = \@queue;
339 49 return 100;
340 }
341 sub get_cached_distance
342 {
343 91 ▶ my $self = shift;
Subroutine get_cached_distance: called CC=4 SCAR=15.5
Pod undocumented
344 91 my $node1 = shift;
345 91 my $node2 = shift;
346 91 ▶ if(exists $self->distance_cache->{$node1} &&
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->distance_cache->{$node1} and exists $self->distance_cache->{$node1}{$node2} and $self->distance_cache->{$node1}{$node2} != -1) { } 0 91 91
Condition50%3/6
exists $self->distance_cache->{$node1} and exists $self->distance_cache->{$node1}{$node2} and $self->distance_cache->{$node1}{$node2} != -1
!ll&&!rl&&r
9100
exists $self->distance_cache->{$node1} and exists $self->distance_cache->{$node1}{$node2}
!ll&&!rl&&r
8290
MC/DC0%0/3
exists $self->distance_cache->{$node1} and exists $self->distance_cache->{$node1}{$node2} and $self->distance_cache->{$node1}{$node2} != -1
exists $self->distance_cache->{$node1} exists $self->distance_cache->{$node1}{$node2} $self->distance_cache->{$node1}{$node2} != -1
Truth table50%2/4
A and B and C
A= exists $self->distance_cache->{$node1}B= exists $self->distance_cache->{$node1}{$node2}C= $self->distance_cache->{$node1}{$node2} != -1
ABCresult
0XX0
10X0
1100
1111
347 exists $self->distance_cache->{$node1}->{$node2} &&
348 $self->distance_cache->{$node1}->{$node2} != -1)
349 {
350 0 return $self->distance_cache->{$node1}->{$node2};
351 }
352 else
353 {
354 91 return undef;
355 }
356 }
357 sub get_cached_nodes
358 {
359 136 ▶ my $self = shift;
Subroutine get_cached_nodes: called CC=3 SCAR=11.0
Pod undocumented
360 136 my $node1 = shift;
361 136 my $nodes_list = shift;
362 136 my %nodes = ();
363 136 ▶ if(exists $self->distance_cache->{$node1})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->distance_cache->{$node1}) { } 84 52 136
364 {
365 84 %nodes = %{$self->distance_cache->{$node1}->{nodes}};
366 }
367 else
368 {
369 52 foreach(@{$nodes_list})
370 {
371 257 $nodes{$_}->{distance} = -1;
372 }
373 52 $nodes{$node1}->{distance} = 0;
374 }
375 136 return %nodes;
376 }
377 sub near
378 {
379 158 ▶ my $self = shift;
Subroutine near: called CC=2 SCAR=6.9
Pod undocumented
380 158 my $node = shift;
381 158 my $nodes = shift;
382 158 ▶ return grep { $self->exists_link($node, $_) && $node ne $_ } @{$nodes};
Branch: 1 branch
BranchTrueFalseTotal
$self->exists_link($node, $_) and $node ne $_ 309 472 781
383 }
384 sub dump
385 {
386 0 ▶ my $self = shift;
Subroutine dump: not called CC=3 SCAR=24.8
Pod undocumented
387 0 my $io = shift;
388 0 ▶ my $indent = shift || "";
Condition0%0/2
shift() || ''
l!l
00
MC/DC0%0/1
shift() || ''
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
389 0 foreach my $l (@{$self->links})
390 {
391 0 $l->dump($io, $indent);
392 }
393 }
394 sub load_pack
395 {
396 0 ▶ my $self = shift;
Subroutine load_pack: not called CC=4 SCAR=30.0
Pod undocumented
397 0 my $class = shift;
398 0 my $data = shift;
399 0 $data .= "EOF\n";
400 0 my @lines = split "\n", $data;
401 0 load $class;
402 0 my $rel_data = "";
403 0 foreach my $l (@lines)
404 {
405 0 ▶ if($l !~ /^\s/)
Branch: 1 branch
BranchTrueFalseTotal
if (not $l =~ /^\s/) { } 0 0 0
406 {
407 0 ▶ if($rel_data)
Branch: 1 branch
BranchTrueFalseTotal
if ($rel_data) 0 0 0
408 {
409 0 my $rel = $class->load($rel_data);
410 0 $self->add_link($rel);
411 }
412 0 $rel_data = $l . "\n";
413 }
414 else
415 {
416 0 $rel_data .= $l . "\n";
417 }
418 }
419 }
420  
421 11 1;
« lib/BalanceOfPower/Role/Broker.pm ↑ Summary blib/lib/BalanceOfPower/Executive.pm »