plantuml使用教程

您所在的位置:网站首页 有道云笔记标签 plantuml使用教程

plantuml使用教程

2024-07-12 16:28| 来源: 网络整理| 查看: 265

Table of Contents 前言什么是PlantUML在Emacs里配置PlantUML(参考:Run it from Emacs)其他软件里的PlantUML下载和安装如何使用 顺序图(Sequence Diagram) 简单示例注释语句申明参与者使用非字母的参与者名称(Use non-letters in participants)发送消息给自己(Message to Self)改变箭头的样式(Change arrow style)改变箭头的颜色(Change arrow color)消息序号(Message sequence numbering)标题(Title)图形图例(Legend the diagram)分割图形(Splitting diagrams)消息分组(Grouping message)消息注解(Notes on messages)一些其他的注解方式(Some other notes)使用HTML进行格式化(Formatting using HTML) 用例图(Use Case Diagram) 用例(Usecase)参与者(Actors)示例 类图(Class Diagram) 示例1 活动图(Activity Diagram) 简单活动(Simple Activity)带标注的箭头(Label on arrows)改变箭头的方向(Changing arrow direction)分支(Branches)多分支(More on Branches)同步块(Synchronization)长文本的活动描述(Long activity description)注释(Notes)分区(Partition)图形标题(Title the diagram)皮肤(Skinparam)完整示例(Complete Example) 活动图Beta 简单活动(Simple Activity)条件符号(Conditional)重复循环(Repeat Loop)条件循环(While Loop)并行处理(Parallel Processing)注解的文本样式(Notes)颜色(Color)完整示例(Complete Example) 前言

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

编写本文的目的旨在记录个人在学习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和二进制 图片。下面是一个简单的示例:

#+BEGIN_SRC plantuml :file ../img/orgmode-babel-sequenceuml.png Alice -> Bob: synchronous call Alice ->> Bob: asynchronous call #+END_SRC

orgmode-babel-sequenceuml.png

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

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

下载 plantuml.jar 到你的硬盘上(官网下载页面)

安装生成图片用的软件Graphviz

## 如果是Ubuntu系统,可以直接运行下面的命令安装 sudo apt-get install graphviz

在 .emacs 里添加配置,把 plantuml 添加到 org-babel-load-languages 加载语言列表里。

;; active Org-babel languages (org-babel-do-load-languages 'org-babel-load-languages '(;; other Babel languages (plantuml . t)))

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

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

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

其他软件里的PlantUML下载和安装 PlantUML官网首页PlantUML各版本的下载页面 (英文)在各种软件里运行PlantUML的相关配置 (英文)ScreenshotsQuestion & Answer 如何使用

顺序图(Sequence Diagram)

简单示例

顺序图用 -> , -->, Alice: Authentication Response Alice -> Bob: Another atuhentication Request Alice Foo2 : To boundary Foo1 -> Foo3 : To control Foo1 -> Foo4 : To entity Foo1 -> Foo5 : To database #+END_SRC

plantuml-quickstart-s2.png

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

#+BEGIN_SRC plantuml :file ../img/plantuml-quickstart-s3.png actor Bob #red ' The only defference between actor ' and participant is the drawing participant Alice participant "I have a really\nlong name" as L #99ff99 /' You can also declare: participant L as "I have a really\nlong name" #99ff99 '/ Alice -> Bob: Authentication Request Bob -> Alice: Authentication Response Bob -> L: Log transaction #+END_SRC

plantuml-quickstart-s3.png

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

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

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

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

plantuml-quickstart-s4.png

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

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

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

plantuml-quickstart-s5.png

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

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

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

plantuml-quickstart-s7.png

消息序号(Message sequence numbering)

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

#+BEGIN_SRC plantuml :file ../img/plantuml-quickstart-s8.png autonumber Bob -> Alice : Authentication Request Bob Alice : Authentication Request Bob Alice : Another authentication Request Bob Alice : Yet another authentication Request Bob Alice : Authentication Request Bob Alice : Another authentication Request Bob Alice : Yet another authentication Request Bob Bob : Authentication Request Bob --> Alice : Authentication Response #+END_SRC

plantuml-quickstart-s11.png

