2

Am using PlantUML to create a sequence diagram which depicts a Dating App hitting a RestfulController (which uses a Service class to process data).

What I am seeking to do is to represent the Service class's internal private methods via the Sequence Diagram.

Note: This is pseudocode please don't consider the semantics.

class DatingApp {
    
    public void hitExternalApi() {
    }
}

class DatingRestController {
    
    @Autowired
    public void DatingService;

    @GetMethod
    public Object processService() {
        return DatingService.findProfile();
    }
}

class DatingService {

    public Object findProfile() {
        Object retValue = new Object(null, null);
        var variable1 = doSomething();
        var varable2 = doSomethingElse();
        return retValue(variable1, variable2);
    }

    private String doSomething() {
    }

    private String doSomethingElse() {
    }
}

PlantUML DSL file:

@startuml
DatingApp -> DatingRestController: hitExternalApi()
DatingRestController -> DatingService: processService()
DatingService -> DatingService: findProfile()
DatingService -> DatingService: doSomething()
DatingService -> DatingService: doSomethingElse()
DatingService -> DatingRestController: sent retValue
DatingRestController -> DatingApp: Send JSON
@enduml

My initial pass:

enter image description here


As you can see that this looks like the DateService class is calling the process() method and then subsequently calling the doSomething() and doSomethingElse() methods.

How do I represent that doSomething() and doSomethingElse() methods are being called from within findProfile() lifeline instead of looking like external public calls?

PacificNW_Lover
  • 4,746
  • 31
  • 90
  • 144
  • `looking like external public calls` how can you say that ? They start from the same instance of *DatingService* so they are reflexive messages, not external ones. However to show that better and not consider they can be independent of the call of *process* look at the answer of Jean-Marc Volle. Anyway the fact the operations are private or not is not relevant here and you cannot show that in a sequence diagram/interaction – bruno Jun 26 '20 at 21:12

2 Answers2

4

You can use activate and deactivate to show which object is active and use return function calls.

Here is an example:

@startuml
DatingApp -> DatingRestController: hitExternalApi()
DatingRestController -> DatingService: process()
activate DatingService
DatingService -> DatingService: doSomething()
DatingService -> DatingService: doSomethingElse()
DatingService --> DatingRestController
deactivate DatingService
@enduml

enter image description here

uml sequence diagram do not have rules for showing private/public methods calls but nothing prevents you from adding your own rules with a legend clarifying how to read your graphics. You could for instance use colors to denote private rules

@startuml
legend top left
  <color blue> Blue calls</color> denote public method calls
  <color red> Red calls</color> denote private method calls
