Structural model example#
This example present the Python code of a basic model (designed with the B-UML Language) that describes the typical domain of libraries, books and authors. The UML diagram is depicted in the following figure.

The Python code to specify the B-UML model, including its classes, attributes, and relationships, is presented in the following
code (lines 1-44). Additionally, the Python_Generator
, DjangoGenerator
, SQLAlchemyGenerator
, SQLGenerator
, RESTAPIGenerator
, Pydantic_Generator
and BackendGenerator
code generators are implemented in this example (lines 50-61). Running this script will generate the output/
folder with the
classes.py
, models.py
, sql_alchemy.py
, tables.sql
, rest_api.py
and pydantic_classes.py
files produced by each of the Generators respectively.
1from besser.BUML.metamodel.structural import DomainModel, Class, Property, \
2 Multiplicity, BinaryAssociation, StringType, IntegerType, DateType
3from besser.generators.python_classes import PythonGenerator
4from besser.generators.sql_alchemy import SQLAlchemyGenerator
5from besser.generators.sql import SQLGenerator
6from besser.generators.rest_api import RESTAPIGenerator
7from besser.generators.rdf import RDFGenerator
8from besser.generators.backend import BackendGenerator
9
10# Library attributes definition
11library_name: Property = Property(name="name", type=StringType)
12address: Property = Property(name="address", type=StringType)
13# Library class definition
14library: Class = Class(name="Library", attributes={library_name, address})
15
16# Book attributes definition
17title: Property = Property(name="title", type=StringType)
18pages: Property = Property(name="pages", type=IntegerType)
19release: Property = Property(name="release", type=DateType)
20# Book class definition
21book: Class = Class(name="Book", attributes={title, pages, release})
22
23# Author attributes definition
24author_name: Property = Property(name="name", type=StringType)
25email: Property = Property(name="email", type=StringType)
26# Author class definition
27author: Class = Class(name="Author", attributes={author_name, email})
28
29# Library-Book association definition
30located_in: Property = Property(name="locatedIn", type=library, multiplicity=Multiplicity(1, 1))
31has: Property = Property(name="has", type=book, multiplicity=Multiplicity(0, "*"))
32lib_book_association: BinaryAssociation = BinaryAssociation(name="lib_book_assoc", ends={located_in, has})
33
34# Book-Author association definition
35publishes: Property = Property(name="publishes", type=book, multiplicity=Multiplicity(0, "*"))
36written_by: Property = Property(name="writtenBy", type=author, multiplicity=Multiplicity(1, "*"))
37book_author_association: BinaryAssociation = BinaryAssociation(name="book_author_assoc", ends={written_by, publishes})
38
39# Domain model definition
40library_model: DomainModel = DomainModel(name="Library_model", types={library, book, author},
41 associations={lib_book_association, book_author_association})
42
43# Getting the attributes of the Book class
44for attribute in book.attributes:
45 print(attribute.name)
46
47# Code Generation
48
49python_model = PythonGenerator(model=library_model)
50python_model.generate()
51
52sql_alchemy = SQLAlchemyGenerator(model=library_model)
53sql_alchemy.generate()
54
55sql = SQLGenerator(model=library_model)
56sql.generate()
57
58rest_api = RESTAPIGenerator(model=library_model)
59rest_api.generate()
60
61backend = BackendGenerator(model=library_model, http_methods=["GET", "POST", "PUT", "DELETE"], nested_creations=False)
62backend.generate()
63
64rdf = RDFGenerator(model=library_model)
65rdf.generate()
Note
This structural model can also be created from a model designed with PlantUML or even from an image.