From 7e92d64db37efd797d1cb392250e767900191abc Mon Sep 17 00:00:00 2001 From: Suneth Warnakulasuriya <7856520+sunethwarna@users.noreply.github.com> Date: Sun, 28 Jul 2024 23:00:33 +0530 Subject: [PATCH] add has methods --- .../utilities/component_data_view.py | 7 ++++-- .../utilities/optimization_problem.py | 24 +++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/applications/OptimizationApplication/python_scripts/utilities/component_data_view.py b/applications/OptimizationApplication/python_scripts/utilities/component_data_view.py index cd73eb516aec..34e220332b1d 100644 --- a/applications/OptimizationApplication/python_scripts/utilities/component_data_view.py +++ b/applications/OptimizationApplication/python_scripts/utilities/component_data_view.py @@ -49,12 +49,15 @@ def SetDataBuffer(self, buffer_size: int): if buffered_data.GetBufferSize() < buffer_size: raise RuntimeError(f"The required buffer size is not satisfied with the existing problem data container. [ component data container buffer size = {buffered_data.GetBufferSize()}, required buffer size = {buffer_size}, component = {self.__component_name}, component type = {self.__component_type}") + def HasDataBuffer(self) -> bool: + return self.__problem_data.HasValue(self.__buffered_data_name) + def GetComponent(self) -> Any: return self.__component def GetBufferedData(self) -> BufferedDict: - if not self.__problem_data.HasValue(self.__buffered_data_name): - raise RuntimeError(f"Buffered data is not set by calling ComponentData::SetBuffer for component of type \"{self.__component_type}\" with component name \"{self.__component_name}\".") + if not self.HasDataBuffer(): + raise RuntimeError(f"Buffered data is not set by calling ComponentData::SetDataBuffer for component of type \"{self.__component_type}\" with component name \"{self.__component_name}\".") return self.__problem_data[self.__buffered_data_name] diff --git a/applications/OptimizationApplication/python_scripts/utilities/optimization_problem.py b/applications/OptimizationApplication/python_scripts/utilities/optimization_problem.py index f2e9a2593edb..d74bd6a15c90 100644 --- a/applications/OptimizationApplication/python_scripts/utilities/optimization_problem.py +++ b/applications/OptimizationApplication/python_scripts/utilities/optimization_problem.py @@ -80,6 +80,14 @@ def GetResponse(self, name: str) -> ResponseFunction: def GetListOfResponses(self) -> 'list[ResponseFunction]': return self.__components[ResponseFunction].values() + def HasResponse(self, name_or_response: 'Union[str, ResponseFunction]') -> bool: + if isinstance(name_or_response, str): + return name_or_response in [response_function.GetName() for response_function in self.GetListOfResponses()] + elif isinstance(name_or_response, ResponseFunction): + return name_or_response in self.GetListOfResponses() + else: + raise RuntimeError(f"Unsupported type provided for name_or_response. Only allowed to have string or ResponseFunction types.") + def RemoveResponse(self, name: str) -> None: self.RemoveComponent(name, ResponseFunction) @@ -89,6 +97,14 @@ def GetExecutionPolicy(self, name: str) -> ExecutionPolicy: def GetListOfExecutionPolicies(self) -> 'list[ExecutionPolicy]': return self.__components[ExecutionPolicy].values() + def HasExecutionPolicy(self, name_or_execution_policy: 'Union[str, ExecutionPolicy]') -> bool: + if isinstance(name_or_execution_policy, str): + return name_or_execution_policy in [execution_policy.GetName() for execution_policy in self.GetListOfExecutionPolicies()] + elif isinstance(name_or_execution_policy, ExecutionPolicy): + return name_or_execution_policy in self.GetListOfExecutionPolicies() + else: + raise RuntimeError(f"Unsupported type provided for name_or_execution_policy. Only allowed to have string or ExecutionPolicy types.") + def RemoveExecutionPolicy(self, name: str) -> None: self.RemoveComponent(name, ExecutionPolicy) @@ -98,6 +114,14 @@ def GetControl(self, name: str) -> Control: def GetListOfControls(self) -> 'list[Control]': return self.__components[Control].values() + def HasControl(self, name_or_control: 'Union[str, Control]') -> bool: + if isinstance(name_or_control, str): + return name_or_control in [control.GetName() for control in self.GetListOfControls()] + elif isinstance(name_or_control, Control): + return name_or_control in self.GetListOfControls() + else: + raise RuntimeError(f"Unsupported type provided for name_or_control. Only allowed to have string or Control types.") + def RemoveControl(self, name: str) -> None: self.RemoveComponent(name, Control)