Source code for pySimBlocks.project.build_model
# ******************************************************************************
# pySimBlocks
# Copyright (c) 2026 Université de Lille & INRIA
# ******************************************************************************
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at your
# option) any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
# for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
# ******************************************************************************
# Authors: see Authors.txt
# ******************************************************************************
from __future__ import annotations
import importlib
from pathlib import Path
from typing import Dict, Any
import yaml
from pySimBlocks.core.model import Model
[docs]
def build_model_from_dict(
model: Model,
model_data: Dict[str, Any],
params_dir: Path | None = None,
) -> None:
"""Build a Model instance from an already-loaded model dictionary.
Reads the block registry index, instantiates each block described in
``model_data``, and wires up the connections.
Args:
model: The :class:`Model` instance to populate with blocks and
connections.
model_data: Model dictionary with ``'blocks'`` and ``'connections'``
sections, as produced by :func:`load_project_config`.
params_dir: Directory used to resolve relative file paths in block
parameters (e.g. scene files, function files). Passed to each
block's ``adapt_params`` classmethod.
Raises:
ValueError: If a block type or category is not found in the registry.
"""
index_path = Path(__file__).parent / "pySimBlocks_blocks_index.yaml"
with index_path.open("r") as f:
blocks_index = yaml.safe_load(f) or {}
for desc in model_data.get("blocks", []):
name = desc["name"]
category = desc["category"]
block_type = desc["type"]
try:
block_info = blocks_index[category][block_type]
except KeyError:
print(f"Available blocks in category '{category}':")
for bt in blocks_index.get(category, {}):
print(f" - {bt}")
print(desc)
raise ValueError(
f"Unknown block '{block_type}' in category '{category}'."
)
module = importlib.import_module(block_info["module"])
BlockClass = getattr(module, block_info["class"])
params = desc.get("parameters", {})
params = BlockClass.adapt_params(params, params_dir=params_dir)
block = BlockClass(name=name, **params)
model.add_block(block)
for src, dst in model_data.get("connections", []):
src_block, src_port = src.split(".")
dst_block, dst_port = dst.split(".")
model.connect(src_block, src_port, dst_block, dst_port)