Preprocessing
Some preprocessing capabilities are included in PlantUML, and available for all diagrams.
Those functionalities are very similar to the C language preprocessor, except that the special character #
has been changed to the exclamation mark !
.
Variable definition [=, ?=]
Although this is not mandatory, we highly suggest that variable names start with a $
.
There are three types of data: * Integer number (int); * String (str) - these must be surrounded by single quote or double quote; * JSON (JSON) - these must be surrounded by curly brackets. (for JSON variable definition and usage, see more details on link::preprocessing-json[Preprocessing-JSON page])
Variables created outside function are global, that is you can access them from everywhere (including from functions). You can emphasize this by using the optional global
keyword when defining a variable.
@startuml
!$a = 42
!$ab = "foo1"
!$cd = "foo2"
!$ef = $ab + $cd
!$foo = { "name": "John", "age" : 30 }
Alice -> Bob : $a
Alice -> Bob : $ab
Alice -> Bob : $cd
Alice -> Bob : $ef
Alice -> Bob : Do you know **$foo.name** ?
@enduml
You can also assign a value to a variable, only if it is not already defined, with the syntax: !$a ?= "foo"
Boolean expression
Boolean representation [0 is false]
There is not real boolean type, but PlantUML use this integer convention:
-
Integer
0
meansfalse
-
and any non-null number (as
1
) or any string (as"1"
, or even"0"
) meanstrue
.
[Ref. QA-9702]
While loop [!while, !endwhile]
You can use !while
and !endwhile
keywords to have repeat loops.
While loop (on Activity diagram)
@startuml
!procedure $foo($arg)
:procedure start;
!while $arg!=0
!$i=3
#palegreen:arg=$arg;
!while $i!=0
:arg=$arg and i=$i;
!$i = $i - 1
!endwhile
!$arg = $arg - 1
!endwhile
:procedure end;
!endprocedure
start
$foo(2)
end
@enduml
[Adapted from QA-10838]
While loop (on Component/Deployment diagram)
@startuml
!procedure $foo($arg)
!while $arg!=0
[Component $arg] as $arg
!$arg = $arg - 1
!endwhile
!endprocedure
$foo(4)
1->2
3-->4
@enduml
[Ref. QA-14088]
Procedure [!procedure, !endprocedure]
-
Procedure names should start with a
$
-
Argument names should start with a
$
-
Procedures can call other procedures
Example:
@startuml
!procedure $msg($source, $destination)
$source --> $destination
!endprocedure
!procedure $init_class($name)
class $name {
$addCommonMethod()
}
!endprocedure
!procedure $addCommonMethod()
toString()
hashCode()
!endprocedure
$init_class("foo1")
$init_class("foo2")
$msg("foo1", "foo2")
@enduml
Variables defined in procedures are local. It means that the variable is destroyed when the procedure ends.
Return function [!function, !endfunction]
A return function does not output any text. It just define a function that you can call: * directly in variable definition or in diagram text * from other return functions * from procedures
-
Function name should start with a
$
-
Argument names should start with a
$
@startuml
!function $double($a)
!return $a + $a
!endfunction
Alice -> Bob : The double of 3 is $double(3)
@enduml
It is possible to shorten simple function definition in one line:
@startuml
!function $double($a) !return $a + $a
Alice -> Bob : The double of 3 is $double(3)
Alice -> Bob : $double("This work also for strings.")
@enduml
As in procedure (void function), variable are local by default (they are destroyed when the function is exited). However, you can access to global variables from function. However, you can use the local
keyword to create a local variable if ever a global variable exists with the same name.
Default argument value
In both procedure and return functions, you can define default values for arguments.
@startuml
!function $inc($value, $step=1)
!return $value + $step
!endfunction
Alice -> Bob : Just one more $inc(3)
Alice -> Bob : Add two to three : $inc(3, 2)
@enduml
Only arguments at the end of the parameter list can have default values.
Unquoted procedure or function [!unquoted]
By default, you have to put quotes when you call a function or a procedure.
It is possible to use the unquoted
keyword to indicate that a function or a procedure does not require quotes for its arguments.
Keywords arguments
Like in Python, you can use keywords arguments :
@startuml
!unquoted procedure $element($alias, $description="", $label="", $technology="", $size=12, $colour="green")
rectangle $alias as "
<<$alias>>
==$label==
//[$technology]//
$description"
!endprocedure
$element(myalias, "This description is %newline()on several lines", $size=10, $technology="Java")
@enduml
Including files or URL [!include, !include\_many, !include\_once]
Use the !include
directive to include file in your diagram. Using URL, you can also include file from Internet/Intranet. Protected Internet resources can also be accessed, this is described in link::url-authentication[URL authentication].
Imagine you have the very same class that appears in many diagrams. Instead of duplicating the description of this class, you can define a file that contains the description.
File List.iuml
interface List List : int size() List : void clear()
The file List.iuml
can be included in many diagrams, and
any modification in this file will change all diagrams that include it.
You can also put several @startuml/@enduml
text block in an included file and then specify which block
you want to include adding !0
where 0
is the block number. The !0
notation denotes the first diagram.
For example, if you use !include foo.txt!1
, the second @startuml/@enduml
block
within foo.txt
will be included.
You can also put an id to some @startuml/@enduml
text block in an included file using
@startuml(id=MY_OWN_ID)
syntax and then include the block adding !MY_OWN_ID
when including the file,
so using something like !include foo.txt!MY_OWN_ID
.
By default, a file can only be included once. You can use !include_many
instead of !include
if you want to include some file several times. Note that there is also a !include_once
directive that raises an error if a file is included several times.
Including Subpart [!startsub, !endsub, !includesub]
You can also use !startsub NAME
and !endsub
to indicate sections of text to include from other files using !includesub
. For example:
file1.puml:
@startuml A -> A : stuff1 !startsub BASIC B -> B : stuff2 !endsub C -> C : stuff3 !startsub BASIC D -> D : stuff4 !endsub @enduml
file1.puml would be rendered exactly as if it were:
@startuml A -> A : stuff1 B -> B : stuff2 C -> C : stuff3 D -> D : stuff4 @enduml
However, this would also allow you to have another file2.puml like this:
file2.puml
@startuml title this contains only B and D !includesub file1.puml!BASIC @enduml
This file would be rendered exactly as if:
@startuml title this contains only B and D B -> B : stuff2 D -> D : stuff4 @enduml
Builtin functions [%]
Some functions are defined by default. Their name starts by %
Name | Description | Example | Return |
---|---|---|---|
|
Return a character from a give Unicode value |
|
|
|
Return a darken color of a given color with some ratio |
|
|
|
Retrieve current date. You can provide an optional format for the date |
|
current date |
|
Return the hexadecimal string (String) of a decimal value (Int) |
|
|
|
Retrieve current dirpath |
|
current path |
|
Check if some feature is available in the current PlantUML running version |
|
|
|
Return always |
|
|
|
Check if a file exists on the local filesystem |
|
|
|
Retrieve current filename |
|
current filename |
|
Check if a function exists |
|
|
|
Retrieve some variable value |
|
the value of the variable |
|
Retrieve environment variable value |
|
the value of |
|
Return the decimal value (Int) of a hexadecimal string (String) |
|
|
|
Return the RGBa color from a HSL color |
|
|
|
Convert a String to Int |
|
42 |
|
Check if a color is a dark one |
|
|
|
Check if a color is a light one |
|
|
|
Return a lighten color of a given color with some ratio |
|
|
|
|
JSON data |
|
|
Return a lowercase string |
|
|
|
Return a newline |
|
a newline |
|
Return the logical negation of an expression |
|
|
|
Return a lighten color of a given color with some ratio |
|
|
|
Reverse a color using RGB |
|
|
|
Reverse a color using HSLuv |
|
|
|
Set a global variable |
|
an empty string |
|
Return the size of any string or JSON structure |
|
|
|
Convert an expression to String |
|
|
|
Calculate the length of a String |
|
|
|
Search a substring in a string |
|
4 (position of |
|
Extract a substring. Takes 2 or 3 arguments |
|
|
|
Return always |
|
|
|
Return an uppercase string |
|
|
|
Check if a variable exists |
|
|
|
Return PlantUML current version |
|
|
Logging [!log]
You can use !log
to add some log output when generating the diagram. This has no impact at all on the diagram itself. However, those logs are printed in the command line’s output stream. This could be useful for debug purpose.
Memory dump [!dump\_memory]
You can use !dump_memory
to dump the full content of the memory when generating the diagram. An optional string can be put after !dump_memory
. This has no impact at all on the diagram itself. This could be useful for debug purpose.
Building custom library [!import, !include]
It’s possible to package a set of included files into a single .zip or .jar archive.
This single zip/jar can then be imported into your diagram using !import
directive.
Once the library has been imported, you can !include
file from this single zip/jar.
Example:
@startuml !import /path/to/customLibrary.zip ' This just adds "customLibrary.zip" in the search path !include myFolder/myFile.iuml ' Assuming that myFolder/myFile.iuml is located somewhere ' either inside "customLibrary.zip" or on the local filesystem ...
Search path
You can specify the java property plantuml.include.path
in the command line.
For example:
java -Dplantuml.include.path="c:/mydir" -jar plantuml.jar atest1.txt
Note the this -D option has to put before the -jar option. -D options after the -jar option will be used to define constants within plantuml preprocessor.
Dynamic invocation [%invoke_procedure()
, %call_user_func()
]
You can dynamically invoke a procedure using the special %invoke_procedure()
procedure.
This procedure takes as first argument the name of the actual procedure to be called. The optional following arguments are copied to the called procedure.
For example, you can have:
@startuml
!procedure $go()
Bob -> Alice : hello
!endprocedure
!$wrapper = "$go"
%invoke_procedure($wrapper)
@enduml
@startuml
!procedure $go($txt)
Bob -> Alice : $txt
!endprocedure
%invoke_procedure("$go", "hello from Bob...")
@enduml
For return functions, you can use the corresponding special function %call_user_func()
:
Evaluation of addition depending of data types [+]
Evaluation of $a + $b
depending of type of $a
or $b
@startuml
title
<#LightBlue>|= |= $a |= $b |= string($a + $b)|
<#LightGray>| type | str | str | str (concatenation) |
| example |= "a" |= "b" |= %string("a" + "b") |
<#LightGray>| type | str | int | str (concatenation) |
| ex.|= "a" |= 2 |= %string("a" + 2) |
<#LightGray>| type | str | int | str (concatenation) |
| ex.|= 1 |= "b" |= %string(1 + "b") |
<#LightGray>| type | bool | str | str (concatenation) |
| ex.|= true() |= "b" |= %string(%true() + "b") |
<#LightGray>| type | str | bool | str (concatenation) |
| ex.|= "a" |= false() |= %string("a" + %false()) |
<#LightGray>| type | int | int | int (addition of int) |
| ex.|= 1 |= 2 |= %string(1 + 2) |
<#LightGray>| type | bool | int | int (addition) |
| ex.|= true() |= 2 |= %string(%true() + 2) |
<#LightGray>| type | int | bool | int (addition) |
| ex.|= 1 |= false() |= %string(1 + %false()) |
<#LightGray>| type | int | int | int (addition) |
| ex.|= 1 |= intval("2") |= %string(1 + %intval("2")) |
end title
@enduml
Preprocessing JSON
You can extend the functionality of the current Preprocessing with link::preprocessing-json[JSON Preprocessing] features:
-
JSON Variable definition
-
Access to JSON data
-
Loop over JSON array
(See more details on link::preprocessing-json[Preprocessing-JSON page])
Migration notes
The current preprocessor is an update from some legacy preprocessor.
Even if some legacy features are still supported with the actual preprocessor, you should not use them any more (they might be removed in some long term future).
-
You should not use
!define
and!definelong
anymore. Use!function
,!procedure
or variable definition instead.-
!define
should be replaced by return!function
-
!definelong
should be replaced by!procedure
.
-
-
!include
now allows multiple inclusions : you don’t have to use!include_many
anymore -
!include
now accepts a URL, so you don’t need!includeurl
-
Some features (like
%date%
) have been replaced by builtin functions (for example%date()
) -
When calling a legacy
!definelong
macro with no arguments, you do have to use parenthesis. You have to usemy_own_definelong()
becausemy_own_definelong
without parenthesis is not recognized by the new preprocessor.
Please contact us if you have any issues.
%Splitstr
builtin function
@startmindmap
!$list = %splitstr("abc~def~ghi", "~")
* root
!foreach $item in $list
** $item
!endfor
@endmindmap
[Ref. QA-15374]