Draw constraint in PlantUML

525 Views Asked by At

How to draw a constraint like this in PlantUML

Example from https://www.uml-diagrams.org/constraint.html

1

There are 1 best solutions below

5
Christophe On BEST ANSWER

PlantUML technique 1

There's no easy way to do this, because PlantUML doesn't give a syntax that allows to relate lines. The best you could achieve is this:

enter image description here

You can achieve this by association two elements and creating a pair of the same elements, e.g. (Account, Person).This creates a composite element with two line segments related by the dot. You can then relate both pairs with ... to get a dotted line, and name this relation by adding a trailing : {xor}:

@startuml
skinparam style strictuml
class Account
class Person
class Corporation 
Account -- Person 
Account -- Corporation 
(Account,Person) ...  (Corporation,Account) : {xor}
@enduml

PlantUML technique 2

Another alternative is to use a comment box to include the constraint. This is by the way the usual notation when attaching a constraint to an element. You can then combine the pairing techniques with a note attached to each of the pairs (source: this tutorial in French):

@startuml
skinparam style strictuml
class Account
class Person
class Corporation 
Account -- Person 
Account -- Corporation 
note "{xor}" as N #white
(Account,Person) .. N  
N .. (Account,Corporation)  
@enduml 

UML Design improvement

In reality such constraints should be minimized as much as possible, because they often only hide a missing abstraction. I would therefore not use the {xor} and rather consider :

enter image description here

By the way, the grouping of multiple inheritance relations is also achieved with pairing, as explained here.