The Buendia Family Tree as a Neo4j graph – Tribute to Gabo

One of the most significant and prominent authors passed away last fortnight – Gabriel Garcia Marquez. I read One Hundred Years of Solitude during my senior college years. Magical Realism has long been associated with the vernacular literature in my home state.

gabriel-garcia-marquez

The Buendia family in One Hundred Years of Solitude is another epic on it’s own. See a very nice graphic family tree below.

100_years_of_solitude_family_t_by_clothos

I always wanted to visualize a family tree using a graph, and the Buendias of Macondo seemed so perfect. My other choice was the political families of India, which is pretttyyy vaasttt..

The visualization is pretty straightforward, two types of nodes

  • Male
  • Female

And 4 simple relationships

  • HUSBAND
  • FATHER
  • MOTHER
  • MISTRESS (Sorry, could not find an euphemism for that)

final-buendia-crop

Apologies if it is too cluttered, but you could play around with the graphgist

Advertisements

Visualizing an xml as a graph – Neo4j 101

Neo4j is quite awesome for pretty much everything. Here is an extract I found from this blog

Whenever someone gives you a problem, think graphs. They are the most fundamental and flexible way of representing any kind of a relationship, so it’s about a 50-50 shot that any interesting design problem has a graph involved in it. Make absolutely sure you can’t think of a way to solve it using graphs before moving on to other solution types.

As a baby step, let us try to visualize an xml as a graph.

We can start with a simple xml here,

<?xml version="1.0" encoding="UTF-8"?>
 <breakfast_menu>
  <food>
   <name attr="one">Belgian Waffles</name>
   <price>$5.95</price>
   <description>Two of our famous Belgian Waffles with plenty of real maple syrup</description>
   <calories>650</calories>
  </food>
 </breakfast_menu>

It is a simple ‘Breakfast Menu’ with just one item, ‘Belgian Waffles‘.

Neo4j has a neat interface which lets us visualize the graph we created. The graph for the above xml looks slick..

small-graph-xml

 

The above XML has 6 Tags and we have 6 nodes in our graph. The nested tags/Nodes has a ‘CHILD-OF’ relationship with the Parent tag/Node.

In the graph above,

  • Node numbered 0 is the root node – Corresponds to the <breakfast_menu> tag
  • Node numbered 1 is the immediate child of the root – Corresponds to the <food> tag
  • Nodes numbered from 2-4 are the tags which come under <food> tag, viz <name>, <price>, <description> and <calories>

Transforming XML..

Let us parse the xml into a data structure which can be easily persisted using Neo4j. Personally, I would prefer SAX parser as there are serious memory constraints when creating a DOM object (really painful if you talking big data as xml). Additionally SAX parsing gives you unlimited freedom to do whatever you want to do with the XML.

This is how I represent a node, using the object XmlElement. Each XmlElement is identified by an id. This is basically an integer or long. Only thing to make sure is that the number has to be unique across all XMLElements.


public class XmlElement {

 private String tagName;
 private String tagValue;
 private Map<String, String> attributes = new HashMap<String,String>();
 private HierarchyIdentifier hierarchyIdentifier;
 private int parentId;
 private boolean persisted;

//Setters and Getters for the members

 public String getAtrributeString(){
  ObjectMapper jsonMapper = new ObjectMapper();
   try {
    return jsonMapper.writeValueAsString(this.attributes);
   } catch (JsonProcessingException e) {
    LOGGER.severe(e.getMessage());
    e.printStackTrace();
   }
  return null;
 }

}

The XML Tag name and value are stored as string and the attributes are stored as a Map. Also, there is another member Object, HierarchyIdentifier

public class HierarchyIdentifier {
 private int depth;
 private int width;
 private int id;

//Getters and Setters for member element
}

The HierarchyIdentifier Class contains the id class which is used to identify an XmlElement, which translates to identifying an XML Tag. Each XmlElement has the id of it’s parent stored.

And after the parse, the XML should be represented as a map of <Integer,XmlElement> and each XmlElement identified by the corresponding Integer id. You can see the SAX PARSER here..

So we pass the Map to the GraphWriter object.

We create a node with the following properties

  • Value – The Value of the XML Tag
  • ID – The ID of the current node
  • Parent – The ID of the parent Tag
  • Attributes – The String representation of the Tag attributes

Also, the node has the following Labels

  • NODE – For all nodes
  • PARENT – For the seed ( highest parent ) node
  • XML tag Name – Say, the node for the tag <food> will have label FOOD

Currently, there is only a single type of relationship, ‘CHILD_OF’ , between immediate nested tags.

See the below the example for a bigger XML

<breakfast_menu>
 <food>
  <name attr="one">Belgian Waffles</name>
  <price>$5.95</price>
  <description>Two of our famous Belgian Waffles with plenty of real maple syrup</description>
  <calories>650</calories>
 </food>
 <food>
  <name attr="two">Masala Dosa</name>
  <price>$10.95</price>
  <description>South India's famous slim pancake with mashed potatoes</description>
  <calories>650</calories>
  <eaten>
   <name first="nikhil"/>
   <age>25</age>
  </eaten>
 </food>
 </breakfast_menu>

I have added a second item to the ‘breakfast_menu’.. the legendary masala dosa.

Also, the second food item has a new child tag, <eaten>. And the resultant graph looks prettier

xml-med

 

The sets of tags,relationships and properties can be seen in the Neo4j local server interface.

xml-med-tags

The entire codebase can be found here in github..

Visualizing as a GraphGist

An easy representation can be done in GraphGist. I have created the graph of this xml.

Here is the link to the gist. Also, the gist script can be found in the github gist here..