Wednesday, August 3, 2011

Create a graph visualization using ANTLR

You can use Graphviz software to generate a graphical version of a tree or graph.
Download link: http://www.graphviz.org/Download..php


Create the parse tree
  1. First, create ANTLR StringTemplates for tree and edges. (design specification for the image)
  2. Get the file content and create an AntlrFileStream to be given as an input to the lexer.
  3. Then create a token stream using the lexer and invoke the rules to parse the tree.
Generate the dot file

Create an instance of ANTLR DotTreeGenerator.
Create the output file.
 StringTemplate st = dotTreeGenerator.ToDOT(ParsedTree, new CommonTreeAdaptor(), _treeStringTemplate, _edgeStringTemplate);
You can create .dot file using this string template. (Ex: test.dot)


Generate image of the graph

Give the .dot file as input and specify the output format. (Ex: .png)
Execute the following command:
dot.exe -Tpng test.dot -O test.png

An image of the input graph will be created with the given design specifications.

Sample image generated for SQL query is given below:

CREATE TABLE Persons
(
P_Id int NOT NULL PRIMARY KEY,
LastName varchar(255) NOT NULL,
FirstName varchar(255)
)

1 comment:

  1. Thanks for such a nice and updated information. I got some interesting tips from this post.
    Sony Laptops

    ReplyDelete