blib/lib/Algorithm/SixDegrees.pm

filter: Statementstmt 89.6% 173 / 193 Branchbran 78.1% 100 / 128 Conditioncond 64.8% 35 / 54 MC/DCmcdc 43.7% 14 / 32 Subroutinesub 100% 12 / 12 Podpod 100% 6 / 6 totaltotal 80.0% 340 / 425 CC 88 SCAR 40.6
CC
88
Cov
82%
make_link
92.5
_match
16.6
forward_data_source
7.0
CRAP 58.0
↑ Summary
1 package Algorithm::SixDegrees;
2  
3 6 require 5.006;
4 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use Scalar::Util qw(reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 =encoding UTF-8
9  
10 =head1 NAME
11  
12 Algorithm::SixDegrees - Find a path through linked elements in a set
13  
14 =head1 VERSION
15  
16 Version 1.01
17  
18 =cut
19  
20 6 our $VERSION = '1.01';
21 6 our $ERROR = '';
22  
23 =head1 SYNOPSIS
24  
25 use Algorithm::SixDegrees;
26  
27 my $sd1 = Algorithm::SixDegrees->new();
28 $sd1->data_source( actors => \&starred_in );
29 $sd1->data_source( movies => \&stars_of );
30 @elems = $sd1->make_link('actors', 'Tom Cruise', 'Kevin Bacon');
31  
32 my $sd2 = Algorithm::SixDegrees->new();
33 $sd2->forward_data_source( friends => \&friends, @args );
34 $sd2->reverse_data_source( friends => \&friend_of, @args );
35 @elems = $sd2->make_link('friends', 'Bob', 'Mark');
36  
37 =head1 DESCRIPTION
38  
39 C<Algorithm::SixDegrees> is a Perl implementation of a breadth-first
40 search through a set of linked elements in order to find the shortest
41 possible chain linking two specific elements together.
42  
43 In simpler terms, this module will take a bunch of related items and
44 attempt to find a relationship between two of them. It looks for the
45 shortest (and generally, simplest) relationship it can find.
46  
47 =head1 CONSTRUCTOR
48  
49 =head2 new()
50  
51 C<Algorithm::SixDegrees> requires use as an object; it can't (yet) be used
52 as a stand-alone module. C<new> takes no arguments, however.
53  
54 =cut
55  
56 10 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
57 10 my $class = shift;
58 10 my $self = {
59 _source_left => {},
60 _source_right => {},
61 _sources => [],
62 _investigated => {},
63 };
64 10 return bless $self,$class;
65 }
66  
67 =head1 FUNCTIONS
68  
69 =head2 forward_data_source( name => \&sub, @args );
70  
71 Tells C<Algorithm::SixDegrees> that all items in the data set relating to
72 C<name> can be retrieved by calling C<sub>. See L</SUBROUTINE RULES>.
73  
74 In our friends example above, if Bob considers Mark a friend, but Mark
75 doesn't consider Bob a friend, calling the sub with "Bob" as an argument
76 should return "Mark", but calling the sub with "Mark" as an argument
77 should not return "Bob".
78  
79 =cut
80  
81 23 ▶sub forward_data_source {
Subroutine forward_data_source: called CC=7 SCAR=19.5
Pod documented
82 23 my ($self, $name, $sub, @args) = @_;
83 23 ▶ die "Data sources must be named\n" unless defined($name);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 4 19 23
84 19 ▶ die "Data sources must have code supplied\n" unless defined($sub);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sub 2 17 19
85 17 ▶ die "Data sources must have a coderef argument\n" unless ref($sub) && reftype($sub) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $sub and builtin::reftype($sub) eq "CODE" 4 13 17
Condition100%3/3
ref $sub and builtin::reftype($sub) eq "CODE"
!ll&&!rl&&r
2213
MC/DC100%2/2
ref $sub and builtin::reftype($sub) eq "CODE"
ref $sub builtin::reftype($sub) eq "CODE"
Truth table100%3/3
A and B
A= ref $subB= builtin::reftype($sub) eq "CODE"
ABresult
0X0
100
111
86 13 $self->{'_source_left'}{$name}{'sub'} = $sub;
87 13 $self->{'_source_left'}{$name}{'args'} = \@args;
88 13 foreach my $source (@{$self->{'_sources'}}) {
89 6 ▶ return if $source eq $name;
Branch: 1 branch
BranchTrueFalseTotal
if $source eq $name 1 5 6
90 }
91 12 push(@{$self->{'_sources'}},$name);
92 12 return;
93 }
94  
95 =head2 reverse_data_source( name => \&sub, @args );
96  
97 Tells C<Algorithm::SixDegrees> that all items in the data set related to
98 by C<name> can be retrieved by calling C<sub>. See L</SUBROUTINE RULES>.
99  
100 In the same friends example, calling the sub with "Bob" as an argument
101 should not return "Mark", but calling the sub with "Mark" as an argument
102 should return "Bob".
103  
104 =cut
105  
106 18 ▶sub reverse_data_source {
Subroutine reverse_data_source: called CC=7 SCAR=19.5
Pod documented
107 18 my ($self, $name, $sub, @args) = @_;
108 18 ▶ die "Data sources must be named\n" unless defined($name);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 2 16 18
109 16 ▶ die "Data sources must have code supplied\n" unless defined($sub);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sub 1 15 16
110 15 ▶ die "Data sources must have a coderef argument\n" unless ref($sub) && reftype($sub) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $sub and builtin::reftype($sub) eq "CODE" 2 13 15
Condition100%3/3
ref $sub and builtin::reftype($sub) eq "CODE"
!ll&&!rl&&r
1113
MC/DC100%2/2
ref $sub and builtin::reftype($sub) eq "CODE"
ref $sub builtin::reftype($sub) eq "CODE"
Truth table100%3/3
A and B
A= ref $subB= builtin::reftype($sub) eq "CODE"
ABresult
0X0
100
111
111 13 $self->{'_source_right'}{$name}{'sub'} = $sub;
112 13 $self->{'_source_right'}{$name}{'args'} = \@args;
113 13 foreach my $source (@{$self->{'_sources'}}) {
114 17 ▶ return if $source eq $name;
Branch: 1 branch
BranchTrueFalseTotal
if $source eq $name 12 5 17
115 }
116 1 push(@{$self->{'_sources'}},$name);
117 1 return;
118 }
119  
120 =head2 data_source( name => \&sub, @args );
121  
122 Sets up a data source as both forward and reverse. This is useful if
123 the data source is mutually relational; that is, in our actors/movies
124 example, Kevin Bacon is always in Mystic River, and Mystic River always
125 has Kevin Bacon in it.
126  
127 =cut
128  
129 15 ▶sub data_source {
Subroutine data_source: called CC=1 SCAR=0.0
Pod documented
130 15 my ($self, $name, $sub, @args) = @_;
131 15 $self->forward_data_source($name,$sub,@args);
132 10 $self->reverse_data_source($name,$sub,@args);
133 10 return;
134 }
135  
136 =head2 make_link
137  
138 Does the work of making the link. Returns a list or arrayref, based
139 on calling context.
140  
141 =cut
142  
143 35 ▶sub make_link {
Subroutine make_link: called CC=58 SCAR=45.3
Pod documented
144 35 my ($self, $mainsource, $start, $end) = @_;
145 35 $ERROR = undef;
146  
147 35 ▶ unless (ref($self) && $self->isa(__PACKAGE__)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $self and $self->isa(__PACKAGE__)) 2 33 35
Condition66%2/3
ref $self and $self->isa(__PACKAGE__)
!ll&&!rl&&r
2033
MC/DC50%1/2
ref $self and $self->isa(__PACKAGE__)
ref $self $self->isa(__PACKAGE__)
Truth table66%2/3
A and B
A= ref $selfB= $self->isa(__PACKAGE__)
ABresult
0X0
100
111
148 2 $ERROR = 'Invalid object reference used to call make_link';
149 2 return;
150 }
151 33 ▶ unless (defined($mainsource)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $mainsource) 1 32 33
152 1 $ERROR = 'Data set name is not defined';
153 1 return;
154 }
155 32 ▶ unless (defined($start)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $start) 1 31 32
156 1 $ERROR = 'Starting identifier is not defined';
157 1 return;
158 }
159 31 ▶ unless (defined($end)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $end) 1 30 31
160 1 $ERROR = 'Ending identifier is not defined';
161 1 return;
162 }
163  
164 # Assume working from "left to right"; therefore, links leading
165 # from the starting identifier are on the "left side", and links
166 # leading to the ending identifier are on the "right side".
167 30 my %leftside = ();
168 30 my %rightside = ();
169  
170 # If $altsource gets defined, that means there are two sources used.
171 30 my $altsource;
172  
173 30 ▶ unless (exists($self->{'_sources'}) && reftype($self->{'_sources'}) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"_sources"} and builtin::reftype($self->{"_sources"}) eq "ARRAY") 2 28 30
Condition100%3/3
exists $self->{"_sources"} and builtin::reftype($self->{"_sources"}) eq "ARRAY"
!ll&&!rl&&r
1128
MC/DC100%2/2
exists $self->{"_sources"} and builtin::reftype($self->{"_sources"}) eq "ARRAY"
exists $self->{"_sources"} builtin::reftype($self->{"_sources"}) eq "ARRAY"
Truth table100%3/3
A and B
A= exists $self->{"_sources"}B= builtin::reftype($self->{"_sources"}) eq "ARRAY"
ABresult
0X0
100
111
174 2 $ERROR = 'No data sources defined';
175 2 return;
176 }
177 28 my @sources = @{$self->{'_sources'}};
178 28 my $source_exists = 0;
179 28 foreach my $source (@sources) {
180 39 ▶ if ($mainsource eq $source) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mainsource eq $source) { } 26 13 39
181 26 $source_exists = 1;
182 26 $leftside{$source} = {$start, undef};
183 26 $rightside{$source} = {$end, undef};
184 } else {
185 13 $altsource = $source;
186 13 $leftside{$source} = {};
187 13 $rightside{$source} = {};
188 }
189 39 ▶ unless (ref($self->{'_source_left'}) &&
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $self->{"_source_left"} and ref $self->{"_source_left"}{$source} and builtin::reftype($self->{"_source_left"}{$source}{"sub"}) eq "CODE") 2 37 39
Condition83%5/6
ref $self->{"_source_left"} and ref $self->{"_source_left"}{$source} and builtin::reftype($self->{"_source_left"}{$source}{"sub"}) eq "CODE"
!ll&&!rl&&r
2037
ref $self->{"_source_left"} and ref $self->{"_source_left"}{$source}
!ll&&!rl&&r
1137
MC/DC66%2/3
ref $self->{"_source_left"} and ref $self->{"_source_left"}{$source} and builtin::reftype($self->{"_source_left"}{$source}{"sub"}) eq "CODE"
ref $self->{"_source_left"} ref $self->{"_source_left"}{$source} builtin::reftype($self->{"_source_left"}{$source}{"sub"}) eq "CODE"
Truth table75%3/4
A and B and C
A= ref $self->{"_source_left"}B= ref $self->{"_source_left"}{$source}C= builtin::reftype($self->{"_source_left"}{$source}{"sub"}) eq "CODE"
ABCresult
0XX0
10X0
1100
1111
190 ref($self->{'_source_left'}{$source}) &&
191 reftype($self->{'_source_left'}{$source}{'sub'}) eq 'CODE') {
192 2 $ERROR = "Source '$source' does not have a valid forward subroutine";
193 2 return;
194 }
195 37 ▶ unless (ref($self->{'_source_right'}) &&
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $self->{"_source_right"} and ref $self->{"_source_right"}{$source} and builtin::reftype($self->{"_source_right"}{$source}{"sub"}) eq "CODE") 2 35 37
Condition83%5/6
ref $self->{"_source_right"} and ref $self->{"_source_right"}{$source} and builtin::reftype($self->{"_source_right"}{$source}{"sub"}) eq "CODE"
!ll&&!rl&&r
2035
ref $self->{"_source_right"} and ref $self->{"_source_right"}{$source}
!ll&&!rl&&r
1135
MC/DC66%2/3
ref $self->{"_source_right"} and ref $self->{"_source_right"}{$source} and builtin::reftype($self->{"_source_right"}{$source}{"sub"}) eq "CODE"
ref $self->{"_source_right"} ref $self->{"_source_right"}{$source} builtin::reftype($self->{"_source_right"}{$source}{"sub"}) eq "CODE"
Truth table75%3/4
A and B and C
A= ref $self->{"_source_right"}B= ref $self->{"_source_right"}{$source}C= builtin::reftype($self->{"_source_right"}{$source}{"sub"}) eq "CODE"
ABCresult
0XX0
10X0
1100
1111
196 ref($self->{'_source_right'}{$source}) &&
197 reftype($self->{'_source_right'}{$source}{'sub'}) eq 'CODE') {
198 2 $ERROR = "Source '$source' does not have a valid reverse subroutine";
199 2 return;
200 }
201 35 $self->{'_investigated'}{$source} = {};
202 }
203 24 ▶ unless ($source_exists) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($source_exists) 2 22 24
204 2 $ERROR = "Source '$mainsource' was not defined";
205 2 return;
206 }
207 22 ▶ if (scalar(keys(%leftside)) > 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %leftside > 2) 1 21 22
208 1 $ERROR = 'Too many defined data sources; maximum is 2';
209 1 return;
210 }
211  
212  
213 21 ▶ if ($start eq $end) {
Branch: 1 branch
BranchTrueFalseTotal
if ($start eq $end) 5 16 21
214 # Only one element if the start and end are the same.
215 5 ▶ return wantarray ? ($start) : [$start];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 3 2 5
216 }
217  
218 16 my $leftcount = 1;
219 16 my $rightcount = 1;
220  
221 # If altsource exists, pull the left side main, then pull the right side main,
222 # and check for middle matches. This reduces database hits as opposed to
223 # where it's pulled left main - left alt; left alt >= 1 at that point, whereas
224 # right main on the first loop == 1. Following that, pull the left alt and
225 # then the right alt, which gets the CHAINLOOP back in synch.
226  
227 16 ▶ if (defined($altsource)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $altsource) 8 8 16
228 8 my ($count,$id,$err) = $self->_match('left',$mainsource,$altsource,\%leftside,\%rightside);
229 0 ▶ if (defined($err)) { $ERROR = $err; return; };
Branch: 1 branch
BranchTrueFalseTotal
if (defined $err) 0 8 8
230 0 ▶ if (defined($id)) { $ERROR = 'Internal error, id cannot match here'; return; };
Branch: 1 branch
BranchTrueFalseTotal
if (defined $id) 0 8 8
231 8 ▶ return if !defined($count) || $count == 0;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $count or $count == 0 0 8 8
Condition33%1/3
not defined $count or $count == 0
l!l&&r!l&&!r
008
MC/DC0%0/2
not defined $count or $count == 0
not defined $count $count == 0
Truth table33%1/3
not(A) or B
A= not defined $countB= $count == 0
ABresult
1X1
011
000
232  
233 8 ($count,$id,$err) = $self->_match('right',$mainsource,$altsource,\%rightside,\%leftside);
234 0 ▶ if (defined($err)) { $ERROR = $err; return; };
Branch: 1 branch
BranchTrueFalseTotal
if (defined $err) 0 8 8
235 8 ▶ if (defined($id)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $id) 2 6 8
236 2 my @abc = ($leftside{$altsource}{$id},$id,$rightside{$altsource}{$id});
237 2 ▶ return wantarray ? @abc : \@abc;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 0 2
238 };
239 6 ▶ return if !defined($count) || $count == 0;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $count or $count == 0 2 4 6
Condition66%2/3
not defined $count or $count == 0
l!l&&r!l&&!r
024
MC/DC50%1/2
not defined $count or $count == 0
not defined $count $count == 0
Truth table66%2/3
not(A) or B
A= not defined $countB= $count == 0
ABresult
1X1
011
000
240  
241 4 ($leftcount,$id,$err) = $self->_match('left',$altsource,$mainsource,\%leftside,\%rightside);
242 0 ▶ if (defined($err)) { $ERROR = $err; return; };
Branch: 1 branch
BranchTrueFalseTotal
if (defined $err) 0 4 4
243 0 ▶ if (defined($id)) { $ERROR = 'Internal error, id cannot match here'; return; };
Branch: 1 branch
BranchTrueFalseTotal
if (defined $id) 0 4 4
244 4 ▶ return if !defined($leftcount) || $leftcount == 0;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $leftcount or $leftcount == 0 0 4 4
Condition33%1/3
not defined $leftcount or $leftcount == 0
l!l&&r!l&&!r
004
MC/DC0%0/2
not defined $leftcount or $leftcount == 0
not defined $leftcount $leftcount == 0
Truth table33%1/3
not(A) or B
A= not defined $leftcountB= $leftcount == 0
ABresult
1X1
011
000
245  
246 4 ($rightcount,$id,$err) = $self->_match('right',$altsource,$mainsource,\%rightside,\%leftside);
247 0 ▶ if (defined($err)) { $ERROR = $err; return; };
Branch: 1 branch
BranchTrueFalseTotal
if (defined $err) 0 4 4
248 4 ▶ if (defined($id)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $id) 2 2 4
249 2 my $la = $leftside{$mainsource}{$id};
250 2 my $lm = $leftside{$altsource}{$la};
251 2 my $ra = $rightside{$mainsource}{$id};
252 2 my $rm = $rightside{$altsource}{$ra};
253 2 ▶ unless (defined($la) && defined($lm) && defined($ra) && defined($rm)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $la and defined $lm and defined $ra and defined $rm) 0 2 2
Condition33%3/9
defined $la and defined $lm and defined $ra and defined $rm
!ll&&!rl&&r
002
defined $la and defined $lm and defined $ra
!ll&&!rl&&r
002
defined $la and defined $lm
!ll&&!rl&&r
002
MC/DC0%0/4
defined $la and defined $lm and defined $ra and defined $rm
defined $la defined $lm defined $ra defined $rm
Truth table20%1/5
A and B and C and D
A= defined $laB= defined $lmC= defined $raD= defined $rm
ABCDresult
0XXX0
10XX0
110X0
11100
11111
254 0 $ERROR = 'Internal error, identifier not defined';
255 0 return;
256 }
257 2 ▶ return wantarray ? ($lm,$la,$id,$ra,$rm) : [$lm,$la,$id,$ra,$rm];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 0 2
258 };
259 2 ▶ return if !defined($rightcount) || $rightcount == 0;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $rightcount or $rightcount == 0 0 2 2
Condition33%1/3
not defined $rightcount or $rightcount == 0
l!l&&r!l&&!r
002
MC/DC0%0/2
not defined $rightcount or $rightcount == 0
not defined $rightcount $rightcount == 0
Truth table33%1/3
not(A) or B
A= not defined $rightcountB= $rightcount == 0
ABresult
1X1
011
000
260  
261 }
262  
263 # There is bias here, but the tie needs to be broken, so in the
264 # event of a tie, move left to right in the chain.
265  
266 CHAINLOOP: {
267 10 my $id;
268 my $err;
269 45 ▶ if ($leftcount <= $rightcount) {
Branch: 1 branch
BranchTrueFalseTotal
if ($leftcount <= $rightcount) { } 42 3 45
270 42 ▶ if (defined($altsource)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $altsource) { } 4 38 42
271 4 ($leftcount,$id,$err) = $self->_match_two('left',$mainsource,$altsource,\%leftside,\%rightside);
272 } else {
273 38 ($leftcount,$id,$err) = $self->_match_one('left',$mainsource,\%leftside,\%rightside);
274 }
275 } else {
276 3 ▶ if (defined($altsource)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $altsource) { } 2 1 3
277 2 ($rightcount,$id,$err) = $self->_match_two('right',$mainsource,$altsource,\%rightside,\%leftside);
278 } else {
279 1 ($rightcount,$id,$err) = $self->_match_one('right',$mainsource,\%rightside,\%leftside);
280 }
281 }
282 45 ▶ if(defined($err)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $err) 0 45 45
283 0 $ERROR = $err;
284 0 return;
285 }
286 45 ▶ if(defined($id)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $id) 7 38 45
287 # If _match returns an id, that means a match was found.
288 # To get it, we simply have to trace out from the "middle"
289 # to get the full link.
290 7 my @match = ($id);
291 # middle, building to left.
292 7 ▶ while($match[0] ne $start) {
Branch: 1 branch
BranchTrueFalseTotal
if ($match[0] ne $start) 33 7 40
293 33 unshift(@match,$leftside{$mainsource}{$match[0]});
294 33 ▶ unshift(@match,$leftside{$altsource}{$match[0]}) if defined($altsource);
Branch: 1 branch
BranchTrueFalseTotal
if defined $altsource 6 27 33
295 33 ▶ if (!defined($match[0])) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $match[0]) 0 33 33
296 0 $ERROR = 'Internal error, left identifier was not defined';
297 0 return;
298 }
299 }
300 # middle building to right
301 7 ▶ while($match[-1] ne $end) {
Branch: 1 branch
BranchTrueFalseTotal
if ($match[-1] ne $end) 4 7 11
302 4 push(@match,$rightside{$mainsource}{$match[-1]});
303 4 ▶ push(@match,$rightside{$altsource}{$match[-1]}) if defined($altsource);
Branch: 1 branch
BranchTrueFalseTotal
if defined $altsource 4 0 4
304 4 ▶ if (!defined($match[-1])) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $match[-1]) 0 4 4
305 0 $ERROR = 'Internal error, right identifier was not defined';
306 0 return;
307 }
308 }
309 7 ▶ return wantarray ? @match : \@match;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 5 2 7
310 }
311 38 ▶ if ($leftcount == 0 || $rightcount == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($leftcount == 0 or $rightcount == 0) 3 35 38
Condition100%3/3
$leftcount == 0 or $rightcount == 0
l!l&&r!l&&!r
2135
MC/DC100%2/2
$leftcount == 0 or $rightcount == 0
$leftcount == 0 $rightcount == 0
Truth table100%3/3
A or B
A= $leftcount == 0B= $rightcount == 0
ABresult
1X1
011
000
312 3 last CHAINLOOP;
313 }
314 35 redo CHAINLOOP;
315 }
316  
317 3 ▶ return wantarray ? () : [];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 3 0 3
318 }
319  
320 =head2 error
321  
322 Returns the current value of C<$Algorithm::SixDegrees::ERROR>. See
323 L</SUBROUTINE RULES>.
324  
325 =cut
326  
327 20 ▶sub error {
Subroutine error: called CC=1 SCAR=0.0
Pod documented
328 20 return $ERROR;
329 }
330  
331 6 ▶sub _match_two {
Subroutine _match_two: called CC=5 SCAR=17.8
332 6 my ($self,$side,$mainsource,$altsource,$thisside,$thatside) = @_;
333 # Assume $self is OK since this is an internal function
334 6 my ($count,$id,$err) = $self->_match($side,$mainsource,$altsource,$thisside,$thatside);
335 6 ▶ return (undef,undef,$err) if defined($err);
Branch: 1 branch
BranchTrueFalseTotal
if defined $err 0 6 6
336 6 ▶ return ($count,$id,$err) if defined($id);
Branch: 1 branch
BranchTrueFalseTotal
if defined $id 0 6 6
337 6 ▶ return (0,undef,undef) if !defined($count) || $count == 0;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $count or $count == 0 0 6 6
Condition33%1/3
not defined $count or $count == 0
l!l&&r!l&&!r
006
MC/DC0%0/2
not defined $count or $count == 0
not defined $count $count == 0
Truth table33%1/3
not(A) or B
A= not defined $countB= $count == 0
ABresult
1X1
011
000
338 # mental note: this should never return an id
339 # after all, you can't have two mains together in a true
340 # alternating chain
341 6 return $self->_match($side,$altsource,$mainsource,$thisside,$thatside);
342 }
343  
344 39 ▶sub _match_one {
Subroutine _match_one: called CC=1 SCAR=0.0
345 39 my ($self,$side,$source,$thisside,$thatside) = @_;
346 # Assume $self is OK since this is an internal function
347 39 return $self->_match($side,$source,$source,$thisside,$thatside);
348 }
349  
350 75 ▶sub _match {
Subroutine _match: called CC=15 SCAR=28.1
351 75 my ($self,$side,$fromsource,$tosource,$thisside,$thatside) = @_;
352 # Assume $self is OK since this is an internal function
353 75 ▶ return (undef,undef,'Internal error: missing code') unless reftype($self->{"_source_$side"}{$fromsource}{'sub'}) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::reftype($$self{"_source_$side"}{$fromsource}{"sub"}) eq "CODE" 0 75 75
354 75 ▶ return (undef,undef,'Internal error: missing side (1)') unless reftype($thisside) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::reftype($thisside) eq "HASH" 0 75 75
355 75 ▶ return (undef,undef,'Internal error: missing side (2)') unless exists($thisside->{$fromsource});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $thisside->{$fromsource} 0 75 75
356 75 ▶ return (undef,undef,'Internal error: missing side (3)') unless reftype($thatside) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::reftype($thatside) eq "HASH" 0 75 75
357 75 ▶ return (undef,undef,'Internal error: missing side (4)') unless exists($thatside->{$tosource});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $thatside->{$tosource} 0 75 75
358  
359 75 my $newsidecount = 0;
360 75 foreach my $id (keys %{$thisside->{$fromsource}}) {
361 203 ▶ next if exists($self->{"_investigated"}{$fromsource}{$id});
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"_investigated"}{$fromsource}{$id} 122 81 203
362 81 $self->{"_investigated"}{$fromsource}{$id} = 1;
363  
364 81 ▶ my $use_args = reftype($self->{"_source_$side"}{$fromsource}{'args'}) eq 'ARRAY' ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
builtin::reftype($$self{"_source_$side"}{$fromsource}{'args'}) eq 'ARRAY' ? : 81 0 81
365  
366 81 ▶ my @ids = &{$self->{"_source_$side"}{$fromsource}{'sub'}}($id,($use_args?@{$self->{"_source_$side"}{$fromsource}{'args'}}:()));
Branch: 1 branch
BranchTrueFalseTotal
$use_args ? : 81 0 81
367 81 ▶ return (undef,undef,$ERROR) if scalar(@ids) == 1 && !defined($ids[0]);
Branch: 1 branch
BranchTrueFalseTotal
if scalar @ids == 1 and not defined $ids[0] 0 81 81
Condition66%2/3
scalar @ids == 1 and not defined $ids[0]
!ll&&!rl&&r
62190
MC/DC0%0/2
scalar @ids == 1 and not defined $ids[0]
scalar @ids == 1 not defined $ids[0]
Truth table66%2/3
A and not(B)
A= scalar @ids == 1B= not defined $ids[0]
ABresult
0X0
100
111
368 81 foreach my $thisid (@ids) {
369 142 ▶ unless (exists($thisside->{$tosource}{$thisid})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $thisside->{$tosource}{$thisid}) 88 54 142
370 88 $thisside->{$tosource}{$thisid} = $id;
371 88 $newsidecount++;
372 }
373 142 ▶ return (0,$thisid,undef) if exists($thatside->{$tosource}{$thisid});
Branch: 1 branch
BranchTrueFalseTotal
if exists $thatside->{$tosource}{$thisid} 11 131 142
374 }
375 }
376  
377 64 return $newsidecount;
378 }
379  
380 =head1 SUBROUTINE RULES
381  
382 Passed-in subroutines should take at least one argument, which
383 should be some form of unique identifier, and return a list of
384 unique identifiers that have a relation to the argument.
385  
386 The unique identifiers must be able to be compared with C<eq>.
387  
388 The identifiers should be unique in datatype; that is, in an
389 actor/movie relationship, "Kevin Bacon" can be both the name of an
390 actor and a movie.
391  
392 A linked data type must return identifiers that relate across the
393 link; that is, for an actor/movie relationship, an actor subroutine
394 should return movies, and a movie subroutine should return actors.
395  
396 Additional arguments can be provided; these will be stored in the
397 object and passed through as the second and further arguments to
398 the subroutine. This may be useful, for example, if you're using
399 some form of results caching and need to pass a C<tie>d handle
400 around.
401  
402 If you return explicit undef, please set C<$Algorithm::SixDegrees::ERROR>
403 with an error code. Explicit undef means that an error occurred
404 that should terminate the search; it should be returned as a
405 one-element list.
406  
407 =head1 AUTHOR
408  
409 Peter Krawczyk, C<< <petek@cpan.org> >>
410  
411 =head1 BUGS
412  
413 Please report any bugs or feature requests to
414 C<bug-algorithm-sixdegrees@rt.cpan.org>, or through the web interface at
415 L<http://rt.cpan.org>. I will be notified, and then you'll automatically
416 be notified of progress on your bug as I make changes.
417  
418 =head1 ACKNOWLEDGEMENTS
419  
420 Thank you to Slaven Rezić and Peter John Acklam for bug reports.
421 Apologies it took so long to see and fix them. (So much for
422 notifications...)
423  
424 Andy Lester and Ricardo Signes wrote Module::Starter, which helped
425 get the framework up and running fairly quickly.
426  
427 Brad Fitzpatrick of L<http://livejournal.com> for giving me access
428 to a LiveJournal interface to determine linking information on that
429 site, which enabled me to write the algorithm that has been reduced
430 into this module.
431  
432 =head1 COPYRIGHT & LICENSE
433  
434 Copyright 2005-2020 Peter Krawczyk, All Rights Reserved.
435  
436 This program is free software; you can redistribute it and/or modify it
437 under the same terms as Perl itself.
438  
439 =cut
440  
441 6 1; # End of Algorithm::SixDegrees
↑ Summary