How to include classes from another Plantuml file into a class diagram? - plantuml

I have two class diagrams in PlantUML and would like to define a class in one file and reuse it in the other. See the example below.
diagram1.puml:
#startuml diagram1
class "Foo" as foo {
...attributes
}
#enduml
and diagram2:
#startuml diagram2
!include diagram1!foo
class "Bar" as bar {
...attributes
}
#enduml
Now my expectation is that diagram2 will show me both classes Foo and Bar in the diagram. However, the included class is not shown in the render.
How can this be solved?

The plantuml include statement includes code as if it was typed in that included place (like include files for e.g C / C++ etc. do too). So the code should be like:
For Diagram2:
#startuml diagram2
!include diagram1!foo
class "Bar" as bar {
...attributes
}
#enduml
and for diagram1
class "Foo" as foo {
...attributes
}
When you want also show diagram1 as a separate file you have to add an extra file with:
#startuml diagram1_show
!include diagram1!foo
#enduml

Related

Why does an arrow make a duplicate component in a PlantUML diagram?

I'm very new to PlantUML, so this is likely something basic.
Sometimes when I add a arrow from one rectangle to another, the original component is not linked. Instead, a new component is created and linked. What am I doing wrong?
In my example, I have this fairly simple diagram:
#startuml
database "DB" {
frame Rules {
rectangle "Item 1"
rectangle "Item 2"
}
}
rectangle "App Server" {
rectangle "My UI"
}
rectangle "System" {
rectangle "Foo"
}
[My UI] --> [Item 1] : create and edit
[System] --> [Item 1] : extract
#enduml
And this is generated:
Note that the arrow from System to Item 1 is for a new component and not the existing "System" element.
What am I doing wrong?
I asked the same question in the PlantUML forum, and was told that this is due to the brackets around the name System in the arrows. I guess they can only be used if the name contains a space.
So replacing
[System] --> [Item 1] : extract
with
System --> [Item 1] : extract
fixes the issue, and creates this diagram:

plantuml : strange behavior when including multiple times startsub parts

Taking this small example to illustrate my problem.
common.puml
#startuml common
!startsub COMMON
abstract Common {
id : uuid
}
!endsub
#enduml
myclass1.puml
#startuml myclass1
!includesub common.puml!COMMON
class MyClass1 extends Common {
text: string
}
#enduml
myclass2.puml
#startuml myclass1
!includesub common.puml!COMMON
class MyClass2 extends Common {
value: int
}
#enduml
all.puml
#startuml all
!include ./sub/common.puml
!include ./sub/myclass1.puml
!include ./sub/myclass2.puml
#enduml
When redering the all.puml diagram, I have a strange result in the Common part.
Attributes are rendered 3 times !
Any idea ?
It's either a feature or a bug. Include files just put the code from the file inside another. Since common.puml ends up being included three times (once in all, once in myclass1, and once in myclass2), you wind up more or less with:
abstract Common {
id : uuid
}
abstract Common {
id : uuid
}
abstract Common {
id : uuid
}
which will produce
Maybe you can prevent this from happening by defining a variable, e.g., !$common = 1 inside the part of common.puml that you don't want to repeat, and check before doing the !includesub, e.g.
...
!if (%not($common == 1))
!includesub common.puml!COMMON
!endif
...
(Note - I didn't test this!)

How to mix different plantuml diagram type elements?

I want to have deployment and sequence stuff in one and the same rendered plantuml picture. So I tried the following but it does not work, the rendering shows the sequence stuff for A, B and C as deployment.
How can I force rendering for "A->B" and "B->C" as sequence diagram stuff?
#startuml
file main.c
note right: I want to have description text here
A -> B : main()
note left : program\nentry function
B -> C : load()
note left : another important function
#enduml
I think you need to add your note later:
#startuml
title file main.c
A -> B : main()
note left : program\nentry function
B -> C : load()
note left : another important function
note right: I want to have description text here
#enduml
Result:

Plantuml class diagram with multiple children: Any way to bifurcate the arrow?

My attempt:
Animal <|-- Cat
Animal <|-- Dog
Result:
┌────────┐
│ Animal │
└────────┘
Δ Δ
│ │
┌──┴──┐┌──┴──┐
│ Cat ││ Dog │
└─────┘└─────┘
That is not how a class diagram is supposed to look like.
This is:
┌────────┐
│ Animal │
└────────┘
Δ
┌──┴───┐
┌──┴──┐┌──┴──┐
│ Cat ││ Dog │
└─────┘└─────┘
As suggested, I asked if this is possible on the PlantUML forum.
You can do something like this:
#startuml
class Animal
together {
class Dog
class Cat
}
Animal <|-- Cat
Dog -- (Animal, Cat)
#enduml
There's skinparam groupInheritance 2 which will serve your purpose, although it doesn't work with skinparam linetype ortho as one might expect. Alas, GraphViz is the rendering engine, so that has limitations.
#startuml
skinparam style strictuml
hide empty members
skinparam groupInheritance 2
class Animal
class Cat extends Animal
class Dog extends Animal
#enduml
An interesting thing to do in plantUML but "this is not how class diagram is supposed to look like" is not correct (to my knowledge, at least).
The notation is clear for inheritance/generalization but whether you join the lines before the arrow or have separate lines with separate arrows is a matter of visual preference/making it easier to understand:
Try to sketch the image on top of that wiki you linked with distinct arrow from each child to it's parent, it will be much more messy
Scroll down a bit on wiki to https://en.wikipedia.org/wiki/Class_diagram#Generalization/Inheritance

How to generate UML package diagram with nested packages which may contain same names in different level?

I want to generate a package diagram with hierarchical packages which may contain duplicated names but not in the same level. e.g.:
#startuml
package A {
package B {
package C
}
package A
}
#enduml
I think, package A.A might be acceptable. But plantuml failed with this error: Thie element (A) is already defined.
I also try the following:
#startuml
folder A {
folder B {
folder C
}
folder A
}
#enduml
Then, plantuml failed with the same error.
One of the options :
#startuml
package A as pkg0{
package B as pkg1{
}
package A as pkg2{
package A as pkg3{
}
}
package A as pkg4{
}
}
#enduml
will give result
You can draw diagrams with duplicated names if you include non-printing characters in the definition, e.g. by putting the name in quotes and including one or more space characters:
#startuml
package A {
package B {
package C
}
package "A "
package "A "
}
#enduml
The additional spaces do not affect the layout in the diagram:

Resources