The Family Tree Software Series
Finite State Machine to the infinite chain #6
Reducing very long relation chains to a named relation
This is the sixth article about building genealogy software using a graph database. In the previous articles of this series, I have gone over the traversal process in gremlin. Some of the special relations are covered. In this article, I would be looking at ways to decode such a long relation chain to a meaningfully short relation like uncle, grandfather, uncle’s daughter-in-law etc.
Shortest Path Requirement
While explaining the relation between two people, we tend to take the shortest relation (route) between the people. This method of finding the relation chain reduces some meaning-less relations such as brother’s brother’s brother who is still a brother. It is good to have a meaningfully shortest path between two people to have a precise relation model.
Finite State Machine
Named relations like a cousin, uncle, sister, etc. are limited in number. Therefore however long the relation chain is, it would only cycle between these limited relations before it finally stops. This cycling stops when there is no direct name for the relation between the two people. For example, there is no direct name for the relation aunt’s Brother’s Sister in law. The model behavior is very close to the behavior of a finite state machine.
The relationship model can be tailored to the region/language. This can be done with the help of a JSON file that contains the names of relations. The one used in the application is the relations in English. In some languages, there may be relations that may not be existent in the English language. Therefore a simple translation of end relations would not suffice.
Standard Model
The one in English is kept as the standard model for the application. This is implemented as a python dictionary. There are still other relations that may be added to this model.
{'Father_Of':{'Father_Of':'Grandfather_Of',
'Brother_Of':'Uncle_Of',
'Sister_Of':'Aunt_Of',
'Mother_Of':'Grandmother_Of'},
'Husband_Of':{'Father_Of': 'Father in law_Of',
'Mother_Of':'Mother in law_Of',
'Brother_Of':'Brother in law_Of',
'Sister_Of':'Sister in law_Of'},
'Grandfather_Of': {'Brother_Of': 'Grandfather_Of',
'Son_Of':'Uncle_Of' },'Uncle_Of':{'Son_Of','Cousin_Of'},'Son_Of':{'Son_Of':'Grandson_Of',
'Wife_Of':'Daughter in law_Of',
'Daughter_Of':'Granddaughter_Of',
'Brother_Of':'Son_Of'},
'Wife_Of':{'Father_Of': 'Father in law_Of',
'Mother_Of':'Mother in law_Of',
'Brother_Of':'Brother in law_Of',
'Sister_Of':'Sister in law_Of'}}
This is the dictionary for reducing two relations to one. For example “Father’s Father’s Son’s Son” is a chain that gets reduced to (Grandfather’s) Son’s Son. Then to (Uncle’s) Son and then to Cousin. It does not get reduced to brother, because of the shortest path rule that we are following. The long-chain gets reduced from the left end to the right end. If there exists a relation that is not named, then python throws a KeyError. Once this is caught in the except block, the relation is added to a list and a new reduction process is started. This process is executed till the end of the relation chain.
There may be some named relations that depend on the age of one person to the next. In some relation models, the relation name of the elder brother’s son is different from the younger brother’s son. In such cases, a tuple of the relation and ‘Young’ / ‘Old’ is used as a composite key for getting the right named relation. For example, a dictionary can have key-value pairs as follows.
{
(Young, Brother):
{
(Old,Son): X,
(Young,Son):Y
},
(Old, Brother):
{
(Old, Son): A,
(Young, Son): B
}
}
This composite key structure can be extended to include all different characteristics about the person based on the requirements of the relationship model. The relation chain was created using the by('Name).by(label)
which if modified returns the required information.The base relations are still Mother, Father, Brother Sister, Wife and Husband. Therefore a model can be built such that these relations definitely exist as keys but can include other named relations as keys and values. Such a model can be saved in a JSON file and can be loaded according to the user’s choice.
Summary
In this article, I have shared my thoughts on reducing the long edge chain labels into the ones we use in our day-to-day lives. In the next article, I will be going over the feature facial recognition and some configurations in my project. If you liked this article, please clap and share it with others. Please feel free to ask your questions in the comment section.