Build a model using the B-UML meta-classes#

Note

This guide shows the definition of a Structural model using the B-UML library in Python. However, any B-UML model can be created using the B-UML libraries. More examples are available here.

The following guide shows how to define the classic Library model (see following image) using the B-UML library in Python.

Library model

To define your model using B-UML, you must first import the B-UML classes you are going to use. The following classes must be imported for this Library modeling example.

from besser.BUML.metamodel.structural import DomainModel, Class, Property, \
    PrimitiveDataType, Multiplicity, BinaryAssociation

Now, we can define the attributes, classes, relationships, and other elements of the model. The following is the definition of the Book class including its attributes.

# Book attributes definition
title: Property = Property(name="title", owner=None, type=PrimitiveDataType("str"))
pages: Property = Property(name="pages", owner=None, type=PrimitiveDataType("int"))
release: Property = Property(name="release", owner=None, type=PrimitiveDataType("date"))

# Book class definition
book: Class = Class (name="Book", attributes={title, pages, release})

Different types of relationships can be specified with B-UML such as associations (including binary associations), generalizations, generalization sets, and class associations. Using the BynaryAssociation class, we can specify the relationship between Library and Book as follows.

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

Finally, create the domain model and add the classes, relationships and other elements of the model.

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

Now, library_model is your model specified using B-UML. You can access the classes, attributes, relationships, and all the elements of the model. For instance, you could print the name attributes of the Book class as follows.

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

You should get output like this:

tittle
pages
release

Note

You can consult the complete code for this basic example here.

Note

For a detailed description of metamodel elements such as classes, attributes, generalizations and others, please refer to the API documentation.

Now that you have a model defined with B-UML, you can use our Code Generators, or even build your own for a target technology.