| 1 |
/* |
| 2 |
* @progname cousins.ll |
| 3 |
* @version 6.0 |
| 4 |
* @author Wetmore |
| 5 |
* @category |
| 6 |
* @output Text |
| 7 |
* @description |
| 8 |
|
| 9 |
Finds the relationship between two persons in a |
| 10 |
LifeLines database. If there is no common ancestor, the program |
| 11 |
will attempt to find a sequence of genetic relations that link the two |
| 12 |
persons. |
| 13 |
|
| 14 |
In the cases where the two persons are |
| 15 |
genetically related (have a common ancestor), the program will |
| 16 |
find and display the relationship. If the two persons are not |
| 17 |
genetically related, the program will attempt to discover a |
| 18 |
sequence of genetic relationships that link the two persons. |
| 19 |
For example, the program will display the relationship between |
| 20 |
your two grandmothers as a sequence of two genetic relations with |
| 21 |
a grandchild as the link between. Note that two persons may be |
| 22 |
related to each other in many ways; this program finds only the |
| 23 |
shortest one (or ones if there are different, equally short paths). |
| 24 |
|
| 25 |
This program requires version 3 of LifeLines. |
| 26 |
|
| 27 |
author -- Tom Wetmore, [email protected] |
| 28 |
|
| 29 |
version history |
| 30 |
1 - 08 Sep 1993 -- modified from the relate program |
| 31 |
2 - 08 Sep 1993 -- fixed niece/nephew bug |
| 32 |
3 - 09 Sep 1993 -- extensive modification |
| 33 |
4 - 13 Aug 1994 -- more modifications |
| 34 |
5 - 2 Mar 1995 -- check for direct descendants first. Chris Bone |
| 35 |
6 - 2 Mar 1995 -- Neater cousin removes, find all short paths. J.F. Chandler |
| 36 |
*/ |
| 37 |
|
| 38 |
global(links) /* table of links back one person */ |
| 39 |
global(rels) /* table showing direction of the links */ |
| 40 |
global(klist) /* list of found persons not linked back to yet */ |
| 41 |
global(numb) /* number of persons considered so far */ |
| 42 |
|
| 43 |
/*====================================================================== |
| 44 |
* notes on global data structures -- |
| 45 |
* o links -- implements the function link(key1) --> key2, where key1 |
| 46 |
* is the key of a person, and key2 is the key of the person that |
| 47 |
* person key1 links back to |
| 48 |
* o rels -- implements the function rels(key1) --> dir, where key1 is |
| 49 |
* the key of a person, and dir is the relationship direction (up or |
| 50 |
* down) between this person and person link(key1) |
| 51 |
*=====================================================================*/ |
| 52 |
|
| 53 |
/*===================================================================== |
| 54 |
* main -- Get the user to identify two persons; if all goes well, call |
| 55 |
* relate to do the hard stuff. |
| 56 |
*===================================================================*/ |
| 57 |
proc main () |
| 58 |
{ |
| 59 |
print("This program finds the relationship between two persons.\n\n") |
| 60 |
getindimsg(from, "Please identify the first person.") |
| 61 |
set(to, 0) |
| 62 |
if (from) { |
| 63 |
getindimsg(to, "Please identify the second person.") |
| 64 |
} |
| 65 |
if (and(from, to)) { |
| 66 |
print("Searching for relationships between:\n\t") |
| 67 |
print(name(from), " and ", name(to)) |
| 68 |
print(".\n\nThis may take a while -- ") |
| 69 |
print("each dot is 25 persons considered.\n") |
| 70 |
set(fkey, save(key(from))) |
| 71 |
set(tkey, save(key(to))) |
| 72 |
call relate(tkey, fkey) |
| 73 |
} else { |
| 74 |
print("Please call again.\n") |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
/*====================================================================== |
| 79 |
* relate -- Attempt to find a relationship between two persons by |
| 80 |
* constructing a path of parent and/or child links between them; if a |
| 81 |
* path is found, call foundpath to display the results; else report |
| 82 |
* that there is no relation between the persons. |
| 83 |
*====================================================================*/ |
| 84 |
proc relate (fkey, tkey) |
| 85 |
{ |
| 86 |
table(links) /* table of links back one person */ |
| 87 |
table(rels) |
| 88 |
list(klist) /* keys of persons not linked back to yet */ |
| 89 |
|
| 90 |
set(up, 1) |
| 91 |
set(down, neg(1)) |
| 92 |
set(numb, 0) |
| 93 |
set(pathlength, 0) |
| 94 |
set(toolong, 0) |
| 95 |
set(found, 0) |
| 96 |
|
| 97 |
/* Link the first person to him/herself with no direction, and make |
| 98 |
him/her the first entry in the list of unlinked back to persons. |
| 99 |
A "zero" person in the list marks the start of next-longer paths. */ |
| 100 |
|
| 101 |
insert(links, fkey, fkey) |
| 102 |
insert(rels, fkey, 0) |
| 103 |
enqueue(klist, fkey) |
| 104 |
enqueue(klist, 0) |
| 105 |
|
| 106 |
/* Iterate through the list of unlinked back to persons; remove them one by |
| 107 |
one; link their parents and children back to them; add their parents and |
| 108 |
children to the unlinked back to list; check each iteration to see if |
| 109 |
one of the new parents or children is the searched for person; if so |
| 110 |
quit the iteration and call foundpath; else continue iterating. */ |
| 111 |
|
| 112 |
while ( gt(length(klist),1) ) { |
| 113 |
set(key, dequeue(klist)) |
| 114 |
if(not(key)) { |
| 115 |
set(pathlength, add(1,pathlength)) |
| 116 |
if(eq(pathlength,toolong)) { break() } |
| 117 |
enqueue(klist, 0) |
| 118 |
continue() |
| 119 |
} |
| 120 |
set(indi, indi(key)) |
| 121 |
set(dir, lookup(rels, key)) |
| 122 |
call include(key, father(indi), down) |
| 123 |
call include(key, mother(indi), down) |
| 124 |
families(indi, fam, spouse, num1) { |
| 125 |
children(fam, child, num2) { |
| 126 |
call include(key, child, up) |
| 127 |
} |
| 128 |
} |
| 129 |
if (key, lookup(links, tkey)) { |
| 130 |
if(found) { |
| 131 |
"\n\nAlternate relationship" |
| 132 |
} else { |
| 133 |
"Relationship from " name(indi(tkey)) |
| 134 |
" to " name(indi(fkey)) |
| 135 |
} |
| 136 |
":\n" |
| 137 |
set(found, 1) |
| 138 |
call foundpath(tkey) |
| 139 |
call fullpath(tkey) |
| 140 |
set(toolong, add(1,pathlength)) |
| 141 |
insert(links, tkey, 0) |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
/* Check to see if there is no relation between the persons, and if there |
| 146 |
is none let the user know and quit. */ |
| 147 |
|
| 148 |
if (not(found)) { |
| 149 |
print("\nThey are not blood-related to one another.") |
| 150 |
"They are not blood-related to one another." |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
/*========================================================================= |
| 155 |
* include -- Links a newly discovered person (indi) back to another person |
| 156 |
* (key), with a specified direction (rel); the new person is then put on |
| 157 |
* the list of unlinked back to persons. |
| 158 |
*=======================================================================*/ |
| 159 |
proc include (key, indi, rel) |
| 160 |
{ |
| 161 |
/* Only include the person if he/she has not been found before. */ |
| 162 |
|
| 163 |
if (and(indi, not(lookup(links, key(indi))))) { |
| 164 |
|
| 165 |
/* Keep user happy watching those dots! */ |
| 166 |
|
| 167 |
set(numb, add(numb, 1)) |
| 168 |
if (eq(0, mod(numb, 25))) { |
| 169 |
print(".") |
| 170 |
} |
| 171 |
|
| 172 |
/* Update the data structures. */ |
| 173 |
|
| 174 |
set(new, save(key(indi))) |
| 175 |
insert(links, new, key) |
| 176 |
insert(rels, new, rel) |
| 177 |
enqueue(klist, new) |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
/*================================================================= |
| 182 |
* foundpath -- Show the relationship path between the two persons. |
| 183 |
*===============================================================*/ |
| 184 |
proc foundpath (key) |
| 185 |
{ |
| 186 |
print("\n") "\n" |
| 187 |
list(nexkeys) |
| 188 |
list(nexlens) |
| 189 |
|
| 190 |
/* Init the nexus person from the first (to) person. */ |
| 191 |
|
| 192 |
set(nexus, key) |
| 193 |
set(dir, lookup(rels, key)) |
| 194 |
set(len, dir) |
| 195 |
set(again, 1) |
| 196 |
|
| 197 |
/* Create the nexus list, the list of persons where relationships change |
| 198 |
direction; a nexus person is either the first person, the last person, |
| 199 |
or the common ancestor or descendent of two other nexus persons. */ |
| 200 |
|
| 201 |
while (again) { |
| 202 |
|
| 203 |
/* Get the next person from the path. */ |
| 204 |
|
| 205 |
set(key, lookup(links, key)) |
| 206 |
set(rel, lookup(rels, key)) |
| 207 |
|
| 208 |
/* If the new person's direction is 0 this is the last person in the path |
| 209 |
(the from person) so add the current nexus person and the last person |
| 210 |
to the nexus list and quit the loop */ |
| 211 |
|
| 212 |
if (eq(0, rel)) { |
| 213 |
enqueue(nexkeys, nexus) |
| 214 |
enqueue(nexlens, len) |
| 215 |
enqueue(nexkeys, key) |
| 216 |
enqueue(nexlens, 0) |
| 217 |
set(again, 0) |
| 218 |
|
| 219 |
/* if new person changes direction, add the current nexus person to the |
| 220 |
nexus list, and make the new person the new current nexus person */ |
| 221 |
|
| 222 |
} elsif (ne(rel, dir)) { |
| 223 |
enqueue(nexkeys, nexus) |
| 224 |
enqueue(nexlens, len) |
| 225 |
set(nexus, key) |
| 226 |
set(dir, rel) |
| 227 |
set(len, rel) |
| 228 |
|
| 229 |
/* if the new person continues in the same direction, record the step */ |
| 230 |
|
| 231 |
} else { |
| 232 |
set(len, add(len, rel)) |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
set(one, dequeue(nexkeys)) |
| 237 |
set(len, dequeue(nexlens)) |
| 238 |
set(again, 1) |
| 239 |
|
| 240 |
/* step down the nexus list, computing and displaying the relationships |
| 241 |
between either two nexus persons (where it is appropriate to show pure |
| 242 |
ancestry or descendency) or three nexus persons (where it is appropriate |
| 243 |
to show two nexus persons as cousins with their common ancestor) */ |
| 244 |
|
| 245 |
while (and(again, length(nexkeys))) { |
| 246 |
set(llen, length(nexkeys)) |
| 247 |
|
| 248 |
/* If the initial direction is down, show first nexus person as a simple |
| 249 |
ancestor of the second nexus person; this condition can only be true |
| 250 |
in the first iteration */ |
| 251 |
|
| 252 |
if (lt(len, 0)) { |
| 253 |
set(two, dequeue(nexkeys)) |
| 254 |
set(new, dequeue(nexlens)) |
| 255 |
call showancs(one, two, neg(len)) |
| 256 |
set(one, two) |
| 257 |
set(len, new) |
| 258 |
|
| 259 |
/* If the direction is up, there are two subcases: */ |
| 260 |
|
| 261 |
} elsif (gt(len, 0)) { |
| 262 |
|
| 263 |
/* If the nexus list has only one remaining entry then show the (last-1)th |
| 264 |
nexus person as a simple descendent of the last nexus person */ |
| 265 |
|
| 266 |
if (eq(1, llen)) { |
| 267 |
set(two, dequeue(nexkeys)) |
| 268 |
set(new, dequeue(nexlens)) |
| 269 |
call showdesc(one, two, len) |
| 270 |
set(again, 0) |
| 271 |
|
| 272 |
/* If the nexus list has more than one remaining entry then show the |
| 273 |
current nexus person and the next two as two "cousins" with an |
| 274 |
intervening common ancestor, and make the last of the three persons the |
| 275 |
current nexus person for the next loop iteration */ |
| 276 |
|
| 277 |
} else { |
| 278 |
set(two, dequeue(nexkeys)) |
| 279 |
set(tmp, dequeue(nexlens)) |
| 280 |
set(three, dequeue(nexkeys)) |
| 281 |
set(new, dequeue(nexlens)) |
| 282 |
call showcous(one, two, three, len, neg(tmp)) |
| 283 |
set(one, three) |
| 284 |
set(len, new) |
| 285 |
} |
| 286 |
|
| 287 |
/* This is the special case where a person is related to him/herself. */ |
| 288 |
|
| 289 |
} else { |
| 290 |
print("They're the same person.\n") |
| 291 |
"They're the same person.\n" |
| 292 |
set(again, 0) |
| 293 |
} |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
/*================================================= |
| 298 |
* showancs -- Show a direct ancestry relationship. |
| 299 |
*==============================================*/ |
| 300 |
proc showancs (one, two, len) |
| 301 |
{ |
| 302 |
set(indi, indi(one)) |
| 303 |
if (male(indi)) { set(pword, "father ") } |
| 304 |
elsif (female(indi)) { set(pword, "mother ") } |
| 305 |
else { set(pword, "parent ") } |
| 306 |
if (eq(1, len)) { set(aword, "the ") } |
| 307 |
else { set(aword, "a ") } |
| 308 |
|
| 309 |
print(name(indi), " is ", aword) |
| 310 |
name(indi) " is " aword |
| 311 |
if (eq(2, len)) { print("grand") "grand" } |
| 312 |
elsif (eq(3, len)) { print("great grand") "great grand" } |
| 313 |
elsif (lt(3, len)) { |
| 314 |
print("great(", d(sub(len, 2)), ") grand") |
| 315 |
"great(" d(sub(len, 2)) ") grand" |
| 316 |
} |
| 317 |
print(pword, "of\n ", name(indi(two)), ".\n") |
| 318 |
pword "of\n " name(indi(two)) ".\n" |
| 319 |
} |
| 320 |
|
| 321 |
/*==================================================== |
| 322 |
* showdesc -- Show a direct descendency relationship. |
| 323 |
*==================================================*/ |
| 324 |
proc showdesc (one, two, len) |
| 325 |
{ |
| 326 |
set(indi, indi(one)) |
| 327 |
if (male(indi)) { set(pword, "son ") } |
| 328 |
elsif (female(indi)) { set(pword, "daughter ") } |
| 329 |
else { set(pword, "child ") } |
| 330 |
|
| 331 |
print(name(indi), " is a ") |
| 332 |
name(indi) " is a " |
| 333 |
if (eq(2, len)) { print("grand") "grand" } |
| 334 |
elsif (eq(3, len)) { print("great grand") "great grand" } |
| 335 |
elsif (lt(3, len)) { |
| 336 |
print("great(", d(sub(len, 2)), ") grand") |
| 337 |
"great(" d(sub(len, 2)) ") grand" |
| 338 |
} |
| 339 |
print(pword, "of\n ", name(indi(two)), ".\n") |
| 340 |
pword "of\n " name(indi(two)) ".\n" |
| 341 |
} |
| 342 |
|
| 343 |
/*========================================================================= |
| 344 |
* showcous -- Show a cousin relationship; for the purposes of this |
| 345 |
* program, siblings, uncles, aunts, nieces and nephews are considered to |
| 346 |
* be special cases of cousins. |
| 347 |
*=======================================================================*/ |
| 348 |
proc showcous (one, two, three, up, down) |
| 349 |
{ |
| 350 |
set(indi, indi(one)) |
| 351 |
if (male(indi)) { |
| 352 |
set(sword, " brother ") |
| 353 |
set(nword, " nephew ") |
| 354 |
set(uword, " uncle ") |
| 355 |
} elsif (female(indi)) { |
| 356 |
set(sword, " sister ") |
| 357 |
set(nword, " niece ") |
| 358 |
set(uword, " aunt ") |
| 359 |
} else { |
| 360 |
set(sword, " sibling ") |
| 361 |
set(nword, " niece or nephew ") |
| 362 |
set(uword, " uncle or aunt ") |
| 363 |
} |
| 364 |
print(name(indi(one)), " is a") |
| 365 |
name(indi(one)) " is a" |
| 366 |
if (and(eq(up,1), eq(down, 1))) { /* sibling cases */ |
| 367 |
print(sword, "of") |
| 368 |
sword "of" |
| 369 |
} elsif (eq(up, 1)) { /* uncle/aunt cases */ |
| 370 |
if (eq(down, 2)) { |
| 371 |
print("n", uword, "of") |
| 372 |
"n" uword "of" |
| 373 |
} elsif (eq(down, 3)) { |
| 374 |
print(" great", uword, "of") |
| 375 |
" great" uword "of" |
| 376 |
} else { |
| 377 |
print(" great(", d(sub(down, 2)), ")", uword, "of") |
| 378 |
" great(" d(sub(down, 2)) ")" uword "of" |
| 379 |
} |
| 380 |
} elsif (eq(down, 1)) { /* niece/nephew cases */ |
| 381 |
if (eq(up, 2)) { |
| 382 |
print(nword, "of") |
| 383 |
nword "of" |
| 384 |
} elsif (eq(up, 3)) { |
| 385 |
print(" great", nword, "of") |
| 386 |
" great" nword "of" |
| 387 |
} else { |
| 388 |
print(" great(", d(sub(up, 2)), ")", nword, "of") |
| 389 |
" great(" d(sub(up, 2)) ")" nword "of" |
| 390 |
} |
| 391 |
} else { /* cousin cases */ |
| 392 |
if (gt(up, down)) { |
| 393 |
set(gen, down) |
| 394 |
set(rem, sub(up, down)) |
| 395 |
} else { |
| 396 |
set(gen, up) |
| 397 |
set(rem, sub(down, up)) |
| 398 |
} |
| 399 |
print(" ", ord(sub(gen,1)), " cousin ") |
| 400 |
" " ord(sub(gen,1)) " cousin " |
| 401 |
if (ne(rem, 0)) { |
| 402 |
if (eq(rem,1)) {print("once") "once"} |
| 403 |
elsif (eq(rem,2)) {print("twice") "twice"} |
| 404 |
elsif (eq(rem,3)) {print("thrice") "thrice"} |
| 405 |
else { |
| 406 |
print(card(rem), " times") |
| 407 |
card(rem) " times" } |
| 408 |
print(" removed ") " removed " |
| 409 |
} |
| 410 |
print("of") "of" |
| 411 |
} |
| 412 |
print("\n ", name(indi(three))) |
| 413 |
"\n " name(indi(three)) |
| 414 |
print(", through their ancestor,\n ", name(indi(two)), ".\n") |
| 415 |
", through their ancestor,\n " name(indi(two)) ".\n" |
| 416 |
} |
| 417 |
|
| 418 |
/*======================================================================= |
| 419 |
* fullpath -- Show full path between the two persons. |
| 420 |
*======================================================================*/ |
| 421 |
proc fullpath (key) |
| 422 |
{ |
| 423 |
"\nThe full relationship path between them is:\n\n" |
| 424 |
set(again, 1) |
| 425 |
while (again) { |
| 426 |
name(indi(key)) |
| 427 |
set(new, lookup(links, key)) |
| 428 |
set(dir, lookup(rels, key)) |
| 429 |
if (gt(dir, 0)) { |
| 430 |
" is the child of" |
| 431 |
} |
| 432 |
if (lt(dir, 0)) { |
| 433 |
" is the parent of" |
| 434 |
} |
| 435 |
"\n" |
| 436 |
if (eq(0, strcmp(key, new))) { |
| 437 |
set(again, 0) |
| 438 |
} else { |
| 439 |
set(key, new) |
| 440 |
} |
| 441 |
} |
| 442 |
} |