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.

Library model

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    PrimitiveDataType, Multiplicity, BinaryAssociation
 3from besser.generators.python_classes import PythonGenerator
 4from besser.generators.django import DjangoGenerator
 5from besser.generators.sql_alchemy import SQLAlchemyGenerator
 6from besser.generators.sql import SQLGenerator
 7from besser.generators.rest_api import RESTAPIGenerator
 8from besser.generators.pydantic_classes import PydanticGenerator
 9from besser.generators.backend import BackendGenerator
10
11# Primitive DataTypes
12t_int: PrimitiveDataType = PrimitiveDataType("int")
13t_str: PrimitiveDataType = PrimitiveDataType("str")
14t_datetime: PrimitiveDataType = PrimitiveDataType("datetime")
15
16# Library attributes definition
17library_name: Property = Property(name="name", type=t_str)
18address: Property = Property(name="address", type=t_str)
19# Library class definition
20library: Class = Class(name="Library", attributes={library_name, address})
21
22# Book attributes definition
23title: Property = Property(name="title", type=t_str)
24pages: Property = Property(name="pages", type=t_int)
25release: Property = Property(name="release", type=t_datetime)
26# Book class definition
27book: Class = Class(name="Book", attributes={title, pages, release})
28
29# Author attributes definition
30author_name: Property = Property(name="name", type=t_str)
31email: Property = Property(name="email", type=t_str)
32# Author class definition
33author: Class = Class(name="Author", attributes={author_name, email})
34
35# Library-Book association definition
36located_in: Property = Property(name="locatedIn", type=library, multiplicity=Multiplicity(1, 1))
37has: Property = Property(name="has", type=book, multiplicity=Multiplicity(0, "*"))
38lib_book_association: BinaryAssociation = BinaryAssociation(name="lib_book_assoc", ends={located_in, has})
39
40# Book-Author association definition
41publishes: Property = Property(name="publishes", type=book, multiplicity=Multiplicity(0, "*"))
42written_by: Property = Property(name="writtenBy", type=author, multiplicity=Multiplicity(1, "*"))
43book_author_association: BinaryAssociation = BinaryAssociation(name="book_author_assoc", ends={written_by, publishes})
44
45# Domain model definition
46library_model: DomainModel = DomainModel(name="Library model", types={library, book, author},
47                                         associations={lib_book_association, book_author_association})
48
49# Getting the attributes of the Book class
50for attribute in book.attributes:
51    print(attribute.name)
52
53# Code Generation
54
55python_model = PythonGenerator(model=library_model)
56python_model.generate()
57
58django = DjangoGenerator(model=library_model)
59django.generate()
60
61sql_alchemy = SQLAlchemyGenerator(model=library_model)
62sql_alchemy.generate()
63
64sql = SQLGenerator(model=library_model)
65sql.generate()
66
67rest_api = RESTAPIGenerator(model=library_model)
68rest_api.generate()
69
70pydantic_model = PydanticGenerator(model=library_model, backend=True)
71pydantic_model.generate()
72
73backend = BackendGenerator(model=library_model, http_methods=["GET", "POST", "PUT", "DELETE"], nested_creations=True)
74backend.generate()

Note

This structural model can also be created from a model designed with PlantUML or even from an image.