plantuml使用教程【转】

 

Table of Contents

前言

本文多数内容引用自官网文档,并非本人原创,也谈不上翻译,只是把自己 理解的东西用中文写出来。

编写本文的目的旨在记录个人在学习PlantUML时对官网上一些内容的理解,以 及总结学习过程中遇到的问题,并将其分享。文章中如有不对之处欢迎大家直 言指正,以免造成误导。

版权:本文可自由转载,但不能商用,不能衍生,保持署名。转载请注明作者及出处.

Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License.

什么是PlantUML

PlantUML是一个快速创建UML图形的组件,官网上之所以称它是一个组件,我 想主要是因为多数情况下我们都是在Eclipse、NetBenas、Intellijidea、 Emacs、Word等软件里来使用PlantUML(参看各软件相关配置)。

  • PlantUML支持的图形有:

    • sequence diagram,
    • use case diagram,
    • class diagram,
    • activity diagram (here is the new syntax),
    • component diagram,
    • state diagram,
    • object diagram,
    • wireframe graphical interface
  • PlantUML通过简单和直观的语言来定义图形,它可以生成PNG、SVG和二进制 图片。下面是一个简单的示例:
    1. #+BEGIN_SRC plantuml :file ../img/orgmode-babel-sequenceuml.png
    2. Alice -> Bob: synchronous call
    3. Alice ->> Bob: asynchronous call
    4. #+END_SRC

    在官网上有一个简单的在线Demo服务, 有兴趣的朋友可以上去看下。

