天天看點

PlantUML-程式員繪圖工具「流程圖、用例圖、時序圖等等」

前言

1、PlantUML 是一個開源項目,支援快速繪制時序圖、用例圖、類圖、活動圖、元件圖、狀态圖、對象圖、部署圖等。

2、PlantUML擁有獨立的一套文法,實作代碼控制畫圖,便于後期的修改等操作

參考文獻

1、https://plantuml.com/zh/activity-diagram-beta

2、https://www.yuque.com/yuque/help/editor-puml

PlantUML文法

簡單活動圖

活動标簽(activity label)以冒号開始,以分号結束。

@startuml

:Hello world;
:這是一個測試步驟;

@enduml
           
PlantUML-程式員繪圖工具「流程圖、用例圖、時序圖等等」

開始/結束

你可以使用關鍵字 start 和 stop 表示圖示的開始和結束。

@startuml

start

:Hello world;
:這是一個測試步驟;

stop

@enduml
           
PlantUML-程式員繪圖工具「流程圖、用例圖、時序圖等等」

也可以使用 end 關鍵字。

@startuml

start

:Hello world;
:這是一個測試步驟;

end

@enduml
           
PlantUML-程式員繪圖工具「流程圖、用例圖、時序圖等等」

條件語句

在圖示中可以使用關鍵字if,then和else設定分支測試。标注文字則放在括号中。

@startuml

start

    if(是否已經吃飯) then(是)
        :不再吃飯;
    else(否)
        :去吃飯;
    endif
    
stop

@enduml
           
PlantUML-程式員繪圖工具「流程圖、用例圖、時序圖等等」

也可以使用關鍵字elseif設定多個分支測試。

@startuml

start

    if(條件a) then(yes)
        :do 條件a;
    elseif(條件b) then(yes)
        :do 條件b;
        end
    elseif(條件c) then(yes)
        :do 條件c;
    elseif(條件d) then(yes)
        :do 條件d;
    else(nothing)
        :do nothing;
    endif
stop

@enduml
           
PlantUML-程式員繪圖工具「流程圖、用例圖、時序圖等等」

如果想要中段一個判斷,可以使用kill或者detach關鍵字。

@startuml

if (條件滿足?) then
  #pink:error;
  kill
endif
#palegreen:action;

@enduml
           
@startuml

if (條件滿足?) then
  #pink:error;
  detach
endif
#palegreen:action;

@enduml
           
PlantUML-程式員繪圖工具「流程圖、用例圖、時序圖等等」

重複循環

你可以使用關鍵字repeat和repeat while和backward進行重複循環。

@startuml

start

repeat :開始;
  :步驟1;
  :步驟2;
backward:傳回到開始;
repeat while (傳回?)

stop

@enduml
           
PlantUML-程式員繪圖工具「流程圖、用例圖、時序圖等等」

while循環

可以使用關鍵字while和end while進行while循環。

@startuml

:i = 5;
while (i = 2 ?) is (no)
  :i--;
endwhile (yes)
:next;

@enduml
           
PlantUML-程式員繪圖工具「流程圖、用例圖、時序圖等等」

注釋

使用floating關鍵字給流程添加注釋。

@startuml

start

:步驟1;
floating note left: 這是一個注釋
:步驟2;
note right
  這是一個注釋
  //lines// and can
  contain <b>HTML</b>
  ====
  * Calling the method ""foo()"" is prohibited
end note

stop

@enduml
           
PlantUML-程式員繪圖工具「流程圖、用例圖、時序圖等等」

顔色

@startuml

start

:步驟1;
#HotPink:步驟2;
#AAAAAA:步驟3;

@enduml
           
PlantUML-程式員繪圖工具「流程圖、用例圖、時序圖等等」

繼續閱讀