用代碼畫流程圖和時(shí)序圖快餐教程(2)
可以嵌入在markdown代碼中的mermaid
mermaid的好處是可以在寫markdown文檔的同時(shí)降瞳,就直接可以畫圖了剩檀。
下面是我用Haroopad工具直接在markdown中畫流圖的情況:
工具下載地址:http://pad.haroopress.com/
獨(dú)立使用mermaid
可以通過(guò)npm安裝mermad:
npm install -g mermaid
npm install -g phantomjs
mermaid依賴于phantomjs
下面是mermaid的幫助信息:
Usage: mermaid [options] <file>...
file The mermaid description file to be rendered
Options:
-s --svg Output SVG instead of PNG (experimental)
-p --png If SVG was selected, and you also want PNG, set this flag
-o --outputDir Directory to save files, will be created automatically, d
efaults to `cwd`
-e --phantomPath Specify the path to the phantomjs executable
-t --css Specify the path to a CSS file to be included when proces
sing output
-c --sequenceConfig Specify the path to the file with the configuration to be
applied in the sequence diagram
-g --ganttConfig Specify the path to the file with the configuration to be
applied in the gantt diagram
-h --help Show this message
-v --verbose Show logging
-w --width width of the generated png (number)
--version Print version and quit
mermaid數(shù)據(jù)流圖速成
決定圖的方向
與graphviz不同蜒秤,graph語(yǔ)句首先需要設(shè)置圖的方向
- TD: 默認(rèn)方向,從上到下
- TB: 從上到下
- BT: 從下到上
- LR: 從左到右
- RL: 從右到左
例:
graph TB;
Context_sendbroadcast --> ContextWrapper_sendBroadcast;
ContextWrapper_sendBroadcast --> ContextImpl_sendBroadcast;
ContextImpl_sendBroadcast --> ActivityManagerService_broadcastIntent;
與graphviz的不同
graphviz的dot圖是用“節(jié)點(diǎn)1 -> 節(jié)點(diǎn)2”的格式扰付,而mermaid是用“節(jié)點(diǎn)1 --> 節(jié)點(diǎn)2”的方式颊埃,中間多了一個(gè)"-".
屬性定義方面,mermaid可以用[]和()幾種方式來(lái)決定圖形的方式牧抵。
比如:
graph TD;
Context_sendbroadcast(Context.sendBroadcast Intent);
ContextWrapper_sendBroadcast[ContextWrapper.sendBroadcast Intent];
Context_sendbroadcast --> ContextWrapper_sendBroadcast;
Context_sendbroadcast就是圓腳的矩形笛匙,而ContextWrapper_sendBroadcast就是方的。
需要注意的一點(diǎn)犀变,在標(biāo)簽屬性定義的時(shí)候妹孙,不能出現(xiàn)()[]之類引起誤會(huì)的符號(hào)。官方文檔說(shuō)可以用""來(lái)引起來(lái)获枝,不過(guò)在我的版本上有點(diǎn)問題蠢正。
下面還是看跟上面一篇中所講的graphviz完全一樣的圖的例子:
graph TD
Context_sendbroadcast[Context.sendBroadcast Intent];
Context_sendbroadcast_asUser[Context.sendBroadcastAsUser Intent,UserHandle];
Context_sendOrderedBroadcast[Context.sendOrderedBroadcast Intent,String,BroadcastReceiver,Handler,int,String,Bundle];
Context_sendOrderedBroadcast2[Context.sendOrderedBroadcast Intent,String];
Context_sendStickyBroadcast[Context.sendStickyBroadcast Intent];
Context_sendStickyBroadcast2[Context.sendStickyOrderedBroadcast Intent,BroadcastReceiver,Handler,int,String,Bundle];
ContextWrapper_sendBroadcast[ContextWrapper.sendBroadcast Intent];
ContextWrapper_sendOrderedBroadcast2[ContextWrapper.sendOrderedBroadcast Intent,String];
ContextWrapper_sendOrderedBroadcast[ContextWrapper.sendOrderedBroadcast Intent,String,BroadcastReceiver,Handler,int,String,Bundle];
ContextWrapper_sendStickyBroadcast[ContextWrapper.sendStickyBroadcast Intent];
ContextWrapper_sendStickyBroadcast2[ContextWrapper.sendStickyOrderedBroadcast Intent,BroadcastReceiver,Handler,int,String,Bundle];
ContextWrapper_sendbroadcast_asUser[ContextWrapper.sendBroadcastAsUser Intent,UserHandle];
ContextImpl_sendBroadcast[ContextImpl.sendBroadcast Intent];
ContextImpl_sendOrderedBroadcast2[ContextImpl.sendOrderedBroadcast Intent,String];
ContextImpl_sendOrderedBroadcast[ContextImpl.sendOrderedBroadcast Intent,String,BroadcastReceiver,Handler,int,String,Bundle];
ContextImpl_sendStickyBroadcast[ContextImpl.sendStickyBroadcast Intent];
ContextImpl_sendStickyBroadcast2[ContextImpl.sendStickyOrderedBroadcast Intent,BroadcastReceiver,Handler,int,String,Bundle];
ContextImpl_sendbroadcast_asUser[ContextImpl.sendBroadcastAsUser Intent,UserHandle];
ContextImpl_sendOrderedBroadcast_all(ContextImpl.sendOrderedBroadcast Intent,String,int,BroadcastReceiver,Handler,int,String,Bundle,Bundle);
ActivityManagerService_broadcastIntent(ActivityManagerService.broadcastIntent IApplicationThread,Intent,String,IIntentReceiver,int,String,Bundle,String,int,Bundle,boolean,boolean,int);
Context_sendbroadcast --> ContextWrapper_sendBroadcast;
ContextWrapper_sendBroadcast --> ContextImpl_sendBroadcast;
ContextImpl_sendBroadcast --> ActivityManagerService_broadcastIntent;
Context_sendbroadcast_asUser --> ContextWrapper_sendbroadcast_asUser;
ContextWrapper_sendbroadcast_asUser --> ContextImpl_sendbroadcast_asUser;
ContextImpl_sendbroadcast_asUser --> ActivityManagerService_broadcastIntent;
Context_sendOrderedBroadcast2 --> ContextWrapper_sendOrderedBroadcast2;
ContextWrapper_sendOrderedBroadcast2 --> ContextImpl_sendOrderedBroadcast2;
ContextImpl_sendOrderedBroadcast2 --> ActivityManagerService_broadcastIntent;
Context_sendOrderedBroadcast --> ContextWrapper_sendOrderedBroadcast;
ContextWrapper_sendOrderedBroadcast --> ContextImpl_sendOrderedBroadcast;
ContextImpl_sendOrderedBroadcast --> ContextImpl_sendOrderedBroadcast_all;
ContextImpl_sendOrderedBroadcast_all --> ActivityManagerService_broadcastIntent;
Context_sendStickyBroadcast --> ContextWrapper_sendStickyBroadcast;
ContextWrapper_sendStickyBroadcast --> ContextImpl_sendStickyBroadcast;
ContextImpl_sendStickyBroadcast --> ActivityManagerService_broadcastIntent;
Context_sendStickyBroadcast2 --> ContextWrapper_sendStickyBroadcast2;
ContextWrapper_sendStickyBroadcast2 --> ContextImpl_sendStickyBroadcast2;
ContextImpl_sendStickyBroadcast2 --> ActivityManagerService_broadcastIntent;
可以嵌入在markdown代碼中的mermaid
mermaid的好處是可以在寫markdown文檔的同時(shí),就直接可以畫圖了省店。
下面是我用Haroopad工具直接在markdown中畫流圖的情況:
工具下載地址:http://pad.haroopress.com/
獨(dú)立使用mermaid
可以通過(guò)npm安裝mermad:
npm install -g mermaid
npm install -g phantomjs
mermaid依賴于phantomjs
下面是mermaid的幫助信息:
Usage: mermaid [options] <file>...
file The mermaid description file to be rendered
Options:
-s --svg Output SVG instead of PNG (experimental)
-p --png If SVG was selected, and you also want PNG, set this flag
-o --outputDir Directory to save files, will be created automatically, d
efaults to `cwd`
-e --phantomPath Specify the path to the phantomjs executable
-t --css Specify the path to a CSS file to be included when proces
sing output
-c --sequenceConfig Specify the path to the file with the configuration to be
applied in the sequence diagram
-g --ganttConfig Specify the path to the file with the configuration to be
applied in the gantt diagram
-h --help Show this message
-v --verbose Show logging
-w --width width of the generated png (number)
--version Print version and quit
mermaid數(shù)據(jù)流圖速成
決定圖的方向
與graphviz不同嚣崭,graph語(yǔ)句首先需要設(shè)置圖的方向
- TD: 默認(rèn)方向,從上到下
- TB: 從上到下
- BT: 從下到上
- LR: 從左到右
- RL: 從右到左
例:
graph TB;
Context_sendbroadcast --> ContextWrapper_sendBroadcast;
ContextWrapper_sendBroadcast --> ContextImpl_sendBroadcast;
ContextImpl_sendBroadcast --> ActivityManagerService_broadcastIntent;
與graphviz的不同
graphviz的dot圖是用“節(jié)點(diǎn)1 -> 節(jié)點(diǎn)2”的格式懦傍,而mermaid是用“節(jié)點(diǎn)1 --> 節(jié)點(diǎn)2”的方式雹舀,中間多了一個(gè)"-".
屬性定義方面,mermaid可以用[]和()幾種方式來(lái)決定圖形的方式粗俱。
比如:
graph TD;
Context_sendbroadcast(Context.sendBroadcast Intent);
ContextWrapper_sendBroadcast[ContextWrapper.sendBroadcast Intent];
Context_sendbroadcast --> ContextWrapper_sendBroadcast;
Context_sendbroadcast就是圓腳的矩形说榆,而ContextWrapper_sendBroadcast就是方的。
需要注意的一點(diǎn)寸认,在標(biāo)簽屬性定義的時(shí)候签财,不能出現(xiàn)()[]之類引起誤會(huì)的符號(hào)。官方文檔說(shuō)可以用""來(lái)引起來(lái)偏塞,不過(guò)在我的版本上有點(diǎn)問題唱蒸。
下面還是看跟上面一篇中所講的graphviz完全一樣的圖的例子:
graph TD
Context_sendbroadcast[Context.sendBroadcast Intent];
Context_sendbroadcast_asUser[Context.sendBroadcastAsUser Intent,UserHandle];
Context_sendOrderedBroadcast[Context.sendOrderedBroadcast Intent,String,BroadcastReceiver,Handler,int,String,Bundle];
Context_sendOrderedBroadcast2[Context.sendOrderedBroadcast Intent,String];
Context_sendStickyBroadcast[Context.sendStickyBroadcast Intent];
Context_sendStickyBroadcast2[Context.sendStickyOrderedBroadcast Intent,BroadcastReceiver,Handler,int,String,Bundle];
ContextWrapper_sendBroadcast[ContextWrapper.sendBroadcast Intent];
ContextWrapper_sendOrderedBroadcast2[ContextWrapper.sendOrderedBroadcast Intent,String];
ContextWrapper_sendOrderedBroadcast[ContextWrapper.sendOrderedBroadcast Intent,String,BroadcastReceiver,Handler,int,String,Bundle];
ContextWrapper_sendStickyBroadcast[ContextWrapper.sendStickyBroadcast Intent];
ContextWrapper_sendStickyBroadcast2[ContextWrapper.sendStickyOrderedBroadcast Intent,BroadcastReceiver,Handler,int,String,Bundle];
ContextWrapper_sendbroadcast_asUser[ContextWrapper.sendBroadcastAsUser Intent,UserHandle];
ContextImpl_sendBroadcast[ContextImpl.sendBroadcast Intent];
ContextImpl_sendOrderedBroadcast2[ContextImpl.sendOrderedBroadcast Intent,String];
ContextImpl_sendOrderedBroadcast[ContextImpl.sendOrderedBroadcast Intent,String,BroadcastReceiver,Handler,int,String,Bundle];
ContextImpl_sendStickyBroadcast[ContextImpl.sendStickyBroadcast Intent];
ContextImpl_sendStickyBroadcast2[ContextImpl.sendStickyOrderedBroadcast Intent,BroadcastReceiver,Handler,int,String,Bundle];
ContextImpl_sendbroadcast_asUser[ContextImpl.sendBroadcastAsUser Intent,UserHandle];
ContextImpl_sendOrderedBroadcast_all(ContextImpl.sendOrderedBroadcast Intent,String,int,BroadcastReceiver,Handler,int,String,Bundle,Bundle);
ActivityManagerService_broadcastIntent(ActivityManagerService.broadcastIntent IApplicationThread,Intent,String,IIntentReceiver,int,String,Bundle,String,int,Bundle,boolean,boolean,int);
Context_sendbroadcast --> ContextWrapper_sendBroadcast;
ContextWrapper_sendBroadcast --> ContextImpl_sendBroadcast;
ContextImpl_sendBroadcast --> ActivityManagerService_broadcastIntent;
Context_sendbroadcast_asUser --> ContextWrapper_sendbroadcast_asUser;
ContextWrapper_sendbroadcast_asUser --> ContextImpl_sendbroadcast_asUser;
ContextImpl_sendbroadcast_asUser --> ActivityManagerService_broadcastIntent;
Context_sendOrderedBroadcast2 --> ContextWrapper_sendOrderedBroadcast2;
ContextWrapper_sendOrderedBroadcast2 --> ContextImpl_sendOrderedBroadcast2;
ContextImpl_sendOrderedBroadcast2 --> ActivityManagerService_broadcastIntent;
Context_sendOrderedBroadcast --> ContextWrapper_sendOrderedBroadcast;
ContextWrapper_sendOrderedBroadcast --> ContextImpl_sendOrderedBroadcast;
ContextImpl_sendOrderedBroadcast --> ContextImpl_sendOrderedBroadcast_all;
ContextImpl_sendOrderedBroadcast_all --> ActivityManagerService_broadcastIntent;
Context_sendStickyBroadcast --> ContextWrapper_sendStickyBroadcast;
ContextWrapper_sendStickyBroadcast --> ContextImpl_sendStickyBroadcast;
ContextImpl_sendStickyBroadcast --> ActivityManagerService_broadcastIntent;
Context_sendStickyBroadcast2 --> ContextWrapper_sendStickyBroadcast2;
ContextWrapper_sendStickyBroadcast2 --> ContextImpl_sendStickyBroadcast2;
ContextImpl_sendStickyBroadcast2 --> ActivityManagerService_broadcastIntent;
下面是生成的圖片:
PlantUML畫時(shí)序圖
用描述性的語(yǔ)言來(lái)寫UML,尤其是Sequence Diagram烛愧,PlantUML應(yīng)該是很多同學(xué)的首選了吧油宜。
網(wǎng)址:
http://plantuml.com/
我們可以使用eclipse的plugin來(lái)寫PlantUML,如下:
速成教程
@startuml和@enduml
這個(gè)沒啥說(shuō)的怜姿,開始的時(shí)候用@startuml慎冤,結(jié)束的時(shí)候要加個(gè)@enduml。
標(biāo)題
格式: title 標(biāo)題名
participant
格式:participant 類名 #顏色
例:
@startuml
title Android Broadcast procedure
participant Activity #Lime
participant ContextWrapper #Cyan
@enduml
消息傳遞
格式:類1 -> 類2 : 消息
例:
@startuml
title Android Broadcast procedure
participant Activity #Lime
participant ContextWrapper #Cyan
Activity -> ContextWrapper : registerReceiver()
@enduml
自動(dòng)編號(hào)
加一句話:autonumber
例子
好了沧卢,快餐教程講多了大家就煩了蚁堤,所以基本夠用的講完了我們就看個(gè)例子吧:
@startuml
title Android Broadcast procedure
'author Ali.Xulun
'version 1.0.0
participant Activity #Lime
participant ContextWrapper #Cyan
participant ContextImpl #Cyan
participant ActivityManagerService #Cyan
participant ActivityStackSupervisor #Cyan
participant ActivityStack #Cyan
participant ApplicationThreadProxy #Silver
participant InnerReceiver #Magenta
participant ReceiverDispatcher #Magenta
participant BroadcastReceiver #Magenta
autonumber
Activity -> ContextWrapper : registerReceiver()
ContextWrapper -> ContextImpl : registerReceiver()
ContextImpl -> LoadedApk : getReceiverDispatcher()
LoadedApk -> ActivityManagerProxy : registerReceiver()
ActivityManagerProxy -> ActivityManagerService : registerReceiver()
Activity -> ContextWrapper : sendBroadcast()
ContextWrapper -> ContextImpl : sendBroadcast()
ContextImpl -> ActivityManagerService: broadcastIntent()
ActivityManagerService -> ActivityManagerService : broadcastIntentLocked()
ActivityManagerService -> ActivityManagerService : collectReceiverComponents()
ActivityManagerService -> ActivityManagerService : scheduleBroadcastsLocked()
ActivityManagerService -> ActivityManagerService : processNextBroadcast()
ActivityManagerService -> ActivityManagerService : deliverToRegisteredReceiverLocked()
ActivityManagerService -> ActivityManagerService : performReceiveLocked()
ActivityManagerService -> ApplicationThreadProxy : scheduleRegisteredReceiver()
ApplicationThreadProxy -> InnerReceiver : performReceive()
InnerReceiver -> ReceiverDispatcher : performReceive()
ReceiverDispatcher -> BroadcastReceiver : onReceive()
Activity -> ContextWrapper : sendOrderedBroadcast()
ContextWrapper -> ContextImpl : sendOrderedBroadcast()
ContextImpl -> ActivityManagerService: broadcastIntent()
@enduml
顯示的結(jié)果如下:
mermaid時(shí)序圖
時(shí)序圖聲明
mermaid支持多種圖,所以要先通過(guò)sequenceDiagram
sequenceDiagram
participant
mermaid的participant不支持直接聲明顏色但狭,只能聲明個(gè)名字了
sequenceDiagram
participant Activity
participant ContextWrapper
消息要用雙箭頭
按照PlantUML的習(xí)慣寫“->”是不行的披诗,這樣會(huì)沒有箭頭。如果要往箭頭立磁,還需要再加一個(gè)">"呈队,變成"->>".
sequenceDiagram
Activity ->> ContextWrapper : registerReceiver()
ContextWrapper ->> ContextImpl : registerReceiver()
完整例子
sequenceDiagram
Activity ->> ContextWrapper : registerReceiver()
ContextWrapper ->> ContextImpl : registerReceiver()
ContextImpl ->> LoadedApk : getReceiverDispatcher()
LoadedApk ->> ActivityManagerProxy : registerReceiver()
ActivityManagerProxy ->> ActivityManagerService : registerReceiver()
Activity ->> ContextWrapper : sendBroadcast()
ContextWrapper ->> ContextImpl : sendBroadcast()
ContextImpl ->> ActivityManagerService: broadcastIntent()
ActivityManagerService ->> ActivityManagerService : broadcastIntentLocked()
ActivityManagerService ->> ActivityManagerService : collectReceiverComponents()
ActivityManagerService ->> ActivityManagerService : scheduleBroadcastsLocked()
ActivityManagerService ->> ActivityManagerService : processNextBroadcast()
ActivityManagerService ->> ActivityManagerService : deliverToRegisteredReceiverLocked()
ActivityManagerService ->> ActivityManagerService : performReceiveLocked()
ActivityManagerService ->> ApplicationThreadProxy : scheduleRegisteredReceiver()
ApplicationThreadProxy ->> InnerReceiver : performReceive()
InnerReceiver ->> ReceiverDispatcher : performReceive()
ReceiverDispatcher ->> BroadcastReceiver : onReceive()
Activity ->> ContextWrapper : sendOrderedBroadcast()
ContextWrapper ->> ContextImpl : sendOrderedBroadcast()
ContextImpl ->> ActivityManagerService: broadcastIntent()
生成的圖形如下: