-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add an endpoint to allow multiple deletion of binding constrain… (
- Loading branch information
1 parent
7edcc46
commit d14454a
Showing
11 changed files
with
269 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
antarest/study/storage/variantstudy/model/command/binding_constraint_utils.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# Copyright (c) 2025, RTE (https://www.rte-france.com) | ||
# | ||
# See AUTHORS.txt | ||
# | ||
# This Source Code Form is subject to the terms of the Mozilla Public | ||
# License, v. 2.0. If a copy of the MPL was not distributed with this | ||
# file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
# | ||
# SPDX-License-Identifier: MPL-2.0 | ||
# | ||
# This file is part of the Antares project. | ||
import typing as t | ||
|
||
from antarest.study.storage.rawstudy.model.filesystem.factory import FileStudy | ||
|
||
|
||
def remove_bc_from_scenario_builder(study_data: FileStudy, removed_groups: t.Set[str]) -> None: | ||
""" | ||
Update the scenario builder by removing the rows that correspond to the BC groups to remove. | ||
NOTE: this update can be very long if the scenario builder configuration is large. | ||
""" | ||
if not removed_groups: | ||
return | ||
|
||
rulesets = study_data.tree.get(["settings", "scenariobuilder"]) | ||
|
||
for ruleset in rulesets.values(): | ||
for key in list(ruleset): | ||
# The key is in the form "symbol,group,year" | ||
symbol, *parts = key.split(",") | ||
if symbol == "bc" and parts[0] in removed_groups: | ||
del ruleset[key] | ||
|
||
study_data.tree.save(rulesets, ["settings", "scenariobuilder"]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
antarest/study/storage/variantstudy/model/command/remove_multiple_binding_constraints.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
# Copyright (c) 2025, RTE (https://www.rte-france.com) | ||
# | ||
# See AUTHORS.txt | ||
# | ||
# This Source Code Form is subject to the terms of the Mozilla Public | ||
# License, v. 2.0. If a copy of the MPL was not distributed with this | ||
# file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
# | ||
# SPDX-License-Identifier: MPL-2.0 | ||
# | ||
# This file is part of the Antares project. | ||
import typing as t | ||
|
||
from typing_extensions import override | ||
|
||
from antarest.study.model import STUDY_VERSION_8_7 | ||
from antarest.study.storage.rawstudy.model.filesystem.config.binding_constraint import DEFAULT_GROUP | ||
from antarest.study.storage.rawstudy.model.filesystem.config.model import FileStudyTreeConfig | ||
from antarest.study.storage.rawstudy.model.filesystem.factory import FileStudy | ||
from antarest.study.storage.variantstudy.model.command.binding_constraint_utils import remove_bc_from_scenario_builder | ||
from antarest.study.storage.variantstudy.model.command.common import CommandName, CommandOutput | ||
from antarest.study.storage.variantstudy.model.command.icommand import MATCH_SIGNATURE_SEPARATOR, ICommand, OutputTuple | ||
from antarest.study.storage.variantstudy.model.command_listener.command_listener import ICommandListener | ||
from antarest.study.storage.variantstudy.model.model import CommandDTO | ||
|
||
|
||
class RemoveMultipleBindingConstraints(ICommand): | ||
""" | ||
Command used to remove multiple binding constraints at once. | ||
""" | ||
|
||
command_name: CommandName = CommandName.REMOVE_MULTIPLE_BINDING_CONSTRAINTS | ||
version: int = 1 | ||
|
||
# Properties of the `REMOVE_MULTIPLE_BINDING_CONSTRAINTS` command: | ||
ids: t.List[str] | ||
|
||
@override | ||
def _apply_config(self, study_data: FileStudyTreeConfig) -> OutputTuple: | ||
# If at least one bc is missing in the database, we raise an error | ||
already_existing_ids = {binding.id for binding in study_data.bindings} | ||
missing_bc_ids = [id_ for id_ in self.ids if id_ not in already_existing_ids] | ||
if missing_bc_ids: | ||
return CommandOutput(status=False, message=f"Binding constraint not found: '{missing_bc_ids}'"), {} | ||
return CommandOutput(status=True), {} | ||
|
||
@override | ||
def _apply(self, study_data: FileStudy, listener: t.Optional[ICommandListener] = None) -> CommandOutput: | ||
command_output, _ = self._apply_config(study_data.config) | ||
|
||
if not command_output.status: | ||
return command_output | ||
|
||
binding_constraints = study_data.tree.get(["input", "bindingconstraints", "bindingconstraints"]) | ||
|
||
old_groups = {bd.get("group", DEFAULT_GROUP).lower() for bd in binding_constraints.values()} | ||
|
||
deleted_binding_constraints = [] | ||
|
||
for key in list(binding_constraints.keys()): | ||
if binding_constraints[key].get("id") in self.ids: | ||
deleted_binding_constraints.append(binding_constraints.pop(key)) | ||
|
||
study_data.tree.save( | ||
binding_constraints, | ||
["input", "bindingconstraints", "bindingconstraints"], | ||
) | ||
|
||
existing_files = study_data.tree.get(["input", "bindingconstraints"], depth=1) | ||
for bc in deleted_binding_constraints: | ||
if study_data.config.version < STUDY_VERSION_8_7: | ||
study_data.tree.delete(["input", "bindingconstraints", bc.get("id")]) | ||
else: | ||
for term in ["lt", "gt", "eq"]: | ||
matrix_id = f"{bc.get('id')}_{term}" | ||
if matrix_id in existing_files: | ||
study_data.tree.delete(["input", "bindingconstraints", matrix_id]) | ||
|
||
new_groups = {bd.get("group", DEFAULT_GROUP).lower() for bd in binding_constraints.values()} | ||
removed_groups = old_groups - new_groups | ||
remove_bc_from_scenario_builder(study_data, removed_groups) | ||
|
||
return command_output | ||
|
||
@override | ||
def to_dto(self) -> CommandDTO: | ||
return CommandDTO( | ||
action=CommandName.REMOVE_MULTIPLE_BINDING_CONSTRAINTS.value, | ||
args={ | ||
"ids": self.ids, | ||
}, | ||
study_version=self.study_version, | ||
) | ||
|
||
@override | ||
def match_signature(self) -> str: | ||
return str(self.command_name.value + MATCH_SIGNATURE_SEPARATOR + ",".join(self.ids)) | ||
|
||
@override | ||
def match(self, other: ICommand, equal: bool = False) -> bool: | ||
if not isinstance(other, RemoveMultipleBindingConstraints): | ||
return False | ||
return self.ids == other.ids | ||
|
||
@override | ||
def _create_diff(self, other: "ICommand") -> t.List["ICommand"]: | ||
return [] | ||
|
||
@override | ||
def get_inner_matrices(self) -> t.List[str]: | ||
return [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters