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.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', emit_ids: bool =False)[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”.
emit_ids (bool, optional) – Write each element’s
idas anid="..."argument. Off by default: BESSER releases beforeElement.idcannot load such a file.
- 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.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.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.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.
-
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
modelwhenexec()’d.
-
besser.utilities.buml_code_builder.project_to_code(project: Project, file_path: str, sm: str =
'', emit_ids: bool =False)[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.
emit_ids (bool) – Write element ids in the structural models (see
domain_model_to_code). Off by default.
- Outputs:
A Python file containing all models in the project
-
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_codeso multiple NNs in one project get unique suffixes —my_nn_1/my_nn_2— instead of colliding on the samemy_nnbinding).title (str, optional) – Diagram title to embed in the section header so
project_to_jsoncan recover it on round-trip. The NN metamodel only stores a sanitized identifier-safename, so without this the user-facing title is lost.
-
besser.utilities.buml_code_builder.kg_model_to_code(model: KnowledgeGraph, file_path: str, model_var_name: str =
'kg_model', title: str | None =None)[source]¶ Generates Python code for a KnowledgeGraph model.
- Parameters:¶
model (KnowledgeGraph) – The knowledge graph model.
file_path (str) – The path to save the generated code.
model_var_name (str, optional) – Name of the KnowledgeGraph variable in the generated code. Defaults to
"kg_model", which is the keyproject_to_jsonlooks up in itsSECTION_CONFIGtable — any other name and the section is silently skipped on import.title (str, optional) – Diagram title to embed in the section banner (
# KNOWLEDGE_GRAPH MODEL: "title" #);project_to_jsonreads it back as the diagram’s title.project_to_codepasses one for every KG; without one the diagram comes back with the default title.
- Raises:¶
TypeError – If a node, edge or axiom carries a value that is not JSON-compatible (see
_literal()).
-
besser.utilities.buml_code_builder.component_model_to_code(model: ComponentModel, file_path: str | None =
None, model_var_name: str ='component_model') str[source]¶ Generate Python code that re-creates
modelonexec().