Family.pm

filter: Statementstmt 87.2% 273 / 313 Branchbran 68.3% 67 / 98 Conditioncond 50.0% 25 / 50 MC/DCmcdc 24.1% 7 / 29 Subroutinesub 89.4% 34 / 38 Podpod 83.3% 10 / 12 totaltotal 77.0% 416 / 540 CC 90 SCAR 25.0
CC
90
Cov
79%
_find_best_ordering
52.0
_add_person_and_all_ascendants
21.3
as_dot
19.2
CRAP 12.2
« Family/Person.pm ↑ Summary
1 =head1 NAME
2  
3 Tree::Family - Represent and visualize a family tree.
4  
5 =cut
6  
7 =head1 SYNOPSIS
8  
9 use Tree::Family;
10  
11 my $tree = Tree::Family->new(filename => '/tmp/mytree.dmp');
12  
13 my $person = Tree::Family::Person->new(name => 'Fred');
14 my $nother = Tree::Family::Person->new(name => 'Wilma');
15  
16 $person->spouse($nother);
17  
18 $tree->add_person($person);
19 $tree->add_person($nother);
20  
21 for ($tree->people) {
22 print $_->name;
23 }
24  
25 my $dot_file = $tree->as_dot;
26  
27 =head1 DESCRIPTION
28  
29 Use this module to represent spousal and parental relationships
30 among a group of people, and generate a graphviz "dot"
31 file to visualize them.
32  
33 =head1 FUNCTIONS
34  
35 =cut
36  
37 package Tree::Family;
38 14 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 14 $Data::Dumper::Sortkeys = 1; # makes diffing easier
40 14 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 14 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 14 ▶use List::MoreUtils qw(first_index last_index uniq);
Subroutine BEGIN: called CC=1 SCAR=0.0
43 14 ▶use Algorithm::Permute;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 14 ▶use Clone qw(clone);
Subroutine BEGIN: called CC=1 SCAR=0.0
45 14 ▶use YAML::XS qw/Dump Load LoadFile/;
Subroutine BEGIN: called CC=1 SCAR=0.0
46  
47 14 our $VERSION = '0.02';
48 14 our $urlBase = 'http://localhost/';
49 14 our $GraphHeader = <<'';
50 graph family {
51 edge [ style=solid ];
52 node [ shape=box style=bold, color="black", fontsize="18", fontname="Times-Roman" ];
53 ranksep=2.0
54  
55 14 our $GraphFooter = <<'';
56 }
57  
58 14 our $bottomInvisibleEdges = ''; # populated and used below.
59 14 our $topInvisibleEdges = ''; # populated and used below.
60  
61 5125 ▶sub debug($) {
Subroutine debug: called CC=1 SCAR=0.0
Pod undocumented
62 # print STDERR "@_";
63 }
64  
65 =head2 new
66  
67 my $tree = Tree::Family->new(filename => '/tmp/foobarfamily.dmp');
68  
69 =cut
70  
71 23 ▶sub new {
Subroutine new: called CC=2 SCAR=7.1
Pod documented
72 23 my ($class,%args) = @_;
73 23 ▶ my $filename = $args{filename} or die "missing filename";
Branch: 1 branch
BranchTrueFalseTotal
my $filename = $args{"filename"} or die "missing filename" 23 0 23
74 23 return bless {
75 filename => $filename,
76 }, $class;
77 }
78  
79 788 ▶sub _init {
Subroutine _init: called CC=5 SCAR=16.2
80 788 my $self = shift;
81 788 ▶ return if exists($self->{people});
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"people"} 765 23 788
82 # $self->{people} will be a hash from ids to T:F:Person objects
83 23 ▶ if (-e $self->{filename} && -s $self->{filename}) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $self->{'filename'} and -s $self->{'filename'}) { } 11 12 23
Condition66%2/3
-e $self->{"filename"} and -s $self->{"filename"}
!ll&&!rl&&r
01211
MC/DC50%1/2
-e $self->{"filename"} and -s $self->{"filename"}
-e $self->{"filename"} -s $self->{"filename"}
Truth table66%2/3
A and B
A= -e $self->{"filename"}B= -s $self->{"filename"}
ABresult
0X0
100
111
84 11 my $filename = $self->{filename};
85 11 $self->{people} = LoadFile $filename;
86 } else {
87 12 $self->{people} = {};
88 }
89 23 $self->{people} = { map { $_ => $self->{people}{$_}->Toast } keys %{ $self->{people} } };
90 12 ▶ die "error reading $self->{filename}, got (".ref($self->{people}).") error: [$!] [$@]" unless ref($self->{people}) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self->{"people"} eq "HASH" 0 12 12
91 }
92  
93 #
94 # Assign numeric generations
95 #
96 15 ▶sub _set_generations {
Subroutine _set_generations: called CC=5 SCAR=16.1
97 15 my $self = shift;
98 15 my %args = @_;
99 15 our $haveSet;
100 15 ▶ return if !$args{force} && $haveSet;
Branch: 1 branch
BranchTrueFalseTotal
if not $args{"force"} and $Tree::Family::haveSet 3 12 15
Condition66%2/3
not $args{"force"} and $Tree::Family::haveSet
!ll&&!rl&&r
0123
MC/DC50%1/2
not $args{"force"} and $Tree::Family::haveSet
not $args{"force"} $Tree::Family::haveSet
Truth table66%2/3
not(A) and B
A= not $args{"force"}B= $Tree::Family::haveSet
ABresult
0X0
100
111
101 12 $haveSet = 1;
102 12 Tree::Family::Person->_clear_generations;
103 12 for my $person (values %{ $self->{people} }) {
104 751 ▶ next if $person->generation;
Branch: 1 branch
BranchTrueFalseTotal
if $person->generation 739 12 751
105 12 $person->_set_all_generations(100);
106 }
107 }
108  
109 =head2 write
110  
111 Write the family tree to a file
112  
113 $tree->write
114  
115 =cut
116  
117 12 ▶sub write {
Subroutine write: called CC=3 SCAR=11.0
Pod documented
118 12 my $self = shift;
119 12 $self->_init;
120 12 $self->_set_generations;
121 12 Tree::Family::Person->_clear_all_partners;
122 12 Tree::Family::Person->_set_all_partners;
123 12 my $filename = $self->{filename};
124 12 my $tmpfile = $filename."-tmp-".$$.time.(rand 1);
125 12 my %write = map { $_ => $self->{people}{$_}->Freeze } keys %{ $self->{people} };
126 12 ▶ open FP, ">$tmpfile" or die "Couldn't write to $tmpfile : $!";
Branch: 1 branch
BranchTrueFalseTotal
open FP, ">$tmpfile" or die "Couldn't write to $tmpfile : $!" 12 0 12
127 12 print FP Dump( \%write );
128 12 close FP;
129 12 ▶ rename $tmpfile, $filename or die "Couldn't rename $tmpfile to $filename : $!";
Branch: 1 branch
BranchTrueFalseTotal
rename $tmpfile, $filename or die "Couldn't rename $tmpfile to $filename : $!" 12 0 12
130 12 return 1;
131 }
132  
133 =head2 add_person
134  
135 Add a person to the tree
136  
137 $tree->add_person($joe);
138  
139 $joe should be a Tree::Family::Person object.
140  
141 =cut
142  
143 752 ▶sub add_person {
Subroutine add_person: called CC=1 SCAR=0.0
Pod documented
144 752 my $self = shift;
145 752 $self->_init;
146 752 my $person = shift;
147 752 $self->{people}{$person->get('id')} = $person;
148 }
149  
150 =head2 delete_person
151  
152 Delete a person
153  
154 $tree->delete_person($joe)
155  
156 =cut
157  
158 1 ▶sub delete_person {
Subroutine delete_person: called CC=3 SCAR=11.0
Pod documented
159 1 my $self = shift;
160 1 $self->_init;
161 1 my $person = shift;
162 1 $person->dad(undef);
163 1 $person->mom(undef);
164 1 $person->spouse(undef);
165 1 for ($person->partners) {
166 0 $person->_delete_partner($_);
167 }
168 1 for ($person->kids) {
169 1 $person->delete_kid($_);
170 }
171 1 delete $self->{people}->{$person->id};
172 1 $person->_delete_self;
173 }
174  
175 =head2 people
176  
177 Get a list of all the people in the tree
178  
179 =cut
180  
181 11 ▶sub people {
Subroutine people: called CC=1 SCAR=0.0
Pod documented
182 11 my $self = shift;
183 11 $self->_init;
184 2 return values %{ $self->{people} };
185 }
186  
187 =head2 find
188  
189 Find a person, specifying keys and values to search for.
190  
191 $tree->find(id => 'sam');
192  
193 $tree->find(first_name => 'joe', last_name => 'dimaggio');
194  
195 =cut
196  
197 8 ▶sub find {
Subroutine find: called CC=1 SCAR=0.0
Pod documented
198 8 my ($class,%args) = @_;
199 8 shift->_init;
200 6 Tree::Family::Person->find(%args);
201 }
202  
203 =head2 min_generation
204  
205 The numeric smallest generation.
206  
207 =cut
208  
209 1 ▶sub min_generation {
Subroutine min_generation: called CC=1 SCAR=0.0
Pod documented
210 1 my $self = shift;
211 1 $self->_init;
212 1 $self->_set_generations;
213 1 Tree::Family::Person->min_generation;
214 }
215  
216 =head2 max_generation
217  
218 The numeric highest generation.
219  
220 =cut
221  
222 2 ▶sub max_generation {
Subroutine max_generation: called CC=1 SCAR=0.0
Pod documented
223 2 my $self = shift;
224 2 $self->_init;
225 2 $self->_set_generations;
226 2 Tree::Family::Person->max_generation;
227 }
228  
229 =head2 write_dotfile
230  
231 Write out a .dot file (graphviz format).
232  
233 $tree->write("output.dot");
234  
235 =cut
236  
237 1 ▶sub write_dotfile {
Subroutine write_dotfile: called CC=5 SCAR=16.8
Pod documented
238 1 my ($self,$filename) = @_;
239 1 ▶ die "missing filename" unless $filename;
Branch: 1 branch
BranchTrueFalseTotal
unless $filename 0 1 1
240 1 my $tmpfile = $filename."-tmp-".$$.time.(rand 1);
241 1 ▶ open FP, ">$tmpfile" or die "Couldn't write to $tmpfile : $!";
Branch: 1 branch
BranchTrueFalseTotal
open FP, ">$tmpfile" or die "Couldn't write to $tmpfile : $!" 1 0 1
242 1 print FP $self->as_dot;
243 1 ▶ close FP or die "couldn't close FP : $!";
Branch: 1 branch
BranchTrueFalseTotal
close FP or die "couldn't close FP : $!" 1 0 1
244 1 ▶ rename $tmpfile, $filename or die "Couldn't rename $tmpfile to $filename : $!";
Branch: 1 branch
BranchTrueFalseTotal
rename $tmpfile, $filename or die "Couldn't rename $tmpfile to $filename : $!" 1 0 1
245 1 return 1;
246 }
247  
248 #
249 # _add_person_and_all_ascendants
250 #
251 # Add a person and all their ascendants to the .dot output
252 #
253 1360 ▶sub _add_person_and_all_ascendants {
Subroutine _add_person_and_all_ascendants: called CC=20 SCAR=30.6
254 1360 my ($class,$person,$person2subgraph,$people_written,$subgraph_written,$all_subgraphs,$person2subgraphpeople) = @_;
255 1360 my $output = '';
256 1360 ▶ die "no person id " if defined($person) && !defined($person->id);
Branch: 1 branch
BranchTrueFalseTotal
if defined $person and not defined $person->id 0 1360 1360
Condition33%1/3
defined $person and not defined $person->id
!ll&&!rl&&r
013600
MC/DC0%0/2
defined $person and not defined $person->id
defined $person not defined $person->id
Truth table33%1/3
A and not(B)
A= defined $personB= not defined $person->id
ABresult
0X0
100
111
257 1360 ▶ return $output if $people_written->{$person->id};
Branch: 1 branch
BranchTrueFalseTotal
if $$people_written{$person->id} 678 682 1360
258 682 debug "adding person and all ascendants for ".$person->first_name."\n";
259  
260 # Find the subgraph containing dad (and hence mom), and then call ourself
261 # recursively for every person in that subgraph.
262 682 my $people;
263 682 ▶ $people = $person2subgraphpeople->{ $person->mom->id } if $person->mom;
Branch: 1 branch
BranchTrueFalseTotal
if $person->mom 340 342 682
264 682 ▶ $people ||= $person2subgraphpeople->{ $person->dad->id } if $person->dad;
Branch: 1 branch
BranchTrueFalseTotal
if $person->dad 340 342 682
Condition33%1/3
$people ||= $$person2subgraphpeople{$person->dad->id}
l!l&&r!l&&!r
34000
MC/DC0%0/2
$people ||= $$person2subgraphpeople{$person->dad->id}
$people $$person2subgraphpeople{$person->dad->id}
Truth table33%1/3
A ||= B
A= $peopleB= $$person2subgraphpeople{$person->dad->id}
ABresult
1X1
011
000
265 682 ▶ if ($people && @$people) {
Branch: 1 branch
BranchTrueFalseTotal
if ($people and @$people) { } 340 342 682
Condition66%2/3
$people and @$people
!ll&&!rl&&r
3420340
MC/DC50%1/2
$people and @$people
$people @$people
Truth table66%2/3
A and B
A= $peopleB= @$people
ABresult
0X0
100
111
266 340 debug "Found subgraph for parents of ".$person->first_name." : ".
267 (join ',', map $_->first_name, @$people)."\n"
268 } else {
269 342 debug "No ascendants for ".$person->first_name."\n";
270 }
271 # annoying dot hacks to untangle the generation above us.
272 682 ▶ if ($person->spouse() &&
Branch: 1 branch
BranchTrueFalseTotal
if ($person->spouse and ($person->mom and $person->dad) and ($person->spouse->mom and $person->spouse->dad)) 340 342 682
Condition66%8/12
$person->spouse and ($person->mom and $person->dad) and ($person->spouse->mom and $person->spouse->dad)
!ll&&!rl&&r
3420340
$person->spouse and ($person->mom and $person->dad)
!ll&&!rl&&r
2340340
$person->mom and $person->dad
!ll&&!rl&&r
3400340
$person->spouse->mom and $person->spouse->dad
!ll&&!rl&&r
00340
MC/DC40%2/5
$person->spouse and ($person->mom and $person->dad) and ($person->spouse->mom and $person->spouse->dad)
$person->spouse $person->mom $person->dad $person->spouse->mom $person->spouse->dad
Truth table50%3/6
A and B and C and D and E
A= $person->spouseB= $person->momC= $person->dadD= $person->spouse->momE= $person->spouse->dad
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
273 ($person->mom && $person->dad) &&
274 ($person->spouse->mom && $person->spouse->dad)) {
275 # TODO also for partners (not spouse?)
276 340 my $parent_node = _kid_node($person->mom,$person->dad);
277 340 my $edges;
278 340 ▶ if (had_kids($person,$person->spouse)) {
Branch: 1 branch
BranchTrueFalseTotal
if (had_kids($person, $person->spouse)) { } 308 32 340
279 308 $edges = _kid_node($person,$person->spouse())." -- $parent_node [style=invis];\n";
280 } else {
281 32 $edges = $person->id." -- $parent_node [style=invis];\n";
282 32 $edges .= $person->spouse()->id." -- $parent_node [style=invis];\n";
283 }
284  
285 340 ▶ if ($person->mom->spouse() || $person->dad->spouse()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($person->mom->spouse or $person->dad->spouse) { } 338 2 340
Condition66%2/3
$person->mom->spouse or $person->dad->spouse
l!l&&r!l&&!r
33802
MC/DC50%1/2
$person->mom->spouse or $person->dad->spouse
$person->mom->spouse $person->dad->spouse
Truth table66%2/3
A or B
A= $person->mom->spouseB= $person->dad->spouse
ABresult
1X1
011
000
286 338 $bottomInvisibleEdges .= $edges;
287 } else {
288 2 $topInvisibleEdges .= $edges;
289 }
290 }
291
292 682 ▶ for (@{ $people || [] }) {
Condition100%2/2
$people || []
l!l
340342
MC/DC100%1/1
$people || []
$people
Truth table100%2/2
A
A= $people
Aresult
11
00
293 678 $output .= $class->_add_person_and_all_ascendants($_,$person2subgraph,$people_written,$subgraph_written,$all_subgraphs,$person2subgraphpeople);
294 }
295 682 $output .= $class->_person_node($person)."\n";
296 682 my $subgraph_index = $person2subgraph->{$person->id};
297 682 ▶ $output .= $all_subgraphs->[$subgraph_index] unless $subgraph_written->{$subgraph_index};
Branch: 1 branch
BranchTrueFalseTotal
unless $subgraph_written->{$subgraph_index} 342 340 682
298 682 $subgraph_written->{$subgraph_index} = 1;
299 682 $people_written->{$person->id} = 1;
300 682 return $output;
301 }
302  
303 =head2 as_dot
304  
305 Return the text for a .dot graphviz file
306  
307 print $tree->as_dot
308  
309 =cut
310  
311 1 ▶sub as_dot {
Subroutine as_dot: called CC=19 SCAR=29.5
Pod documented
312 1 my $class = shift;
313 1 debug "as dot called\n";
314 1 $class->_init;
315 my @people = sort {
316 1 ▶ warn "generation for $a or $b not set" unless defined($a->get('generation')) && defined($b->get('generation'));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $a->get("generation") and defined $b->get("generation") 0 4182 4182
Condition33%1/3
defined $a->get("generation") and defined $b->get("generation")
!ll&&!rl&&r
004182
MC/DC0%0/2
defined $a->get("generation") and defined $b->get("generation")
defined $a->get("generation") defined $b->get("generation")
Truth table33%1/3
A and B
A= defined $a->get("generation")B= defined $b->get("generation")
ABresult
0X0
100
111
317 4182 $a->get('generation') <=> $b->get('generation') } $class->people;
318 1 my $output;
319  
320 # Make subgraphs for people with partners/spouses
321 my %person2subgraph; # map from person id to the dot text
322 1 my @all_subgraphs;
323 1 my %generation_subgraphs; # keys are generations, values are arrays of arrays of people who are in a subgraph.
324 1 my %person2subgraphpeople; # map from person id to an array of people in the subgraph
325 1 for my $person (@people) {
326 682 ▶ next if $person2subgraph{$person->get('id')};
Branch: 1 branch
BranchTrueFalseTotal
if $person2subgraph{$person->get("id")} 339 343 682
327 343 my @together = $class->_partner_and_marriage_group($person);
328 343 debug "doing subgraph for : ".(join ',', map $_->first_name, @together)."\n";
329 343 ▶ next unless @together > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless @together > 0 0 343 343
330 343 $person2subgraph{$_->get('id')} = scalar(@all_subgraphs) for @together;
331 343 push @all_subgraphs, $class->_partner_subgraph(\@together);
332 343 debug "best ordering : ".(join ',', map $_->first_name, @together)."\n";
333 343 $person2subgraphpeople{$_->get('id')} = \@together for @together;
334 }
335
336 # People
337 1 my %people_written; # keeps track of people who have been written
338 my %subgraph_written; # ids of subgraphs that have been written
339 1 my %people_by_generation;
340 1 for (@people) {
341 682 push @{ $people_by_generation{$_->get('generation')} }, $_;
342 }
343 # starting with the bottom-most generation, do depth-first traversals to add
344 # all ascendants and their partner subgraphs.
345 # This also builds $bottomInvisibleEdges. If this isn't on the
346 # bottom of the graph, dot segfaults.
347 # maybe on the top? TODO
348 # if it isn't on the top, they're in the wrong place
349 1 $bottomInvisibleEdges = '';
350 1 for (sort {$b <=> $a } keys %people_by_generation) {
351 5 my $this_generation = $people_by_generation{$_};
352 5 ▶ next unless $this_generation;
Branch: 1 branch
BranchTrueFalseTotal
unless $this_generation 0 5 5
353 5 debug "adding generation $_\n";
354 5 for my $person (@$this_generation) {
355 682 debug "starting generation with person ".$person->first_name."\n";
356 682 $output .= $class->_add_person_and_all_ascendants($person,\%person2subgraph,\%people_written,\%subgraph_written,\@all_subgraphs,\%person2subgraphpeople);
357 }
358 }
359 1 ▶ die "unwritten subgraphs, should not happen" if grep {!$_} values %subgraph_written;
Branch: 1 branch
BranchTrueFalseTotal
if grep {not $_;} values %subgraph_written 0 1 1
360  
361 # Parent edges
362 1 for my $person (@people) {
363 682 my $parent_key = join '_', map $_->get('id'), grep defined, ($person->dad,$person->mom);
364 682 ▶ next unless $parent_key;
Branch: 1 branch
BranchTrueFalseTotal
unless $parent_key 342 340 682
365 340 $output .= "$parent_key -- ".$person->get('id')." // Parents of ".$person->get('id')."\n";
366 }
367
368 # Generations
369 1 my $min_generation = $class->min_generation;
370 1 my $max_generation = $class->max_generation;
371 1 $output .= "/* generations : ".$min_generation." to ".$max_generation." */\n";
372 1 my @generation_nodes;
373 1 my $i = 0;
374 1 for my $g ($min_generation .. $max_generation) {
375 5 my $generation_node = "generation_".(++$i);
376 5 push @generation_nodes, $generation_node;
377 5 my @this = $class->find(generation => $g);
378 5 ▶ my $which = $g==$min_generation ? 'source' : $g==$max_generation ? 'sink' : 'same';
Branch: 2 branches
BranchTrueFalseTotal
$g == $min_generation ? : 1 4 5
$g == $max_generation ? : 1 3 4
379 5 $output .= "{rank=$which; $generation_node ".
380 (join ' ',map $_->get('id'), @this)."}\n";
381 }
382 # Now add an invisible edge between the first member of each generation.
383 1 my $generation_edges;
384 1 $generation_edges .= join "--", @generation_nodes;
385 1 $generation_edges .= "[style=invis];\n";
386 1 for (@generation_nodes) {
387 5 $generation_edges .= qq{$_ [label="" style=invis];\n};
388 }
389  
390 1 return join "\n",$GraphHeader,$topInvisibleEdges,$generation_edges,$output,$bottomInvisibleEdges,$GraphFooter;
391 }
392  
393 # All people who are connected to a given person via marriage or partnership
394 # ...and all people connected to those people, etc.
395 343 ▶sub _partner_and_marriage_group {
Subroutine _partner_and_marriage_group: called CC=5 SCAR=16.1
396 343 my ($class, $person ) = @_;
397 343 my @all = ($person);
398 343 my @add_me = $person->partners_and_spouse;
399 343 debug "partners and spouse for ".$person->id." : ".@add_me."\n";
400 #debug (join ',',map $_->id, @add_me)."\n";
401 343 ▶ while (@add_me) {
Branch: 1 branch
BranchTrueFalseTotal
if (@add_me) 341 343 684
402 341 push @all, @add_me;
403 341 my @just_added = @add_me;
404 341 @add_me = ();
405 341 for (@just_added) {
406 341 for my $p ($_->partners_and_spouse) {
407 341 ▶ next if grep { $p eq $_ } @all;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$p eq $_;} @all 341 0 341
408 0 push @add_me, $p;
409 }
410 }
411 }
412 343 my %uniq = map { ( $_->get('id') => $_ ) } @all;
413 343 return values %uniq;
414 }
415  
416 #
417 # _remove_duplicates
418 #
419 # Given a list of pairs of people, return a list of
420 # unique unordered pairs. e.g. given
421 # ( [a,b], [b,a], [c,d] )
422 # return ( [a,b], [c,d] )
423 # where a,b,c,d are person objects.
424 #
425 341 ▶sub _remove_duplicates {
Subroutine _remove_duplicates: called CC=4 SCAR=13.9
426 341 my @edges = @_;
427 341 my @ret;
428 my %h;
429 341 for my $e (@edges) {
430 682 ▶ next if $h{$e->[0]->id,$e->[1]->id}++;
Branch: 1 branch
BranchTrueFalseTotal
if $h{join $;, $e->[0]->id, $e->[1]->id}++ 341 341 682
431 341 ▶ next if $h{$e->[1]->id,$e->[0]->id}++;
Branch: 1 branch
BranchTrueFalseTotal
if $h{join $;, $e->[1]->id, $e->[0]->id}++ 0 341 341
432 341 push @ret, $e;
433 }
434 341 return @ret;
435 }
436  
437 #
438 # _distance
439 #
440 # a metric on a list of ordered pairs :
441 #
442 # The sum of the difference between the first and last positions of each
443 # unique element, e.g.
444 #
445 # ( [a,b], [b,c], [c,d] ) == a -- b b -- c c -- d
446 # 0 1 2 3 4 5
447 # 0-0 (a) + 2-1 (b) + 4-3 (c) + 5-5 (d) == 2
448 #
449 # ( [a,b], [c,d], [b,c] ) == a -- b c -- d b -- c
450 # 0 1 2 3 4 5
451 # 0-0 (a) + (4-2) b + (5-2) c + 3-3 (d) = 5
452 #
453 # a,b,c,d are Tree::Family::Person objects
454 #
455 0 ▶sub _distance {
Subroutine _distance: not called CC=3 SCAR=24.8
456 0 my @edges = @_;
457 0 my @flattenned = map @$_, @edges;
458 0 my %seen;
459 0 my $distance = 0;
460 0 for my $m (@flattenned) {
461 0 ▶ next if $seen{$m->id}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$m->id}++ 0 0 0
462 0 ▶ my $first = first_index { $_->id eq $m->id } @flattenned;
Subroutine __ANON__: not called CC=1 SCAR=6.9
463 0 ▶ my $last = last_index { $_->id eq $m->id } @flattenned;
Subroutine __ANON__: not called CC=1 SCAR=6.9
464 0 $distance += ($last - $first);
465 }
466 0 return $distance;
467 }
468  
469 #
470 #_are_married
471 #
472 #_are_married($joe,$sue)
473 #
474 #returns true iff $joe and $sue are married
475 #
476 682 ▶sub _are_married {
Subroutine _are_married: called CC=3 SCAR=14.2
477 682 my ($a,$b) = @_;
478 682 ▶ return ($a->spouse() && $b->spouse() && $a->spouse->id eq $b->id);
Condition33%2/6
$a->spouse && $b->spouse && $a->spouse->id eq $b->id
!ll&&!rl&&r
00682
$a->spouse and $b->spouse
!ll&&!rl&&r
00682
MC/DC0%0/3
$a->spouse && $b->spouse && $a->spouse->id eq $b->id
$a->spouse $b->spouse $a->spouse->id eq $b->id
Truth table25%1/4
A and B && C
A= $a->spouseB= $b->spouseC= $a->spouse->id eq $b->id
ABCresult
0XX0
10X0
1100
1111
479 }
480  
481 #
482 # return --- or -+- for two people depending on whether they
483 # are married or not.
484 #
485 341 ▶sub _ascii_pair {
Subroutine _ascii_pair: called CC=2 SCAR=7.6
486 341 my ($a,$b) = @_;
487 341 ▶ if (_are_married($a,$b)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_are_married($a, $b)) 341 0 341
488 341 return join '-+-', $a->id, $b->id;
489 }
490 0 return join '---', $a->id, $b->id;
491 }
492  
493 #
494 # parameters : an array ref of pairs of people
495 # returns : nothing, but puts 'em in a decent order, to minimize the
496 # distance between elements of the pairs.
497 #
498 # e.g. given ( [d,c], [a,b], [b,c] )
499 # the best ordering would be one of
500 # ( [a,b], [b,c], [c,d] )
501 # ( [d,c], [c,b], [b,a] )
502 # since then they could appear like so:
503 # a -- b -- c -- d
504 #
505 341 ▶sub _find_best_ordering {
Subroutine _find_best_ordering: called CC=8 SCAR=39.5
506 341 my @pairs = @_;
507 341 debug "-- finding best ordering of ".@pairs." marriages/partnerships\n";
508 341 ▶ return @pairs unless @pairs > 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @pairs > 1 341 0 341
509 0 my $min_distance;
510 0 my @best = @pairs;
511 0 my $i = Algorithm::Permute->new(\@pairs);
512 0 my @m = $i->next;
513 0 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
not @m or do { do { &debug('-- starting with permutation : ' . join(" ", map(_ascii_pair(@$_), @m)) . "\n"); foreach my $b (0 .. 2 ** @m - 1) { &debug("-- b is $b\n"); my $m = &clone(\@m); my $k = 0; foreach $_ (@$m) { $_ = [$_->[1], $_->[0]] if $b & 1 << $k++; } ; my $d = _distance(@$m); &debug(' -- distance for ' . join(" ", map(_ascii_pair(@$_), @$m)) . " : $d\n"); if (not defined $min_distance or $d < $min_distance) { $min_distance = $d; @best = @$m; } ; } ; @m = $i->next } } 0 0 0
514 0 debug "-- starting with permutation : ".(join ' ', map _ascii_pair(@$_), @m)."\n";
515 # flip the order of each possible edge
516 0 for my $b (0..(2**(@m)-1)) {
517 0 debug "-- b is $b\n";
518 0 my $m = clone \@m;
519 0 my $k = 0;
520 0 for (@$m) {
521 0 ▶ $_ = [$_->[1],$_->[0]] if $b & (1 << $k++);
Branch: 1 branch
BranchTrueFalseTotal
if $b & 1 << $k++ 0 0 0
522 }
523 0 my $d = _distance(@$m);
524 0 debug " -- distance for ".(join ' ', map _ascii_pair(@$_), @$m)." : $d\n";
525 0 ▶ if (!defined($min_distance) || $d < $min_distance) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $min_distance or $d < $min_distance) 0 0 0
Condition0%0/3
not defined $min_distance or $d < $min_distance
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $min_distance or $d < $min_distance
not defined $min_distance $d < $min_distance
Truth table0%0/3
not(A) or B
A= not defined $min_distanceB= $d < $min_distance
ABresult
1X1
011
000
526 0 $min_distance = $d;
527 0 @best = @$m;
528 }
529 }
530 0 @m = $i->next;
531 } until (!@m);
532 0 debug "-- best distance : $min_distance\n";
533 0 return @best;
534 }
535  
536 #
537 # make a subgraph of people who are partners (i.e. married or had kids together)
538 # also rearranges @people
539 #
540 343 ▶sub _partner_subgraph {
Subroutine _partner_subgraph: called CC=9 SCAR=22.6
541 343 my ($class,$people) = @_;
542 343 my @people = @$people;
543 343 ▶ return '' if @$people==1;
Branch: 1 branch
BranchTrueFalseTotal
if @$people == 1 2 341 343
544 341 my @marriages;
545 my @parentships;
546 341 for my $p (@people) {
547 682 ▶ push @marriages, [ $p, $p->spouse() ] if $p->spouse;
Branch: 1 branch
BranchTrueFalseTotal
if $p->spouse 682 0 682
548 682 push @parentships, [ $p, $_ ] for $p->partners;
549 }
550 341 my @cluster = (@marriages, @parentships);
551 341 @cluster = _remove_duplicates(@cluster);
552 341 @cluster = _find_best_ordering(@cluster);
553  
554 341 my $best = join ' ', map _ascii_pair(@$_), @cluster;
555 341 debug "** best ordering : $best\n";
556  
557 341 my $graph_name = join '_and_', map $_->get('id'), @people;
558 341 my $output = "subgraph cluster_$graph_name {\n /* $best */\ncolor=white;\n";
559  
560 341 for my $e (@cluster) {
561 341 ▶ if (_are_married(@$e)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_are_married(@$e)) { } 341 0 341
562 341 $output .= $class->_marriage_subgraph(@$e);
563 } else {
564 0 $output .= $class->_parent_edge(@$e);
565 }
566 }
567  
568 341 ▶ return "" unless $output && $output =~ /\w/;
Branch: 1 branch
BranchTrueFalseTotal
unless $output and $output =~ /\w/ 0 341 341
Condition33%1/3
$output and $output =~ /\w/
!ll&&!rl&&r
00341
MC/DC0%0/2
$output and $output =~ /\w/
$output $output =~ /\w/
Truth table33%1/3
A and B
A= $outputB= $output =~ /\w/
ABresult
0X0
100
111
569 341 return $output." } \n";
570 }
571  
572 #
573 # intersect two array refs
574 #
575 681 ▶sub _intersection {
Subroutine _intersection: called CC=4 SCAR=14.1
576 # probably a little slow compared to perldoc -q intersect, but can we use objects as hash keys?
577 681 my ($a,$b) = @_;
578 681 my @i;
579 681 for my $x (@$b) {
580 675 ▶ die "undefs in intersection" unless defined $x;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $x 0 675 675
581 675 ▶ push @i, $x if grep { $_ eq $x } @$a;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ eq $x;} @$a 675 0 675
582 }
583 681 return @i;
584 }
585  
586 #
587 # node from which a kid comes; a --+-- b
588 # |
589 # kid
590 # the "+" is the kid node.
591 #
592 973 ▶sub _kid_node {
Subroutine _kid_node: called CC=4 SCAR=14.6
593 973 my ($a,$b) = @_;
594 973 ▶ die "no kid node for single parents" unless ($a && $b);
Branch: 1 branch
BranchTrueFalseTotal
unless $a and $b 0 973 973
Condition33%1/3
$a and $b
!ll&&!rl&&r
00973
MC/DC0%0/2
$a and $b
$a $b
Truth table33%1/3
A and B
A= $aB= $b
ABresult
0X0
100
111
595 973 ▶ ($a,$b) = ($b,$a) if $b->get('gender') eq 'm';
Branch: 1 branch
BranchTrueFalseTotal
if $b->get("gender") eq "m" 664 309 973
596 973 return join '_',$a->get('id'),$b->get('id');
597 }
598  
599 #
600 681 ▶sub had_kids {
Subroutine had_kids: called CC=1 SCAR=0.0
Pod undocumented
601 681 my ($a,$b) = @_;
602 681 my $x = [map $_->id, $a->kids ];
603 681 my $y = [map $_->id, $b->kids ];
604 681 debug "intersecting ".Dumper($x,$y);
605 681 my @i = _intersection($x,$y);
606 681 debug "number of kids shared by ".$a->id." and ".$b->id." is ".@i."\n";
607 681 return (@i > 0);
608 }
609  
610 341 ▶sub _marriage_subgraph {
Subroutine _marriage_subgraph: called CC=2 SCAR=6.9
611 341 my ($class,$x,$y) = @_;
612 341 my ($one,$two) = map $_->get('id'), ($x,$y);
613 341 my $graph;
614 my %k;
615 341 ▶ if (had_kids($x,$y)) {
Branch: 1 branch
BranchTrueFalseTotal
if (had_kids($x, $y)) { } 325 16 341
616 325 my $kid_node = _kid_node($x,$y);
617 325 $graph = "$one -- $kid_node -- $two; rank=same;$one $two $kid_node;";
618 325 $graph .= qq+\n$kid_node [label="",width=.01,height=.01]+;
619 } else {
620 16 $graph = "$one -- $two; rank=same;$one $two;";
621 }
622 341 return "subgraph marriage_${one}_${two} {\nedge [style=bold]; $graph }\n",
623 }
624  
625 0 ▶sub _parent_edge {
Subroutine _parent_edge: not called CC=1 SCAR=6.9
626 # Draw an edge between two people who had a kid together
627 0 my ($class,$x,$y) = @_;
628 0 my ($one,$two) = map $_->get('id'), ($x,$y);
629 0 my $kid_node = _kid_node($x,$y);
630 0 return join "\n",
631 "edge [style=dotted]; $one -- $kid_node -- $two { rank=same;$one $two $kid_node }",
632 "$kid_node [ shape=point ]";
633 }
634  
635 682 ▶sub _person_node {
Subroutine _person_node: called CC=2 SCAR=6.9
636 682 my ($class, $person) = @_;
637 682 our $urlBase;
638 682 ▶ return $person->id . " ["
Branch: 1 branch
BranchTrueFalseTotal
$person->get('gender') eq 'm' ? : 341 341 682
639 . ($person->get('gender') eq 'm' ? 'color="#093AB5"' : 'color="#C666B8"')
640 . ' label = "'
641 . $class->_person_label($person)
642 . qq|" href="$urlBase?id=|
643 . $person->id . '"];';
644 }
645  
646 682 ▶sub _person_label {
Subroutine _person_label: called CC=1 SCAR=0.0
647 682 my ($class,$p) = @_;
648 682 return join ' ', grep defined($_), $p->get('first_name'), $p->get('last_name');
649 }
650  
651 23 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
652 23 %Tree::Family::Person::globalHash = ();
653 }
654  
655 =head1 SEE ALSO
656  
657 Tree::Family::Person
658 family.cgi (in this distribution)
659  
660 =head1 AUTHOR
661  
662 Brian Duggan, C<< <bduggan at matatu.org> >>
663  
664 =head1 BUGS
665  
666 graphviz uses a lot of heuristics to create a nice layout. This package
667 attempts to micro-manage the contents of the dot file in order to produce
668 a nice layout, while still letting graphviz do the brunt of the work.
669 This approach doesn't always produce optimal results. Patches welcome.
670  
671 =head1 COPYRIGHT & LICENSE
672  
673 Copyright 2006 Brian Duggan, all rights reserved.
674  
675 This program is free software; you can redistribute it and/or modify it
676 under the same terms as Perl itself.
677  
678 =cut
679  
680 14 1;
« Family/Person.pm ↑ Summary