图形图例(Legend the diagram)

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

#+BEGIN_SRC plantuml :file ../img/plantuml-quickstart-s12.png Alice -> Bob : Hello legend right Short legend endlegend #+END_SRC

plantuml-quickstart-s12.png

分割图形(Splitting diagrams)

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

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

#+BEGIN_SRC plantuml :file ../img/plantuml-quickstart-s13.png Bliss -> Tia : I love you Bliss -> Tia : I miss you newpage Bliss -> Tia : Let's go home Bliss -> Tia : Quick newpage A title for the\nlast page Tia -> Bliss : Give me money Tia -> Bliss : No money No love #+END_SRC

消息分组(Grouping message)

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

alt/elseoptloopparbreakcriticalgroup, 这个关键词后面的文字会作为组名显示在图形上

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

#+BEGIN_SRC plantuml :file ../img/plantuml-quickstart-s14.png Alice -> Bob: Authentication Request alt successful case Bob -> Alice: Authentication Accepted else some kind of failure Bob -> Alice: Atuhentication Failue group My own label Alice -> Log : Log attack start loop 1000 times Alice -> Bob: DNS Attack end Alice -> Log : Loag alice end end else Another type of failue Bob -> Alice: Please repeat end #+END_SRC

plantuml-quickstart-s14.png

消息注解(Notes on messages)

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

#+BEGIN_SRC plantuml :file ../img/plantuml-quickstart-s15.png Alice -> Bob : hello note left: this is a first note Bob -> Alice : ok note right: this is anther note Bob -> Bob : I am thinking note left a note can also be defined on several lines end note #+END_SRC

plantuml-quickstart-s15.png

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

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

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

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

#+BEGIN_SRC plantuml :file ../img/plantuml-quickstart-s16.png participant Alice participant Bob note left of Alice #aqua This is displayed left of Alice. end note note right of Alice: This is displayed right of Alice. note over Alice: This displayed over Alice. note over Alice, Bob #FFAAAA: This is displayed\n over Bob and Alice. note over Bob, Alice This is yet another example of a long note. end note #+END_SRC

plantuml-quickstart-s16.png

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

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

加粗文本 或 或 用来加下划线 斜体 或 或 用来加删除线 或 或 用来加波浪线 或 用来设置文本颜色 或 用来设置背景色 设置字体大小 或 用来添加图片,图片文件必须 是可以访问得到才行。 或 用来添加一个互 联网图片,同样的图片地址必须是可用的才行。 #+BEGIN_SRC plantuml :file ../img/plantuml-quickstart-s17.png participant Alice participant "The Famous Bob" as Bob Alice -> Bob : A well formated message note right of Alice This is displayed left of Alice. end note note left of Bob This is displayed left of Alice Bob end note note over Alice, Bob This is hosted by end note #+END_SRC

plantuml-quickstart-s17.png

用例图(Use Case Diagram)

用例(Usecase)

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

#+BEGIN_SRC plantuml :file ../img/plantuml-quickstart-u1.png @startuml (Usecase One) (Usecase Two) as (UC2) usecase UC3 usecase (Last\nusecase) as UC4 @enduml #+END_SRC

plantuml-quickstart-u1.png

参与者(Actors)

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

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

plantuml-quickstart-u2.png

示例

#+BEGIN_SRC plantuml :file ../img/plantuml-quickstart-u99.png left to right direction skinparam packageStyle rect actor customer actor clerk rectangle checkout { customer -- (checkout) (checkout) .> (payment) : include (help) .> (checkout) : extends (checkout) -- clerk } #+END_SRC

plantuml-quickstart-u99.png

类图(Class Diagram)

示例1

class Company { } class Department { name : Name } class Office { address : String phone : Number } class Headquarters { } class Person { name : Name employeeID : Integer title : String getPhoto() : Photo getPhone() : Number getContactInformation() : ContactInformation getPersonalRecords() : PersonnelRecord } class ContactInformation { address : String } class PersonnelRecord { taxID employmentHistory salary } Company "1" *-do- "1..*" Department Company "1" *-do- "1..*" Office '(Department, Office) -up-* Company Department "*" -ri- "*" Office : Location > Department "*" --* "0..1" Department Office


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3