From 492edb7b51decc5a9bb5ce47be2b0dfdac1b9ab9 Mon Sep 17 00:00:00 2001 From: Prithwish Mukherjee <109645853+prmukherj@users.noreply.github.com> Date: Wed, 28 Aug 2024 10:15:46 +0530 Subject: [PATCH] fix: Compound child creation and naming. (#3240) --- src/ansys/fluent/core/workflow.py | 51 +++++++++++++++++++----------- tests/test_new_meshing_workflow.py | 2 +- 2 files changed, 34 insertions(+), 19 deletions(-) diff --git a/src/ansys/fluent/core/workflow.py b/src/ansys/fluent/core/workflow.py index 3d4ffea0eb3..d20ac66b86b 100644 --- a/src/ansys/fluent/core/workflow.py +++ b/src/ansys/fluent/core/workflow.py @@ -1027,8 +1027,22 @@ def python_name(self) -> str: str Pythonic name of the task. """ + if not self._python_name: + self._python_name = self._get_python_names_for_compound_child() return self._python_name + def _get_python_names_for_compound_child(self): + py_name = ( + self._command_source._parent_of_compound_child + + "_child_" + + str( + self._command_source._compound_child_map[ + self._command_source._parent_of_compound_child + ] + ) + ) + return py_name + class CompositeTask(BaseTask): """Composite task representation for wrapping a Workflow TaskObject instance of @@ -1160,6 +1174,15 @@ def add_child_and_update(self, state=None, defer_update=None): Whether to defer the update. """ self._add_child(state) + py_name = self.python_name() + if py_name not in self._command_source._compound_child_map: + self._command_source._compound_child_map[py_name] = 1 + else: + self._command_source._compound_child_map[py_name] = ( + self._command_source._compound_child_map[py_name] + 1 + ) + self._command_source._compound_child = True + self._command_source._parent_of_compound_child = py_name if self._fluent_version >= FluentVersion.v241: if defer_update is None: defer_update = False @@ -1171,9 +1194,10 @@ def add_child_and_update(self, state=None, defer_update=None): PyFluentUserWarning, ) self._task.AddChildAndUpdate() - return self._last_child() + self._command_source._compound_child = False + return self.last_child() - def _last_child(self) -> BaseTask: + def last_child(self) -> BaseTask: """Get the last child of this CompoundTask and set their Python name. Returns @@ -1183,23 +1207,8 @@ def _last_child(self) -> BaseTask: """ children = self.tasks() if children: - py_name = self._get_python_names_for_compound_child() - self.tasks()[-1]._python_name = py_name - self._command_source.tasks()[-1]._python_name = py_name return children[-1] - def _get_python_names_for_compound_child(self): - py_name = None - if self.python_name() in self._command_source.task_names(): - py_name = self.python_name() + "_child_1" - - while py_name in self._command_source.task_names(): - temp_name = py_name - temp_name = temp_name[:-1] + str(int(temp_name[-1]) + 1) - py_name = temp_name - - return py_name - def compound_child(self, name: str): """Get the compound child task of this CompoundTask by name. @@ -1229,7 +1238,10 @@ def _makeTask(command_source, name: str) -> BaseTask: "Conditional": ConditionalTask, } task_type = task.TaskType() - kind = kinds[task_type] + if task_type is None and command_source._compound_child: + kind = CompoundChild + else: + kind = kinds[task_type] if not kind: message = ( "Unhandled empty workflow task type." @@ -1288,6 +1300,9 @@ def __init__( _python_name_display_text_map={}, _repeated_task_python_name_display_text_map={}, _initial_task_python_names_map={}, + _parent_of_compound_child=None, + _compound_child_map={}, + _compound_child=False, _unwanted_attrs={ "reset_workflow", "initialize_workflow", diff --git a/tests/test_new_meshing_workflow.py b/tests/test_new_meshing_workflow.py index 507e200d1a9..25586e499f2 100644 --- a/tests/test_new_meshing_workflow.py +++ b/tests/test_new_meshing_workflow.py @@ -716,7 +716,7 @@ def test_watertight_workflow_children( assert added_sizing.name() == "facesize_1" assert len(added_sizing.arguments()) added_sizing_by_name = add_local_sizing.compound_child("facesize_1") - added_sizing_by_pos = add_local_sizing.tasks()[-1] + added_sizing_by_pos = add_local_sizing.last_child() assert added_sizing.arguments() == added_sizing_by_name.arguments() assert added_sizing.arguments() == added_sizing_by_pos.arguments() assert added_sizing.python_name() == "add_local_sizing_child_1"