endlegend
DatingApp -[#blue]> DatingRestController: hitExternalApi()
DatingRestController -[#blue]> DatingService: process()
activate DatingService 
DatingService -[#red]> DatingService: doSomething()
DatingService -[#red]> DatingService: doSomethingElse()
DatingService -[#blue]-> DatingRestController
deactivate DatingService 
@enduml

enter image description here

Color can also be used on activation lines but the sequence description is a bit more complex:

@startuml
legend top left
  <color blue> Blue activation </color> denote public method calls
  <color red> Red activation</color> denote private method calls
endlegend
DatingApp -> DatingRestController: hitExternalApi()
DatingRestController -> DatingService: process()
activate DatingService #blue

DatingService -> DatingService: doSomething()
activate DatingService #red
deactivate DatingService

DatingService -> DatingService: doSomethingElse()
activate DatingService #red
deactivate DatingService

DatingService --> DatingRestController


deactivate DatingService 
@enduml

enter image description here

Jean-Marc Volle
  • 3,113
  • 1
  • 16
  • 20
  • Thank you, but ```doSomething()``` and ```doSomethingElse()``` methods are private methods and I need to show that they are called inside the public ```DatingService.process()``` method. Your example doesn't show that ```DatingService``` has a ```process``` method. – PacificNW_Lover Jun 26 '20 at 20:23
  • @PacificNW_Lover she diagram shows exactly what you want, why are you saying this is not the case ? Do you not see the message `process()` to *DatingService* then the reflexive messages `doSomething()` and `doSomethingElse()` ? If not buy glasses ... and/or read some documentation about sequence diagrams because you do not understand them ... ;-) – bruno Jun 26 '20 at 21:09
  • @PacificNW_Lover again you cannot indicate an operation is private or not in a sequence diagram/interaction, this is not relevant in it, use a class diagram for that. But that answer show without any ambiguity `doSomething()` and `doSomethingElse()` are called by `process()` – bruno Jun 26 '20 at 21:19
  • @PacificNW_Lover I updated my answer with a proposal for documenting private/public methods calls. – Jean-Marc Volle Jun 27 '20 at 14:58
  • As you explicitly state that the private methods are colored red shouldn't you express that explicitly by means of `#red` in de plantuml code as well and not rely on the defaults? The default color is not `#red` but some brownish red. – albert Jun 27 '20 at 16:13
  • @albert yes sure. I gave an example on how to do it. Any color could be chosen. I personnaly don't see that much difference between red and the default redish color ;-) – Jean-Marc Volle Jun 27 '20 at 16:29
  • Well the difference was quite obvious for me and one also should take into mind that users can set default colors by means of a `skinparameter` – albert Jun 27 '20 at 16:31
  • What do you mean by nested pipelines? Are you referring to activation lines? – Jean-Marc Volle Jun 29 '20 at 06:58
  • Meant nested lifelines, not pipelines. Are activation lines the same as nested lifelines? Sorry about the typo regarding pipelines. Just re-edited my post's title with lifelines. – PacificNW_Lover Jun 29 '20 at 07:02
  • You can use `autoactivate on` to start a lifeline for each function call but I do not see how this would "show" that one lifeline represents private function call. Again there is no standard for this. You could use specific colors for public vs private lifelines. – Jean-Marc Volle Jun 29 '20 at 07:07
  • @Jean-MarcVolle - just edited my post to clearly depict my question and posted my solution. Sorry, if my early version of my post was confusing. – PacificNW_Lover Jun 29 '20 at 08:19
1

So, after much trial and error, I looked at the PlantUML documentation and figured out that the real question was how to denote / represent private method calls as a nested lifeline.

Here's my solution:

@startuml
skinparam Shadowing false
title __Dating API Sequence Diagram__\n
caption \nVersion 1.0 - 6/26/2020 (Draft)\n
autonumber
activate DatingApp
DatingApp -> DatingRestController: hitExternalApi()
activate DatingRestController
DatingRestController -> DatingService: processService()
activate DatingService
DatingService -> DatingService: findProfile()
activate DatingService #90EE90
DatingService -> DatingService: doSomething()
DatingService -> DatingService: doSomethingElse()
deactivate DatingService
DatingService -> DatingRestController: return retValue
DatingRestController -> DatingApp: jsonPayload
deactivate DatingRestController
deactivate DatingApp
legend bottom right
Legend
|=Color |= Name |= Type |= Lifeline |
|<back:#FFFFFF>           </back>| DatingApp.hitExternalApi() | method | default |
|<back:#FFFFFF>           </back>| DatingRestController.processService() | method | default |
|<back:#FFFFFF>           </back>| DatingService.findProfile | method | default |
|<back:#90EE90>           </back>| DatingService.doSomething() | method | nested |
|<back:#90EE90>           </back>| DatingService.doSomethingElse() | method | nested |
endlegend
@enduml

Here's the generated Sequence Diagram from IntelliJ IDEA:

enter image description here


Is there anyway that the doSomethingElse() method can still point to the green nested pipeline or does its arrow pointing to the default white lifeline containing the calling findProfile() method correct?


PlantUML is a great tool and am looking forward to getting better and better at using it and helping others here with their questions!

PacificNW_Lover
  • 4,746
  • 31
  • 90
  • 144
  • I think in the end of all those posts are really confusing because you changed several time your question and I answered to each revision of your question in the original post. As originally stated be reviewers in the original post, your solution do not describe in any way if calls are private or public. – Jean-Marc Volle Jun 29 '20 at 09:25
  • Sorry, the original post was meant to ask how to show a private method as a nested lifeline, but I didn’t know the correct wording until I read the docs. It’s the same question just with refactored method calls for readability purposes. – PacificNW_Lover Jun 29 '20 at 09:28