From charlesreid1

Revision as of 16:40, 20 April 2012 by Admin (talk | contribs)

You can use Graphviz's Dot package to create UML diagrams.

Examples

Simple Class

You can make a simple class UML diagram like this:

<graphviz>

digraph G {

rankdir="LR"
node [shape=record];

Arches [shape=record,label="<f0> Arches | <f1> problemSetup() : void \\
 \lscheduleInitialize() : void \\
 \linitialize() : void \\
 \lscheduleComputeStableTimestep() : void \\
 \lcomputeStableTimestep() : void \\
 \lscheduleTimeAdvance() : void \\
 \ltimeAdvance() : void \\
                                        |  \\
 <f2> sharedState_ : SimulationState  \\
 \lmyMaterial_ : Material \\
                                    "];
}

</graphviz>
<graphviz>

digraph G {

rankdir="LR" node [shape=record];

Arches [shape=record,label="<f0> Arches | <f1> problemSetup() : void \\

\lscheduleInitialize() : void \\
\linitialize() : void \\
\lscheduleComputeStableTimestep() : void \\
\lcomputeStableTimestep() : void \\
\lscheduleTimeAdvance() : void \\
\ltimeAdvance() : void \\
\\
<f2> sharedState_ : SimulationState  \\
\lmyMaterial_ : Material \\
                                   "];

}

</graphviz>

Inheritance Diagram

Class inheritance can be illustrated with a UML diagram with a black arrow pointing from the child to the parent.

This code:
<graphviz>
BaseClass [shape=record,label="{<f0> BaseClass \\
    | <f1> + doSomething() : void \\
    }"];

DerivedClass [shape=record,label="{<f0> ScalarEqn \\
    | <f1> + doSomethingDerived() : void \\
    }"];


BaseClass->DerivedClass  [dir=back];
</graphviz>
Makes this diagram:

<graphviz> BaseClass [shape=record,label="{<f0> BaseClass \\

<f1> + doSomething() : void \\
   }"];

DerivedClass [shape=record,label="{<f0> ScalarEqn \\

<f1> + doSomethingDerived() : void \\
   }"];


BaseClass->DerivedClass [dir=back]; </graphviz>

Composition & Aggregation

Composition

Composition is a strong relationship between two entities, usually linked strongly by lifecycle. To say "an instance of class A MUST have an instance of class B" means that B composes A. It is a 1-to-1 (or 1-to-N) relationship. Think of a situation where you call class A's constructor, and class A's constructor calls class B's constructor; and vice-versa with the destructor (A's destructor calls B's destructor).

A concrete example would be a car and an engine. The two are related by composition: there is a strong lifecycle dependency between the two, and the relationship is 1-to-1.

Composition can be illustrated in a UML diagram as follows:

<graphviz>

Car    [shape=record,label="Car"];
Engine [shape=record,label="Engine"];

Car->Engine [dir=back];
</graphviz>


</graphviz>

<graphviz>

Car [shape=record,label="Car"]; Engine [shape=record,label="Engine"];

Car->Engine [dir=back]; </graphviz>


</graphviz>

Aggregation

Aggregation is a weaker, many-to-many relationship.