Generating UML and Sequence Diagrams
by Phil Crow
|
Pages: 1, 2
Using UML::Sequence
Now that you understand how to read a sequence diagram, I can show you how to make them without mouse-driven drawing tools.
Making diagrams with UML::Sequence is a three-step process:
- Create a program or a text file.
- Use genericseq.pl to create an XML description of the diagram.
- Use a rendering script to turn the XML into an image file.
If the image is in the wrong format for your purposes, you might need an extra step to convert to another format.
Running Perl Programs
Here is how I generated Figure 1 above by running the driver program. If your program is in Perl, you can use this approach (see the next subsection for Java programs).
First, create a file listing the subs you want to see in the diagram:
DiePair::new
DiePair::roll
Die::new
Die::roll
I called this file roller.methods to correspond to the script's name, roller. When you make your method list, remember that sequence diagrams are visual space hogs, so pick a short list of the most important methods.
Then, run the program through the genericseq.pl script:
$ genericseq.pl UML::Sequence::PerlSeq roller.methods roller > roller.xml
UML::Sequence::PerlSeq uses the Perl debugger's hooks to profile the code as it runs, watching for the methods listed in roller.methods. The result is an XML file describing the calls that actually happened during this run.
To turn this into a picture, use one of the image scripts:
$ seq2svg.pl roller.xml > roller.svg
Obviously, seq2svg.pl makes SVG images. If you have no way to view those, get Firefox 1.5, use a tool like the batik rasterizer, or use seq2rast.pl, which makes PNG images directly using the GD module.
If you want diagrams like Figure 2, use UML::Sequence::PerlOOSeq in place of UML::Sequence::PerlSeq when you run genericseq.pl.
Running Java Programs
I wrote UML::Sequence while working as a Java programmer, so I made it work on Java (at least sometimes it works). The process is similar to the above. First, make a methods file:
ALL
Roller
DiePair
Die
Here I use ALL to mean all methods from the following classes. You can also list full signatures (but they have to be full, valid, and expressed in the internal signature format as if generated by javap).
Then run genericseq.pl with UML::Sequence::JavaSeq in place of UML::Sequence::PerlSeq. Of course, this requires you to have a Java development environment on your machine. In particular, it must be able to find tools.jar, which provides the debugger hooks necessary to watch the calls.
Produce the image from the resulting XML file as shown earlier for Perl programs.
Text File Input
While I pat myself on the back every time I make a sequence diagram of a running program, that's not always (or even usually) practical. For instance, you might want to show the boss what you have planned for code you haven't written yet. Alternately, you might have a program that is so complex that no amount of tweaking the methods file will restrict the diagram enough to make it useful.
In these cases, there is a small text language you can use to specify the diagram. It is based on indentation and uses dot notation for method names. Here is a sample:
At Home.Wash Car
Garage.retrieve bucket
Kitchen.prepare bucket
Kitchen.pour soap in bucket
Kitchen.fill bucket
Garage.get sponge
Garage.open door
Driveway.apply soapy water
Driveway.rinse
Driveway.empty bucket
Garage.close door
Garage.replace sponge
Garage.replace bucket
Each line will become an arrow in the final diagram (except the first line). Indentation indicates the call depth. The "class" name comes before the dot and the "method" name after it.
There is no need for a methods file in this case, because presumably you didn't bother to type things you didn't care about. You may go directly to running genericseq.pl:
$ genericseq.pl UML::Sequence::SimpleSeq inputfile > wash.xml
Once you have the XML file, render it as before.
Getting Fancy
As I mentioned earlier, Dean Arnold recently added lots of cool features to amaze and impress your bosses and/or clients. In particular, he expanded the legal syntax for text outlines. Here is his sample of car washing with the new features:
AtHome.Wash Car
/* the bucket is in the garage */
Garage.retrieve bucket
Kitchen.prepare bucket
Kitchen.pour soap in bucket
Kitchen.fill bucket
Garage.get sponge
Garage.checkDoor
-> clickDoorOpener
[ ifDoorClosed ] Garage.open door
* Driveway.apply soapy water
! Driveway.rinse
Driveway.empty bucket
Garage.close door
Garage.replace sponge
Garage.replace bucket
There are several new features here:
- You can include UML annotations by using C-style comments, as shown on the second line of the example. Each annotation attaches to the following line as a footnote (or tooltip, if you install a third-party open source library).
- There is a
->in front ofclickDoorOpener. This becomes an asynchronous message arrow. When->comes between a method and additional text, it indicates that a regular method is returning the value on the righthand side of the arrow. The return appears as a dashed arrow from the called activation back to the caller. ifDoorClosedis in brackets, which mark a conditional in UML. These appear in the diagram in front of the method name.- There is a star in front of
Driveway.apply, which indicates a loop construct in UML. (UML people call this iteration.) - There is an exclamation point in front of
Driveway.rinse, indicating urgency.
In addition to these changes to the outline syntax, both seq2svg.pl and seq2rast.pl now support options to control appearance (including colors) and to generate HTML imagemaps for raster versions of the diagrams. The imagemaps hyperlink diagram elements--columns header and method call names--to supporting documents. For example, clicking on the Garage header will open Garage.html, while clicking on checkDoor will also open Garage.html, but at the #checkDoor anchor.
Summary
UML Sequence diagrams are a great way to see how function or method calls (or network messages) flow through a multi-module application, whether it is object-oriented or not. Using UML::Sequence and its helper scripts, you can make those diagrams without having to point and click in a drawing program.
References
The imagemapped HTML version of car washing is viewable online.
To read more about UML diagrams, check out the aptly named UML Distilled, by Martin Fowler, available from your favorite bookseller.
I recommend Walter Zorn's JavaScript, DHTML tooltips package to display embedded annotations.
Batik is an Apache project for managing and viewing SVG.
You must be logged in to the O'Reilly Network to post a talkback.
Showing messages 1 through 7 of 7.
- appending command line options
2006-08-21 14:33:36 stathy [Reply]
I have an application I would like to run through UML::Sequence but requires command line options.
I am running in a Win32 environment and can not get it to work.
Any tips?- appending command line options
2006-08-21 16:07:23 philcrow [Reply]
I'm sorry that I don't have a Windows environment on which to test. You could try to run you program directly through the UML::Sequence perl profiler:
perl -d:CallSeq script_name command line params
If that works, you will want to redirect it to a file. You should then be able to use that file to build the sequence with UML::Sequence::SimpleSeq. If it doesn't work, it would probably give us some error messages to work with. In that case, please send them, along with a small script which failed, to my email address philcrow2000@yahoo.com.
Phil
- appending command line options
- i wish install would be little easier
2006-08-04 18:29:37 avetiktopchyan [Reply]
Install on Windows is problematic: missing modules Win32::Font::NameToFile, which requires IO::Tty which is also somewhat a pain to install.
After an hour of manipulations, this module was finally configured and seems to work (keep your fingers crossed) on Windows. However, I can't use it for production without additional testing...
But I love the idea of showing method calls graphically like this. Keep up your good work!..- i wish install would be little easier
2006-08-05 11:32:41 renodino [Reply]
It appears our Canadian friends have once again stepped into the breach: a
Win32 PPM is available at the
University of Winnipeg PPM respository (http://theoryx5.uwinnipeg.ca/ppms/) .
- i wish install would be little easier
- Successful with Problems
2006-08-04 08:25:12 matthewmusgrove [Reply]
Very cool. I had two problems though.
I had to add parens to the rand call in Die.pm so that Perl didn't interpret the * as the start of a glob. ($self->{VALUE} = int( rand() * $self->{SIDES} ) + 1;)
It also looks like the internals of UML::Sequence have changed because when I run 'genericseq.pl UML::Sequence::PerlOOSeq roller.methods roller > rollerOO.xml' I receive the following error:
Can't coerce array into hash at /path/to/lib/perl5/site_perl/5.8.5/UML/Sequence.pm line 148.
- Successful with Problems
2006-08-05 06:50:39 philcrow [Reply]
> Very cool. I had two problems though.
Thanks.
> I had to add parens to the rand call in Die.pm
> so that Perl didn't interpret the * as the start
> of a glob. ($self->{VALUE} = int( rand() *
> $self->{SIDES} ) + 1;)
I apologize for this syntax error. The code in the samples directory of the distribution has a more verbose correction than yours:
my $random_number = rand;
$self->{VALUE} = int ($random_number *
$self->{SIDES}) + 1;
Unfortunately, I didn't get this into the article. Thanks for pointing it out.
> It also looks like the internals of
> UML::Sequence have changed because when I run
>'genericseq.pl UML::Sequence::PerlOOSeq
> roller.methods roller > rollerOO.xml' I receive
> the following error:
> Can't coerce array into hash at...
This error occurred because you need to use a different methods file syntax for the OO version. The samples directory of the distribution provides rollerinst.methods for use with PerlOOSeq. For brevity, I didn't mention that in the article, but I probably should have. And I admit that the error message should be more helpful.
Phil
- Successful with Problems
- Neat.. should work nicely with autodia
2006-08-04 06:22:36 teejay2 [Reply]
This is cool, I've been generating UML Class Diagrams from perl for years using autodia, but hopefully I can apply the same techniques to output sequence diagrams and flowcharts as well.



