lib/BalanceOfPower/Relations/RelPack.pm

filter: Statementstmt 6.2% 16 / 257 Branchbran 0.0% 0 / 74 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 11.7% 4 / 34 Podpod 0.0% 0 / 27 totaltotal 4.9% 20 / 407 CC 56 SCAR 30.5
CC
56
Cov
5%
distance
72.0
links_for_node
30.0
links_for_node1
30.0
CRAP 21.1
« lib/BalanceOfPower/Dice.pm ↑ Summary lib/BalanceOfPower/Role/Historian.pm »
1 package BalanceOfPower::Relations::RelPack;
2 11 $BalanceOfPower::Relations::RelPack::VERSION = '0.300010';
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 0 ▶ default => sub { [] }
Subroutine __ANON__: not called CC=1 SCAR=6.9
12 11 );
13 has links_grid => (
14 is => 'rw',
15 0 ▶ default => sub { {} }
Subroutine __ANON__: not called CC=1 SCAR=6.9
16 11 );
17 has distance_cache => (
18 is => 'rw',
19 0 ▶ default => sub { {} }
Subroutine __ANON__: not called CC=1 SCAR=6.9
20 11 );
21  
22  
23 sub all
24 {
25 0 ▶ my $self = shift;
Subroutine all: not called CC=1 SCAR=6.9
Pod undocumented
26 0 return @{$self->links};
27 }
28 sub reset
29 {
30 0 ▶ my $self = shift;
Subroutine reset: not called CC=1 SCAR=6.9
Pod undocumented
31 0 $self->links([]);
32 0 $self->links_grid({});
33 0 $self->distance_cache({});
34 }
35  
36 sub exists_link
37 {
38 0 ▶ my $self = shift;
Subroutine exists_link: not called CC=2 SCAR=17.9
Pod undocumented
39 0 my $node1 = shift;
40 0 my $node2 = shift;
41 0 ▶ if(exists $self->links_grid->{$node1}->{$node2})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->links_grid->{$node1}{$node2}) { } 0 0 0
42 {
43 0 return $self->links_grid->{$node1}->{$node2}
44 }
45 else
46 {
47 0 return undef;
48 }
49 }
50  
51 sub add_link
52 {
53 0 ▶ my $self = shift;
Subroutine add_link: not called CC=2 SCAR=17.9
Pod undocumented
54 0 my $link = shift;
55 0 my $node1 = $link->node1;
56 0 my $node2 = $link->node2;
57 0 ▶ if(! $self->exists_link($node1, $node2))
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->exists_link($node1, $node2)) { } 0 0 0
58 {
59 0 push @{$self->links}, $link;
60 0 $self->links_grid->{$node1}->{$node2} = $link;
61 0 $self->links_grid->{$node2}->{$node1} = $link;
62 0 return 1;
63 }
64 else
65 {
66 0 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 0 ▶ my $self = shift;
Subroutine delete_references: not called CC=1 SCAR=6.9
Pod undocumented
79 0 my $node1 = shift;
80 0 my $node2 = shift;
81 0 $self->links_grid->{$node1}->{$node2} = undef;
82 0 $self->links_grid->{$node2}->{$node1} = undef;
83 }
84 sub delete_references_for_node
85 {
86 0 ▶ my $self = shift;
Subroutine delete_references_for_node: not called CC=3 SCAR=24.8
Pod undocumented
87 0 my $node1 = shift;
88 0 foreach my $k (%{$self->links_grid->{$node1}})
89 {
90 0 ▶ if($k)
Branch: 1 branch
BranchTrueFalseTotal
if ($k) 0 0 0
91 {
92 0 $self->links_grid->{$k}->{$node1} = undef;
93 }
94 }
95 0 $self->links_grid->{$node1} = undef;
96 }
97 sub delete_link
98 {
99 0 ▶ my $self = shift;
Subroutine delete_link: not called CC=1 SCAR=6.9
Pod undocumented
100 0 my $node1 = shift;
101 0 my $node2 = shift;
102 0 @{$self->links} = grep { ! $_->involve($node1, $node2) } @{$self->links};
103 0 $self->delete_references($node1, $node2);
104 }
105  
106 sub delete_link_for_node
107 {
108 0 ▶ my $self = shift;
Subroutine delete_link_for_node: not called CC=1 SCAR=6.9
Pod undocumented
109 0 my $n1 = shift;
110 0 @{$self->links} = grep { ! $_->has_node($n1) } @{$self->links};
111 0 $self->delete_references_for_node($n1);
112 }
113 sub garbage_collector
114 {
115 0 ▶ my $self = shift;
Subroutine garbage_collector: not called CC=3 SCAR=24.8
Pod undocumented
116 0 my $query = shift;
117 0 my @new = ();
118 0 for(@{$self->links})
119 {
120 0 ▶ if(! $query->($_))
Branch: 1 branch
BranchTrueFalseTotal
if (not &$query($_)) { } 0 0 0
121 {
122 0 push @new, $_;
123 }
124 else
125 {
126 0 $self->delete_references($_->node1, $_->node2);
127 }
128 }
129 0 @{$self->links} = @new;
130 }
131 sub links_for_node
132 {
133 0 ▶ my $self = shift;
Subroutine links_for_node: not called CC=5 SCAR=34.0
Pod undocumented
134 0 my $node = shift;
135 0 ▶ return $self->all() if(! $node);
Branch: 1 branch
BranchTrueFalseTotal
unless $node 0 0 0
136 0 my @out = ();
137 0 foreach my $k (keys %{$self->links_grid->{$node}})
138 {
139 0 ▶ if($k)
Branch: 1 branch
BranchTrueFalseTotal
if ($k) 0 0 0
140 {
141 0 my $r = $self->links_grid->{$node}->{$k};
142 0 ▶ push @out, $r if($r);
Branch: 1 branch
BranchTrueFalseTotal
if $r 0 0 0
143 }
144 }
145 0 return @out;
146 }
147 sub links_for_node1
148 {
149 0 ▶ my $self = shift;
Subroutine links_for_node1: not called CC=5 SCAR=34.0
Pod undocumented
150 0 my $node = shift;
151 0 ▶ return $self->all() if(! $node);
Branch: 1 branch
BranchTrueFalseTotal
unless $node 0 0 0
152 0 my @out = ();
153 0 foreach my $r (@{$self->links})
154 {
155 0 ▶ if($r->bidirectional)
Branch: 1 branch
BranchTrueFalseTotal
if ($r->bidirectional) 0 0 0
156 {
157 0 return $self->links_for_node($node);
158 }
159 0 ▶ if($r->node1 eq $node)
Branch: 1 branch
BranchTrueFalseTotal
if ($r->node1 eq $node) 0 0 0
160 {
161 0 push @out, $r;
162 }
163 }
164 0 return @out;
165 }
166 sub links_for_node2
167 {
168 0 ▶ my $self = shift;
Subroutine links_for_node2: not called CC=5 SCAR=34.0
Pod undocumented
169 0 my $node = shift;
170 0 ▶ return $self->all() if(! $node);
Branch: 1 branch
BranchTrueFalseTotal
unless $node 0 0 0
171 0 my @out = ();
172 0 foreach my $r (@{$self->links})
173 {
174 0 ▶ if($r->bidirectional)
Branch: 1 branch
BranchTrueFalseTotal
if ($r->bidirectional) 0 0 0
175 {
176 0 return $self->links_for_node($node);
177 }
178 0 ▶ if($r->node2 eq $node)
Branch: 1 branch
BranchTrueFalseTotal
if ($r->node2 eq $node) 0 0 0
179 {
180 0 push @out, $r;
181 }
182 }
183 0 return @out;
184 }
185 sub first_link_for_node
186 {
187 0 ▶ my $self = shift;
Subroutine first_link_for_node: not called CC=3 SCAR=24.8
Pod undocumented
188 0 my $node = shift;
189 0 foreach my $r (@{$self->links})
190 {
191 0 ▶ if($r->has_node($node))
Branch: 1 branch
BranchTrueFalseTotal
if ($r->has_node($node)) 0 0 0
192 {
193 0 return $r;
194 }
195 }
196 0 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 0 ▶ my $self = shift;
Subroutine first_link_for_node2: not called CC=2 SCAR=17.9
Pod undocumented
215 0 my $node = shift;
216 0 my @links = $self->links_for_node2($node);
217 0 ▶ if(@links)
Branch: 1 branch
BranchTrueFalseTotal
if (@links) { } 0 0 0
218 {
219 0 return $links[0]
220 }
221 else
222 {
223 0 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 0 ▶ my $self = shift;
Subroutine query: not called CC=5 SCAR=34.0
Pod undocumented
246 0 my $query = shift;
247 0 my $node1 = shift;
248 0 my @out = ();
249 0 for(@{$self->links})
250 {
251 0 ▶ if($query->($_))
Branch: 1 branch
BranchTrueFalseTotal
if (&$query($_)) 0 0 0
252 {
253 0 ▶ if($node1)
Branch: 1 branch
BranchTrueFalseTotal
if ($node1) { } 0 0 0
254 {
255 0 ▶ if($_->has_node($node1))
Branch: 1 branch
BranchTrueFalseTotal
if ($_->has_node($node1)) 0 0 0
256 {
257 0 push @out, $_;
258 }
259 }
260 else
261 {
262 0 push @out, $_;
263 }
264 }
265 }
266 0 return @out;
267 }
268 sub output_links
269 {
270 0 ▶ my $self = shift;
Subroutine output_links: not called CC=4 SCAR=30.0
Pod undocumented
271 0 my $n = shift;
272 0 ▶ my $mode = shift || 'print';
Condition0%0/2
shift() || 'print'
l!l
00
MC/DC0%0/1
shift() || 'print'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
273 0 ▶ if($mode eq 'print')
Branch: 2 branches
BranchTrueFalseTotal
if ($mode eq 'print') { } 0 0 0
elsif ($mode eq 'html') { } 0 0 0
274 {
275 0 return $self->print_links($n);
276 }
277 elsif($mode eq 'html')
278 {
279 0 return $self->html_links($n);
280 }
281 }
282  
283 sub print_links
284 {
285 0 ▶ my $self = shift;
Subroutine print_links: not called CC=4 SCAR=30.0
Pod undocumented
286 0 my $n = shift;
287 0 my $out = "";
288 0 foreach my $b (@{$self->links})
289 {
290 0 ▶ if($n)
Branch: 1 branch
BranchTrueFalseTotal
if ($n) { } 0 0 0
291 {
292 0 ▶ if($b->has_node($n))
Branch: 1 branch
BranchTrueFalseTotal
if ($b->has_node($n)) 0 0 0
293 {
294 0 $out .= $b->print($n) . "\n";
295 }
296 }
297 else
298 {
299 0 $out .= $b->print($n) . "\n";
300 }
301 }
302 0 return $out;
303 }
304 sub html_links
305 {
306 0 ▶ my $self = shift;
Subroutine html_links: not called CC=4 SCAR=30.0
Pod undocumented
307 0 my $n = shift;
308 0 my $out = "";
309 0 foreach my $b (@{$self->links})
310 {
311 0 ▶ if($n)
Branch: 1 branch
BranchTrueFalseTotal
if ($n) { } 0 0 0
312 {
313 0 ▶ if($b->has_node($n))
Branch: 1 branch
BranchTrueFalseTotal
if ($b->has_node($n)) 0 0 0
314 {
315 0 $out .= $b->html($n) . "<br />";
316 }
317 }
318 else
319 {
320 0 $out .= $b->html($n) . "<br />";
321 }
322 }
323 0 return "<p>$out</p>";
324 }
325  
326 #BFS implementation
327 sub distance
328 {
329 0 ▶ my $self = shift;
Subroutine distance: not called CC=8 SCAR=42.8
Pod undocumented
330 0 my $node1 = shift;
331 0 my $node2 = shift;
332 0 my $nodes_list = shift;
333 0 my %nodes = $self->get_cached_nodes($node1, $nodes_list);
334 0 my $log;
335 0 ▶ if($nodes{$node2}->{distance} != -1)
Branch: 1 branch
BranchTrueFalseTotal
if ($nodes{$node2}{"distance"} != -1) 0 0 0
336 {
337 0 return $nodes{$node2}->{distance};
338 }
339 0 ▶ 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 0 0
340 {
341 0 $nodes{$node2}->{distance} = $cached_distance;
342 0 $self->distance_cache->{$node1}->{nodes} = \%nodes;
343 0 return $cached_distance;
344 }
345  
346 0 my @queue = ( $node1 );
347 0 ▶ if(exists $self->distance_cache->{$node1}->{queue})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->distance_cache->{$node1}{"queue"}) 0 0 0
348 {
349 0 @queue = @{$self->distance_cache->{$node1}->{queue}};
350 }
351 0 ▶ while(@queue)
Branch: 1 branch
BranchTrueFalseTotal
if (@queue) 0 0 0
352 {
353
354 0 my $n = shift @queue;
355 0 foreach my $near ($self->near($n, $nodes_list))
356 {
357 0 ▶ if($nodes{$near}->{distance} == -1)
Branch: 1 branch
BranchTrueFalseTotal
if ($nodes{$near}{"distance"} == -1) 0 0 0
358 {
359 0 my $d = $nodes{$n}->{distance} + 1;
360 0 $nodes{$near}->{distance} = $nodes{$n}->{distance} + 1;
361 0 push @queue, $near;
362 }
363 }
364 0 ▶ if($nodes{$node2}->{distance} != -1)
Branch: 1 branch
BranchTrueFalseTotal
if ($nodes{$node2}{"distance"} != -1) 0 0 0
365 {
366 0 $self->distance_cache->{$node1}->{nodes} = \%nodes;
367 0 $self->distance_cache->{$node1}->{queue} = \@queue;
368 0 return $nodes{$node2}->{distance};
369 }
370 }
371 0 $nodes{$node2}->{distance} = 100;
372 0 $self->distance_cache->{$node1}->{nodes} = \%nodes;
373 0 $self->distance_cache->{$node1}->{queue} = \@queue;
374 0 return 100;
375 }
376 sub get_cached_distance
377 {
378 0 ▶ my $self = shift;
Subroutine get_cached_distance: not called CC=4 SCAR=30.0
Pod undocumented
379 0 my $node1 = shift;
380 0 my $node2 = shift;
381 0 ▶ 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 0 0
Condition0%0/6
exists $self->distance_cache->{$node1} and exists $self->distance_cache->{$node1}{$node2} and $self->distance_cache->{$node1}{$node2} != -1
!ll&&!rl&&r
000
exists $self->distance_cache->{$node1} and exists $self->distance_cache->{$node1}{$node2}
!ll&&!rl&&r
000
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 table0%0/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
382 exists $self->distance_cache->{$node1}->{$node2} &&
383 $self->distance_cache->{$node1}->{$node2} != -1)
384 {
385 0 return $self->distance_cache->{$node1}->{$node2};
386 }
387 else
388 {
389 0 return undef;
390 }
391 }
392 sub get_cached_nodes
393 {
394 0 ▶ my $self = shift;
Subroutine get_cached_nodes: not called CC=3 SCAR=24.8
Pod undocumented
395 0 my $node1 = shift;
396 0 my $nodes_list = shift;
397 0 my %nodes = ();
398 0 ▶ if(exists $self->distance_cache->{$node1})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->distance_cache->{$node1}) { } 0 0 0
399 {
400 0 %nodes = %{$self->distance_cache->{$node1}->{nodes}};
401 }
402 else
403 {
404 0 foreach(@{$nodes_list})
405 {
406 0 $nodes{$_}->{distance} = -1;
407 }
408 0 $nodes{$node1}->{distance} = 0;
409 }
410 0 return %nodes;
411 }
412 sub near
413 {
414 0 ▶ my $self = shift;
Subroutine near: not called CC=2 SCAR=17.9
Pod undocumented
415 0 my $node = shift;
416 0 my $nodes = shift;
417 0 ▶ return grep { $self->exists_link($node, $_) && $node ne $_ } @{$nodes};
Branch: 1 branch
BranchTrueFalseTotal
$self->exists_link($node, $_) and $node ne $_ 0 0 0
418 }
419 sub dump
420 {
421 0 ▶ my $self = shift;
Subroutine dump: not called CC=3 SCAR=24.8
Pod undocumented
422 0 my $io = shift;
423 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
424 0 foreach my $l (@{$self->links})
425 {
426 0 $l->dump($io, $indent);
427 }
428 }
429 sub load_pack
430 {
431 0 ▶ my $self = shift;
Subroutine load_pack: not called CC=4 SCAR=30.0
Pod undocumented
432 0 my $class = shift;
433 0 my $data = shift;
434 0 $data .= "EOF\n";
435 0 my @lines = split "\n", $data;
436 0 load $class;
437 0 my $rel_data = "";
438 0 foreach my $l (@lines)
439 {
440 0 ▶ if($l !~ /^\s/)
Branch: 1 branch
BranchTrueFalseTotal
if (not $l =~ /^\s/) { } 0 0 0
441 {
442 0 ▶ if($rel_data)
Branch: 1 branch
BranchTrueFalseTotal
if ($rel_data) 0 0 0
443 {
444 0 my $rel = $class->load($rel_data);
445 0 $self->add_link($rel);
446 }
447 0 $rel_data = $l . "\n";
448 }
449 else
450 {
451 0 $rel_data .= $l . "\n";
452 }
453 }
454 }
455  
456 11 1;
« lib/BalanceOfPower/Dice.pm ↑ Summary lib/BalanceOfPower/Role/Historian.pm »