Skip to content

Commit

Permalink
fix: Compound child creation and naming. (#3240)
Browse files Browse the repository at this point in the history
  • Loading branch information
prmukherj authored Aug 28, 2024
1 parent 58c98cc commit 492edb7
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 19 deletions.
51 changes: 33 additions & 18 deletions src/ansys/fluent/core/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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."
Expand Down Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion tests/test_new_meshing_workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 492edb7

Please sign in to comment.