Search code examples
plantuml

Can states in a Plant UML state diagram be positioned relative to another state?


I've made a plantuml state diagram which looks like this:

enter image description here

I'd like to position "MainActivity" state to the right of the "Idle" state, such that the arrows for [instruction] and [done] end up horizontal.

I know this is can be done for notes and text, but I haven't seen any syntax for the states themselves.

Is this possible in PlantUML?

For reference, this is my PlantUML script:

@startuml

skinparam state {
    StartColor PaleGreen
    EndColor Red
    BackgroundColor Gold
    BackgroundColor<<Junction>> GreenYellow  
    BorderColor Gray
    FontName Consolas
}

[*] -> Creating
Creating --> Idle : initialised
Idle : wait here for instructions
Idle --> MainActivity : [instruction]

state MainActivity {    
    [*] --> DoFirstThing    
    DoFirstThing --> FirstThingSuccess : [OK]
    DoFirstThing --> FirstThingFailed : [not OK]
    FirstThingSuccess --> SecondThing
    SecondThing --> SecondThingFailed
    SecondThing --> SecondThingSuccess  
    SecondThingSuccess --> WritingReport
    SecondThingFailed --> WritingReport
    FirstThingFailed --> WritingReport
    WritingReport --> [*]
}

MainActivity --> Idle : [done]

@enduml

Solution

  • Yes. You can define the direction of the arrows by inserting them in between the dashes of the arrow notation, such as "MainActivity -left-> Idle" (see section 7.5 in the PlantUML reference guide).

    The following code :

    plantuml
    @startuml
    
    skinparam state {
        StartColor PaleGreen
        EndColor Red
        BackgroundColor Gold
        BackgroundColor<<Junction>> GreenYellow  
        BorderColor Gray
        FontName Consolas
    }
    
    [*] -> Creating
    Creating --> Idle : initialised
    Idle : wait here for instructions
    Idle -right-> MainActivity : [instruction]
    
    state MainActivity {    
        [*] --> DoFirstThing    
        DoFirstThing --> FirstThingSuccess : [OK]
        DoFirstThing --> FirstThingFailed : [not OK]
        FirstThingSuccess --> SecondThing
        SecondThing --> SecondThingFailed
        SecondThing --> SecondThingSuccess  
        SecondThingSuccess --> WritingReport
        SecondThingFailed --> WritingReport
        FirstThingFailed --> WritingReport
        WritingReport --> [*]
    }
    
    MainActivity -left-> Idle : [done]
    
    @enduml
    

    gives you this diagramme :

    enter image description here

    (PS : You could do the same for the "Creating" state to make it look more consisten)