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.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from besser.BUML.metamodel.structural import DomainModel, Class, Property, \
    Multiplicity, BinaryAssociation, StringType, IntegerType, DateType
from besser.generators.python_classes import PythonGenerator
from besser.generators.sql_alchemy import SQLAlchemyGenerator
from besser.generators.sql import SQLGenerator
from besser.generators.rest_api import RESTAPIGenerator
from besser.generators.rdf import RDFGenerator
from besser.generators.backend import BackendGenerator

# Library attributes definition
library_name: Property = Property(name="name", type=StringType)
address: Property = Property(name="address", type=StringType)
# Library class definition
library: Class = Class(name="Library", attributes={library_name, address})

# Book attributes definition
title: Property = Property(name="title", type=StringType)
pages: Property = Property(name="pages", type=IntegerType)
release: Property = Property(name="release", type=DateType)
# Book class definition
book: Class = Class(name="Book", attributes={title, pages, release})

# Author attributes definition
author_name: Property = Property(name="name", type=StringType)
email: Property = Property(name="email", type=StringType)
# Author class definition
author: Class = Class(name="Author", attributes={author_name, email})

# Library-Book association definition
located_in: Property = Property(name="locatedIn", type=library, multiplicity=Multiplicity(1, 1))
has: Property = Property(name="has", type=book, multiplicity=Multiplicity(0, "*"))
lib_book_association: BinaryAssociation = BinaryAssociation(name="lib_book_assoc", ends={located_in, has})

# Book-Author association definition
publishes: Property = Property(name="publishes", type=book, multiplicity=Multiplicity(0, "*"))
written_by: Property = Property(name="writtenBy", type=author, multiplicity=Multiplicity(1, "*"))
book_author_association: BinaryAssociation = BinaryAssociation(name="book_author_assoc", ends={written_by, publishes})

# Domain model definition
library_model: DomainModel = DomainModel(name="Library_model", types={library, book, author},
                                         associations={lib_book_association, book_author_association})

# Getting the attributes of the Book class
for attribute in book.attributes:
    print(attribute.name)

# Code Generation

python_model = PythonGenerator(model=library_model)
python_model.generate()

sql_alchemy = SQLAlchemyGenerator(model=library_model)
sql_alchemy.generate()

sql = SQLGenerator(model=library_model)
sql.generate()

rest_api = RESTAPIGenerator(model=library_model)
rest_api.generate()

backend = BackendGenerator(model=library_model, http_methods=["GET", "POST", "PUT", "DELETE"], nested_creations=False)
backend.generate()

rdf = RDFGenerator(model=library_model)
rdf.generate()

Note

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