Sunday 8 June 2014

A look at HypergraphDB (Part 1)

According to the HypergraphDB website it is described as a "general purpose, open source data storage mechanism based on a powerful knowledge management formalism known as directed hypergraphs".

So you may be asking the question, what is a hypergraph. I found a relatively simple explanation on StackOverflow "Hypergraphs are like simple graphs, except that instead of having edges that only connect 2 vertices, their edges are sets of any number of vertices. This happens to mean that all graphs are just a subset of hypergraphs." - link (when I imagine what a hypergraph looks like I imagine graphs within graphs!)

What I really like about HypergraphDB is how easy it is to setup, so lets get into setting up a Java project in Netbeans, and recreating the example from the HypergraphDB website.

The first thing I did after opening Netbeans is create a new project, I chose to create a Java application from under the Maven menu (this will auto generate your pom.xml file). I then added the code below to the pom.xml before the ending project tag.

 <repositories>  
   <repository>  
    <id>hypergraphdb</id>  
    <url>http://hypergraphdb.org/maven</url>  
   </repository>  
  </repositories>  
  <dependencies>  
  <dependency>  
        <groupId>org.hypergraphdb</groupId>  
        <artifactId>hgdb</artifactId>  
        <version>1.2</version>  
   </dependency>  
   <dependency>  
        <groupId>org.hypergraphdb</groupId>  
        <artifactId>hgbdbje</artifactId>  
        <version>1.2</version>  
   </dependency>  
  </dependencies>  

When the pom.xml file has been updated, right click on the project and choose Build with Dependancies this will download the required jar files, now the project is set up you will need a folder to store your HypergraphDB, I made a folder in my home directory called Hypergraph for this example.


 public class HyperGraphExample {  
   public static void main(String args[]){  
     HyperGraph graph = new HyperGraph("/home/mhoward/Hypergraph");  
     HGHandle handle = graph.add("Hello World");  
     String hello = graph.get(handle);  
     System.out.println(hello);  
     graph.close();  
   }  
 }  

The line "HyperGraph graph = new HyperGraph("/home/mhoward/Hypergraph");" simply sets up the graph and data will be saved in the /home/mhoward/Hypergraph folder.

graph.add will add an object to your graph, it returns a handle which can be used to retrieve that object at a later stage using graph.get.

graph.close is called to close the database, on the website it is recommended that graph.close is called to reduce the chances of data corruption, they recommend wrapping the code in a try/catch and finally closing the database to ensure close always gets called.

In part 2 I will be looking at adding sample data to the graph and querying that data, using the built in query mechanism.


No comments:

Post a Comment