Class Dependency Graphs

There are several tools for creating UML models of program source code. Some work for java, some for C++, and some not at all. Unfortunately, the latter case seems to be prominent. I searched google and looked in wikipedia. I saw many comments in forums lamenting that there are no useful uml diagram utilities in linux. In this post I comment on some of these methods and present two simple shell scripts for generating class dependency graphs.

One tool I tried is umbrello, which at the first try worked nicely, then however crashed (maybe there was too much source code) and never launched. I have seen people commenting similar behavior with umbrello. ArgoUML is another java-based tool, which produced diagrams from my code, however hardly legible ones. Class names overlapped and were not readable at all.

Actually, if you just want to see the dependency of you classes, it should not be so difficult to generate. Dependency diagrams from source code can be made in a very simple way, by using a bash 3-line shell script and visualizing with graphviz (see my introduction to graphviz).

For C++, you just parse the header files and find "class bar: public foo {." Then you parse this line and output to a file (here diagram.dot):

echo "digraph G {"> diagram.dot
sed -n -e "s/class [ ]*\([a-zA-Z0-9_]*\)[ ]*:[ ]*public [ ]*\([a-zA-Z0-9_]*\)[ ]*[{][ ]*/\1 -> \2 ;/p" *.h >> diagram.dot
echo "}" >> diagram.dot


compile to pdf:
dot -Tpdf diagram.dot -o diagram.pdf

And finished. You can see the inheritance of your classes in the pdf. Note that this script -- as it is -- only handles single inheritance from public classes.

You can use very similar commands for Java:

echo "digraph G {"> diagram.dot
sed -n -e "s/class [ ]*\([a-zA-Z0-9_]*\)[ ]* extends [ ]*\([a-zA-Z0-9_]*\)[ ]*[{][ ]*/\1 -> \2 ;/p" *.java >> diagram.dot
echo "}" >> diagram.dot


Enjoy. Please leave a comment for questions or suggestions.
[ Read more... ]

Graph Visualization Software (Graphviz)

For a long time already I have wanted to post about this cool tool. If you haven't hear of it yet, you maybe should. Graph Visualization Software (short graphviz) is an open-source software package that visualizes graphs, structures, networks, and dependency diagrams.

In order to show how neat, simple, and useful it is, two examples:

An Undirected Graph
Write a text file "graph.dot". We'll define two nodes in our graph: n1 and n2. They are connected.
graph G {
n1 -- n2 ;
}


We compile the file:
neato -Tpdf graph.dot -o undirgraph.pdf



A Directed Graph
Now, we try a directed graph. N1 connects to n2. N2 doesn't connect back to n1.
digraph G {
n1 -> n2 ;
}



Again: compiling:
neato -Tpdf graph.dot -o dirgraph.pdf




You might want to try out the different rendering algorithms provided with dot, neato, twopi, circo, and fdp.

BTW, you might want to try inkscape to edit the pdf. You can move network nodes around, changes fonts, etc. See my other blog post about how to produce high quality figures in linux for latex using scalable vector graphics.

Enjoy. Please leave a comment for questions or suggestions.
[ Read more... ]