Sequence diagram (sequence diagram)
A sequence diagram is an interaction diagram that shows how processes operate with each other and in what order.
sequenceDiagram participant l as timber wolf participant y as lamb l->>y:Little villain, dirty the water I drink y->>l:Mr. wolf, you are upstream and I am downstream l->>y:I heard you spoke ill of me last year y->>l:I wasn't born last year l->>y:It's not you, it's your father, and he jumped on it %% left or right note left of l:For bad guys,<br>There is always a reason to want to do bad things.
Basic grammar
sequenceDiagram participant A as Mr Black : Participant declaration (not required unless you need to define a simple alias) [Actor][Arrow][Actor]:Message text
- participant: participant
The participant is participant by default, so it is generally unnecessary to display the declaration. However, when the participant's characters are long, you can define a simple alias by displaying the declaration, so that you can knock the keyboard less.
sequenceDiagram participant A as Mr Black participant B as Tom Cat
- Participant: actor (due to the higher version, many mrkdown editors don't support it, so they just skim over)
If you especially want to use actor symbols instead of rectangles with text, you can use actor to declare participants.
sequenceDiagram actor Alice actor Bob Alice->>Bob: Hi Bob Bob->>Alice: Hi Alic
alias
Participants can have convenient identifiers and descriptive labels, that is, aliases. If the participant name is too long, it can be replaced by an alias.
sequenceDiagram participant A as Alice participant J as John A->>J: Hello John, how are you? J->>A: Great!
Connection and arrow types
type | describe |
---|---|
-> | Solid line without arrow |
--> | Dotted line without arrow |
->> | Solid lines have arrows |
-->> | The dotted line has an arrow |
-x | Solid line crossing arrow |
--x | Dashed cross arrow |
-) | Solid line with an open arrow at the end (async) The version is too high and is not supported by most markdown editors. |
--) | Dotted line with a open arrow at the end (async) The version is too high. Most markdown editors do not support it |
sequenceDiagram participant A as Tom participant B as Cat note left of A: Test line shapes and arrows A->B:have you had dinner B->>A:Eat. A-->B:It's almost the new year. Are you going home? B-->>A:Novel coronavirus pneumonia is so serious that it can't go back. A-xB:Yes, I have to be isolated for 14 days. B--xA:Shit, isolation for 14 days, not so many holidays. %%A-) B:hello %%B--)A:ok
activity
Make a participant active. This is done by activating and deactivating a participant
sequenceDiagram participant t as Math teacher participant s as Gaussian t->>s:Give a question:<br>1+2+3+...+100=? activate s s-->>s:I think it over. Small case s->>t:1+2+3+...+100=5050 deactivate s t-->>s:0-0,Smart. s--xt: *_*
This is also a quick representation by appending + / - suffix to the message arrow (recommended, this method is more concise):
sequenceDiagram participant t as Math teacher participant s as Gaussian t->>+s:Give a question:<br>1+2+3+...+100=? s-->>s:I think it over. Small case s->>-t:1+2+3+...+100=5050 t-->>s:0-0,Smart. s--xt: *_*
notes
You can add comments to the sequence diagram.
grammar
Note [ right of | left of | over ] [Paticipant]: Text in note content
eg:
sequenceDiagram participant l as timber wolf participant y as lamb participant d as Dog Warrior %% left or right Note left of l:For bad guys,<br>There is always a reason to want to do bad things. Note right of y:Countermeasures for bad guys: do or hide. Note over d:The Ranger stood up for justice and roared when he saw injustice.
Comments across 2 participants (up to 2)
Syntax:
Note [ right of | left of | over ] [Paticipant,Participant]: Text in note content
eg:
sequenceDiagram participant l as timber wolf participant y as lamb participant d as Dog Warrior %% left or right note left of l:For bad guys,<br>There is always a reason to want to do bad things.<br>The mutton is delicious. note over d,y:Sister sheep, don't be afraid. Note over l,d:Brother dog, I roared when I saw injustice. Big gray wolf, you go quickly
loop
Loops can be represented in sequence diagrams. The syntax is as follows:
loop Loop text ... statements ... end
eg:
sequenceDiagram participant t as sir participant s as scholar participant k as Ministry of Rites loop persevere ten years in one 's studies in spite of hardships t->>+ s:teach s-->>s:study&reflection s->>-t:ask end s->>+k:Go to Beijing for the exam and participate in the autumn palace k-->>k:Find problems and select materials for our country k->>-s:Live up to your heart<br>Trouble on the wedding night loop A day on the street s-->>s:Complacent horseshoe disease,<br>See all the Changan flowers in one day. end
Route selection
Alternative paths can be represented in sequence diagrams. The syntax is as follows:
alt Describing text ... statements ... else Describing text ... statements ... end
Other options
opt Describing text ... statements ... end
eg:
sequenceDiagram participant s as successful candidates of the national civil service examination participant d as Palace examination s->> d:Take the palace exam alt One armour d->>s:Jinshi and else Two armour d->>s:Jinshi origin else Top three d->>s:The same Jinshi origin end opt Not in it d--xs:Take it. You're not a Gonz. end
parallel
You can display actions that occur in parallel. The syntax is as follows:
par [Action 1] ... statements ... and [Action 2] ... statements ... and [Action N] ... statements ... end
Parallelism can also be nested. The syntax is as follows:
par [Action 1] ... statements ... and [Action 2] ... statements ... and [Action N] ... statements ... par [actionNn] ... statements ... end end
eg
sequenceDiagram participant t as Tom participant k as Kite participant b as Black par notice t->>k:Went out together? and t->> b:Go out together? end k->>t:OK b->>t:ok
Background color
You can highlight the stream by providing a colored background rectangle. grammar
rect rgb(0, 255, 0) ... content ... end
Or add transparent color
rect rgba(0, 255, 0,.1) ... content ... end
0.1 can be expressed as. 1.
Introduction to rgb: https://www.cnblogs.com/wwwxxxyyy/p/13973380.html
eg
serial number
You can add a serial number to each connection in the sequence diagram. Turn on automatic numbering with code (autonumber):
sequenceDiagram autonumber participant k as Laokong participant m as Lao Meng k->>m: Have you eaten yet? m-->>k: Ate rougamo!
Code annotation
You can enter comments in the sequence diagram, which the parser ignores. Comments require a separate line and must begin with%% (double percentile). Any text from the beginning of a comment to the next line break is considered a comment, including any chart syntax.
sequenceDiagram Alice->>John: Have you eaten yet? %% this is a comment John-->>Alice: Ate rougamo!