使用Emacs / IDEA 敲出UML,PlantUML快速指南(新手使用教程)

原文地址:http://archive.3zso.com/archives/plantuml-quickstart.html

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和二進制 圖片。下面是一個簡單的示例:

    #+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

  1. 下載 plantuml.jar 到你的硬盤上(官網下載頁面
  2. 安裝生成圖片用的軟件Graphviz

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

  3. 在 .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,複製上面的示例代碼試一下吧!


如何使用

 
  • 順序圖(Sequence Diagram)
    • 簡單示例

      順序圖用 -> , --><-<-- 來繪製參與者(Participants)之 間的消息(Message)。

      #+BEGIN_SRC plantuml :file ../img/plantuml-quickstart-s1.png
        Alice -> Bob: Authentication Request
        Bob –> Alice: Authentication Response
      
        Alice -> Bob: Another atuhentication Request
        Alice <– Bob: Another authentication Response
      
      
      
      
      
#+END_SRC

plantuml-quickstart-s1.png


  • 註釋語句
  • 以單引號開始頭行,即是一個單行註釋。多行註釋可以使用”’”表 示註釋內容的開始,然後使用”’”來表示註釋內容的結束。

  • 申明參與者
  • 申明參與者,可以使用 participant 關鍵詞,也可以使用下面的參與者 分類關鍵詞來申明參與者:

    • actor
    • boundary
    • control
    • entity
    • database

    不同的參與者類型,其圖標也是不一樣的。

    #+BEGIN_SRC plantuml :file ../img/plantuml-quickstart-s2.png
      actor Foo1
      boundary Foo2
      control Foo3
      entity Foo4
      database Foo5
      Foo1 -> 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 <<-\\o Alice
    
    
    
    
    
    #+END_SRC

    plantuml-quickstart-s6.png


  • 改變箭頭的顏色(Change arrow color)
  • 要改變箭頭的顏色,可以使用HTML顏色符號,參看下面的例子:

    #+BEGIN_SRC plantuml :file ../img/plantuml-quickstart-s7.png
      Bob -[#red]> Alice : hello
      Alice -[#0000FF]->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 Response
    
    
    
    
    
    #+END_SRC

    plantuml-quickstart-s8.png

    如果需要指定一個起始號碼,可以直接在 autonumber 後面加個數字就行 了,如果要設置自增量,再在後面加一個數字就行了( autonumber start increment )。

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

    plantuml-quickstart-s9.png

    我們也可以爲序號指定數字格式,這個格式化的過程實際上是JavaDecimalFormat 來執行的( 0 表示數字, # 缺省補零位數)。

    同樣的,也可以使用一些HTML標籤來控制數字的樣式。

    #+BEGIN_SRC plantuml :file ../img/plantuml-quickstart-s10.png
      autonumber "<b>[000]"
      Bob -> Alice : Authentication Request
      Bob <- Alice : Authentication Response
    
      autonumber 15 "<b>(<u>##</u>)"
      Bob -> Alice : Another authentication Request
      Bob <- Alice : Another authentication Response
    
      autonumber 40 10 "<font color=red>Message 0  "
      Bob -> Alice : Yet another authentication Request
      Bob <- Alice : Yet another authentication Response
    
    
    
    
    
    #+END_SRC

    plantuml-quickstart-s10.png


  • 標題(Title)
  • 要給圖形加一個標題可以用 title 關鍵詞來設置。

    #+BEGIN_SRC plantuml :file ../img/plantuml-quickstart-s11.png
      title Simple Comunication example
    
      Alice -> 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/else
    • opt
    • loop
    • par
    • break
    • critical
    • group, 這個關鍵詞後面的文字會作爲組名顯示在圖形上

    上面的關鍵詞後可以添加一些文本用來顯示在頭部(注: 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標籤來格式化文本:

    • <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> 用來添加一個互 聯網圖片,同樣的圖片地址必須是可用的才行。
    #+BEGIN_SRC plantuml :file ../img/plantuml-quickstart-s17.png
      participant Alice
      participant “The <b>Famous</b> Bob” as Bob
    
      Alice -> Bob : A <i>well formated</i> message
      note right of Alice
        This is <back:cadetblue><size:18>displayed</size></back>
        <u>left of</u> Alice.
      end note
      note left of Bob
        <u:red>This</u> is <color #118888>displayed</color>
        <b><color purple>left of</color> <s:red>Alice</strike> Bob</b>
      end note
      note over Alice, Bob
        <w:#FF33FF>This is hosted</w> by <img ../img/code.png>
      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 <|-do- Headquarters
      Department "*" -do- "member 1..*" Person
      Person "1 {subsets member}" -u- "*" Department : manager
      Person .ri.> ContactInformation
      Person ..> PersonnelRecord
      PersonnelRecord -() ISecureInformation
      

  • 活動圖(Activity Diagram)
    • 簡單活動(Simple Activity)

      在活動圖中,你可以使用 (*) 來表示活動開始點和結束點。使用 --> 來表示箭頭。

      #+BEGIN_SRC plantuml :file ../img/plantuml-quickstart-a1.png
        (*) –> “First Activity”
        “First Activity” –> (*)
      
      
      
      
      
    #+END_SRC

    plantuml-quickstart-a1.png

  • 帶標註的箭頭(Label on arrows)

    缺省情況下,活動圖的箭頭是沒有標註的。但我們可以通過方括號 [labels]來設置標註,只要把它放在箭頭定義的後面就可以了。

    #+BEGIN_SRC plantuml :file ../img/plantuml-quickstart-a2.png
      (*) –> “First Activity”
      –>[You can put also labels] “Second Activity”
      –>(*)
    
    
    
    
    
  • #+END_SRC

    plantuml-quickstart-a2.png

  • 改變箭頭的方向(Changing arrow direction)

    我們可以使用 -> 創建一個水平箭頭,也可以通過下面的方式來改變箭頭 的方向:

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

    plantuml-quickstart-a3.png

    在描述箭頭時, up|down|left|right 這幾個單詞的寫法可以簡化, 用單詞開頭的一個或兩個字母來替換就行了,比如 -down-> 也可以寫成 -d-> 或者-do-> 。


  • 分支(Branches)
  • 在PlantUML裏,我們可以使用 if/then/else 關鍵詞來定義分支。

    #+BEGIN_SRC plantuml :file ../img/plantuml-quickstart-a4.png
      (*) –> “Initialisation”
    
      if ” Some Test” then
        –>[ture] “Some Activity”
        –> “Another Activity”
        -right-> (*)
      else
        ->[false] “Something else”
        –>[Ending process] (*)
      endif
    
    
    
    
    
    #+END_SRC

    plantuml-quickstart-a4.png


  • 多分支(More on Branches)
  • 直接給例子:

    #+BEGIN_SRC plantuml :file ../img/plantuml-quickstart-a5.png
      (*) –> if “Some Test” then
        –>[true] “1”
    
        if “” then
          -> “3” as a3
        else
          if “Other test” then
            -left-> “5”
            –> (*)
          else
            –> “6”
            –> (*)
          endif
        endif
    
      else
        ->[false] “2”
        –> (*)
      endif
    
      a3 –> if “last test” then
        –> “7”
        –> (*)
      else
        -> “8”
        –> (*)
      endif
    
    
    
    
    
    #+END_SRC

    plantuml-quickstart-a5.png


  • 同步塊(Synchronization)
  • 同步塊可以用“=== code ===”來表示。

    #+BEGIN_SRC plantuml :file ../img/plantuml-quickstart-a6.png
      (*) –> ===B1===
      –> “parallel Activity 1”
      –> ===B2===
    
      ===B1=== –> “Parallel Activity 2”
      –> ===B2===
    
      –> (*)
    
    
    
    
    
    #+END_SRC

    plantuml-quickstart-a6.png

    一個小實例

    #+BEGIN_SRC plantuml :file ../img/plantuml-quickstart-a7.png
      (*) --> "Select site"
      --> "Commission architect"
      --> "Develop plan"
      --> "Bid plan" as bp
      if "" then
        -->[else] ===B1===
        --> "Do site work"
        --> ===B2===
        ===B1=== --> "Do trade work"
        --> ===B2===
        --> "Finish construction"
        --> (*)
      else
        -u->[not accepted] bp
      endif
    
    
    
    
    
    #+END_SRC

    plantuml-quickstart-a7.png


  • 長文本的活動描述(Long activity description)
  • 在定義活動的時候,有時候需要用多行文字來描述這個活動,這時我們可以 在描述裏添加換行符 \n ,也可以使用少量的HTML標籤。

    以下是可以使用的HTML標籤:

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

    針對較長文本描述活動,可以起一個較短別名(如:“long text” as A1), 在圖形定義腳本中可以直接使用別名,參看下面的例子中的A1

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

    plantuml-quickstart-a8.png


  • 註釋(Notes)

    PlantUML可以通過在腳本里使用 note 來添加註釋文本塊。

    note commands:

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

    PlantUML用上面列表裏的命令來標註一個註釋塊的開始,然後用 end note來標註註釋塊的結束。同時note命令也允許使用單行定義一個文本塊, 詳見下面的例子。

    #+BEGIN_SRC plantuml :file ../img/plantuml-quickstart-a9.png
      (*) –> “Some Activity” as s
      note right: This activity has to be defined
      s –> (*)
      note left
        This note is on
        serveral lines
      end note
    
    
    
    
    
  • #+END_SRC

    plantuml-quickstart-a9.png


  • 分區(Partition)
  • 通過分區關鍵詞 partition 可以定義一個分區,並且可以使用HTML的 顏色碼或顏色名來設置分區的背景色。在你申明一個活動時,PlantUML會自動 的把這個活動對象放置到最後使用的分區中。當然,也可以使用 end partitio 關閉分區定義。

    #+BEGIN_SRC plantuml :file ../img/plantuml-quickstart-a10.png
      partition Conductor
      (*) –> “Climbs on Platform”
      –> === S1 ===
      –> Bows
      end partition
    
      partition Aduience #LightSkyBlue
      === S1 === –> Applauds
    
      partition Conductor
      Bows –> === S2 ===
      –> WavesArmes
      Applauds –> === S2 ===
      end partition
    
      partition Orchestra #CCCCEE
      WavesArmes –> Introduction
      –> “Play music”
     end partition
    
    
    
    
    
    #+END_SRC

    plantuml-quickstart-a10.png


  • 圖形標題(Title the diagram)
  • 標題關鍵詞 title 用來設置一個圖形的標題文本,我們可以在 title 和end title 兩個關鍵詞之間放置比較長的標題文本。

    #+BEGIN_SRC plantuml :file ../img/plantuml-quickstart-a11.png
      title Simple example\nof title
      (*) –> “First activity”
      –> (*)
    
    
    
    
    
    #+END_SRC

    plantuml-quickstart-a11.png


  • 皮膚(Skinparam)
  • 皮膚命令 skinparam 可以改變圖形的顏色和字體。這些命令可以在以下 的位置中使用:

    • 在圖形定義裏使用,
    • 在包含的文件裏使用,
    • 在一個配置文件裏使用,這個配置文件一般由命令行或ANT的Task來提供。
    #+BEGIN_SRC plantuml :file ../img/plantuml-quickstart-a12.png
      skinparam backgroundColor #AAFFFF
      skinparam activityStartColor red
      skinparam activityBarColor SaddleBrown
      skinparam activityEndColor Silver
      skinparam activityBackgroundColor Peru
      skinparam activityBorderColor Peru
      skinparam activityFontName Impact
      skinparam activityShape octagon
    
      (*) –> “Climbs on Platform”
      –> === S1 ===
      –> Bows
      –> === S2 ===
      –> WavesArmes
      –> (*)
    
    
    
    
    
    #+END_SRC

    plantuml-quickstart-a12.png

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


  • 完整示例(Complete Example)
  • #+BEGIN_SRC plantuml :file ../img/plantuml-quickstart-a13.png
      ‘http://click.sourceforge.net/images/activity-diagram-small.png
      title Servlet Container
    
      (*) –> “ClickServlet.handleRequest()”
      –> “new Page”
    
      if “Page.onSecurityCheck” then
        ->[true] “Page.onInit()”
    
        if “isForward?” then
          ->[no] “Process controls”
    
          if “continue processing?” then
            –>[yes] ===RENDERING===
          else
            –>[no] ===REDIRECT_CHECK===
          endif
    
        else
          –>[yes] ===RENDERING===
        endif
    
        if “is Post?” then
          –>[yes] “Page.onPost()”
          –> “Page.onRender()” as render
          –> ===REDIRECT_CHECK===
        else
          –>[no] “Page.onGet()”
          –> render
        endif
    
      else
        –>[false] ===REDIRECT_CHECK===
      endif
    
      if “Do redirect?” then
        ->[yes] “redirect request”
        –> ==BEFORE_DESTORY===
      else
        if “Do Forward?” then
          -left->[yes] “Forward request”
          –> ==BEFORE_DESTORY===
        else
          -right->[no] “Render page template”
          –> ==BEFORE_DESTORY===
        endif
      endif
    
      –> “Page.onDestory()”
      –>(*)
    
    
    
    
    
    #+END_SRC

    plantuml-quickstart-a13.png



  • 活動圖Beta
  • Beta版本的活動圖簡化了活動圖的符號定義,從 V7947 這個版本開始, PlantUML就開始引入了一些簡化寫法,當然到目前(20140627)爲止還不是 很完善,但這個版本里的一些簡化寫法已經是PlantUML後續版本的發展方向。

    下文中將會用幾個簡單的示例來介紹Beta版活動圖的新功能,有興趣的朋友 也可以試一下,在使用新的寫法之前需要把 GraphViz 更新到最新版本。

    關於更多的PlantUML版本更新信息可以參考官網頁面(What’s New?

    • 簡單活動(Simple Activity)

      在這個例子裏,活動元素從一個 : 開始,然後到一個 ; 結束。 開始和結束符號,可以用 start 和 end 兩個關鍵詞來表示。之前版 本的開始和結束符都是用同一個符號 (*) 來表示的,個人覺得新的寫法 邏輯更清晰,代碼可讀性更高。

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

      #+BEGIN_SRC plantuml :file ../img/plantuml-quickstart-ab1.png
        start
      
        :Hello world;
      
        :This is on defined on
        serveral **lines**;
      
        stop
      
      
      
      
      
    #+END_SRC

    plantuml-quickstart-ab1.png

  • 條件符號(Conditional)

    和之前一樣,還是使用 if , then 和 else 關鍵詞,但分支條件的 標籤Labels 可以直接寫在關鍵詞 then 和 else 的後面,並用小括 號括起來就可以了(如: (Labels) )。

    #+BEGIN_SRC plantuml :file ../img/plantuml-quickstart-ab2.png
      start
    
      if (graphviz installed?) then (yes)
        :process all\ndiagrams;
      else (no)
        :process only
        __sequence__ and __activity__ diagrams;
      endif
    
      stop
    
    
    
    
    
  • #+END_SRC

    plantuml-quickstart-ab2.png

    在新版本里除了使用 else 外,還新加了一個 elseif 關鍵詞,有了這 個語法,我們就可以繪製一系列條件的活動圖。

    #+BEGIN_SRC plantuml :file ../img/plantuml-quickstart-ab3.png
      start
      if (condition A) then (yes)
        :Text1;
      elseif (condition B) then (yes)
        :Text2;
        stop
      elseif (condition C) then (yes)
        :Text 3;
      elseif (condition D) then (yes)
        :Text 4;
      else (nothing)
        :Text else;
      endif
      stop
    
    
    
    
    
    #+END_SRC

    plantuml-quickstart-ab3.png

  • 重複循環(Repeat Loop)

    通過 repeat 和 repeat while 關鍵詞可以創建循環結構的圖形。

    #+BEGIN_SRC plantuml :file ../img/plantuml-quickstart-ab4.png
      start
    
      repeat
        :read data;
        :generate diagrams;
      repeat while (more data?)
    
      stop
    
    
    
    
    
  • #+END_SRC

    plantuml-quickstart-ab4.png

  • 條件循環(While Loop)

    “條件循環”和上面的“重複循環”不太一樣,上面的“重複循環”是先執行一次 循環體裏的內容,然後再執行斷言條件,看是否重複執行循環體;而條件循 環則將斷言放到了最前面,因此它是先判斷是否滿足條件再執行循環體裏的 內容。

    要創建條件循環結構的圖形可以通過使用 while 和 end while 兩個關 鍵詞來實現。如果要給條件分支加上標註,可以在 while 條件後加上一 個 is 關鍵詞,然後用小括號括上要標註的內容;在 end while 後可 以直接用小括號括上要標註的內容。

    #+BEGIN_SRC plantuml :file ../img/plantuml-quickstart-ab5.png
      start
      while (data available?) is (not empty)
        :read data;
        :generate diagrams;
      end while (empty)
      stop
    
    
    
    
    
  • #+END_SRC

    plantuml-quickstart-ab5.png

  • 並行處理(Parallel Processing)

    fork , fork again 和 end fork 三個關鍵詞用來表示並行處理結 構。

    #+BEGIN_SRC plantuml :file ../img/plantuml-quickstart-ab6.png
      start
      if (multiprocessor?) then (yes)
        fork
          :Treatment 1;
        fork again
          :Treatment 2;
        end fork
      else (monoproc)
        :Treatment 1;
        :Treatment 2;
      endif
      stop
    
    
    
    
    
  • #+END_SRC

    plantuml-quickstart-ab6.png

  • 註解的文本樣式(Notes)

    註解裏的文本樣式是通過 Creole wiki syntax 來實現的。關於Creole引擎, 大家可以參考維基百科上的介紹。

    #+BEGIN_SRC plantuml :file ../img/plantuml-quickstart-ab7.png
      start
      :fool;
      note left: This is a note
      :foo2;
      note right
        This note is on serveral
        //lines// and can
        contain <b>HTML</b>
        ====
        * Calling the method “”foo()”” is prohibited
      end note
      stop
    
    
    
    
    
  • #+END_SRC

    plantuml-quickstart-ab7.png

  • 顏色(Color)

    爲活動元素指定背景色可以直接在活動開始標記 : 前加上顏色描述符:

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

    plantuml-quickstart-ab8.png

  • 完整示例(Complete Example)
    #+BEGIN_SRC plantuml :file ../img/plantuml-quickstart-ab9.png
      start
      :ClickServlet.handleRequest();
      :new page;
      if (Page.onSecurityCheck) then (true)
        :(Page.onInit();
        if (isForward?) then (no)
          :Process controls;
          if (continue processing?) then (no)
            stop
          endif
    
          if (isPost?) then (yes)
            :Page.onPost();
          else (no)
            :Page.onGet();
          endif
          :Page.onRender();
        endif
      else (false)
      endif
    
      if (do redirect?) then (yes)
        :redirect process;
      else
        if (do forward?) then (yes)
          :Forward request;
        else (no)
          :Render page template;
        endif
      endif
    
      stop
    
    
    
    
    
  • #+END_SRC

    plantuml-quickstart-ab9.png



    發表評論
    所有評論
    還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
    相關文章