Example: Calculating Relationships

This is an example of a program which uses gedcom4j to calculate the relationships between individuals in a GEDCOM.

It loads a GEDCOM, and finds a specific starting person (in this case, my grandfather). Then it iterates through every individual in the GEDCOM file, and calculates their relationship to my grandfather. The relationship(s) calculated are simplified – see the true as the last parameter in the call to calculateRelationships() – and returned as a list (because two individuals can be related in multiple ways). The name of the individual and their relationships to my grandfather are then printed to stdout.

package com.mattharrah.genealogy;
 
import java.io.IOException;
import java.util.List;
 
import org.gedcom4j.exception.GedcomParserException;
import org.gedcom4j.model.Gedcom;
import org.gedcom4j.model.Individual;
import org.gedcom4j.parser.GedcomParser;
import org.gedcom4j.query.Finder;
import org.gedcom4j.relationship.Relationship;
import org.gedcom4j.relationship.RelationshipCalculator;
 
/**
 * A demonstration of showing relationships between people. Finds a
 * starting person, and then shows the relationship between that person
 * and every other individual in the file.
 * 
 * @author frizbog
 */
public class Relationships {
    /**
     * Main method
     * 
     * @param args
     *            command line arguments - ignored
     * @throws IOException
     *             if there is a problem reading the GEDCOM data
     * @throws GedcomParserException
     *             if the GEDCOM file cannot be parsed
     */
    public static void main(String[] args) throws IOException,
            GedcomParserException {
        // Make a relationship calculator instance
        RelationshipCalculator rc = new RelationshipCalculator();
 
        // Load the GEDCOM
        GedcomParser gp = new GedcomParser();
        gp.load("/Users/frizbog/Documents/Genealogy Misc/Harrah.ged");
        Gedcom g = gp.getGedcom();
 
        // Find the starting person
        Finder f = new Finder(g);
        List<Individual> results = f.findByName("Harrah",
                "Robert Watson");
        Individual grandpaHarrah = results.get(0);
 
        // Go through every person in the file...
        for (Individual i : g.getIndividuals().values()) {
            // ...calculate their relationship(s) to the starting person
            // in simplest form available...
            rc.calculateRelationships(grandpaHarrah, i, true);
 
            // .. and print them to stdout
            System.out.println(i.getFormattedName());
            for (Relationship r : rc.getRelationshipsFound()) {
                System.out.println("   " + r);
            }
        }
    }
}

This produces output like this:

Henry /Ramsey/
   <Robert Watson /Harrah/'s GREAT_UNCLE James Asbury /Hypes/, James Asbury /Hypes/'s WIFE Virginia Belle /Renick/, Virginia Belle /Renick/'s GREAT_GREAT_GRANDMOTHER Hannah /Tincher/, Hannah /Tincher/'s GREAT_NEPHEW Samuel /Tincher/, Samuel /Tincher/'s WIFE Hannah Margaret /Harrah/, Hannah Margaret /Harrah/'s FIRST_COUSIN Elizabeth /Harrah/, Elizabeth /Harrah/'s HUSBAND Mathew /Kincaid/, Mathew /Kincaid/'s GREAT_NIECE Margaret /Ellis/, Margaret /Ellis/'s HUSBAND Henry /Ramsey/>, 9 step(s)
John Elmer /Harrah/
   <Robert Watson /Harrah/'s GREAT_UNCLE James Asbury /Hypes/, James Asbury /Hypes/'s WIFE Virginia Belle /Renick/, Virginia Belle /Renick/'s GREAT_GREAT_GRANDMOTHER Hannah /Tincher/, Hannah /Tincher/'s GREAT_NEPHEW Samuel /Tincher/, Samuel /Tincher/'s WIFE Hannah Margaret /Harrah/, Hannah Margaret /Harrah/'s UNCLE James /Harrah/, James /Harrah/'s GREAT_GREAT_GREAT_GRANDSON John Elmer /Harrah/>, 7 step(s)
Polly /Wagner/
   <Robert Watson /Harrah/'s GRANDSON Living /Harrah/, Living /Harrah/'s WIFE Living /Bedford/, Living /Bedford/'s GREAT_GREAT_GREAT_GRANDMOTHER Louisa Melvina /Wagner/, Louisa Melvina /Wagner/'s SISTER Polly /Wagner/>, 4 step(s)
Daniel /Torrey/
   <Robert Watson /Harrah/'s GRANDSON Living /Harrah/, Living /Harrah/'s WIFE Living /Bedford/, Living /Bedford/'s GREAT_GREAT_GRANDMOTHER Clara L /Torrey/, Clara L /Torrey/'s GREAT_GREAT_GRANDFATHER James /Torrey/, James /Torrey/'s GRANDFATHER James /Torrey/, James /Torrey/'s NEPHEW Thomas /Torrey/, Thomas /Torrey/'s WIFE Elizabeth /Torrey/, Elizabeth /Torrey/'s GREAT_GREAT_UNCLE William /Torrey/, William /Torrey/'s GREAT_GRANDDAUGHTER Ann /Torrey/, Ann /Torrey/'s HUSBAND James /Humphrey/, James /Humphrey/'s FIRST_COUSIN Daniel /Torrey/>, 11 step(s)
Paul Clarence /Brown/ Jr.
   <Robert Watson /Harrah/'s SON Living /Harrah/, Living /Harrah/'s WIFE Living /Barney/, Living /Barney/'s GREAT_UNCLE Ernie Vance /Doles/, Ernie Vance /Doles/'s WIFE Sylvia Gertrude /Brooks/, Sylvia Gertrude /Brooks/'s NIECE Ruth /Graves/, Ruth /Graves/'s HUSBAND Clarence O. /Brown/, Clarence O. /Brown/'s GREAT_UNCLE John Wesley /Doles/, John Wesley /Doles/'s GRANDSON Paul Clarence /Brown/ Jr.>, 8 step(s)
...

Note that if you want to see each individual hop from person to person in the relationships, you can pass false to the calculateRelationships() method, which will stop gedcom4j from simplifying the relationships.

Also, note that there are still some deficincies with the RelationshipCalculator, and for this reason, the newer KinshipNameCalculator is probably more suited for most cases. Please see the javadoc for details.