B-UML Code Builder

BUML Code Builder Package

This package contains code builders for converting BUML models to executable Python code.

besser.utilities.buml_code_builder._escape_python_string(value: str) str[source]

Escape a string for safe interpolation into generated Python source code.

Prevents code injection when user-controlled values (names, labels, etc.) are embedded inside string literals in generated Python files that may later be executed with exec().

besser.utilities.buml_code_builder.agent_model_to_code(model: Agent, file_path: str, model_var_name: str = 'agent')[source]

Generates Python code for a B-UML Agent model and writes it to a specified file.

Parameters: model (Agent): The B-UML Agent model object containing states, intents, and transitions. file_path (str): The path where the generated code will be saved. model_var_name (str, optional): Name of the Agent variable in the generated code.

Defaults to “agent”.

Outputs: - A Python file containing the code representation of the B-UML agent model.

besser.utilities.buml_code_builder.bpmn_model_to_code(model: BPMNModel, file_path: str | None = None, model_var_name: str = 'bpmn_model') str[source]

Generate Python code that reconstructs model when exec()’d.

Parameters:
  • model – The BPMNModel instance to serialise.

  • file_path – Optional path to write the generated code to.

  • model_var_name – Variable name to use for the top-level model (default: "bpmn_model").

Returns:

The generated Python source as a string.

besser.utilities.buml_code_builder.domain_model_to_code(model: DomainModel, file_path: str, objectmodel: ObjectModel = None, model_var_name: str = 'domain_model', metadata_var_name: str | None = None, object_model_var_name: str = 'object_model')[source]

Generates Python code for a B-UML model and writes it to a specified file.

Parameters:
  • model (DomainModel) – The B-UML model object containing classes, enumerations, associations, and generalizations.

  • file_path (str) – The path where the generated code will be saved.

  • objectmodel (ObjectModel, optional) – The B-UML object model to include in the same file.

  • model_var_name (str, optional) – Name of the DomainModel variable in the generated code.

  • metadata_var_name (Optional[str], optional) – Name for the metadata helper variable. Defaults to “<model_var_name>_metadata” when not provided.

  • object_model_var_name (str, optional) – Name of the ObjectModel variable when an object model is included. Defaults to “object_model”.

Outputs:
  • A Python file containing the base code representation of the B-UML domain model and optionally the object model instances.

besser.utilities.buml_code_builder.gui_model_to_code(model: GUIModel, file_path: str, domain_model=None, model_var_name: str = 'gui_model')[source]

Generates Python code for a BUML GUI model and writes it to a specified file.

Parameters:
  • model (GUIModel) – The BUML GUI model containing modules, screens, and components

  • file_path (str) – The path where the generated code will be saved

  • domain_model (DomainModel, optional) – Structural model to emit before the GUI so that data bindings can reference the same domain_model variable.

Outputs:

A Python file containing the code representation of the BUML GUI model

besser.utilities.buml_code_builder.nn_model_to_code(model: NN, file_path: str, model_var_name: str = None, title: str = None)[source]

Generates Python code for an NN model, including any sub_nns.

The generated code follows the alexnet.py pattern: 1. First, sub_nns are defined as standalone NN objects 2. Then the main NN is created with add_sub_nn() calls

Parameters:
  • model (NN) – The neural network model.

  • file_path (str) – The path to save the generated code.

  • model_var_name (str, optional) – Override the main NN’s Python variable name (used by project_to_code so multiple NNs in one project get unique suffixes — my_nn_1 / my_nn_2 — instead of colliding on the same my_nn binding).

  • title (str, optional) – Diagram title to embed in the section header so project_to_json can recover it on round-trip. The NN metamodel only stores a sanitized identifier-safe name, so without this the user-facing title is lost.

besser.utilities.buml_code_builder.project_to_code(project: Project, file_path: str, sm: str = '')[source]

Generates Python code for a complete B-UML project and writes it to a specified file.

This function handles projects with multiple models of the same type. When a project contains more than one model of a given kind (e.g. two DomainModels), each generated variable receives a numeric suffix (domain_model_1, domain_model_2, …). When there is only one model of a given kind the output is identical to the previous single-model behaviour (no suffix).

Parameters:
  • project (Project) – The B-UML project containing multiple models

  • file_path (str) – The path where the generated code will be saved

  • sm (str) – Deprecated. Legacy state machine code string (kept for backward compatibility). StateMachine objects in the project’s model list are now handled automatically.

Outputs:
  • A Python file containing all models in the project

besser.utilities.buml_code_builder.state_machine_to_code(model: StateMachine, file_path: str = None, model_var_name: str = 'sm') str[source]

Generate Python code from a StateMachine metamodel instance.

Parameters:
  • model – The StateMachine instance to convert.

  • file_path – Optional path to write the generated code to.

  • model_var_name – Variable name to use for the state machine (default: “sm”).

Returns:

The generated Python code as a string.