Debian Codegraph @

texmf-dist/doc/metapost/metauml/manual/metauml-manual.tex

raw
1 % MetaUML: Tutorial, Reference and Test Suite
2 %
3 % Copyright (c) 2005-2019 Ovidiu Gheorghies
4 % Permission is granted to copy, distribute and/or modify this document
5 % under the terms of the GNU Free Documentation License, Version 1.2
6 % or any later version published by the Free Software Foundation;
7 % with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
8 % A copy of the license is included in the section entitled "GNU
9 % Free Documentation License".
10
11 \documentclass{article}
12
13 \usepackage[utf8]{inputenc}
14 \usepackage[pdftex,colorlinks=true]{hyperref}
15 \usepackage{multicol}
16 \usepackage{multido}
17 \usepackage[style=ieee]{biblatex}
18 \addbibresource{metauml-manual.bib}
19
20 \ifx\pdftexversion\undefined
21 \usepackage[dvips]{graphicx}
22 \else
23 \usepackage[pdftex]{graphicx}
24 \DeclareGraphicsRule{*}{mps}{*}{}
25 \fi
26
27 \newcommand{\code}{\ttfamily}
28
29 \setcounter{page}{1}
30
31 \begin{document}
32
33 MetaUML: A Manual and Test Suite
34
35 \begin{quote}
36 Copyright \copyright 2005-2019 Ovidiu Gheorghie\c{s}.
37 Permission is granted to copy, distribute and/or modify this document
38 under the terms of the GNU Free Documentation License, Version 1.2
39 or any later version published by the Free Software Foundation;
40 with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
41 \end{quote}
42
43 \pagebreak
44 This page is intentionally left blank.
45
46 \pagebreak
47 \title{MetaUML: A Manual and Test Suite}
48
49 \author{Ovidiu Gheorghie\c{s}}
50
51 \maketitle
52
53 \begin{abstract}
54 MetaUML is a MetaPost \cite {metapost} library for creating UML \cite{umlomg} diagrams by means of a textual notation.
55 While presenting the inner workings of MetaUML, this manual doubles as a step-by-step tutorial.
56 More importantly, its source code contains many useful examples of diagrams, ranging from the very basic to the
57 more advanced and customized.
58 \end{abstract}
59
60 \section{Introduction}
61
62 Here is a quick MetaUML showcase:
63
64 \begin{multicols}{2}
65 \paragraph{A} Class Diagram\\
66 \includegraphics[scale=.55]{fig/appetizer.1}
67 \paragraph{B} Activity Diagram\\
68 \includegraphics[scale=.55]{fig/appetizer.2}
69 \paragraph{C} Notes\\
70 \includegraphics[scale=.55]{fig/appetizer.5}
71 \columnbreak
72 \paragraph{D} Use Case Diagram\\
73 \includegraphics[scale=.55]{fig/appetizer.3}
74 \paragraph{E} State Machine Diagram\\
75 \includegraphics[scale=.55]{fig/appetizer.4}
76 \paragraph{F} Package Diagram\\
77 \includegraphics[scale=.55]{fig/appetizer.6}
78 \end{multicols}
79
80 \pagebreak
81
82 The code that generates these diagrams is quite straightforward, combining a natural object-oriented parlance
83 with the power of MetaPost equation solving.
84
85 For example, a UML class is drawn as follows:
86
87 \begin{multicols}{2}
88 \begin{verbatim}
89 Class.A("MyClass")
90 ("attr1: int", "attr2: int")
91 ("method1(): void",
92 "method2(): void");
93
94 A.nw = (0, 0); % optional, implied
95 drawObject(A);
96 \end{verbatim}
97 \columnbreak
98 \hspace{1cm}\includegraphics{fig/appetizer.7}
99 \end{multicols}
100
101 This code creates a visual object, referenced by its name {\code A}, of the MetaUML-defined type {\code Class}.
102 Object {\code A} has the following content properties: a name
103 ({\code MyClass}), a list of attributes ({\code attr1}, {\code attr2})
104 and a list of methods ({\code method1}, {\code method2}). To set the object's location, we assign a value to the
105 so-called ``north-west'' point of the encompassing rectangle, {\code A.nw} --- a point which in actual fact references
106 the upper-left corner.
107
108 Every MetaUML visual object has the layout properties shown in figure \ref{fig:properties}.
109 These properties may be used to set the location of any given object, either by assigning to them absolute values,
110 or by linking them relatively to other objects via equations.
111
112 \begin{figure}
113 \centering
114 \includegraphics{fig/properties.1}
115 \caption{Layout properties of MetaUML objects. Here, a {\code Class} object is depicted.}
116 \label{fig:properties}
117 \end{figure}
118
119 The following example demonstrates, respectively, the use of absolute and relative positioning for two classes, {\code A} and {\code B}.
120
121 \begin{multicols}{2}
122 \begin{verbatim}
123 A.nw = (0,0);
124 B.w = A.e + (20, 0);
125 \end{verbatim}
126 \columnbreak
127 \includegraphics{fig/appetizer.8}
128 \end{multicols}
129
130 After the objects have been drawn, it becomes possible to attach links to them. In a class diagram, inheritance
131 or association relations are meaningful links between classes, while in a state machine diagram, transitions between
132 states can be used. Here is the general pattern used by MetaUML for drawing links:
133
134 \begin{verbatim}
135 link(<how-to-draw-information>)(<path-to-draw>);
136 \end{verbatim}
137
138 The ``how-to-draw-information'' is an object which defines the style of the line (e.g. solid, dashed) and the appearance
139 of the heads (e.g. nothing, arrow, diamond). One such object, appropriately called {\code inheritance}, defines a solid
140 line style and a white triangle head. The other parameter, the ``path-to-draw'', is simply a MetaPost path.
141
142 For example, the following call draws an inheritance relation from class {\code B} to class {\code A}.
143
144 \begin{verbatim}
145 link(inheritance)(B.e -- A.w);
146 \end{verbatim}
147
148 The direction of the path is important, as MetaUML uses it to determine the
149 type of adornment to attach to the link ends (if applicable). In our example, a white triangle,
150 denoting inheritance, points towards the end of the path, that is towards class {\code A}.
151
152 Let us sum up with a diagram typical for MetaUML use. Firstly, we define the objects that we want to include
153 in our diagram. Secondly, we position these objects relative to each other. Thirdly, we draw the objects. Finally, we
154 draw the links, by referencing the layout properties of the previously drawn objects. Note that in our example the
155 positioning of {\code A} need not be set explicitly because ``floating'' objects are automatically positioned at
156 {\code (0,0)} by their draw method.
157
158 \begin{multicols}{2}
159
160 \begin{verbatim}
161 input metauml;
162
163 beginfig(1);
164 Class.A("A")()(); % 1. Define the objects
165 Class.B("B")()();
166 B.w = A.e + (20, 0); % 2. Position the objects
167 drawObjects(A, B); % 3. Draw the objects
168 link(inheritance)(B.w -- A.e); % 4. Draw links between objects
169 endfig;
170 end
171 \end{verbatim}
172 \columnbreak
173 \includegraphics{fig/appetizer.9}
174 \end{multicols}
175
176 As far as a user is concerned, this is all there is to MetaUML. With a reference describing how the
177 UML elements are created, arbitrarily complex diagrams can be crafted.
178
179 \section{Class Diagrams}
180
181 A class is created as follows:
182
183 \begin{verbatim}
184 Class.<name>(<class-name>)
185 (<list-of-attributes>)
186 (<list-of-methods>);
187 \end{verbatim}
188
189 The suffix {\code <name>} specifies an identifier for the newly created {\code Class} object
190 (which, of course, represents a UML class).
191 The name of the UML class is a string given by {\code <class-name>};
192 the attributes and methods are given as list of strings, {\code <list-of-attributes>} and {\code <list-of-methods>}
193 respectively. The list of attributes and the list of methods may be void.
194
195 An attribute or a method string may begin with a visibility marker: ``$+$'' for
196 public, ``\#'' for protected, ``$-$'' for private, and ``\textasciitilde'' for package private.
197 The default visibility is package private.
198
199 \begin{multicols}{2}
200 \begin{verbatim}
201 Class.A("Point")
202 ("#x:int", "#y:int")
203 ("+set(x:int, y:int)",
204 "+getX():int",
205 "+getY():int",
206 "-debug():void",
207 "test():void");
208 drawObject(A);
209 \end{verbatim}
210 \columnbreak
211 \includegraphics{fig/class.1}
212 \end{multicols}
213
214 To disable showing the visibility markers, use {\code Class\_noVisibilityMarkers}, as shown below:
215
216 \begin{multicols}{2}
217 \begin{verbatim}
218 Class.A("Point")
219 ("#x:int", "#y:int")
220 ("+toString():String");
221 Class_noVisibilityMarkers.A;
222
223 drawObject(A);
224 \end{verbatim}
225 \columnbreak
226 \includegraphics{fig/class.15}
227 \end{multicols}
228
229 \subsection{Stereotypes}
230
231 After a class is created, but before it is drawn, its stereotypes may be specified by using {\code Class\_stereotypes}:
232
233 \begin{verbatim}
234 Class_stereotypes.<name>(<list-of-stereotypes>);
235 \end{verbatim}
236
237 Here, {\code <name>} is the object name of a previously created class and {\code <list-of-stereotypes>}
238 is a comma-separated list of strings. Here is an example:
239
240 \begin{multicols}{2}
241 \begin{verbatim}
242 Class.A("User")()();
243 Class_stereotypes.A("<<interface>>","<<home>>");
244
245 drawObject(A);
246 \end{verbatim}
247 \columnbreak
248 \hspace{3cm}\includegraphics{fig/class.2}
249 \end{multicols}
250
251 \subsection{Interfaces and Abstract Classes}
252
253 At times it is preferred to write the name of an interface in an oblique font, rather than using the ``interface''
254 stereotype. This can be easily achieved by using the macro {\code Interface}:
255
256 \begin{verbatim}
257 Interface.name(class-name)
258 (list-of-methods);
259 \end{verbatim}
260
261 Here is an example:
262
263 \begin{multicols}{2}
264 \begin{verbatim}
265 Interface.A("Observer")
266 ("+update(src:Object)");
267
268 drawObject(A);
269 \end{verbatim}
270 \columnbreak
271 \hspace{1cm}\includegraphics{fig/class.11}
272 \end{multicols}
273
274 Since internally {\code Interface} treated as a special kind of {\code Class}, the code above is equivalent to:
275 \begin{verbatim}
276 EClass.A(iInterface)("Observer")()
277 ("+update(src:Object)");
278 \end{verbatim}
279
280 Abstract classes can be drawn similarly using the {\code iAbstractClass} style:
281 \begin{samepage}
282 \begin{multicols}{2}
283 \begin{verbatim}
284 EClass.A(iAbstractClass)("Observable")
285 ("observers: Observer[0..*]")
286 ("+addObserver(o: Observer)",
287 "+notify()");
288
289 drawObject(A);
290 \end{verbatim}
291 \columnbreak
292 \hspace{1cm}\includegraphics{fig/class.12}
293 \end{multicols}
294 \end{samepage}
295
296 If you prefer, you can use equivalent construct:
297
298 \begin{verbatim}
299 AbstractClass.A("Observable")
300 ("observers: Observer[0..*]")
301 ("+addObserver(o: Observer)",
302 "+notify()");
303 \end{verbatim}
304
305 \subsection{Displaying Class Name Only}
306
307 If you want the empty methods and attributes compartments in a class not being displayed, one way is to set the spacing
308 at their top and the bottom to {\code 0}:
309 \begin{samepage}
310 \begin{multicols}{2}
311 \begin{verbatim}
312 Class.A("MyModel")()();
313 A.info.iName.top := 10;
314 A.info.iName.bottom := 10;
315 A.info.iAttributeStack.top := 0;
316 A.info.iAttributeStack.bottom := 0;
317 A.info.iMethodStack.top := 0;
318 A.info.iMethodStack.bottom := 0;
319
320 drawObject(A);
321 \end{verbatim}
322 \columnbreak
323 \hspace{1cm}\includegraphics{fig/class.13}
324 \end{multicols}
325 \end{samepage}
326
327 The same effect can be achieved by using the formatting information object {\code iClassNameOnly} or the {\code ClassName} macro:
328
329 \begin{multicols}{2}
330 \begin{verbatim}
331 EClass.A(iClassNameOnly)("MyModel")()();
332 ClassName.B("AnotherModel");
333 Class_stereotypes.B("<<smart>>");
334
335 topToBottom(20)(A, B);
336
337 drawObjects(A, B);
338 \end{verbatim}
339 \columnbreak
340 \hspace{2cm}\includegraphics{fig/class.14}
341 \end{multicols}
342
343 To customize the space around the class name globally, you can set the values of {\code iClassNameOnly.iName.top} and {\code iClassNameOnly.iName.bottom}. Individually, for a given object, say {\code B}, the attributes {\code B.info.iName.top} and {\code B.info.iName.bottom} can be used.
344
345 \subsection{Objects (or Class Instances)}
346
347 A UML object (or class instance) is created as follows:
348
349 \begin{verbatim}
350 Instance.name(object-name)
351 (list-of-attributes);
352 \end{verbatim}
353
354 The suffix {\code name} gives a name to the {\code Instance} object. The name of the object (given by {\code object-name}) is typeset underlined. The attributes are given as a comma-separated list of strings, {\code list-of-attributes}.
355
356 \begin{multicols}{2}
357 \begin{verbatim}
358 Instance.order("o: Order")
359 ("name='book'", "{placed}", "{payed}");
360 drawObject(order);
361 \end{verbatim}
362 \columnbreak
363 \hspace{2cm}\includegraphics{fig/instance.1}
364 \end{multicols}
365
366
367 \subsection{Parametrized Classes (Templates)}
368
369 The most convenient way of typesetting a class template in MetaUML is to use the macro {\code ClassTemplate}.
370 This macro creates a visual object which is appropriately positioned near the class object it adorns.
371
372 \begin{verbatim}
373 ClassTemplate.name(list-of-templates)
374 (class-object);
375 \end{verbatim}
376
377 The {\code name} is the name of the template object, {\code list-of-templates} is a comma-separated list of strings and the {\code class-object} is the name of a class object.
378
379 Here is an example:
380
381 \begin{multicols}{2}
382 \begin{verbatim}
383 Class.A("Vector")()();
384 ClassTemplate.T("T", "size: int")(A);
385
386 drawObjects(A, T);
387 \end{verbatim}
388 \columnbreak
389 \hspace{1cm}\includegraphics{fig/class.3}
390 \end{multicols}
391
392 The macro {\code Template} can also be used to create a template object, but this time the resulting
393 object can be positioned freely.
394
395 \begin{verbatim}
396 Template.name(list-of-templates);
397 \end{verbatim}
398
399 Of course, it is possible to specify both stereotypes and template parameters for a given class.
400
401 \subsection{Types of Links}
402
403 In this section we enumerate the relations that can be drawn between classes by means
404 of MetaUML macros. Suppose that we have the declared two points, {\code A} (on the left)
405 and {\code B} (on the right):
406
407 \begin{verbatim}
408 pair A, B;
409 A = (0,0);
410 B = (50,0);
411 \end{verbatim}
412
413 \begin{tabular}{||l|c||}
414 \hline
415 {\code link(association)(X.e -- Y.w)} & \includegraphics{fig/class_diagrams.4} \\
416 \hline
417 {\code link(associationUni)(X.e -- Y.w)} & \includegraphics{fig/class_diagrams.5} \\
418 \hline
419 {\code link(inheritance)(X.e -- Y.w)} & \includegraphics{fig/class_diagrams.6} \\
420 \hline
421 {\code link(realization)(X.e -- Y.w)} & \includegraphics{fig/class_diagrams.12} \\
422 \hline
423 {\code link(aggregation)(X.e -- Y.w)} & \includegraphics{fig/class_diagrams.7} \\
424 \hline
425 {\code link(aggregationUni)(X.e -- Y.w)} & \includegraphics{fig/class_diagrams.8} \\
426 \hline
427 {\code link(composition)(X.e -- Y.w)} & \includegraphics{fig/class_diagrams.9} \\
428 \hline
429 {\code link(compositionUni)(X.e -- Y.w)} & \includegraphics{fig/class_diagrams.10} \\
430 \hline
431 {\code link(dependency)(X.e -- Y.w)} & \includegraphics{fig/class_diagrams.11} \\
432 \hline
433 \end{tabular}
434
435 \subsection{Associations}
436 In UML an association typically has two of association ends and may have a name specified for it.
437 In turn, each association end may specify a multiplicity, a role, a visibility, an ordering.
438 These entities are treated in MetaUML as pictures having specific drawing information
439 (spacings, font).
440
441 The first method of creating association ``items'' is by giving them explicit names.
442 Having a name for an association item comes in handy when referring to its properties
443 is later needed (see the non UML-compliant diagram below). The last parameter of the macro {\code item} is an equation which may use the item's name to perform positioning.
444
445 \begin{multicols}{2}
446 \begin{verbatim}
447 Class.P("Person")()();
448 Class.C("Company")()();
449 % drawing code ommited
450
451 item.aName(iAssoc)("works for")
452 (aName.s = .5[P.w, C.w]);
453 draw aName.n -- (aName.n + (20,20));
454 label.urt("association name" infont "tyxtt",
455 aName.n + (20,20));
456 \end{verbatim}
457 \columnbreak
458 \hspace{1cm}\includegraphics[scale=.8]{fig/class_association.1}
459 \end{multicols}
460
461 However, giving names to every association item may become an annoying burden
462 (especially when there are many of them). Because of this, MetaUML also allows for
463 ``anonymous items''. In this case, the positioning is set by an equation
464 which refers to the anonymous item as {\code obj}.
465
466 \begin{multicols}{2}
467 \begin{verbatim}
468 % P and C defined as in the previous example
469
470 item(iAssoc)("employee")(obj.nw = P.s);
471 item(iAssoc)("1..*")(obj.ne = P.s);
472
473 % other items are drawn similarly
474 \end{verbatim}
475 \columnbreak
476 \hspace{3cm}\includegraphics{fig/class_association.2}
477 \end{multicols}
478
479 \subsection{Dependencies and Stereotypes}
480
481 Stereotypes are frequently used with dependencies. Below is an example.
482 \pagebreak
483
484 \begin{multicols}{2}
485 \begin{verbatim}
486 Class.F("Factory")()();
487 Class.O("Object")()();
488
489 O.n = F.s - (0, 50);
490 drawObjects(F, O);
491
492 clink(dependency)(F, O);
493 item(iStereo)("<<creates>>")(obj.w = .5[F.s,O.n])
494 \end{verbatim}
495 \columnbreak
496 \hspace{3cm}\includegraphics{fig/class_association.3}
497 \end{multicols}
498
499 \section{Notes}
500
501 A note is created as follows:
502
503 \begin{verbatim}
504 Note.name(list-of-lines);
505 \end{verbatim}
506
507 The suffix {\code name} is the name of the {\code Note} object. The contents of the note is given by a comma-separated
508 list of strings, {\code list-of-lines}, gives the text contents of the note object, each string being drawn on its own
509 line.
510
511 Here is an example:
512
513 \begin{multicols}{2}
514 \begin{verbatim}
515 Note.A("This note", "has two lines.");
516 drawObject(A);
517 \end{verbatim}
518 \columnbreak
519 \hspace{3cm}\includegraphics{fig/note.1}
520 \end{multicols}
521
522 \subsection{Attaching notes to diagram elements}
523
524 Notes can be attached to diagram elements by using a link of type {\code dashedLink}.
525
526 \begin{multicols}{2}
527 \begin{verbatim}
528 Note.A("This is a class");
529 Class.C("Object")()();
530
531 A.sw = C.ne + (20, 20);
532
533 drawObject(A, C);
534
535 clink(dashedLink)(A, C);
536 \end{verbatim}
537 \columnbreak
538 \hspace{1cm}\includegraphics{fig/note.2}
539 \end{multicols}
540
541 Now let us see a more complex example, which demontrates the ability of accessing sub-elements in a MetaUML diagram.
542 \pagebreak
543
544 \begin{multicols}{2}
545 \begin{verbatim}
546 Note.nA("This is the class name");
547 Note.nB("This is a key attribute");
548 Note.nC("This is a nice method");
549
550 Class.C("Object")("+id:int")
551 ("+clone()", "+serialize()");
552
553 topToBottom.left(10)(nA, nB, nC);
554 leftToRight(10)(C, nB);
555
556 drawObjects(C, nA, nB, nC);
557
558 clink(dashedLink)(C.namePict, nA);
559 clink(dashedLink)(C.attributeStack.pict[0], nB);
560 clink(dashedLink)(C.methodStack.pict[1], nC);
561 \end{verbatim}
562 \columnbreak
563 \hspace{1cm}\includegraphics{fig/note.3}
564 \end{multicols}
565
566 Macros like {\code leftToRight} and {\code topToBottom} are presented in section \ref{section:positioning}.
567
568 \subsection{Using mathematical formulae}
569
570 MetaUML notes can contain mathematical formulae written in TeX \cite{texbook}. Regretably, LaTeX \cite{latexbook} support for formulae is {\bf not} available.
571 Limited as it may be, this feature is considered experimental, as it is not always straightforward to use. In the example below, note that the MetaPost package {\code TEX} is imported.
572
573 \begin{multicols}{2}
574 \begin{verbatim}
575 input metauml;
576 input TEX;
577
578 beginfig(1);
579 Note.A("This class implements the formula:",
580 TEX("$\sum_1^n f(x) \cdot dx$"));
581 drawObjects(A);
582 endfig;
583
584 end
585 \end{verbatim}
586 \columnbreak
587 \hspace{0.5cm}\includegraphics{fig/note.4}
588 \end{multicols}
589
590 For taller formulae, you must be prepared to do some advanced stunts. Remark: {\code "aaa" \& "bbb"} is MetaPost's way to concatenate the strings into {\code "aaabbb"};
591 the string containing the formula was split in two for layout reasons.
592
593 \begin{multicols}{2}
594 \begin{verbatim}
595 Note.A("Can you do it?",
596 TEX("$\sum_1^n f(x) \cdot dx " &
597 "\over \sum_1^m g(y) \cdot dy$"));
598 A.stack.info.spacing := 30;
599 A.stack.pict[1].info.ignoreNegativeBase := 0;
600
601 drawObject(A);
602 \end{verbatim}
603 \columnbreak
604 \hspace{3cm}\includegraphics{fig/note.5}
605 \end{multicols}
606
607 Alas, this trick does not entirely solve the problem: a third line in the note would be badly aligned. Therefore,
608 until MetaUML's {\code Note} class is upgraded to better support this scenario, you may want to limit yourself
609 to two lines per note --- at least when tall formulae are involved.
610
611 \section{Packages}
612
613 MetaUML allows for the creation of packages in various forms. Firstly, we have the option of writing the package
614 name in the middle of the main box. Secondly, we can write the name on the tiny box above the main box, leaving
615 the main box empty. Lastly, we can write the package name as in the second case, but the main box can have an arbitrary
616 contents: classes, other packages, or even other UML items.
617
618 The macro that creates a package has the following synopsis:
619
620 \begin{verbatim}
621 Package.name(package-name)(subitems-list);
622 \end{verbatim}
623
624 The parameter {\code package-name} is a string or a list of comma-separated strings representing the package's name.
625 The {\code subitems-list} parameter is used to specify the subitems (tipically classes or packages) of this package;
626 its form is as a comma-separated list of objects, which can be void.
627
628 \begin{multicols}{2}
629 \begin{verbatim}
630 Package.P("java.lang")();
631 drawObject(P);
632 \end{verbatim}
633 \columnbreak
634 \hspace{3cm}\includegraphics{fig/package.1}
635 \end{multicols}
636
637 Below is another example:
638
639 \begin{multicols}{2}
640 \begin{verbatim}
641 Package.P("An important", "package")();
642 drawObject(P);
643 \end{verbatim}
644 \columnbreak
645 \hspace{3cm}\includegraphics{fig/package.2}
646 \end{multicols}
647
648 If you wish to leave the main box empty, you can use the following code:
649
650 \begin{multicols}{2}
651 \begin{verbatim}
652 Package.P("java.lang")();
653 P.info.forceEmptyContent := 1;
654 drawObject(P);
655 \end{verbatim}
656 \columnbreak
657 \hspace{3cm}\includegraphics{fig/package.3}
658 \end{multicols}
659
660 The same effect as above can be achieved globally by doing:
661
662 \begin{verbatim}
663 iPackage.forceEmptyContent := 1;
664 \end{verbatim}
665
666 More information on MetaUML's way of managing global and per-object configuration data can be found in
667 section \ref{section:infrastructure} and section \ref{section:customization}.
668
669 Here is an example involving items contained in a package.
670
671 \begin{multicols}{2}
672 \begin{verbatim}
673 Class.A("A")()();
674 Class.B("B")()();
675 Package.P("net.metauml")(A, B);
676
677 leftToRight(10)(A, B);
678
679 drawObject(P);
680 \end{verbatim}
681 \columnbreak
682 \hspace{3cm}\includegraphics{fig/package.4}
683 \end{multicols}
684
685 \subsection{Types of Links}
686
687 The nesting relation between packages is created by using the {\code nest} link information.
688
689 \begin{tabular}{||l|c||}
690 \hline
691 {\code link(nest)(X.e -- Y.w)} & \includegraphics{fig/package.5} \\
692 \hline
693 \end{tabular}
694
695 \section{Component Diagrams}
696
697 A component is created by the macro {\code Component}:
698
699 \begin{verbatim}
700 Component.name(component-name)
701 (subitems-list)
702 \end{verbatim}
703
704 The parameter {\code component-name} is a string representing the component's name. The {\code subitems-list} parameter
705 is used to specify the subitems of this component (possibly classes, packages or other components); its form is as a
706 comma-separated list of objects, which can be void.
707
708 \begin{multicols}{2}
709 \begin{verbatim}
710 Component.C("Business Logic")();
711 drawObject(C);
712 \end{verbatim}
713 \columnbreak
714 \hspace{3cm}\includegraphics{fig/component.1}
715 \end{multicols}
716
717 Here is an example involving subitems in a component:
718
719 \begin{multicols}{2}
720 \begin{verbatim}
721 Class.A("A")()();
722 Package.B("B")();
723 Component.C("C")();
724
725 Component.BigC("Big Component")(A, B, C);
726
727 leftToRight(10)(A, B);
728 topToBottom(10)(A, C);
729
730 drawObject(BigC);
731 \end{verbatim}
732 \columnbreak
733 \hspace{3cm}\includegraphics{fig/component.2}
734 \end{multicols}
735
736 \subsection{Types of Links}
737
738 \begin{tabular}{||l|c||}
739 \hline
740 {\code link(requiredInterface)( A.e -- .5[A.e, B.w] );} & \includegraphics{fig/component.3} \\
741 \hline
742 {\code link(providedInterface)( .5[A.e, B.w] -- B.w );} & \includegraphics{fig/component.4} \\
743 \hline
744 \end{tabular}
745
746 \vspace{0.5cm}
747
748 The {\code requiredInterface} and {\code providedInterface} visual constructs can be easily combined, as shown in the following example:
749
750 \begin{multicols}{2}
751 \begin{verbatim}
752 Component.A("A")();
753 Component.B("B")();
754
755 leftToRight(80)(A, B);
756
757 drawObjects(A, B);
758
759 link(providedInterface)( A.e -- .5[A.e, B.w] );
760 link(requiredInterface)( B.w -- .5[A.e, B.w] );
761 \end{verbatim}
762 \columnbreak
763 \hspace{-1cm}\includegraphics{fig/component.5}
764 \end{multicols}
765
766
767 \section{Use Case Diagrams}
768
769 \subsection{Use Cases}
770 An use case is created by the macro {\code Usecase}:
771
772 \begin{verbatim}
773 Usecase.name(list-of-lines);
774 \end{verbatim}
775
776 The {\code list-of-lines} is a comma-separated list of strings. These strings are placed
777 on top of each other, centered and surrounded by the appropriate visual UML notation.
778
779 Here is an use case example:
780
781 \begin{multicols}{2}
782 \begin{verbatim}
783 Usecase.U("Authenticate user",
784 "by name, password");
785 drawObject(U);
786 \end{verbatim}
787 \columnbreak
788 \hspace{1cm}\includegraphics{fig/usecase.1}
789 \end{multicols}
790
791 \subsection{Actors}
792
793 An actor is created by the macro {\code Actor}:
794
795 \begin{verbatim}
796 Actor.name(list-of-lines);
797 \end{verbatim}
798
799 Here, {\code list-of-lines} represents the actor's name. For convenience, the name may be
800 given as a list of strings which are placed on top of each other, to provide support for
801 the situations when the role is quite long. Otherwise, giving a single string
802 as an argument to the Actor constructor is perfectly fine.
803
804 Here is an actor example:
805
806 \begin{multicols}{2}
807 \begin{verbatim}
808 Actor.A("User");
809 drawObject(A);
810 \end{verbatim}
811 \columnbreak
812 \hspace{1cm}\includegraphics{fig/usecase.2}
813 \end{multicols}
814
815 Sometimes it may be preferable to draw diagram relations positioned relatively to
816 the visual representation of an actor (the ``human'') rather than relatively to the whole
817 actor object (which also includes the text). Because of that, MetaUML provides access
818 to the ``human'' of every actor object {\code actor} by means of the sub-object {\code actor.human}.
819
820 \begin{multicols}{2}
821 \begin{verbatim}
822 Actor.A("Administrator");
823 drawObject(A);
824 draw objectBox(A);
825 draw objectBox(A.human);
826 \end{verbatim}
827 \columnbreak
828 \hspace{1cm}\includegraphics{fig/usecase.3}
829 \end{multicols}
830
831 In MetaUML, {\code objectBox(X)} is equivalent to {\code X.nw -- X.ne -- X.se -- X.sw -- cycle} for every object {\code X}. {\code A.human} is considered a MetaUML object, so you can use expressions like {\code A.human.n} or {\code A.human.midx}.
832
833 \subsection{Types of Links}
834
835 Some of the types of links defined for class diagrams (such as inheritance, association etc.) can be used with similar semantics within use case diagrams.
836
837 \section{Activity Diagrams}
838
839 \subsection{Begin, End and Flow End}
840
841 The begin and the end of an activity diagram can be marked by using the macros {\code Begin}
842 and {\code End} or {\code FlowFinal}, respectively. The constructors of these visual objects take no parameters:
843
844 \begin{verbatim}
845 Begin.beginName;
846 End.endName;
847 \end{verbatim}
848
849 Below is an example:
850
851 \begin{multicols}{2}
852 \begin{verbatim}
853 Begin.b;
854 End.e;
855 FlowFinal.f;
856
857 leftToRight(20)(b, e, f);
858
859 drawObjects(b, e, f);
860 \end{verbatim}
861 \columnbreak
862 \hspace{1cm}\includegraphics{fig/activity.1}
863 \end{multicols}
864
865 \subsection{Activity}
866
867 An activity is constructed as follows:
868 \begin{verbatim}
869 Activity.name(list-of-strings);
870 \end{verbatim}
871
872 The parameter {\code list-of-strings} is a comma-separated list of strings. These strings are
873 centered on top of each other to allow for the accommodation of a longer activity description
874 within a reasonable space.
875
876 An example is given below:
877
878 \begin{multicols}{2}
879 \begin{verbatim}
880 Activity.A("Learn MetaUML -",
881 "the MetaPost UML library");
882 drawObject(A);
883 \end{verbatim}
884 \columnbreak
885 \hspace{1cm}\includegraphics{fig/activity.2}
886 \end{multicols}
887
888 \subsection{Fork and Join}
889
890 A fork or join is created by the macro:
891
892 \begin{verbatim}
893 Fork.name(type, length);
894 \end{verbatim}
895
896 The parameter {\code type} is a string and can be either of {\code "h"}, {\code "horiz"}, {\code "horizontal"}
897 for horizontal bars, and either of {\code "v"}, {\code "vert"}, {\code "vertical"} for vertical bars.
898 The {\code length} gives the bar's length.
899
900 \begin{multicols}{2}
901 \begin{verbatim}
902 Fork.forkA("h", 100);
903 Fork.forkB("v", 20);
904
905 leftToRight(10)(forkA, forkB);
906
907 drawObject(forkA, forkB);
908 \end{verbatim}
909 \columnbreak
910 \hspace{1cm}\includegraphics{fig/activity.3}
911 \end{multicols}
912
913 \subsection{Branch}
914
915 A branch is created by the macro:
916
917 \begin{verbatim}
918 Branch.name;
919 \end{verbatim}
920
921 Here is an example:
922
923 \begin{multicols}{2}
924 \begin{verbatim}
925 Branch.testA;
926
927 drawObject(testA);
928 \end{verbatim}
929 \columnbreak
930 \hspace{1cm}\includegraphics{fig/activity.4}
931 \end{multicols}
932
933
934 \subsection{Types of Links}
935
936 In activity diagrams, transitions between activities are needed. They are typeset
937 as in the example below. In section \ref{composite-states} such a transition
938 is showed. This type of link is also used for state machine diagrams.
939
940 \begin{verbatim}
941 link(transition)( pointA -- pointB );
942 \end{verbatim}
943
944 \section{State Diagrams}
945
946 The constructor of a state allows for aggregated sub-states:
947
948 \begin{verbatim}
949 State.name(state-name)(substates-list);
950 \end{verbatim}
951
952 The parameter {\code state-name} is a string or a list of comma-separated strings representing
953 the state's name or description. The {\code substates-list} parameter is used to specify
954 the substates of this state as a comma-separated list of objects; this list may be void.
955
956 An example of a simple state:
957
958 \begin{multicols}{2}
959 \begin{verbatim}
960 State.s("Take order")();
961 drawObject(s);
962 \end{verbatim}
963 \columnbreak
964 \hspace{1cm}\includegraphics{fig/state.1}
965 \end{multicols}
966
967
968 \subsection{Composite States}
969 \label{composite-states}
970
971 A composite state is defined by enumerating at the end of its constructor the inner
972 states. Interestingly enough, the composite state takes care of drawing the sub-states it
973 contains. The transitions must be drawn after the composite state, as seen in the
974 next example:
975
976 \begin{multicols}{2}
977 \begin{verbatim}
978 Begin.b;
979 End.e;
980 State.c("Component")();
981 State.composite("Composite")(b, e, c);
982
983 b.midx = e.midx = c.midx;
984 c.top = b.bottom - 20;
985 e.top = c.bottom - 20;
986
987 composite.info.drawNameLine := 1;
988 drawObject(composite);
989
990 link(transition)(b.s -- c.n);
991 link(transition)(c.s -- e.n);
992 \end{verbatim}
993 \columnbreak
994 \hspace{1cm}\includegraphics{fig/state.2}
995 \end{multicols}
996
997 \subsection{Internal Transitions}
998
999 Internal transitions can be specified by using the macro:
1000 \begin{verbatim}
1001 stateTransitions.name(list-transitions);
1002 \end{verbatim}
1003
1004 Identifier {\code name} gives the state object whose internal transitions are being set,
1005 and parameter {\code list-transitions} is a comma-separated string list.
1006
1007
1008 An example is given below:
1009
1010 \begin{multicols}{2}
1011 \begin{verbatim}
1012 State.s("An interesting state",
1013 "which is worth mentioning")();
1014 stateTransitions.s(
1015 "OnEntry / Open eyes",
1016 "OnExit / Sleep well");
1017 s.info.drawNameLine := 1;
1018
1019 drawObject(s);
1020 \end{verbatim}
1021 \columnbreak
1022 \hspace{1cm}\includegraphics{fig/state.3}
1023 \end{multicols}
1024
1025 \subsection{Special States}
1026
1027 Similarly to the usage of {\code Begin} and {\code End} macros, one can define history states,
1028 exit/entry point states and terminate pseudo-states, by using the following constructors.
1029
1030 \begin{verbatim}
1031 History.nameA;
1032 ExitPoint.nameB;
1033 EntryPoint.nameC;
1034 Terminate.nameD;
1035 \end{verbatim}
1036
1037 \section{Drawing Paths}
1038
1039 The {\code link} macro is powerful enough to draw relations following arbitrary paths:
1040
1041 \begin{multicols}{2}
1042 \begin{verbatim}
1043 path cool;
1044 cool := A.e .. A.e+(20,10) ..
1045 B.s+(20,-40) .. B.s+(-10,-30)
1046 -- B.s;
1047 link(inheritance)(cool);
1048
1049 link(aggregationUni)
1050 (A.n ..(30,30)..B.w);
1051 \end{verbatim}
1052 \columnbreak
1053 \hspace{1cm}\includegraphics{fig/paths.1}
1054 \end{multicols}
1055
1056 Amusing as it may be, this feature gets old soon. When typesetting UML diagrams in good style, rectangular paths are usually preferred.
1057 It is for this kind of paths that MetaUML offers extensive support, by means of ``syntactic sugar'' constructs which
1058 are not only self-documenting, but reduce the amount of typing and thinking required.
1059
1060 \subsection{Manhattan Paths}
1061
1062 The ``Manhattan'' path macros generate a path between two points consisting of one
1063 horizontal and one vertical segment. The macro {\code pathManhattanX} generates first a
1064 horizontal segment, while the macro {\code pathManhattanY} generates first a
1065 vertical segment. In MetaUML it also matters the direction of a path, so you
1066 can choose to reverse it by using {\code rpathManhattanX} and {\code rpathManhattanY}
1067 (note the prefix ``{\code r}''):
1068
1069 \begin{verbatim}
1070 pathManhattanX(A, B)
1071 pathManhattanY(A, B)
1072
1073 rpathManhattanX(A, B)
1074 rpathManhattanY(A, B)
1075 \end{verbatim}
1076
1077 \pagebreak
1078 Here is an example:
1079
1080 \begin{multicols}{2}
1081 \begin{verbatim}
1082 Class.A("A")()();
1083 Class.B("B")()();
1084
1085 B.sw = A.ne + (10,10);
1086 drawObjects(A, B);
1087
1088 link(aggregationUni)
1089 (rpathManhattanX(A.e, B.s));
1090 link(inheritance)
1091 (pathManhattanY(A.n, B.w));
1092 \end{verbatim}
1093 \columnbreak
1094 \hspace{1cm}\includegraphics{fig/paths.2}
1095 \end{multicols}
1096
1097 \subsection{Stair Step Paths}
1098
1099 These path macros generate stair-like paths between two points.
1100 The ``stair'' can ``rise'' first in the direction of $Ox$ axis ({\code pathStepX})
1101 or in the direction of $Oy$ axis ({\code pathStepY}). How much should a step
1102 rise is given by an additional parameter, {\code delta}. Again, the macros
1103 prefixed with ``{\code r}'' reverse the direction of the path given by their
1104 unprefixed counterparts.
1105
1106 \begin{verbatim}
1107 pathStepX(A, B, delta)
1108 pathStepY(A, B, delta)
1109
1110 rpathStepX(A, B, delta)
1111 rpathStepY(A, B, delta)
1112 \end{verbatim}
1113
1114 Here is an example:
1115
1116 \begin{multicols}{2}
1117 \begin{verbatim}
1118 stepX:=60;
1119 link(aggregationUni)
1120 (pathStepX(A.e, B.e, stepX));
1121
1122 stepY:=20;
1123 link(inheritance)
1124 (pathStepY(B.n, A.n, stepY));
1125 \end{verbatim}
1126 \columnbreak
1127 \hspace{1cm}\includegraphics{fig/paths.3}
1128 \end{multicols}
1129
1130 \subsection{Horizontal and Vertical Paths}
1131
1132 There are times when drawing horizontal or vertical links is required,
1133 even when the objects are not properly aligned. To this aim, the following macros
1134 are useful:
1135
1136 \begin{verbatim}
1137 pathHorizontal(pA, untilX)
1138 pathVertical(pA, untilY)
1139
1140 rpathHorizontal(pA, untilX)
1141 rpathVertical(pA, untilY)
1142 \end{verbatim}
1143
1144 A path created by {\code pathHorizonal} starts from the point {\code pA}
1145 and continues horizontally until coordinate {\code untilX} is reached. The macro
1146 {\code pathVertical} constructs the path dually, working vertically.
1147 The prefix ``{\code r}'' reverses the direction of the path.
1148
1149 Usage example:
1150
1151 \begin{multicols}{2}
1152 \begin{verbatim}
1153 untilX := B.left;
1154 link(association)
1155 (pathHorizontal(A.e, untilX));
1156
1157 untilY:= C.bottom;
1158 link(association)
1159 (pathVertical(A.n, untilY));
1160 \end{verbatim}
1161 \columnbreak
1162 \hspace{1cm}\includegraphics{fig/paths.4}
1163 \end{multicols}
1164
1165 \subsection{Direct Paths}
1166
1167 A direct path can be created with {\code directPath}. The call {\code directPath(A, B)}
1168 is equivalent to {\code A -{}- B}.
1169
1170 \subsection{Paths between Objects}
1171
1172 Using the constructs presented above, links between diagram objects are drawn easily like this:
1173
1174 \begin{verbatim}
1175 link(transition)(directPath(objA.nw, objB.se));
1176 \end{verbatim}
1177
1178 There are times however when this direct approach may yield unsatisfactory visual results,
1179 especially when the object's corners is round. To tackle these situations, MetaUML provides the macro
1180 {\code pathCut}, whose aim is to limit a given path exactly to the region outside the actual
1181 borders of the objects it connects. The macro's synopsis is:
1182
1183 \begin{verbatim}
1184 pathCut(thePath)(objectA, objectB)
1185 \end{verbatim}
1186
1187 Here, {\code thePath} is a given MetaPost path and {\code objectA} and {\code objectB}
1188 are two MetaUML objects. By contract, each MetaUML object of type, say, {\code X}
1189 defines a macro {\code X\_border} which returns the path that surrounds it. Because
1190 of that, {\code pathCut} can make the appropriate modifications to {\code thePath}.
1191
1192 The following code demonstrates the benefits of the {\code pathCut} macro:
1193
1194 \begin{multicols}{2}
1195 \begin{verbatim}
1196 z = A.se + (30, -10);
1197 link(transition)
1198 (pathCut(A, B)(A.c--z--B.c));
1199 \end{verbatim}
1200 \columnbreak
1201 \hspace{1cm}\includegraphics{fig/paths.5}
1202 \end{multicols}
1203
1204 \subsubsection{Direct Paths between Centers}
1205
1206 At times is quicker to just draw direct paths between the center of two objects,
1207 minding of course the object margins. The macro which does this is {\code clink}:
1208
1209 \begin{verbatim}
1210 clink(how-to-draw-information)(objA, objB);
1211 \end{verbatim}
1212
1213 The parameter {\code how-to-draw-information} is the same as for the macro {\code link};
1214 {\code objA} and {\code objB} are two MetaUML objects.
1215
1216 Below is an example which involves the inheritance relation:
1217
1218 \begin{multicols}{2}
1219 \begin{verbatim}
1220 clink(inheritance)(A, B);
1221 \end{verbatim}
1222 \columnbreak
1223 \hspace{1cm}\includegraphics{fig/paths.6}
1224 \end{multicols}
1225
1226 \section{Arranging Diagram Items}
1227 \label{section:positioning}
1228
1229 Using equations involving cardinal points, such as {\code A.nw = B.ne + (10,0)}, is
1230 good enough for achieving the desired results. However, programs are best to
1231 be written for human audience, rather than for compilers. It does become a bit
1232 tiresome to think all the time of cardinal points and figure out the
1233 direction of positive or negative offsets. Because of that, MetaUML offers
1234 syntactic sugar which allows for an easier understanding of the intent behind
1235 the positioning code.
1236
1237 Suppose that we have three classes, {\code A}, {\code B}, {\code C} and their base class
1238 {\code Base}. We want the base class to be at the top, and the derived classes to be
1239 on a line below. This code will do:
1240
1241 \begin{verbatim}
1242 A.ne = B.nw + (20,0);
1243 B.ne = C.nw + (20,0);
1244 Base.s = B.n + (0,-20);
1245 \end{verbatim}
1246
1247 Unfortunately, writing code such as this makes it hard for fellow programmers to visualize
1248 its intent upon reading it. And ``fellow programmers`` include the author, five minutes later.
1249
1250 Perhaps the next version of the code will drive home the point. The outcome is
1251 the same as before, but the layout is stated in a more human-friendly way. You might even
1252 infer by yourself that the numeric argument represents the distance between the objects.
1253
1254 \begin{multicols}{2}
1255 \begin{verbatim}
1256 leftToRight(20)(A, B, C);
1257 topToBottom(20)(Base, B);
1258 \end{verbatim}
1259 \columnbreak
1260 \hspace{1cm}\includegraphics{fig/positioning.2}
1261 \end{multicols}
1262
1263 Below there are examples which show how these macros can be used. Suppose that we have the
1264 following definitions for objects {\code X}, {\code Y}, and {\code Z}; also, let's assume
1265 that {\code spacing} is a numeric variable set to {\code 5}.
1266
1267 \begin{verbatim}
1268 Picture.X("a");
1269 Picture.Y("...");
1270 Picture.Z("Cyan");
1271 \end{verbatim}
1272
1273 \begin{tabular}{||l|c||}
1274 \hline
1275 {\code leftToRight.top(spacing)(X, Y, Z);} & \includegraphics{fig/positioning.3} \\
1276 \hline
1277 {\code leftToRight.midy(spacing)(X, Y, Z);} & \includegraphics{fig/positioning.4} \\
1278 \hline
1279 {\code leftToRight.bottom(spacing)(X, Y, Z);} & \includegraphics{fig/positioning.5} \\
1280 \hline
1281 {\code topToBottom.left(spacing)(X, Y, Z);} & \includegraphics{fig/positioning.6} \\
1282 \hline
1283 {\code topToBottom.midx(spacing)(X, Y, Z);} & \includegraphics{fig/positioning.7} \\
1284 \hline
1285 {\code topToBottom.right(spacing)(X, Y, Z);} & \includegraphics{fig/positioning.8} \\
1286 \hline
1287 \end{tabular} \\
1288
1289 To make things even easier, the following equivalent contructs are also allowed:
1290
1291 \begin{verbatim}
1292 leftToRight.midy(spacing)(X, Y, Z);
1293 leftToRight(spacing)(X, Y, Z);
1294 \end{verbatim}
1295
1296 \begin{verbatim}
1297 topToBottom.midx(spacing)(X, Y, Z);
1298 topToBottom(spacing)(X, Y, Z);
1299 \end{verbatim}
1300
1301 If you want to specify that some objects have a given property equal, while the distance between them is given elsewhere, you can use the macro {\code same}.
1302 This macro accepts a variable number of parameters, but at least two. The following table gives the interpretation of the macro for a simple example.
1303
1304 \begin{tabular}{||l|l||}
1305 \hline
1306 {\code same.top(X, Y, Z);} & {\code X.top = Y.top = Z.top;} \\
1307 \hline
1308 {\code same.midy(X, Y, Z);} & {\code X.midy = Y.midy = Z.midy;} \\
1309 \hline
1310 {\code same.bottom(X, Y, Z);} & {\code X.bottom = Y.bottom = Z.bottom;} \\
1311 \hline
1312 {\code same.left(X, Y, Z);} & {\code X.left = Y.left = Z.left;} \\
1313 \hline
1314 {\code same.midx(X, Y, Z);} & {\code X.midx = Y.midx = Z.midx;} \\
1315 \hline
1316 {\code same.right(X, Y, Z);} & {\code X.right = Y.right = Z.right;} \\
1317 \hline
1318 \end{tabular} \\
1319
1320 Relative positions of two points can be declared more easily using the macros {\code below}, {\code above}, {\code atright}, {\code atleft}.
1321 Let us assume that {\code A} and {\code B} are two points (objects of type {\code pair} in MetaPost). The following constructs are equivalent:
1322
1323 \begin{tabular}{||l|l||}
1324 \hline
1325 {\code B = A + (5,0);} & {\code B = atright(A, 5);} \\
1326 {\code B = A - (5,0);} & {\code B = atleft(A, 5);} \\
1327 {\code B = A + (0,5);} & {\code B = above(A, 5);} \\
1328 {\code B = A - (0,5);} & {\code B = below(A, 5);} \\
1329 \hline
1330 \end{tabular}
1331
1332
1333 \section{The MetaUML Infrastructure}
1334 \label{section:infrastructure}
1335
1336 MetaPost is a macro language based on equation solving. Using it may seem quite
1337 tricky at first for a programmer accustomed to modern object-oriented languages.
1338 However, the great power of MetaPost consists in its versatility. Indeed, it is possible to write
1339 a system which mimics quite well object-oriented behavior. Along this line, METAOBJ
1340 \cite{metaobj} is a library worth mentioning: it provides a high-level objects
1341 infrastructure along with a battery of predefined objects.
1342
1343 Surprisingly enough, MetaUML does not use METAOBJ. Instead, it uses a custom written,
1344 lightweight object-oriented infrastructure, provisionally called ``{\code util}''.
1345 METAOBJ's facilities, although impressive, were perceived by me as being a bit too much
1346 for what was initially intented as a quick way of getting some UML diagrams layed out.
1347 Inspired by METAOBJ, ``{\code util}'' was designed to fulfill with minimal effort
1348 the specific tasks needed to confortably position, allign or group visual objects
1349 which include text.
1350
1351 Another library having some object-oriented traits is the {\code boxes}
1352 library, which comes with the standard MetaPost distribution. Early versions of
1353 MetaUML did use {\code boxes} as an infrastructure, but this approach had to be abandoned eventually.
1354 The main reason was that it was difficult to achieve good visual results when stacking texts
1355 (more on that further on). For all it's worth, it did not fit well with the way in which MetaUML's
1356 layout mechanism was shaping up at the time.
1357
1358 \subsection{Motivation}
1359
1360 Suppose that we want to typeset two texts with their bottom lines aligned, using {\code boxit}:
1361
1362 \begin{multicols}{2}
1363 \begin{verbatim}
1364 boxit.a ("yummy");
1365 boxit.b ("cool");
1366
1367 a.nw = (0,0); b.sw = a.se + (10,0);
1368
1369 drawboxed (a, b); % or drawunboxed(a,b)
1370 draw a.sw -- b.se dashed evenly
1371 withpen pencircle scaled 1.1;
1372 \end{verbatim}
1373 \columnbreak
1374 \hspace{1cm}\includegraphics{fig/boxes_vs_util.1}
1375 \end{multicols}
1376
1377 Note that, despite supposedly having their bottom lines alligned,
1378 ``yummy'' {\it looks} slightly higher than ``cool''. This would be unacceptable
1379 in a UML class diagram, when roles are placed at the ends of a horizontal association.
1380 Regardless of the default spacing being smaller in the {\code util} library,
1381 the very same unfortunate misalignment effect rears its ugly head:
1382
1383 \begin{multicols}{2}
1384 \begin{verbatim}
1385 Picture.a("yummy");
1386 Picture.b("cool");
1387 % comment next line for unboxed
1388 a.info.boxed := b.info.boxed := 1;
1389
1390 b.sw = a.se + (10,0);
1391
1392 drawObjects(a, b);
1393 \end{verbatim}
1394 \columnbreak
1395 \hspace{1cm}\includegraphics{fig/boxes_vs_util.2}
1396 \end{multicols}
1397
1398 However, the strong point of {\code util} is that we have a recourse to this problem:
1399
1400 \begin{multicols}{2}
1401 \begin{verbatim}
1402 iPict.ignoreNegativeBase := 1;
1403
1404 Picture.a("yummy");
1405 Picture.b("cool");
1406 % the rest the same as above
1407 drawObjects(a, b);
1408 \end{verbatim}
1409 \columnbreak
1410 \hspace{1cm}\includegraphics{fig/boxes_vs_util.3}
1411 \end{multicols}
1412
1413 \subsection{The Picture Macro}
1414
1415 We have seen previously the line {\code iPict.ignoreNegativeBase := 1}.
1416 Who is {\code iPict} and what is it doing in our program? MetaUML
1417 aims at separating the ``business logic'' (what to draw) from the
1418 ``interface'' (how to draw). In order to achieve this, it records the ``how to draw''
1419 information within the so-called {\code Info} structures. The object {\code iPict}
1420 is an instance of {\code PictureInfo} structure, which has the following properties
1421 (or attributes):
1422 \begin{verbatim}
1423 left, right, top, bottom
1424 ignoreNegativeBase
1425 boxed, borderColor
1426 \end{verbatim}
1427
1428 The first four attributes specify how much space should be left around the
1429 actual item to be drawn. The marvelous effect of {\code ignoreNegativeBase}
1430 has just been shown (off), while the last two attributes control whether the border
1431 should be drawn (when {\code boxed=1}) and if drawn, in which color.
1432
1433 There's one more thing: the font to typeset the text in. This is specified
1434 in a {\code FontInfo} structure which has two attributes: the font name
1435 and the font scale. This information is kept within the {\code PictureInfo} structure
1436 as a contained attribute {\code iFont}. Both {\code FontInfo} and {\code PictureInfo}
1437 have ``copy constructors'' which can be used to make copies. We have already
1438 the effect of these copy constructors at work, when we used:
1439
1440 \begin{verbatim}
1441 Picture.a("yummy");
1442 a.info.boxed := 1;
1443 \end{verbatim}
1444
1445 A copy of the default info for a picture, {\code iPict}, has been made within
1446 the object {\code a} and can be accessed as {\code a.info}. Having a copy of the
1447 info in each object may seem like an overkill, but it allows for a fine grained
1448 control of the drawing mode of each individual object. This feature comes in very
1449 handy when working with a large number of settings, as it is the case for MetaUML.
1450
1451 Let us imagine for a moment that we have two types of text to write: one with a small font
1452 and a small margin and one with a big font and a big margin. We could in theory
1453 configure each individual object or set back and forth global parameters, but
1454 this is far for convenient. It is preferable to have two sets of settings and specify
1455 them explicitly when they are needed. The following code could be placed somewhere
1456 in a configuration file and loaded before any {\code beginfig} macro:
1457 \begin{verbatim}
1458 PictureInfoCopy.iBig(iPict);
1459 iBig.left := iBig.right := 20;
1460 iBig.top := 10;
1461 iBig.bottom := 1;
1462 iBig.boxed := 1;
1463 iBig.ignoreNegativeBase := 1;
1464 iBig.iFont.name := defaultfont;
1465 iBig.iFont.scale := 3;
1466
1467 PictureInfoCopy.iSmall(iPict);
1468 iSmall.boxed := 1;
1469 iSmall.borderColor := green;
1470 \end{verbatim}
1471
1472 Below is an usage example of these definitions. Note the name of the macro: {\code EPicture}.
1473 The prefix comes form ``explicit'' and it's used to acknowledge that the
1474 ``how to draw'' information is given explicitly --- as a parameter,
1475 rather than defaulted to what's recorded in {\code iPict}, as with the {\code Picture} macro.
1476 Having predefined configurations yields short, convenient code.
1477
1478 \begin{multicols}{2}
1479 \begin{verbatim}
1480 EPicture.a(iBig)("yummy");
1481 EPicture.b(iSmall)("cool");
1482 % you can still modify a.info, b.info
1483
1484 b.sw = a.se + (10,0);
1485
1486 drawObjects(a, b);
1487 \end{verbatim}
1488 \columnbreak
1489 \hspace{1cm}\includegraphics{fig/picture_info.1}
1490 \end{multicols}
1491
1492 \subsubsection{Fixed Sizes}
1493
1494 By default, the size of a {\code Picture} object is set by its contents. However,
1495 it is possible to specify fixed dimensions both the width and the height, independently.
1496 This can be done by setting the {\code info}'s attributes {\code fixedWidth} and {\code fixedHeight} to values
1497 greater than 0. If any of these attributes is left to its default value, {\code -1}, then for the corresponding
1498 axis the dimension is set according to the dimension of the content. Nevertheless, the fixed dimensions are enforced, even though the contained object would have needed additional space.
1499
1500 \begin{multicols}{2}
1501 \begin{verbatim}
1502 PictureInfoCopy.myFixed(iPict);
1503 myFixed.ignoreNegativeBase := 1;
1504 myFixed.fixedWidth := 15;
1505 myFixed.fixedHeight := 20;
1506 myFixed.boxed := 1;
1507
1508 EPicture.a(myFixed)("a");
1509 EPicture.b(myFixed)(".-.");
1510 EPicture.c(myFixed)("toolong");
1511
1512 leftToRight.bottom(10)(a, b, c);
1513
1514 drawObjects(a, b, c);
1515 \end{verbatim}
1516 \columnbreak
1517 \hspace{1cm}\includegraphics{fig/picture_info.2}
1518 \end{multicols}
1519
1520 \subsubsection{Content alignment}
1521
1522 When fixed dimensions are used, one most likely would prefer a centered alignement of the contents in the
1523 {\code Picture} box. This option can be expressed independently for each of the axes,
1524 by setting the {\code info}'s attributes {\code valign} and {\code halign} to descriptive string values.
1525 For horizontal alignement, {\code halign} can be set to {\code "left"} or {\code "center"}, and for
1526 vertical alignement, {\code valign} can be set to {\code "bottom} or {\code "center"}. The default
1527 values for these attributes are {\code "left"} and {\code "bottom"}, respectively.
1528
1529 The next example uses horizontal centered alignement and a bottom alignement with a {\code 4.5} base offset, for
1530 vertical alignement. This vertical alignement gives a better visual result than the centered one, at
1531 least for the situations in which there are texts to be placed horizontally.
1532
1533 \begin{multicols}{2}
1534 \begin{verbatim}
1535 PictureInfoCopy.myFixed(iPict);
1536 myFixed.ignoreNegativeBase := 1;
1537 myFixed.bottom := 4.5;
1538 myFixed.valign := "bottom";
1539 myFixed.halign := "center";
1540 myFixed.fixedWidth := 25;
1541 myFixed.fixedHeight := 15;
1542 myFixed.boxed := 1;
1543
1544 EPicture.a(myFixed)("a");
1545 EPicture.b(myFixed)("yum");
1546 EPicture.c(myFixed)("b");
1547
1548 leftToRight.bottom(10)(a, b, c);
1549
1550 drawObjects(a, b, c);
1551 \end{verbatim}
1552 \columnbreak
1553 \hspace{1cm}\includegraphics{fig/picture_info.3}
1554 \end{multicols}
1555
1556 \subsection{Stacking Objects}
1557
1558 It is possible to stack objects, much in the style of {\code setboxjoin}
1559 from {\code boxes} library.
1560
1561 \begin{multicols}{2}
1562 \begin{verbatim}
1563 Picture.a0("yummy");
1564 Picture.a1("cool");
1565 Picture.a2("fool");
1566
1567 setObjectJoin(pa.sw = pb.nw);
1568 joinObjects(scantokens listArray(a)(3));
1569
1570 drawObjects(scantokens listArray(a)(3));
1571 % or drawObjects (a0, a1, a2);
1572 \end{verbatim}
1573 \columnbreak
1574 \hspace{1cm}\includegraphics{fig/object_stack.1}
1575 \end{multicols}
1576
1577 The {\code listArray} macro provides here a shortcut for writing
1578 {\code a0, a1, a2}. This macro is particularly useful for generic
1579 code which does not know beforehand the number of elements to be drawn.
1580 Having to write the {\code scantokens} keyword is admittedly a nuisance, but
1581 this is required.
1582
1583
1584 \subsection{The Group Macro}
1585
1586 It is possible to group objects in MetaUML. This feature is the cornerstone
1587 of MetaUML, allowing for the easy development of complex objects, such as
1588 composite stats in state machine diagrams.
1589
1590 Similarly to the macro {\code Picture}, the structure {\code GroupInfo}
1591 is used for specifying group properties; its default instantiation is
1592 {\code iGroup}. Furthermore, the macro {\code EGroup} explicitely sets the
1593 layout information.
1594
1595 Here is an example:
1596
1597 \begin{multicols}{2}
1598 \begin{verbatim}
1599 iGroup.left:=20;
1600 iGroup.right:=15;
1601 iGroup.boxed:=1;
1602 iPicture.boxed:=1;
1603
1604 Picture.a("yummy");
1605 Picture.b("cool");
1606 Picture.c("fool");
1607
1608 b.nw = a.nw + (20,20); % A
1609 c.nw = a.nw + (15, 40); % B
1610
1611 Group.g(a, b, c);
1612 g.nw = (10,10); % C
1613
1614 drawObject(g);
1615 \end{verbatim}
1616 \columnbreak
1617 \hspace{1cm}\includegraphics{fig/group.1}
1618 \end{multicols}
1619
1620 After some objects are grouped, they can only be drawn
1621 by invoking the {\code drawObject} macro on the group that aggregates them, and not individually.
1622 Conveniently, once the relative positioning of objects within a group is set (line A and B), the whole
1623 group can be ``moved'' do the desired position (line C), and all the contained objects will move along.
1624
1625 \subsection{The PictureStack Macro}
1626
1627 The {\code PictureStack} macro is a syntactic sugar for a set of pictures,
1628 stacked according to predefined equations and grouped together.
1629
1630 \begin{multicols}{2}
1631 \begin{verbatim}
1632 iStack.boxed := 1;
1633 iStack.iPict.boxed := 1;
1634 PictureStack.myStack("foo",
1635 "bar: int" infont "tyxtt",
1636 "nicely-centered" infont defaultfont,
1637 "nice")("vcenter");
1638
1639 drawObject(myStack);
1640 \end{verbatim}
1641 \columnbreak
1642 \hspace{1cm}\includegraphics{fig/picture_stack.1}
1643 \end{multicols}
1644
1645 Note the last parameter of the macro {\code PictureStack}, here {\code vcenter}.
1646 It is used to generate appropriate equations based on a descriptive name.
1647 The spacing between individual picture objects is set by the field
1648 {\code iStack.spacing}. Currently, the following alignment names are
1649 defined: {\code vleft}, {\code vright}, {\code vcenter},
1650 {\code vleftbase}, {\code vrightbase}, {\code vcenterbase}. All these
1651 names refer to vertical alignment (the prefix ``{\code v}''); alignment can
1652 be at left, right or centered. The variants having the suffix ``{\code base}'' align
1653 the pictures so that {\code iStack.spacing} refer to the distance between the
1654 bottom lines of the pictures. The unsuffixed variants use {\code iStack.spacing} as
1655 the distance between one's bottom line and the next's top line.
1656
1657 The ``{\code base}'' alignment is particularly useful for stacking text, since it
1658 offers better visual appearance when {\code iPict.ignoreNegativeBase} is set to {\code 1}.
1659
1660 \section{Components Design}
1661
1662 Each MetaUML component (e.g. {\code Picture}, {\code PictureStack}, {\code Class}) is
1663 designed according to an established pattern. This section gives more insight
1664 on this.
1665
1666 In order to draw a component, MetaUML categorizes the required information as follows:
1667 \begin{itemize}
1668 \item what to draw, or what are the elements of a component.
1669 \item how to draw, or how are the elements positioned in relation to each other within the component
1670 \item where to draw
1671 \end{itemize}
1672
1673 For example, in order to draw a picture object we must know, respectively:
1674 \begin{itemize}
1675 \item what is the text or the native picture that needs to be drawn
1676 \item what are the margins that should be left around the contents
1677 \item where is the picture to be drawn
1678 \end{itemize}
1679
1680 Why do we bother with these questions? Why don't we just simply draw the picture
1681 component as soon as it was created and get it over with?
1682 That is, why doesn't the following code just work?
1683
1684 \begin{verbatim}
1685 Picture.pict("foo");
1686 \end{verbatim}
1687
1688 Well, although we have the answer to question 1 (what to draw),
1689 we still need to have question 3 answered. The code below becomes thus a
1690 necessity (actually, you are not forced to specify the positioning of an object,
1691 because its draw method positions it to {\code (0,0)} by default):
1692
1693 \begin{verbatim}
1694 % question 1: what to draw
1695 Picture.pict("foo");
1696
1697 % question 3: where to draw
1698 pict.nw = (10,10);
1699
1700 % now we can draw
1701 drawObject(pict);
1702 \end{verbatim}
1703
1704 How about question 2, how to draw? By default, this problem is addressed behind the
1705 scenes by the component. This means, for the Picture object, that a native picture is created
1706 from the given string, and around that picture certain margins are placed, by means of MetaPost equations.
1707 (The margins also come in handy when stacking Picture objects, so that the result doesn't look too cluttered.)
1708 If these equations were defined within the Picture constructor, then an
1709 usability problem would have appeared, because it wouldn't have been possible to modify the margins,
1710 as in the code below:
1711
1712 \begin{verbatim}
1713 % question 1: what to draw
1714 Picture.pict("foo");
1715
1716 % question 2: how to draw
1717 pict.info.left := 10;
1718 pict.info.boxed := 1;
1719
1720 % question 3: where to draw
1721 pict.nw = (0,0);
1722
1723 % now we can draw
1724 drawObject(pict);
1725 \end{verbatim}
1726
1727 To allow for this type of code, the equations that define the layout of the {\code Picture} object (here, what the margins are)
1728 must be defined somewhere after the constructor. This is done by a macro called {\code Picture\_layout}.
1729 This macro defines all the equations which link the ``what to draw'' information to the ``how to draw''
1730 information (which in our case is taken from the {\code info} member, a copy of {\code iPict}).
1731 Nevertheless, notice that {\code Picture\_layouts} is not explicitly invoked. To the user's
1732 great relief, this is taken care of automatically within the {\code Picture\_draw} macro.
1733
1734 There are times however, when explicitly invoking a macro like {\code Picture\_layout}
1735 becomes a necessity. This is because, by contract, it is only after the {\code layout}
1736 macro is invoked that the final dimensions (width, height) of an object are
1737 definitely and permanently known. Imagine that we have a component whose job is to
1738 surround in a red-filled rectangle some other objects. This component
1739 needs to know what the dimensions of the contained objects are, in order to be able to set
1740 its own dimensions. At drawing time, the contained objects must not have been drawn already,
1741 because the red rectangle of the container would overwrite them.
1742 Therefore, the whole pseudo-code would be:
1743 \begin{verbatim}
1744 Create objects o1, o2, ... ok;
1745 Create container c(o1, o2, ..., ok);
1746 Optional: modify info-s for o1, o2, ... ok;
1747 Optional: modify info for c;
1748
1749 layout c, requiring layout of o1, o2, ... ok;
1750 establish where to draw c;
1751 draw red rectangle defined by c;
1752 draw components o1, o2, ...ok within c
1753 \end{verbatim}
1754
1755 A natural conclusion is that an object must not be laid out more than once, because otherwise
1756 inconsistent or superfluous equations would arise. To enforce this, by contract,
1757 any object must keep record of whether its layout method has already been invoked,
1758 and if the answer is affirmative, subsequent invocations of the layout macro would
1759 do nothing. It is very important to mention that after the {\code layout} macro is
1760 invoked over an object, modifying the {\code info} member of that object has
1761 no subsequent effect, since the layout equations are declared and interpreted only once.
1762
1763 \subsection{Notes on the Implementation of Links}
1764
1765 MetaUML considers edges in diagram graphs as links. A link is composed of a path and the
1766 heads (possible none, one or two). For example, since an association has no heads, it suffices
1767 to draw along the path with a solid pen; however, an unidirectional aggregation has, in addition
1768 to a solid path, two heads: one is an arrow and the other is a diamond.
1769
1770 The general algorithm for drawing a link is:
1771
1772 \begin{verbatim}
1773 0. Reserve space for heads
1774 1. Draw the path (except for the heads)
1775 2. Draw head 1
1776 3. Draw head 2
1777 \end{verbatim}
1778
1779 Each of the UML link types define how the drawing should be done, in each of the
1780 cases (1, 2 and 3). Consider the link type of unidirectional composition.
1781 Its ``class'' is declared as:
1782
1783 \begin{verbatim}
1784 vardef CompositionUniInfo@# =
1785 LinkInfo@#;
1786
1787 @#widthA = defaultRelationHeadWidth;
1788 @#heightA = defaultRelationHeadHeight;
1789 @#drawMethodA = "drawArrow";
1790
1791 @#widthB = defaultRelationHeadWidth;
1792 @#heightB = defaultRelationHeadHeight;
1793 @#drawMethodB = "drawDiamondBlack";
1794
1795 @#drawMethod = "drawLine";
1796 enddef;
1797 \end{verbatim}
1798
1799 Using this definition, the actual description is created like this:
1800
1801 \begin{verbatim}
1802 CompositionUniInfo.compositionUni;
1803 \end{verbatim}
1804
1805 As shown previously, is is the macro {\code link} which
1806 performs the actual drawing, using the link description information
1807 which is given as parameter (generally called {\code iLink}).
1808 For example, we can use:
1809
1810 \begin{verbatim}
1811 link(aggregationUni)((0,0)--(40,0));
1812 \end{verbatim}
1813
1814 %\begin{figure}
1815 %\centering
1816 %\includegraphics{fig/how-links-work.1}
1817 %\caption{An example of a picture stack.}
1818 %\label{fig:hlw}
1819 %\end{figure}
1820
1821 Let us see now the inner workings of macro {\code link}. Its definition is:
1822
1823 \begin{verbatim}
1824 vardef link(text iLink)(expr myPath)=
1825 LinkStructure.ls(myPath,
1826 iLink.widthA, iLink.widthB);
1827 drawLinkStructure(ls)(iLink);
1828 enddef;
1829 \end{verbatim}
1830
1831 \begin{figure}
1832 \centering
1833 \begin{tabular}{l|l}
1834 $AB$ & the path specified by the user \\
1835 $|AA'|$ & {\code iLink.widthA}\\
1836 $|BB'|$ & {\code iLink.widthB}
1837 \end{tabular}
1838 \includegraphics{fig/how-links-work.2}
1839 \caption{Details on how a link is drawn by MetaUML.}
1840 \label{fig:hlw2}
1841 \end{figure}
1842
1843 First, space is reserved for heads, by ``shortening'' the given path {\code myPath}
1844 by {\code iLink.widthA} at the beginning and by {\code iLink.widthB} at the end.
1845 After that, the shortened path is drawn with the ``method''
1846 given by {\code iLink.drawMethod} and the heads with the ``methods''
1847 {\code iLink.drawMethodA} and {\code iLink.drawMethodB},
1848 respectively (figure \ref{fig:hlw2}).
1849
1850 \subsection{Object Definitions: Easier {\code generic\_declare}}
1851
1852 In MetaPost, if somebody wants to define something resembling a class in an object-oriented language,
1853 named, say, {\code Person}, he would do something like this:
1854
1855 \begin{verbatim}
1856 vardef Person@#(expr _name, _age)=
1857 % @# prefix can be seen as `this` pointer
1858 string @#name;
1859 numeric @#age;
1860
1861 @#name := _name;
1862 @#age := _age;
1863 enddef;
1864 \end{verbatim}
1865
1866 This allows for the creation of instances (or objects) of class {\code Person} by using
1867 declarations like:
1868
1869 \begin{verbatim}
1870 Person.personA;
1871 Person.personB;
1872 \end{verbatim}
1873
1874 However, if one also wants to able able to create indexed arrays of persons, such as
1875 {\code Person.student0}, {\code Person.student1} etc., the definition of class
1876 {\code Person} must read:
1877
1878 \begin{verbatim}
1879 vardef Person@#(expr _name, _age)=
1880 _n_ := str @#;
1881 generic_declare(string) _n.name;
1882 generic_declare(numeric) _n.age;
1883
1884 @#name := _name;
1885 @#age := _age;
1886 enddef;
1887 \end{verbatim}
1888
1889 This construction is rather inelegant. MetaUML offers alternative macros to achieve
1890 the same effect, uncluttering the code by removing the need for the unaesthetic {\code \_n\_} and
1891 {\code \_n}.
1892
1893 \begin{verbatim}
1894 vardef Person@#(expr _name, _age)=
1895 attributes(@#);
1896 var(string) name;
1897 var(numeric) age;
1898
1899 @#name := _name;
1900 @#age := _age;
1901 enddef;
1902 \end{verbatim}
1903
1904 \section{Customization in MetaUML: Examples}
1905 \label{section:customization}
1906
1907 We have seen that in MetaUML the ``how to draw'' information is memorized into the so-called
1908 ``{\code Info}'' structures. For example, the default way in which a {\code Picture} object is
1909 to be drawn is recorded into an instance of {\code PictureInfo}, named {\code iPict}. In this section we
1910 present a case study involving the customization of {\code Class} objects. The customization of
1911 any other MetaUML objects works similarly. Here we cannot possibly present all the customization
1912 options for all kinds of MetaUML objects: this would take too long. Nevertheless, an interested reader can refer
1913 to the top of the appropriate MetaUML library file, where {\code Info} structures are defined.
1914 For example, class diagram related definitions are in {\code metauml\_class.mp}, activity diagram
1915 definitions are in {\code metauml\_activity.mp} etc.
1916
1917 \subsection{Global settings}
1918
1919 Let us assume that we do not particularly like the default foreground color of all classes, and wish
1920 to change it so something yellowish. In this scenario, one would most likely want to change
1921 the appropriate field in {\code iClass}:
1922
1923 \begin{verbatim}
1924 iClass.foreColor := (.9, .9, 0);
1925 \end{verbatim}
1926
1927 After this, we can obtain the following result:
1928
1929 \begin{multicols}{2}
1930 \begin{verbatim}
1931 Class.A("A")()();
1932 Class.B("B")()();
1933 Class.C("C")()();
1934
1935 B.w = A.e + (20,0);
1936 C.n = .5[A.se, B.sw] + (0, -10);
1937
1938 drawObjects(A, B, C);
1939 \end{verbatim}
1940 \columnbreak
1941 \hspace{1cm}\includegraphics{fig/class_customization.1}
1942 \end{multicols}
1943
1944 \subsection{Individual settings}
1945
1946 To modify the settings of one particular {\code Class} objects, another strategy is more appropriate. How about having class
1947 {\code C} stand out with a light blue foreground color, a bigger font size for the class name and a blue border?
1948
1949 \begin{multicols}{2}
1950 \begin{verbatim}
1951 iPict.foreColor := (.9, .9, 0);
1952
1953 Class.A("A")()();
1954 Class.B("B")()();
1955 Class.C("C")()();
1956 C.info.foreColor := (.9, .7, .7);
1957 C.info.borderColor := green;
1958 C.info.iName.iFont.scale := 2;
1959
1960 % positioning code ommited
1961 drawObjects(A, B, C);
1962 \end{verbatim}
1963 \columnbreak
1964 \hspace{1cm}\includegraphics{fig/class_customization.2}
1965 \end{multicols}
1966
1967 As an aside, each {\code Class} object has an {\code info} member which is created as
1968 a copy of {\code iClass}; the actual drawing is performed using this copied
1969 information. Because of that, the {\code info} member can be safely modified after the object
1970 has been created, obtaining the expected results and not influencing other objects.
1971
1972 Another thing worth mentioning is that the {\code ClassInfo} structure contains
1973 the {\code iName} member, which is an instance of {\code PictureInfo}. In our example we
1974 do not want to modify the spacings around the {\code Picture} object,
1975 but the characteristics of the font its contents is typeset into. To do that,
1976 we modify the {\code iName.iFont} member, which by default is a copy of {\code iFont}
1977 (an instance of {\code FontInfo}, defined in {\code util\_picture.mp}).
1978 If, for example, we want to change the font the class name is rendered into, we would set
1979 the attribute {\code iName.iFont.name} to a string representing a font name
1980 on our system (as used with the MetaPost {\code infont} operator).
1981
1982 \subsection{Predefined settings}
1983
1984 This usage scenario is perhaps more interesting. Suppose that we have two
1985 types of classes which we want to draw differently. Making the setting adjustments
1986 for each individual class object would soon become a nuisance. MetaUML's solution consists in the
1987 ability of using predefined ``how to draw'' {\code Info} objects. Let us create such objects:
1988
1989 \begin{verbatim}
1990 ClassInfoCopy.iHome(iClass);
1991 iHome.foreColor := (0, .9, .9);
1992
1993 ClassInfo.iRemote;
1994 iRemote.foreColor := (.9, .9, 0);
1995 iRemote.borderColor := green;
1996 \end{verbatim}
1997
1998 Object {\code iHome} is a copy of {\code iClass} (as it might have been set at
1999 the time of the macro call). Object {\code iRemote} is created just as {\code iClass}
2000 is originally created. We can now use these {\code Info} objects to easily set the
2001 ``how to draw'' information for classes. The result is depicted below,
2002 please note the ``{\code E}'' prefix in {\code EClass}:
2003
2004 \begin{multicols}{2}
2005 \begin{verbatim}
2006 EClass.A(iHome)("UserHome")()();
2007 EClass.B(iRemote)("UserRemote")()();
2008 EClass.C(iHome)("CartHome")()();
2009 EClass.D(iRemote)("CartRemote")()();
2010 \end{verbatim}
2011 \columnbreak
2012 \hspace{1cm}\includegraphics{fig/class_customization.3}
2013 \end{multicols}
2014
2015 \subsection{Extreme customization}
2016
2017 When another font (or font size) is used, it may become necessary to change the space between the
2018 baselines of attributes and methods. Figure below is the result of the (unlikely) code:
2019
2020 \begin{multicols}{2}
2021 \begin{verbatim}
2022 Class.A("Foo")
2023 ("a: int", "b: int")
2024 ("foo()", "bar()", "gar()");
2025
2026 A.info.iName.iFont.name := metauml_defaultFontBold;
2027 A.info.iName.iFont.scale := 1.2;
2028
2029 A.info.iAttributeStack.iPict.iFont.scale := 0.8;
2030 A.info.iAttributeStack.top := 10;
2031 A.info.iAttributeStack.spacing := 11;
2032
2033 A.info.iMethodStack.iPict.iFont.scale := 2;
2034 A.info.iMethodStack.spacing := 17;
2035 A.info.iMethodStack.bottom := 10;
2036
2037 drawObject(A);
2038 \end{verbatim}
2039 \columnbreak
2040 \hspace{4cm}\includegraphics{fig/class_customization.4}
2041 \end{multicols}
2042
2043 \begin{verbatim}
2044 \end{verbatim}
2045
2046 Both {\code iAttributeStack} and {\code iMethodStack} are instances of
2047 {\code PictureStackInfo}, which is used to control the display of {\code PictureStack} objects.
2048 %We can also customize the size and colors of the ``locks'' by setting {\code A.info.iLock}.
2049
2050 As font names, you can choose from the globally defined {\code metauml\_defaultFont}, {\code metauml\_defaultFontOblique}, {\code metauml\_defaultFontBold}, {\code metauml\_defaultFontBoldOblique}, or any other name of a font that is available on your system.
2051
2052 \section{Alternatives to MetaUML}
2053
2054 No software package is perfect, and for this MetaUML is a prime example. Here is a list of packages that may also be used to create UML diagrams for LaTeX work:
2055
2056 \begin{itemize}
2057 \item uml.sty \cite{umlsty}
2058 \item pst-uml \cite{pstumlsty}
2059 \item umldoc \cite{umldoc}
2060 \item TiKZ-UML \cite{tikzuml}
2061 \end{itemize}
2062
2063 Do not ignore the possibility of creating your diagrams using a GUI program, and then exporting them into a LaTex-friendly open format such as SVG \cite{svglatex}.
2064
2065 \pagebreak
2066 \input{test-suite}
2067
2068 \pagebreak
2069 \section{References}
2070 \printbibliography[heading=none]
2071
2072 \end{document}