collab/mermaid/packages/mermaid-zenuml/README.mdblame
View source
6dd74de1# @mermaid-js/mermaid-zenuml
6dd74de2
6dd74de3MermaidJS plugin for ZenUML integration - A powerful sequence diagram rendering engine.
6dd74de4
6dd74de5> A Sequence diagram is an interaction diagram that shows how processes operate with one another and in what order.
6dd74de6
6dd74de7Mermaid can render sequence diagrams with [ZenUML](https://zenuml.com). Note that ZenUML uses a different
6dd74de8syntax than the original Sequence Diagram in mermaid.
6dd74de9
6dd74de10```mermaid
6dd74de11zenuml
6dd74de12 BookLibService.Borrow(id) {
6dd74de13 User = Session.GetUser()
6dd74de14 if(User.isActive) {
6dd74de15 try {
6dd74de16 BookRepository.Update(id, onLoan, User)
6dd74de17 receipt = new Receipt(id, dueDate)
6dd74de18 } catch (BookNotFoundException) {
6dd74de19 ErrorService.onException(BookNotFoundException)
6dd74de20 } finally {
6dd74de21 Connection.close()
6dd74de22 }
6dd74de23 }
6dd74de24 return receipt
6dd74de25 }
6dd74de26```
6dd74de27
6dd74de28## Installation
6dd74de29
6dd74de30### With bundlers
6dd74de31
6dd74de32```sh
6dd74de33npm install @mermaid-js/mermaid-zenuml
6dd74de34```
6dd74de35
6dd74de36```ts
6dd74de37import mermaid from 'mermaid';
6dd74de38import zenuml from '@mermaid-js/mermaid-zenuml';
6dd74de39
6dd74de40await mermaid.registerExternalDiagrams([zenuml]);
6dd74de41```
6dd74de42
6dd74de43### With CDN
6dd74de44
6dd74de45```html
6dd74de46<script type="module">
6dd74de47 import mermaid from 'https://cdn.jsdelivr.net/npm/mermaid@11/dist/mermaid.esm.min.mjs';
6dd74de48 import zenuml from 'https://cdn.jsdelivr.net/npm/@mermaid-js/mermaid-zenuml@0.2.0/dist/mermaid-zenuml.core.mjs';
6dd74de49 await mermaid.registerExternalDiagrams([zenuml]);
6dd74de50</script>
6dd74de51```
6dd74de52
6dd74de53> [!NOTE]
6dd74de54> ZenUML uses experimental lazy loading & async rendering features which could change in the future.
6dd74de55
6dd74de56## Basic Usage
6dd74de57
6dd74de58Once the plugin is registered, you can create ZenUML diagrams using the `zenuml` syntax:
6dd74de59
6dd74de60```mermaid
6dd74de61zenuml
6dd74de62 Controller.Get(id) {
6dd74de63 Service.Get(id) {
6dd74de64 item = Repository.Get(id)
6dd74de65 if(item) {
6dd74de66 return item
6dd74de67 } else {
6dd74de68 return null
6dd74de69 }
6dd74de70 }
6dd74de71 return result
6dd74de72 }
6dd74de73```
6dd74de74
6dd74de75## ZenUML Syntax Reference
6dd74de76
6dd74de77### Participants
6dd74de78
6dd74de79The participants can be defined implicitly as in the first example on this page. The participants or actors are
6dd74de80rendered in order of appearance in the diagram source text. Sometimes you might want to show the participants in a
6dd74de81different order than how they appear in the first message. It is possible to specify the actor's order of
6dd74de82appearance by doing the following:
6dd74de83
6dd74de84```mermaid
6dd74de85zenuml
6dd74de86 title Declare participant (optional)
6dd74de87 Bob
6dd74de88 Alice
6dd74de89 Alice->Bob: Hi Bob
6dd74de90 Bob->Alice: Hi Alice
6dd74de91```
6dd74de92
6dd74de93### Annotators
6dd74de94
6dd74de95If you specifically want to use symbols instead of just rectangles with text you can do so by using the annotator syntax to declare participants as per below.
6dd74de96
6dd74de97```mermaid
6dd74de98zenuml
6dd74de99 title Annotators
6dd74de100 @Actor Alice
6dd74de101 @Database Bob
6dd74de102 Alice->Bob: Hi Bob
6dd74de103 Bob->Alice: Hi Alice
6dd74de104```
6dd74de105
6dd74de106Available annotators include:
6dd74de107
6dd74de108- `@Actor` - Human figure
6dd74de109- `@Database` - Database symbol
6dd74de110- `@Boundary` - Boundary symbol
6dd74de111- `@Control` - Control symbol
6dd74de112- `@Entity` - Entity symbol
6dd74de113- `@Queue` - Queue symbol
6dd74de114
6dd74de115### Aliases
6dd74de116
6dd74de117The participants can have a convenient identifier and a descriptive label.
6dd74de118
6dd74de119```mermaid
6dd74de120zenuml
6dd74de121 title Aliases
6dd74de122 A as Alice
6dd74de123 J as John
6dd74de124 A->J: Hello John, how are you?
6dd74de125 J->A: Great!
6dd74de126```
6dd74de127
6dd74de128## Messages
6dd74de129
6dd74de130Messages can be one of:
6dd74de131
6dd74de1321. Sync message
6dd74de1332. Async message
6dd74de1343. Creation message
6dd74de1354. Reply message
6dd74de136
6dd74de137### Sync message
6dd74de138
6dd74de139You can think of a sync (blocking) method in a programming language.
6dd74de140
6dd74de141```mermaid
6dd74de142zenuml
6dd74de143 title Sync message
6dd74de144 A.SyncMessage
6dd74de145 A.SyncMessage(with, parameters) {
6dd74de146 B.nestedSyncMessage()
6dd74de147 }
6dd74de148```
6dd74de149
6dd74de150### Async message
6dd74de151
6dd74de152You can think of an async (non-blocking) method in a programming language. Fire an event and forget about it.
6dd74de153
6dd74de154```mermaid
6dd74de155zenuml
6dd74de156 title Async message
6dd74de157 Alice->Bob: How are you?
6dd74de158```
6dd74de159
6dd74de160### Creation message
6dd74de161
6dd74de162We use `new` keyword to create an object.
6dd74de163
6dd74de164```mermaid
6dd74de165zenuml
6dd74de166 new A1
6dd74de167 new A2(with, parameters)
6dd74de168```
6dd74de169
6dd74de170### Reply message
6dd74de171
6dd74de172There are three ways to express a reply message:
6dd74de173
6dd74de174```mermaid
6dd74de175zenuml
6dd74de176 // 1. assign a variable from a sync message.
6dd74de177 a = A.SyncMessage()
6dd74de178
6dd74de179 // 1.1. optionally give the variable a type
6dd74de180 SomeType a = A.SyncMessage()
6dd74de181
6dd74de182 // 2. use return keyword
6dd74de183 A.SyncMessage() {
6dd74de184 return result
6dd74de185 }
6dd74de186
6dd74de187 // 3. use @return or @reply annotator on an async message
6dd74de188 @return
6dd74de189 A->B: result
6dd74de190```
6dd74de191
6dd74de192The third way `@return` is rarely used, but it is useful when you want to return to one level up.
6dd74de193
6dd74de194```mermaid
6dd74de195zenuml
6dd74de196 title Reply message
6dd74de197 Client->A.method() {
6dd74de198 B.method() {
6dd74de199 if(condition) {
6dd74de200 return x1
6dd74de201 // return early
6dd74de202 @return
6dd74de203 A->Client: x11
6dd74de204 }
6dd74de205 }
6dd74de206 return x2
6dd74de207 }
6dd74de208```
6dd74de209
6dd74de210## Advanced Features
6dd74de211
6dd74de212### Nesting
6dd74de213
6dd74de214Sync messages and Creation messages are naturally nestable with `{}`.
6dd74de215
6dd74de216```mermaid
6dd74de217zenuml
6dd74de218 A.method() {
6dd74de219 B.nested_sync_method()
6dd74de220 B->C: nested async message
6dd74de221 }
6dd74de222```
6dd74de223
6dd74de224### Comments
6dd74de225
6dd74de226It is possible to add comments to a sequence diagram with `// comment` syntax.
6dd74de227Comments will be rendered above the messages or fragments. Comments on other places
6dd74de228are ignored. Markdown is supported.
6dd74de229
6dd74de230```mermaid
6dd74de231zenuml
6dd74de232 // a comment on a participant will not be rendered
6dd74de233 BookService
6dd74de234 // a comment on a message.
6dd74de235 // **Markdown** is supported.
6dd74de236 BookService.getBook()
6dd74de237```
6dd74de238
6dd74de239### Loops
6dd74de240
6dd74de241It is possible to express loops in a ZenUML diagram. This is done by any of the
6dd74de242following notations:
6dd74de243
6dd74de2441. while
6dd74de2452. for
6dd74de2463. forEach, foreach
6dd74de2474. loop
6dd74de248
6dd74de249```zenuml
6dd74de250while(condition) {
6dd74de251 ...statements...
6dd74de252}
6dd74de253```
6dd74de254
6dd74de255Example:
6dd74de256
6dd74de257```mermaid
6dd74de258zenuml
6dd74de259 Alice->John: Hello John, how are you?
6dd74de260 while(true) {
6dd74de261 John->Alice: Great!
6dd74de262 }
6dd74de263```
6dd74de264
6dd74de265### Alt (Alternative paths)
6dd74de266
6dd74de267It is possible to express alternative paths in a sequence diagram. This is done by the notation
6dd74de268
6dd74de269```zenuml
6dd74de270if(condition1) {
6dd74de271 ...statements...
6dd74de272} else if(condition2) {
6dd74de273 ...statements...
6dd74de274} else {
6dd74de275 ...statements...
6dd74de276}
6dd74de277```
6dd74de278
6dd74de279Example:
6dd74de280
6dd74de281```mermaid
6dd74de282zenuml
6dd74de283 Alice->Bob: Hello Bob, how are you?
6dd74de284 if(is_sick) {
6dd74de285 Bob->Alice: Not so good :(
6dd74de286 } else {
6dd74de287 Bob->Alice: Feeling fresh like a daisy
6dd74de288 }
6dd74de289```
6dd74de290
6dd74de291### Opt (Optional)
6dd74de292
6dd74de293It is possible to render an `opt` fragment. This is done by the notation
6dd74de294
6dd74de295```zenuml
6dd74de296opt {
6dd74de297 ...statements...
6dd74de298}
6dd74de299```
6dd74de300
6dd74de301Example:
6dd74de302
6dd74de303```mermaid
6dd74de304zenuml
6dd74de305 Alice->Bob: Hello Bob, how are you?
6dd74de306 Bob->Alice: Not so good :(
6dd74de307 opt {
6dd74de308 Bob->Alice: Thanks for asking
6dd74de309 }
6dd74de310```
6dd74de311
6dd74de312### Parallel
6dd74de313
6dd74de314It is possible to show actions that are happening in parallel.
6dd74de315
6dd74de316This is done by the notation
6dd74de317
6dd74de318```zenuml
6dd74de319par {
6dd74de320 statement1
6dd74de321 statement2
6dd74de322 statement3
6dd74de323}
6dd74de324```
6dd74de325
6dd74de326Example:
6dd74de327
6dd74de328```mermaid
6dd74de329zenuml
6dd74de330 par {
6dd74de331 Alice->Bob: Hello guys!
6dd74de332 Alice->John: Hello guys!
6dd74de333 }
6dd74de334```
6dd74de335
6dd74de336### Try/Catch/Finally (Break)
6dd74de337
6dd74de338It is possible to indicate a stop of the sequence within the flow (usually used to model exceptions).
6dd74de339
6dd74de340This is done by the notation
6dd74de341
6dd74de342```
6dd74de343try {
6dd74de344 ...statements...
6dd74de345} catch {
6dd74de346 ...statements...
6dd74de347} finally {
6dd74de348 ...statements...
6dd74de349}
6dd74de350```
6dd74de351
6dd74de352Example:
6dd74de353
6dd74de354```mermaid
6dd74de355zenuml
6dd74de356 try {
6dd74de357 Consumer->API: Book something
6dd74de358 API->BookingService: Start booking process
6dd74de359 } catch {
6dd74de360 API->Consumer: show failure
6dd74de361 } finally {
6dd74de362 API->BookingService: rollback status
6dd74de363 }
6dd74de364```
6dd74de365
6dd74de366## Contributing
6dd74de367
6dd74de368This package is part of the [Mermaid](https://github.com/mermaid-js/mermaid) project. See the main repository for contributing guidelines.
6dd74de369
6dd74de370## Contributors
6dd74de371
6dd74de372- [Peng Xiao](https://github.com/MrCoder)
6dd74de373- [Sidharth Vinod](https://sidharth.dev)
6dd74de374- [Dong Cai](https://github.com/dontry)
6dd74de375
6dd74de376## License
6dd74de377
6dd74de378MIT
6dd74de379
6dd74de380## Links
6dd74de381
6dd74de382- [ZenUML Official Website](https://zenuml.com)
6dd74de383- [Mermaid Documentation](https://mermaid.js.org)
6dd74de384- [GitHub Repository](https://github.com/mermaid-js/mermaid)