首发于迷思
那些年,我追过的绘图语言(续)

那些年,我追过的绘图语言(续)

自从上一篇文章发布后,大家给我推荐了不少绘图工具,比如startUML,rose,TikZ package,flowchart.js,matlab,R等等。感兴趣的可以自行研究。至于matlab/R这样的工具,虽然强大,但跟本文讨论的画一般意义的设计图(如uml图)无关。


鉴于很多读者想进一步了解plantUML,这篇文章就多讲讲PlantUML。


plantUML支持如下UML图:


  • Sequence diagram

  • Usecase diagram

  • Class diagram

  • Activity diagram

  • Component diagram

  • State diagram

  • Object diagram

  • GUI Wireframe

这里面,我用的最多的是sequence diagram(序列图)和activity diagram(活动图),也就主要讲讲这两个图,其它的请自行阅读plantUML的文档。


Sequence diagram


sequence diagram里每个角色被称为participant,participant之间可以有message,比如这样一个最基本的序列图:


@startuml                       // (1)
participant Tars                // (2)
actor Copper                    // (3)
database Murphy

Tars -> Copper                  // (4)
Copper --> Murphy: morse code   // (5)
@enduml                         // (6)

(1) 声明一个图形的起始

(2) 声明一个participant,可以省略

(3) 如果想使用其它图例(不是participant),则不能省略

(4) 声明两个participants间的消息, -→ 为虚线, → 为实线

(5) 消息可以添加说明

(6) 声明一个图形的结束

生成出来如下图所示:



相信不用解释,大家都懂。


你可以尝试将participant换成如下图示:


  • actor

  • boundary

  • control

  • entity

  • database

会有不同的效果。如果你的participant的名字很复杂,可以使用 as 起别名:


@startuml
actor "星际穿越的\n<b>男主角</b>" as copper #99ff99 // (1) (2) (3)
actor "星际穿越的\n男主女儿" as murphy #red

copper -[#orange]> murphy: 爱和<font color=red>引力</font>可以穿越时空      // (4) (5)
@enduml

(1) 可以给participant起别名,别名不必和显示的字符一致

(2) 显示的字符可以使用\n等ascii控制字符,也可以使用html标签

(3) participant可以在结尾赋一个颜色

(4) message可以在 - 和 > 间插入一个颜色,以 [] 区隔

(5) startuml支持中文,如果编译时遇到问题,请查看charset设置(设成utf-8)

生成出来的图表如下:


如果明白了这两个例子,咱们继续:


@startuml
scale 1024*768      (1)
[--> Tars: "They" provides data inside singularity (2)

activate Tars       (3)
Tars -> Copper: sending data
activate Copper

Copper -> Copper: translate it to morse code    (4)
activate Murphy

Copper -> Murphy: send morse code through watch

Copper -> Tars: ask for next batch
deactivate Copper   (5)

Murphy -> Murphy: record and parse morse code

Murphy -->]: figured out the formula    (6)

deactivate Murphy
deactivate Tars
@enduml

(1) 我们希望生成的图片大一些

(2) [→(注意中间不要有空格),传入到当前序列图的消息(participant不在该图中)

(3)(5) activate / deactivate 用于指定participant的lifeline

(4) participant可以发消息给自己

(6) →](中间不要有空格),传出当前序列图的消息(participant不在该图中)

生成的图表如下:


Activity diagram


有了sequence diagram的基础,学习activity diagram易如反掌,直接上代码,不过多解释(程序猿应该对if else很熟悉了):


@startuml

scale 1024*768

start

if (exec Lazarus?) then (yes)
    :find a livable planet;    (*)
    :save **human beings**;
else (no)
    :keep adapting,
    __keep farming__ and <font color=red>keep dying</font>;
endif

stop

@enduml

(*) 一个activity以 : 开始,以 ; 结束。有了sequential diagram的基础,开始写activity diagram总会忘记后面的分号。嗯,你忘呀忘呀,错呀错呀,就慢慢习惯了。


这个生成的图表如下:



来个进阶的:


@startuml

scale 512*1024

|Romilly|  (1)
start
repeat     (2)
    :record the data from black hole;
    :keep waiting;
repeat while (Copper & Brand are not back?)

|#AntiqueWhite|Copper| (3)

:enter the Endurance;

while (has more video tapes?)  (4)
    :watch it;
    :cry;
endwhile

end

@enduml

(1)(3) 使用 | 创建带泳道的活动图,自泳道声明以下的活动都属于该泳道,泳道可以定义颜色

(2)(4) 两种不同的循环方式,像不像写代码?

几乎一下子就能看懂了,是不?

生成的图表如下:



继续进阶:


@startuml
scale 1024*768

start

:first planet: Miller;

fork        (1)
    :Romilly: stay in the Endurance;
fork again  (2)
    :Copper et al: go to planet Miller;
    :giant wave comes;
    fork
        :Copper found wave, but helpless;
    fork again
        :Brand is racing against the wave;
    fork again
        :Doyle wait for Brand;
        :Doyle died;
        kill    (3)
    endfork
    :they finally left the planet;
endfork         (4)

@enduml

(1)(2)(4) fork,fork again,endfork 用来描述并发线程

(3) kill 终结一个线程,plantuml的例子中使用 detach,经测试,detach 不可用

生成的图表如下:


最后,; 作为一个活动的终止,这是最标准的图例;如果将每个活动最后的 ; 换成其它符号:|,<,>,/,},可以显示不同的图例。不解释,具体看下述代码和对应的图表:


@startuml
scale 2
:Ready;
:next(o)|
:Receiving;
split
 :nak(i)<
 :ack(o)>
split again
 :ack(i)<
 :next(o)
 on several line|
 :i := i + 1]
 :ack(o)>
split again
 :err(i)<
 :nak(o)>
split again
 :foo/
split again
 :i > 5}
stop
end split
:finish;
@enduml

生成的图表:



就这些,应该够你学一阵子的啦。


最近「奇博士的管理课」正在构思第二章的内容,更新有点慢,请稍安勿躁。感兴趣可以点击「阅读原文」在百度阅读订阅。 如果您觉得这篇文章不错,请点赞。多谢!


欢迎订阅公众号『程序人生』(搜索微信号 programmer_life)。每篇文章都力求原汁原味,早8点与您相会。

发布于 2014-11-25 07:51