Draws UML Class Diagram with Graphviz

When I was studying OpenNMS in 2011, I found a database schema diagram of it(the link).

It’s generated by SchemaSpy who dumps the relationships between the tables (by Foreign Keys) and generates “dot” files and use Graphviz to convert these files to graphs.

So I knew about two powerful applications:

  1. SchemaSpy (It’s console application, there is a GUI wrapper for it: SchemaSpyGUI)
  2. Graphviz.

By using Graphviz, we can draw certain types of UML diagrams, like Class Diagram.

This is the example:

uml_basic_class_diagram_by_graphviz

And this is the Graphviz DOT script of it:

 dot |  copy code |? 
01
<!--more-->digraph {
02
  rankdir=BT // Graph direction : Bottom-Top
03
  node [shape=record] // all nodes are in Box shape
04
  edge [dir=normal labeldistance=1] // "labeldistance=1" is default value
05
 
06
  // Multiplicity types by Crow's Foot Notation
07
  Member1 -> Group1 [dir=both arrowtail=crowodot arrowhead=teetee label="0or* to 1"]
08
  Member2 -> Group2 [dir=both arrowtail=teeodot arrowhead=teetee label="0or1 to 1"]
09
  Member3 -> Group3 [dir=both arrowtail=crowtee arrowhead=crowodot label="1or* to 0or*"]
10
}

The SchemaSpy uses Crow’s Foot Notation to express the multiplicity types, like:

uml_crows_foot_notation_in_class_diagram

And this is the script of it:

 dot |  copy code |? 
01
digraph {
02
  rankdir=BT // Graph direction : Bottom-Top
03
  node [shape=record] // all nodes are in Box shape
04
  edge [dir=normal labeldistance=1] // "labeldistance=1" is default value
05
 
06
  // Multiplicity types by Crow's Foot Notation
07
  Member1 -> Group1 [dir=both arrowtail=crowodot arrowhead=teetee label="0or* to 1"]
08
  Member2 -> Group2 [dir=both arrowtail=teeodot arrowhead=teetee label="0or1 to 1"]
09
  Member3 -> Group3 [dir=both arrowtail=crowtee arrowhead=crowodot label="1or* to 0or*"]
10
}

I studied the RBAC(Role-Based Access Control) once, and here is a Domain Object Model (a simple class diagram) of it:

uml_rbac_by_graphviz

 dot |  copy code |? 
01
digraph {
02
//rankdir=LR // from bottom to top
03
node[shape=record]
04
 
05
// User, Role, and Assignment
06
_UserRole [shape=point label=""]
07
User -> _UserRole [dir=both arrowtail=crowodot arrowhead=none taillabel="has"]
08
_UserRole -> Role [dir=both arrowtail=none arrowhead=crowodot]
09
Assignment -> _UserRole [style=dotted arrowhead=none]
10
 
11
// User, Session and Role
12
Session -> User [dir=both arrowtail=crowodot arrowhead=teetee headlabel="establishes" taillabel="_"] // empty label
13
Session -> Role [dir=both arrowtail=crowodot arrowhead=crowodot taillabel="___activates"] // use _ instead of whitespace
14
 
15
// Role inheritance
16
Role -> Role [arrowhead=onormal taillabel="inherits"]
17
 
18
// Role and Permission
19
Role -> Permission [dir=both arrowtail=crowodot arrowhead=crowodot taillabel="has"]
20
 
21
// Operation and Resource, and the ASSOCIATION with Permission
22
_OperationResource [shape=point label=""]
23
Operation -> _OperationResource [dir=both arrowtail=crowodot arrowhead=none]
24
_OperationResource -> Resource [dir=both arrowtail=none arrowhead=crowodot]
25
Permission -> _OperationResource [style=dotted arrowhead=none]
26
}

Also See

Graphviz

RBAC



Leave a Reply