From Draw.io to B-UML#
A B-UML model can also be generated from a class model built with Draw.io . All you need is to provide the DrawIO or XML file exported from Draw.io and our T2M transformation will produce the B-UML based model, including the source code to build the model, in case you want to modify any part of your model.
Let’s see an example with the classic library model. The Draw.io model is shown below.

Export your Draw.io diagram as an XML file, e.g. library.drawio
.
Then, load and process the model using our grammar and apply the transformation to obtain the B-UML based model. You can do this in two ways:
Generate only the B-UML model object:
# Import methods and classes
from besser.BUML.notations.structuralDrawIO import structural_drawio_to_buml
from besser.BUML.metamodel.structural import DomainModel
# Draw.io to B-UML model (without generating Python file)
library_buml: DomainModel = structural_drawio_to_buml(drawio_file_path='library.drawio')
Generate both the model object and a Python file:
# Generate model and save to Python file
library_buml: DomainModel = structural_drawio_to_buml(
drawio_file_path='library.drawio',
buml_file_path='library_model'
)
Note
The drawio_file_path
parameter contains the path and name of the .drawio
model to be transformed
The optional buml_file_path
parameter specifies the name for the generated Python file (without extension)
library_buml
is the B-UML model containing the domain specification. You can look up the classes, attributes, relationships,
etc. For example, the following is the way to get the class names.
# Print class names
for cls in library_buml.get_classes():
print(cls.name)
You should get output like this:
Library
Book
Author
Supported Class Notations#
The Draw.io parser supports several UML class notations:
- Swimlane style (recommended)
Uses Draw.io’s built-in swimlane shape
Supports attributes and methods
Clear visual separation of class sections
- HTML style with verticalAlign
Uses Draw.io’s rectangle with HTML formatting
Supports attributes and methods
Sections separated by horizontal lines
Warning
All associations must have unique names
Class names must be unique
Attributes and methods should follow UML notation (e.g., “+ name: String”)
Supported multiplicity formats: “1”, “*”, “0..1”, “1..*”, etc.
To add multiplicities and association/end names, click on the association end and create labels
Association names should start with an uppercase letter (e.g., “HasBooks”)
Association end names should start with a lowercase letter (e.g., “books”, “author”)
B-UML model source code#
When you run this Draw.io to B-UML transformation, the file buml/buml_model.py
will be created with the python code of your B-UML model definition.
You could directly reuse this code to make quick modifications to your model. For example, you could add more classes, properties, or update the name
of the ends of an association.
The B-UML model source code generated by the transformation from the library.drawio
model would look similar to this:
1# Generated B-UML Model
2from besser.BUML.metamodel.structural import (
3 Class, Property, Method, Parameter,
4 BinaryAssociation, Generalization, DomainModel,
5 Enumeration, EnumerationLiteral, Multiplicity,
6 StringType, IntegerType, FloatType, BooleanType,
7 TimeType, DateType, DateTimeType, TimeDeltaType
8)
9
10# Enumerations
11MemberType: Enumeration = Enumeration(
12 name="MemberType",
13 literals={
14 EnumerationLiteral(name="ADULT"),
15 EnumerationLiteral(name="SENIOR"),
16 EnumerationLiteral(name="STUDENT"),
17 EnumerationLiteral(name="CHILD")
18 }
19)
20
21# Classes
22Book = Class(name="Book")
23Author = Class(name="Author")
24Library = Class(name="Library")
25
26# Book class attributes and methods
27Book_release: Property = Property(name="release", type=DateType)
28Book_title: Property = Property(name="title", type=StringType)
29Book_pages: Property = Property(name="pages", type=IntegerType)
30Book.attributes={Book_release, Book_title, Book_pages}
31
32# Author class attributes and methods
33Author_email: Property = Property(name="email", type=StringType)
34Author_member: Property = Property(name="member", type=MemberType)
35Author_m_method: Method = Method(name="method", parameters={Parameter(name='sms', type=StringType, default_value='message')})
36Author.attributes={Author_email, Author_member}
37Author.methods={Author_m_method}
38
39# Library class attributes and methods
40Library_name: Property = Property(name="name", type=StringType)
41Library_address: Property = Property(name="address", type=StringType)
42Library_m_findBook: Method = Method(name="findBook", parameters={Parameter(name='title', type=StringType)}, type=Book)
43Library.attributes={Library_name, Library_address}
44Library.methods={Library_m_findBook}
45
46# Relationships
47Has: BinaryAssociation = BinaryAssociation(
48 name="Has",
49 ends={
50 Property(name="Book_end", type=Book, multiplicity=Multiplicity(0, "*")),
51 Property(name="Library_end", type=Library, multiplicity=Multiplicity(1, 1))
52 }
53)
54BookAuthor_Relation: BinaryAssociation = BinaryAssociation(
55 name="BookAuthor_Relation",
56 ends={
57 Property(name="writtenBy", type=Author, multiplicity=Multiplicity(1, "*")),
58 Property(name="Book_end", type=Book, multiplicity=Multiplicity(0, "*"))
59 }
60)
61
62# Domain Model
63domain_model = DomainModel(
64 name="Generated_Model",
65 types={Book, Author, Library, MemberType},
66 associations={Has, BookAuthor_Relation},
67 generalizations={}
68)
Getting Started with Draw.io#
To create UML class diagrams in Draw.io:
Click on “More Shapes” at the bottom of the left sidebar
Enable “UML” or “UML 2.5” in the libraries list
Use the proper UML shapes from these libraries for your diagram: - Class shapes from the UML library - Association arrows - Inheritance arrows - etc.
Tip
Always use shapes from the UML libraries rather than generic shapes to ensure proper UML notation and compatibility with the parser.
Note
An extension for Draw.io is available in Visual Studio Code, which allows you to create diagrams directly in the code editor.