Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/gems/study/resolve_components.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,15 @@ def _resolve_component(
lib_id, model_id = component.model.split(".")
model = libraries[lib_id].models[f"{lib_id}.{model_id}"]

provided_param_ids = {p.id for p in component.parameters or []}
missing_params = sorted(k for k in model.parameters if k not in provided_param_ids)
if missing_params:
raise ValueError(
f"Component {component.id!r} (model {model.id!r}) is missing "
f"parameter{'s' if len(missing_params) > 1 else ''} declared by the model: "
f"{missing_params}."
)

properties = _resolve_properties_raw_to_dict(component.properties, component.id)
missing = sorted(k for k in model.properties if k not in properties)
if missing:
Expand Down
54 changes: 54 additions & 0 deletions tests/unittests/system_parsing/test_components_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,11 @@ def test_sum_connections_with_linear_port_field_ok() -> None:
value: load_data
- id: nuclear_1
model: basic.generator
parameters:
- id: cost
value: 30
- id: p_max
value: 100
properties:
- id: technology
value: nuclear
Expand Down Expand Up @@ -297,6 +302,55 @@ def test_resolve_component_missing_declared_property_raises() -> None:
resolve_system(system, resolve_library([lib]))


# --- model-declared parameters ---

_LIB_WITH_MODEL_PARAMETERS = """\
library:
id: basic
models:
- id: generator
parameters:
- id: cost
time-dependent: false
scenario-dependent: false
- id: p_max
time-dependent: false
scenario-dependent: false
"""


def test_resolve_component_with_declared_parameters_ok() -> None:
lib = parse_yaml_library(io.StringIO(_LIB_WITH_MODEL_PARAMETERS))
system = parse_yaml_components(io.StringIO("""\
system:
components:
- id: G
model: basic.generator
parameters:
- id: cost
value: 30
- id: p_max
value: 100
"""))
resolved = resolve_system(system, resolve_library([lib]))
assert resolved.get_component("G") is not None


def test_resolve_component_missing_declared_parameter_raises() -> None:
lib = parse_yaml_library(io.StringIO(_LIB_WITH_MODEL_PARAMETERS))
system = parse_yaml_components(io.StringIO("""\
system:
components:
- id: G
model: basic.generator
parameters:
- id: cost
value: 30
"""))
with pytest.raises(ValueError, match="p_max"):
resolve_system(system, resolve_library([lib]))


def test_resolve_component_extra_undeclared_property_allowed() -> None:
lib = parse_yaml_library(io.StringIO(_LIB_WITH_MODEL_PROPERTIES))
system = parse_yaml_components(io.StringIO("""\
Expand Down
Loading