在Emacs里配置PlantUML(参考:Run it from Emacs

  1. 下载 plantuml.jar 到你的硬盘上(官网下载页面
  2. 安装生成图片用的软件Graphviz
    1. ## 如果是Ubuntu系统,可以直接运行下面的命令安装
    2. sudo apt-get install graphviz
  3. 在 .emacs 里添加配置,把 plantuml 添加到 org-babel-load-languages 加载语言列表里。
    1. ;; active Org-babel languages
    2. (org-babel-do-load-languages
    3. 'org-babel-load-languages
    4. '(;; other Babel languages
    5. (plantuml . t)))

    然后把刚下载到的 plantuml.jar 文件的存放路径也添加到 .emacs 文件中,以方便Emacs调用。

    1. (setq org-plantuml-jar-path
    2. (expand-file-name "~/path/to/plantuml.jar"))

重启Emacs,复制上面的示例代码试一下吧!

如何使用

 

顺序图(Sequence Diagram)

 

简单示例

顺序图用 -> , --><-<-- 来绘制参与者(Participants)之 间的消息(Message)。

  1. #+BEGIN_SRC plantuml :file ../img/plantuml-quickstart-s1.png
  2. Alice -> Bob: Authentication Request
  3. Bob --> Alice: Authentication Response
  4.  
  5. Alice -> Bob: Another atuhentication Request
  6. Alice <-- Bob: Another authentication Response
  7. #+END_SRC

注释语句

以单引号开始头行,即是一个单行注释。多行注释可以使用"/'"表 示注释内容的开始,然后使用"'/"来表示注释内容的结束。

申明参与者

申明参与者,可以使用 participant 关键词,也可以使用下面的参与者 分类关键词来申明参与者:

  • actor
  • boundary
  • control
  • entity
  • database

不同的参与者类型,其图标也是不一样的。

  1. #+BEGIN_SRC plantuml :file ../img/plantuml-quickstart-s2.png
  2. actor Foo1
  3. boundary Foo2
  4. control Foo3
  5. entity Foo4
  6. database Foo5
  7. Foo1 -> Foo2 : To boundary
  8. Foo1 -> Foo3 : To control
  9. Foo1 -> Foo4 : To entity
  10. Foo1 -> Foo5 : To database
  11. #+END_SRC

使用 as 关键词可以为参与者起一个别名,这样在对引用长名的参与者时, 会方便很多。在参与者申明语句后行尾可以追加背景色的设置,只要把标准 的HTML颜色值 写在后面就行了。

  1. #+BEGIN_SRC plantuml :file ../img/plantuml-quickstart-s3.png
  2. actor Bob #red
  3. ' The only defference between actor
  4. ' and participant is the drawing
  5. participant Alice
  6. participant "I have a really\nlong name" as L #99ff99
  7. /' You can also declare:
  8. participant L as "I have a really\nlong name" #99ff99
  9. '/
  10.  
  11. Alice -> Bob: Authentication Request
  12. Bob -> Alice: Authentication Response
  13. Bob -> L: Log transaction
  14. #+END_SRC

使用非字母的参与者名称(Use non-letters in participants)

针对非字母的参与者名,我们可以使用双引号,同样也可以为比较长的名字 起个别名,方法同上使用 as 关键词。

使用上面的关键词来申明参与者,是一种显示申明;而采用引号来申明参与 者则是一种隐示申明方法,它不需要专门的位置去定义。

  1. #+BEGIN_SRC plantuml :file ../img/plantuml-quickstart-s4.png
  2. Alice -> "Bob()" : Hello
  3. "Bob()" -> "This is very\nlong" as Long
  4. ' You can also declare:
  5. ' "Bob()" -> Long as "This is very\nlong"
  6. Long --> "Bob()" : ok
  7. #+END_SRC

发送消息给自己(Message to Self)

一个参与者可以给自己发送消息,消息名如果需要有多行文本,可以用 \n 来表示换行。

  1. #+BEGIN_SRC plantuml :file ../img/plantuml-quickstart-s5.png
  2. Alice -> Alice: This is a signal to self.\nIt also demonstrates\nmultiline \ntext
  3. #+END_SRC

改变箭头的样式(Change arrow style)

在用例图里可以通过以下方式来改变箭头的样式:

  • 使用 \ 或 / 来替换 < 或 > 可以让箭头只显示上半部分或下半 部分。
  • 重复输入箭头或斜杠( >> // ),用来绘制空心箭头。
  • 使用双横线 -- 替代 - 可以用来绘制点线。
  • 在箭头后面加个 o 可以在箭头前绘制一个圆圈。
  • 使用 <-> 可用来绘制双向箭头。
  1. #+BEGIN_SRC plantuml :file ../img/plantuml-quickstart-s6.png
  2. Bob -> Alice
  3. Bob ->> Alice
  4. Bob -\ Alice
  5. Bob \\- Alice
  6. Bob //-- Alice
  7.  
  8. Bob ->o Alice
  9. Bob o\\-- Alice
  10.  
  11. Bob <-> Alice
  12. Bob <<-\\o Alice
  13. #+END_SRC

改变箭头的颜色(Change arrow color)

要改变箭头的颜色,可以使用HTML颜色符号,参看下面的例子:

  1. #+BEGIN_SRC plantuml :file ../img/plantuml-quickstart-s7.png
  2. Bob -[#red]> Alice : hello
  3. Alice -[#0000FF]->Bob : ok
  4. #+END_SRC

消息序号(Message sequence numbering)

关键词 autonumber 用来给自动的给消息添加上序号。

  1. #+BEGIN_SRC plantuml :file ../img/plantuml-quickstart-s8.png
  2. autonumber
  3. Bob -> Alice : Authentication Request
  4. Bob <- Alice : Authentication Response
  5. #+END_SRC

如果需要指定一个起始号码,可以直接在 autonumber 后面加个数字就行 了,如果要设置自增量,再在后面加一个数字就行了( autonumber start increment )。

  1. #+BEGIN_SRC plantuml :file ../img/plantuml-quickstart-s9.png
  2. autonumber
  3. Bob -> Alice : Authentication Request
  4. Bob <- Alice : Authentication Response
  5.  
  6. autonumber 15
  7. Bob -> Alice : Another authentication Request
  8. Bob <- Alice : Another authentication Response
  9.  
  10. autonumber 40 10
  11. Bob -> Alice : Yet another authentication Request
  12. Bob <- Alice : Yet another authentication Response
  13. #+END_SRC

我们也可以为序号指定数字格式,这个格式化的过程实际上是Java类 DecimalFormat 来执行的( 0 表示数字, # 缺省补零位数)。

同样的,也可以使用一些HTML标签来控制数字的样式。

  1. #+BEGIN_SRC plantuml :file ../img/plantuml-quickstart-s10.png
  2. autonumber "<b>[000]"
  3. Bob -> Alice : Authentication Request
  4. Bob <- Alice : Authentication Response
  5.  
  6. autonumber 15 "<b>(<u>##</u>)"
  7. Bob -> Alice : Another authentication Request
  8. Bob <- Alice : Another authentication Response
  9.  
  10. autonumber 40 10 "<font color=red>Message 0 "
  11. Bob -> Alice : Yet another authentication Request
  12. Bob <- Alice : Yet another authentication Response
  13. #+END_SRC

标题(Title)

要给图形加一个标题可以用 title 关键词来设置。

  1. #+BEGIN_SRC plantuml :file ../img/plantuml-quickstart-s11.png
  2. title Simple Comunication example
  3.  
  4. Alice -> Bob : Authentication Request
  5. Bob --> Alice : Authentication Response
  6. #+END_SRC

图形图例(Legend the diagram)

使用 legend 和 end legend 关键词可以设置图形的图例。图例可以设 为左对齐、右对齐和居中对齐。

  1. #+BEGIN_SRC plantuml :file ../img/plantuml-quickstart-s12.png
  2. Alice -> Bob : Hello
  3. legend right
  4. Short
  5. legend
  6. endlegend
  7. #+END_SRC

分割图形(Splitting diagrams)

关键词 newpage 是用来把图形分割成几个图片的。每一个被分割出来的 图片可以看作是一个新的页面( new page ),如果要给新的页面添加一 个标题,可以紧跟在关键词 newpage 之后来设置。

使用这个方法可以方便的在Word里把比较长的图形分别打印到几个不同的页 面上(有点分页符的概念)。

  1. #+BEGIN_SRC plantuml :file ../img/plantuml-quickstart-s13.png
  2. Bliss -> Tia : I love you
  3. Bliss -> Tia : I miss you
  4.  
  5. newpage
  6.  
  7. Bliss -> Tia : Let's go home
  8. Bliss -> Tia : Quick
  9.  
  10. newpage A title for the\nlast page
  11.  
  12. Tia -> Bliss : Give me money
  13. Tia -> Bliss : No money No love
  14. #+END_SRC

消息分组(Grouping message)

有时候可能需要对消息进行分组,那么可以使用下面的关键词来实现:

  • alt/else
  • opt
  • loop
  • par
  • break
  • critical
  • group, 这个关键词后面的文字会作为组名显示在图形上

上面的关键词后可以添加一些文本用来显示在头部(注: group 除外,因 为它后面的文本用来显示在组名称的位置)。在组嵌套组的结构里可以用关 键词 end 来关闭组或者说是表示一个组符号的结束符(类似if/endif )。

  1. #+BEGIN_SRC plantuml :file ../img/plantuml-quickstart-s14.png
  2. Alice -> Bob: Authentication Request
  3.  
  4. alt successful case
  5. Bob -> Alice: Authentication Accepted
  6. else some kind of failure
  7. Bob -> Alice: Atuhentication Failue
  8. group My own label
  9. Alice -> Log : Log attack start
  10. loop 1000 times
  11. Alice -> Bob: DNS Attack
  12. end
  13. Alice -> Log : Loag alice end
  14. end
  15. else Another type of failue
  16. Bob -> Alice: Please repeat
  17. end
  18. #+END_SRC

消息注解(Notes on messages)

我们可能经常会在消息的左边或右边使用注解,要添加注解,只要使用 note left 或 note right 关键词就可以了。

  1. #+BEGIN_SRC plantuml :file ../img/plantuml-quickstart-s15.png
  2. Alice -> Bob : hello
  3. note left: this is a first note
  4.  
  5. Bob -> Alice : ok
  6. note right: this is anther note
  7.  
  8. Bob -> Bob : I am thinking
  9. note left
  10. a note
  11. can also be defined
  12. on several lines
  13. end note
  14. #+END_SRC

一些其他的注解方式(Some other notes)

通过使用关键词 note left of , note right of 或 note over , 我们还可以把注解放置在与之相关的参与者的左边或右边,或下方。

通过改变注解的背景色,我们还可以高亮一个注解文本块。

如果要使用多行注解,可以使用关键词 end note 来表示注解的结束。

  1. #+BEGIN_SRC plantuml :file ../img/plantuml-quickstart-s16.png
  2. participant Alice
  3. participant Bob
  4. note left of Alice #aqua
  5. This is displayed
  6. left of Alice.
  7. end note
  8.  
  9. note right of Alice: This is displayed right of Alice.
  10.  
  11. note over Alice: This displayed over Alice.
  12.  
  13. note over Alice, Bob #FFAAAA: This is displayed\n over Bob and Alice.
  14.  
  15. note over Bob, Alice
  16. This is yet another
  17. example of
  18. a long note.
  19. end note
  20. #+END_SRC

使用HTML进行格式化(Formatting using HTML)

我们可以使用少量的HTML标签来格式化文本:

  • <b> 加粗文本
  • <u> 或 <u:#AAAAAA> 或 <u:colorName> 用来加下划线
  • <i> 斜体
  • <s> 或 <s:#AAAAAA> 或 <s:colorName> 用来加删除线
  • <w> 或 <w:#AAAAAA> 或 <w:colorName> 用来加波浪线
  • <color:#AAAAAA> 或 <color:colorName> 用来设置文本颜色
  • <back:#AAAAAA> 或 <back:colorName> 用来设置背景色
  • <size:nn> 设置字体大小
  • <img src="file"> 或 <img:file> 用来添加图片,图片文件必须 是可以访问得到才行。
  • <img src="http://url"> 或 <img:http://url> 用来添加一个互 联网图片,同样的图片地址必须是可用的才行。
  1. #+BEGIN_SRC plantuml :file ../img/plantuml-quickstart-s17.png
  2. participant Alice
  3. participant "The <b>Famous</b> Bob" as Bob
  4.  
  5. Alice -> Bob : A <i>well formated</i> message
  6. note right of Alice
  7. This is <back:cadetblue><size:18>displayed</size></back>
  8. <u>left of</u> Alice.
  9. end note
  10. note left of Bob
  11. <u:red>This</u> is <color #118888>displayed</color>
  12. <b><color purple>left of</color> <s:red>Alice</strike> Bob</b>
  13. end note
  14. note over Alice, Bob
  15. <w:#FF33FF>This is hosted</w> by <img ../img/code.png>
  16. end note
  17. #+END_SRC

用例图(Use Case Diagram)

 

用例(Usecase)

用例可以用一对小括号括起来表示,也可以使用 usecase 关键词来定义。 用例也可以通过使用 as 关键词来设置别名,在建立关系的时候可以使用 别名。

  1. #+BEGIN_SRC plantuml :file ../img/plantuml-quickstart-u1.png
  2. @startuml
  3. (Usecase One)
  4. (Usecase Two) as (UC2)
  5. usecase UC3
  6. usecase (Last\nusecase) as UC4
  7. @enduml
  8. #+END_SRC

参与者(Actors)

定义参与者时,可以把参与者的名称放在两个冒号的中间,也可以用 actor 关键词来定义参与者。同样参与着也可以使用别名。

  1. #+BEGIN_SRC plantuml :file ../img/plantuml-quickstart-u2.png
  2. @startuml
  3. :Actor 1:
  4. :Another\nactor: as Men2
  5. actor Men3
  6. actor :Last actor: as Men4
  7. @enduml
  8. #+END_SRC

示例

  1. #+BEGIN_SRC plantuml :file ../img/plantuml-quickstart-u99.png
  2. left to right direction
  3. skinparam packageStyle rect
  4. actor customer
  5. actor clerk
  6. rectangle checkout {
  7. customer -- (checkout)
  8. (checkout) .> (payment) : include
  9. (help) .> (checkout) : extends
  10. (checkout) -- clerk
  11. }
  12. #+END_SRC

类图(Class Diagram)

 

示例1

活动图(Activity Diagram)

 

简单活动(Simple Activity)

在活动图中,你可以使用 (*) 来表示活动开始点和结束点。使用 --> 来表示箭头。

  1. #+BEGIN_SRC plantuml :file ../img/plantuml-quickstart-a1.png
  2. (*) --> "First Activity"
  3. "First Activity" --> (*)
  4. #+END_SRC

带标注的箭头(Label on arrows)

缺省情况下,活动图的箭头是没有标注的。但我们可以通过方括号 [labels] 来设置标注,只要把它放在箭头定义的后面就可以了。

  1. #+BEGIN_SRC plantuml :file ../img/plantuml-quickstart-a2.png
  2. (*) --> "First Activity"
  3. -->[You can put also labels] "Second Activity"
  4. -->(*)
  5. #+END_SRC

改变箭头的方向(Changing arrow direction)

我们可以使用 -> 创建一个水平箭头,也可以通过下面的方式来改变箭头 的方向:

  • -down-> 向下(这个是默认的,等同于 =–>=)
  • -right-> 向右
  • -left-> 向左
  • -up-> 向上
  1. #+BEGIN_SRC plantuml :file ../img/plantuml-quickstart-a3.png
  2. (*) --> "1"
  3. -right-> "2"
  4. -down-> "3"
  5. -left-> "4"
  6. -le-> "5"
  7. -up-> "6"
  8. -l-> "7"
  9. -do-> "8"
  10. -d-> "9"
  11. -> "10"
  12. --> (*)
  13. #+END_SRC

在描述箭头时, up|down|left|right 这几个单词的写法可以简化, 用单词开头的一个或两个字母来替换就行了,比如 -down-> 也可以写成 -d-> 或者 -do-> 。

分支(Branches)

在PlantUML里,我们可以使用 if/then/else 关键词来定义分支。

  1. #+BEGIN_SRC plantuml :file ../img/plantuml-quickstart-a4.png
  2. (*) --> "Initialisation"
  3.  
  4. if " Some Test" then
  5. -->[ture] "Some Activity"
  6. --> "Another Activity"
  7. -right-> (*)
  8. else
  9. ->[false] "Something else"
  10. -->[Ending process] (*)
  11. endif
  12. #+END_SRC

多分支(More on Branches)

直接给例子:

  1. #+BEGIN_SRC plantuml :file ../img/plantuml-quickstart-a5.png
  2. (*) --> if "Some Test" then
  3. -->[true] "1"
  4.  
  5. if "" then
  6. -> "3" as a3
  7. else
  8. if "Other test" then
  9. -left-> "5"
  10. --> (*)
  11. else
  12. --> "6"
  13. --> (*)
  14. endif
  15. endif
  16.  
  17. else
  18. ->[false] "2"
  19. --> (*)
  20. endif
  21.  
  22. a3 --> if "last test" then
  23. --> "7"
  24. --> (*)
  25. else
  26. -> "8"
  27. --> (*)
  28. endif
  29. #+END_SRC

同步块(Synchronization)

同步块可以用“=== code ===”来表示。

  1. #+BEGIN_SRC plantuml :file ../img/plantuml-quickstart-a6.png
  2. (*) --> ===B1===
  3. --> "parallel Activity 1"
  4. --> ===B2===
  5.  
  6. ===B1=== --> "Parallel Activity 2"
  7. --> ===B2===
  8.  
  9. --> (*)
  10. #+END_SRC

一个小实例

  1. #+BEGIN_SRC plantuml :file ../img/plantuml-quickstart-a7.png
  2. (*) --> "Select site"
  3. --> "Commission architect"
  4. --> "Develop plan"
  5. --> "Bid plan" as bp
  6. if "" then
  7. -->[else] ===B1===
  8. --> "Do site work"
  9. --> ===B2===
  10. ===B1=== --> "Do trade work"
  11. --> ===B2===
  12. --> "Finish construction"
  13. --> (*)
  14. else
  15. -u->[not accepted] bp
  16. endif
  17. #+END_SRC

长文本的活动描述(Long activity description)

在定义活动的时候,有时候需要用多行文字来描述这个活动,这时我们可以 在描述里添加换行符 \n ,也可以使用少量的HTML标签。

以下是可以使用的HTML标签:

  1. <b>
  2. <i>
  3. <font size="nn"> or <size:nn> to change font size
  4. <font color="#AAAAAA"> or <font color="colorName">
  5. <color:#AAAAAA> or <color:colorName>
  6. <img:file.png> to include an image

针对较长文本描述活动,可以起一个较短别名(如:"long text" as A1), 在图形定义脚本中可以直接使用别名,参看下面的例子中的A1

  1. 1: #+BEGIN_SRC plantuml :file ../img/plantuml-quickstart-a8.png
  2. 2: (*) -l-> "this <size:20>activity</size>
  3. 3: is <b>very</b> <color:red>long</color>
  4. 4: and defined on serveral lines
  5. 5: that contains many <i>text</i>" as A1
  6. 6: -up-> "Another activity\n on serveral lines"
  7. 7:
  8. 8: A1 --> "Short activity\n<img:../img/code.png>"
  9. 9: #+END_SRC

注释(Notes)

PlantUML可以通过在脚本里使用 note 来添加注释文本块。

note commands:

  • note left
  • note right
  • note top
  • note bottom

PlantUML用上面列表里的命令来标注一个注释块的开始,然后用 end note 来标注注释块的结束。同时note命令也允许使用单行定义一个文本块, 详见下面的例子。

  1. #+BEGIN_SRC plantuml :file ../img/plantuml-quickstart-a9.png
  2. (*) --> "Some Activity" as s
  3. note right: This activity has to be defined
  4. s --> (*)
  5. note left
  6. This note is on
  7. serveral lines
  8. end note
  9. #+END_SRC

分区(Partition)

通过分区关键词 partition 可以定义一个分区,并且可以使用HTML的 颜色码或颜色名来设置分区的背景色。在你申明一个活动时,PlantUML会自动 的把这个活动对象放置到最后使用的分区中。当然,也可以使用end partitio 关闭分区定义。

  1. #+BEGIN_SRC plantuml :file ../img/plantuml-quickstart-a10.png
  2. partition Conductor
  3. (*) --> "Climbs on Platform"
  4. --> === S1 ===
  5. --> Bows
  6. end partition
  7.  
  8. partition Aduience #LightSkyBlue
  9. === S1 === --> Applauds
  10.  
  11. partition Conductor
  12. Bows --> === S2 ===
  13. --> WavesArmes
  14. Applauds --> === S2 ===
  15. end partition
  16.  
  17. partition Orchestra #CCCCEE
  18. WavesArmes --> Introduction
  19. --> "Play music"
  20. end partition
  21. #+END_SRC

图形标题(Title the diagram)

标题关键词 title 用来设置一个图形的标题文本,我们可以在 title 和 end title 两个关键词之间放置比较长的标题文本。

  1. #+BEGIN_SRC plantuml :file ../img/plantuml-quickstart-a11.png
  2. title Simple example\nof title
  3. (*) --> "First activity"
  4. --> (*)
  5. #+END_SRC

皮肤(Skinparam)

皮肤命令 skinparam 可以改变图形的颜色和字体。这些命令可以在以下 的位置中使用:

  • 在图形定义里使用,
  • 在包含的文件里使用,
  • 在一个配置文件里使用,这个配置文件一般由命令行或ANT的Task来提供。
  1. #+BEGIN_SRC plantuml :file ../img/plantuml-quickstart-a12.png
  2. skinparam backgroundColor #AAFFFF
  3. skinparam activityStartColor red
  4. skinparam activityBarColor SaddleBrown
  5. skinparam activityEndColor Silver
  6. skinparam activityBackgroundColor Peru
  7. skinparam activityBorderColor Peru
  8. skinparam activityFontName Impact
  9. skinparam activityShape octagon
  10.  
  11. (*) --> "Climbs on Platform"
  12. --> === S1 ===
  13. --> Bows
  14. --> === S2 ===
  15. --> WavesArmes
  16. --> (*)
  17. #+END_SRC

使用 skinparam activityShape octagon 命令可以把活动图形改成八角 形的。(好像没效果!)

完整示例(Complete Example)

  1. #+BEGIN_SRC plantuml :file ../img/plantuml-quickstart-a13.png
  2. 'http://click.sourceforge.net/images/activity-diagram-small.png
  3. title Servlet Container
  4.  
  5. (*) --> "ClickServlet.handleRequest()"
  6. --> "new Page"
  7.  
  8. if "Page.onSecurityCheck" then
  9. ->[true] "Page.onInit()"
  10.  
  11. if "isForward?" then
  12. ->[no] "Process controls"
  13.  
  14. if "continue processing?" then
  15. -->[yes] ===RENDERING===
  16. else
  17. -->[no] ===REDIRECT_CHECK===
  18. endif
  19.  
  20. else
  21. -->[yes] ===RENDERING===
  22. endif
  23.  
  24. if "is Post?" then
  25. -->[yes] "Page.onPost()"
  26. --> "Page.onRender()" as render
  27. --> ===REDIRECT_CHECK===
  28. else
  29. -->[no] "Page.onGet()"
  30. --> render
  31. endif
  32.  
  33. else
  34. -->[false] ===REDIRECT_CHECK===
  35. endif
  36.  
  37. if "Do redirect?" then
  38. ->[yes] "redirect request"
  39. --> ==BEFORE_DESTORY===
  40. else
  41. if "Do Forward?" then
  42. -left->[yes] "Forward request"
  43. --> ==BEFORE_DESTORY===
  44. else
  45. -right->[no] "Render page template"
  46. --> ==BEFORE_DESTORY===
  47. endif
  48. endif
  49.  
  50. --> "Page.onDestory()"
  51. -->(*)
  52. #+END_SRC

活动图Beta

Beta版本的活动图简化了活动图的符号定义,从 V7947 这个版本开始, PlantUML就开始引入了一些简化写法,当然到目前(20140627)为止还不是 很完善,但这个版本里的一些简化写法已经是PlantUML后续版本的发展方向。

下文中将会用几个简单的示例来介绍Beta版活动图的新功能,有兴趣的朋友 也可以试一下,在使用新的写法之前需要把 GraphViz 更新到最新版本。

关于更多的PlantUML版本更新信息可以参考官网页面(What's New?

简单活动(Simple Activity)

在这个例子里,活动元素从一个 : 开始,然后到一个 ; 结束。 开始和结束符号,可以用 start 和 end 两个关键词来表示。之前版 本的开始和结束符都是用同一个符号 (*) 来表示的,个人觉得新的写法 逻辑更清晰,代码可读性更高。

至于更多的文本格式,大家可以参考:Creole engine

  1. #+BEGIN_SRC plantuml :file ../img/plantuml-quickstart-ab1.png
  2. start
  3.  
  4. :Hello world;
  5.  
  6. :This is on defined on
  7. serveral **lines**;
  8.  
  9. stop
  10. #+END_SRC

条件符号(Conditional)

和之前一样,还是使用 if , then 和 else 关键词,但分支条件的 标签 Labels 可以直接写在关键词 then和 else 的后面,并用小括 号括起来就可以了(如: (Labels) )。

  1. #+BEGIN_SRC plantuml :file ../img/plantuml-quickstart-ab2.png
  2. start
  3.  
  4. if (graphviz installed?) then (yes)
  5. :process all\ndiagrams;
  6. else (no)
  7. :process only
  8. __sequence__ and __activity__ diagrams;
  9. endif
  10.  
  11. stop
  12. #+END_SRC

在新版本里除了使用 else 外,还新加了一个 elseif 关键词,有了这 个语法,我们就可以绘制一系列条件的活动图。

  1. #+BEGIN_SRC plantuml :file ../img/plantuml-quickstart-ab3.png
  2. start
  3. if (condition A) then (yes)
  4. :Text1;
  5. elseif (condition B) then (yes)
  6. :Text2;
  7. stop
  8. elseif (condition C) then (yes)
  9. :Text 3;
  10. elseif (condition D) then (yes)
  11. :Text 4;
  12. else (nothing)
  13. :Text else;
  14. endif
  15. stop
  16. #+END_SRC

重复循环(Repeat Loop)

通过 repeat 和 repeat while 关键词可以创建循环结构的图形。

  1. #+BEGIN_SRC plantuml :file ../img/plantuml-quickstart-ab4.png
  2. start
  3.  
  4. repeat
  5. :read data;
  6. :generate diagrams;
  7. repeat while (more data?)
  8.  
  9. stop
  10. #+END_SRC

条件循环(While Loop)

“条件循环”和上面的“重复循环”不太一样,上面的“重复循环”是先执行一次 循环体里的内容,然后再执行断言条件,看是否重复执行循环体;而条件循 环则将断言放到了最前面,因此它是先判断是否满足条件再执行循环体里的 内容。

要创建条件循环结构的图形可以通过使用 while 和 end while 两个关 键词来实现。如果要给条件分支加上标注,可以在 while 条件后加上一 个 is 关键词,然后用小括号括上要标注的内容;在 end while 后可 以直接用小括号括上要标注的内容。

  1. #+BEGIN_SRC plantuml :file ../img/plantuml-quickstart-ab5.png
  2. start
  3. while (data available?) is (not empty)
  4. :read data;
  5. :generate diagrams;
  6. end while (empty)
  7. stop
  8. #+END_SRC

并行处理(Parallel Processing)

fork , fork again 和 end fork 三个关键词用来表示并行处理结 构。

  1. #+BEGIN_SRC plantuml :file ../img/plantuml-quickstart-ab6.png
  2. start
  3. if (multiprocessor?) then (yes)
  4. fork
  5. :Treatment 1;
  6. fork again
  7. :Treatment 2;
  8. end fork
  9. else (monoproc)
  10. :Treatment 1;
  11. :Treatment 2;
  12. endif
  13. stop
  14. #+END_SRC

注解的文本样式(Notes)

注解里的文本样式是通过 Creole wiki syntax 来实现的。关于Creole引擎, 大家可以参考维基百科上的介绍。

  1. #+BEGIN_SRC plantuml :file ../img/plantuml-quickstart-ab7.png
  2. start
  3. :fool;
  4. note left: This is a note
  5. :foo2;
  6. note right
  7. This note is on serveral
  8. //lines// and can
  9. contain <b>HTML</b>
  10. ====
  11. * Calling the method ""foo()"" is prohibited
  12. end note
  13. stop
  14. #+END_SRC

颜色(Color)

为活动元素指定背景色可以直接在活动开始标记 : 前加上颜色描述符:

  1. #+BEGIN_SRC plantuml :file ../img/plantuml-quickstart-ab8.png
  2. start
  3. #purple:starting progress;
  4. :reading configuration files
  5. These files must do be edited at this point;
  6. #00AAAA:ending of the process;
  7. stop
  8. #+END_SRC

完整示例(Complete Example)

  1. #+BEGIN_SRC plantuml :file ../img/plantuml-quickstart-ab9.png
  2. start
  3. :ClickServlet.handleRequest();
  4. :new page;
  5. if (Page.onSecurityCheck) then (true)
  6. :(Page.onInit();
  7. if (isForward?) then (no)
  8. :Process controls;
  9. if (continue processing?) then (no)
  10. stop
  11. endif
  12.  
  13. if (isPost?) then (yes)
  14. :Page.onPost();
  15. else (no)
  16. :Page.onGet();
  17. endif
  18. :Page.onRender();
  19. endif
  20. else (false)
  21. endif
  22.  
  23. if (do redirect?) then (yes)
  24. :redirect process;
  25. else
  26. if (do forward?) then (yes)
  27. :Forward request;
  28. else (no)
  29. :Render page template;
  30. endif
  31. endif
  32.  
  33. stop
  34. #+END_SRC

plantuml使用教程【转】的更多相关文章

  1. plantuml 基本语法(转摘)

    <div id="topics"> <div class="post"> <h1 class="postTitle&qu ...

  2. idea使用PlantUML画类图教程

    嗯,在学设计模式时,画类图画的麻烦,就查了一下,发现idea可以通过插件实现.查了一下,学习,整理笔记和大家交流.  2019.9.11 安装可以百度,网上有好多. PlantUML 官网.如果时间多 ...

  3. CSDN Markdown简明教程4-UML画画

    0.文件夹 文件夹 前言 序列图 1 序列图演示样例 2 序列图语法 流程图 1 流程图演示样例 2 流程图语法 节点定义 节点连接 Gravizo 声明 1. 前言 Markdown是一种轻量级的标 ...

  4. 使用 PlantUML 高效画图

    PlantUML 是一种程序员看了就会爱上的画图方式:自然,高效. 支持快速绘制: 时序图 类图 用例图 活动图 状态图 等等 安装教程 Intellij IDEA中安装 & 使用PlantU ...

  5. 转+更新 Graphviz 教程,例子+ 高级应用 写代码,编程绘制架构图(分层拓扑图) 转自官网

    1. Graphviz介绍 Graphviz是大名鼎鼎的贝尔实验室的几位牛人开发的一个画图工具. 它的理念和一般的“所见即所得”的画图工具不一样,是“所想即所得”. Graphviz提供了dot语言来 ...

  6. Windows平台下在Emacs中使用plantuml中文乱码问题(已解决)

    Windows平台下在Emacs中使用plantuml中文乱码问题(已解决) */--> code {color: #FF0000} pre.src {background-color: #00 ...

  7. Angular2入门系列教程7-HTTP(一)-使用Angular2自带的http进行网络请求

    上一篇:Angular2入门系列教程6-路由(二)-使用多层级路由并在在路由中传递复杂参数 感觉这篇不是很好写,因为涉及到网络请求,如果采用真实的网络请求,这个例子大家拿到手估计还要自己写一个web ...

  8. Angular2入门系列教程6-路由(二)-使用多层级路由并在在路由中传递复杂参数

    上一篇:Angular2入门系列教程5-路由(一)-使用简单的路由并在在路由中传递参数 之前介绍了简单的路由以及传参,这篇文章我们将要学习复杂一些的路由以及传递其他附加参数.一个好的路由系统可以使我们 ...

  9. Angular2入门系列教程5-路由(一)-使用简单的路由并在在路由中传递参数

    上一篇:Angular2入门系列教程-服务 上一篇文章我们将Angular2的数据服务分离出来,学习了Angular2的依赖注入,这篇文章我们将要学习Angualr2的路由 为了编写样式方便,我们这篇 ...

随机推荐

  1. hbase Problem binding to node1/192.168.1.13:16020 : 地址已在使用

    这是hbase 从0.9.x升级到1.x后HMaster与HRegionServer端口冲突问题 在hbase升级到1.0.0版本后,默认端口做了改动.其中16020端口是hmaster服务和hreg ...

  2. Scrum立会报告+燃尽图(十月二十九日总第二十次)

    此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2288 项目地址:https://git.coding.net/zhang ...

  3. mysql+linux 忘记密码

    方法一: # /etc/init.d/mysql stop # mysqld_safe --user=mysql --skip-grant-tables --skip-networking & ...

  4. HDU 5496 Beauty of Sequence

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5496 Beauty of Sequence Problem Description Sequence ...

  5. Android 8悬浮窗适配

    背景 APP推出时,提示是退出还是更改账号,这个提示框是系统级别的.然而我的Android 9 会崩溃,宁外一个小伙伴Android 7运行理想.报错提示permission denied for w ...

  6. PXE Centos7和Centos6

    外网网卡:192.168.23.10, 内网网卡:192.168.10.2 PXE(preboot execute environment,预引导执行环境)是由Intel公司开发的最新技术,工作于Cl ...

  7. CentOS安装crontab及使用方法(转)

    CentOS安装crontab及使用方法(转)    安装crontab:[root@CentOS ~]# yum install vixie-cron[root@CentOS ~]# yum ins ...

  8. 手机uc不支持伪元素使用animation动画;移动端background-attachment:fixed不兼容性

    20170503 1.手机uc不支持伪元素使用animation动画 (暂未解决) 2.移动端background-attachment:fixed不兼容性,没有任何效果, element:befor ...

  9. Hibernate 中 load() 和 get() 的区别

    get 和 load 方式都是是根据 id 取得一个记录.下边详细说一下 get 和 load 的不同,因为有些时候为了对比也会把 find 加进来. 1.从返回结果上对比: load 方式检索不到的 ...

  10. 第103天:CSS3中Flex布局(伸缩布局)详解

    一.Flex布局 Flex是Flexible Box的缩写,意为”弹性布局”,用来为盒状模型提供最大的灵活性. 任何一个容器都可以指定为Flex布局. .box{ display: flex; } 行 ...