Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Customise enhanced meshing workflow #2701

Merged
merged 41 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
f6286b9
Clean up.
prmukherj Apr 16, 2024
1ba4dde
Handle an issue with camel_to_snake convertor.
prmukherj Apr 16, 2024
0a3ca72
Merge branch 'main' into feat/enhanced_meshing_workflow_custom
prmukherj Apr 17, 2024
432cc07
Clean up test and code.
prmukherj Apr 17, 2024
4d84114
Clean up test and code.
prmukherj Apr 17, 2024
9bf9069
Update test.
prmukherj Apr 17, 2024
f5ddf1b
Update possible task list.
prmukherj Apr 17, 2024
8918e7d
Fix tests.
prmukherj Apr 17, 2024
af0acd2
Fix camel case conversion.
prmukherj Apr 17, 2024
3c92bbb
Add test for duplicate tasks.
prmukherj Apr 17, 2024
4745eed
Object oriented task insertion.
prmukherj Apr 18, 2024
9997e79
Fix test.
prmukherj Apr 18, 2024
7d3a271
Update src/ansys/fluent/core/workflow.py
prmukherj Apr 19, 2024
8902e3d
Update src/ansys/fluent/core/workflow.py
prmukherj Apr 19, 2024
ff511ad
Update src/ansys/fluent/core/workflow.py
prmukherj Apr 19, 2024
159ffa4
Update src/ansys/fluent/core/workflow.py
prmukherj Apr 19, 2024
2ec895b
Update src/ansys/fluent/core/workflow.py
prmukherj Apr 19, 2024
937cbb0
Merge branch 'main' into feat/enhanced_meshing_workflow_custom
prmukherj Apr 19, 2024
12917c5
Merge branch 'main' into feat/enhanced_meshing_workflow_custom
prmukherj Apr 19, 2024
9d60a93
feat: Load saved workflows. (#2718)
prmukherj Apr 19, 2024
00ef7d1
Update src/ansys/fluent/core/meshing/meshing_workflow.py
prmukherj Apr 20, 2024
0a905c2
Update src/ansys/fluent/core/meshing/meshing_workflow.py
prmukherj Apr 20, 2024
8b39504
Update src/ansys/fluent/core/meshing/meshing_workflow.py
prmukherj Apr 20, 2024
6738356
Update src/ansys/fluent/core/session_base_meshing.py
prmukherj Apr 20, 2024
a683bed
Update src/ansys/fluent/core/session_pure_meshing.py
prmukherj Apr 20, 2024
4f1cb85
Update src/ansys/fluent/core/workflow.py
prmukherj Apr 20, 2024
c42dd5a
Update src/ansys/fluent/core/workflow.py
prmukherj Apr 20, 2024
1b91ebf
Update src/ansys/fluent/core/workflow.py
prmukherj Apr 20, 2024
c48d595
Update src/ansys/fluent/core/workflow.py
prmukherj Apr 20, 2024
e4487da
Merge branch 'main' into feat/enhanced_meshing_workflow_custom
prmukherj Apr 20, 2024
0a30191
rename -> insertable_tasks
prmukherj Apr 20, 2024
b7f54b4
ordered_children -> tasks
prmukherj Apr 20, 2024
20318bb
Renaming.
prmukherj Apr 20, 2024
ffff8d5
Update doc.
prmukherj Apr 20, 2024
aa3a312
Refactor.
prmukherj Apr 20, 2024
225e302
Refactor.
prmukherj Apr 20, 2024
02ebfe9
Fix tests.
prmukherj Apr 20, 2024
e3428fa
Refactor.
prmukherj Apr 21, 2024
6e5411e
Refactor.
prmukherj Apr 21, 2024
8770222
Rename.
prmukherj Apr 22, 2024
1f1e7d6
Update tests.
prmukherj Apr 22, 2024
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
152 changes: 71 additions & 81 deletions src/ansys/fluent/core/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,15 @@ def camel_to_snake_case(camel_case_str: str) -> str:
return camel_to_snake_case.cache[camel_case_str]
except KeyError:
if not camel_case_str.islower():
_snake_case_str = re.sub(
"((?<=[a-z])[A-Z0-9]|(?!^)[A-Z](?=[a-z0-9]))", r"_\1", camel_case_str
).lower()
_snake_case_str = (
re.sub(
"((?<=[a-z])[A-Z0-9]|(?!^)[A-Z](?=[a-z0-9]))",
r"_\1",
camel_case_str,
)
.lower()
.replace("__", "_")
)
else:
_snake_case_str = camel_case_str
camel_to_snake_case.cache[camel_case_str] = _snake_case_str
Expand Down Expand Up @@ -178,6 +184,7 @@ def __init__(
_cmd=None,
_python_name=None,
_python_task_names=[],
_python_task_names_map={},
_lock=command_source._lock,
_ordered_children=[],
_task_list=[],
Expand Down Expand Up @@ -349,7 +356,9 @@ def python_name(self) -> str:
try:
this_command = self._command()
# temp reuse helpString
self._python_name = this_command.get_attr("helpString")
self._python_name = camel_to_snake_case(
this_command.get_attr("helpString")
)
if (
self._python_name
in self._command_source._help_string_display_text_map
Expand Down Expand Up @@ -470,6 +479,7 @@ def rename(self, new_name: str):
self._command_source._repeated_task_help_string_display_text_map[
new_name
] = new_name
self._python_name = new_name
return self._task.Rename(NewName=new_name)

def add_child_to_task(self):
Expand All @@ -484,18 +494,30 @@ def insert_compound_child_task(self):
"""Insert a compound child task."""
return self._task.InsertCompoundChildTask()

def _get_next_python_task_names(self) -> list[str]:
self._python_task_names_map = {}
for command_name in self._task.GetNextPossibleTasks():
self._python_task_names_map[
camel_to_snake_case(
getattr(self._command_source._command_source, command_name)
.create_instance()
.get_attr("helpString")
)
] = command_name
return list(self._python_task_names_map.keys())

def get_next_possible_tasks(self) -> list[str]:
"""Get the list of possible Python names that can be inserted as tasks after
this current task is executed."""
return [camel_to_snake_case(task) for task in self._task.GetNextPossibleTasks()]
return self._get_next_python_task_names()

def insert_next_task(self, command_name: str):
def insert_next_task(self, task_name: str):
"""Insert a task based on the Python name after the current task is executed.

Parameters
----------
command_name: str
Name of the new task.
task_name: str
Python name of the new task.

Returns
-------
Expand All @@ -504,19 +526,52 @@ def insert_next_task(self, command_name: str):
Raises
------
ValueError
If the command name does not match a task name.
If the python name does not match the next possible task names.
prmukherj marked this conversation as resolved.
Show resolved Hide resolved
"""
if command_name not in self.get_next_possible_tasks():
# The next line populates the python name map for next possible task
prmukherj marked this conversation as resolved.
Show resolved Hide resolved
self._get_next_python_task_names()
if task_name not in self.get_next_possible_tasks():
raise ValueError(
f"'{command_name}' cannot be inserted next to '{self.python_name()}'. \n"
f"'{task_name}' cannot be inserted next to '{self.python_name()}'. \n"
"Please use 'get_next_possible_tasks()' to view list of allowed tasks."
prmukherj marked this conversation as resolved.
Show resolved Hide resolved
)
return self._task.InsertNextTask(
CommandName=snake_to_camel_case(
command_name, self._task.GetNextPossibleTasks()
)
CommandName=self._python_task_names_map[task_name]
)

@property
def next_tasks(self):
"""Tasks that can be inserted after this current task."""
prmukherj marked this conversation as resolved.
Show resolved Hide resolved
return self._NextTask(self)

class _NextTask:
def __init__(self, base_task):
"""Initialize _NextTask."""
prmukherj marked this conversation as resolved.
Show resolved Hide resolved
self._base_task = base_task
self._insertable_tasks = []
for item in self._base_task.get_next_possible_tasks():
insertable_task = type("Insert", (self._Insert,), {})(
self._base_task, item
)
setattr(self, item, insertable_task)
self._insertable_tasks.append(insertable_task)

def __call__(self):
return self._insertable_tasks

class _Insert:
def __init__(self, base_task, name):
"""Initialize _Insert."""
prmukherj marked this conversation as resolved.
Show resolved Hide resolved
self._base_task = base_task
self._name = name

def insert(self):
"""Inserts a task in the workflow."""
prmukherj marked this conversation as resolved.
Show resolved Hide resolved
self._base_task.insert_next_task(task_name=self._name)

def __repr__(self):
return f"<Insertable '{self._name}' task>"

def __call__(self, **kwds) -> Any:
if kwds:
self._task.Arguments.set_state(**kwds)
Expand Down Expand Up @@ -1241,6 +1296,8 @@ def __init__(
"reset_workflow",
"initialize_workflow",
"load_workflow",
"insert_new_task",
"create_composite_task",
"create_new_workflow",
"rules",
"service",
Expand Down Expand Up @@ -1428,44 +1485,10 @@ def load_state(self, list_of_roots: list):
"""Load the state of the workflow."""
self._workflow.LoadState(ListOfRoots=list_of_roots)

def get_insertable_tasks(self):
"""Get the list of possible Python names that can be inserted as tasks."""
return [
item
for item in self._help_string_command_id_map.keys()
if item not in self._repeated_task_help_string_display_text_map.keys()
]

def get_available_task_names(self):
"""Get the list of the Python names for the available tasks."""
return [child.python_name() for child in self.ordered_children()]

def insert_new_task(self, task: str):
"""Insert a new task based on the Python name.

Parameters
----------
task: str
Name of the new task.

Returns
-------
None

Raises
------
ValueError
If 'task' does not match a task name.
"""
if task not in self.get_insertable_tasks():
raise ValueError(
f"'{task}' is not an allowed task.\n"
"Use the 'get_insertable_tasks()' method to view a list of allowed tasks."
)
return self._workflow.InsertNewTask(
CommandName=self._help_string_command_id_map[task]
)

def delete_tasks(self, list_of_tasks: list[str]):
"""Delete the provided list of tasks.

Expand Down Expand Up @@ -1502,39 +1525,6 @@ def delete_tasks(self, list_of_tasks: list[str]):

return self._workflow.DeleteTasks(ListOfTasks=list_of_tasks_with_display_name)

def create_composite_task(self, list_of_tasks: list[str]):
"""Create the list of tasks based on the Python names.

Parameters
----------
list_of_tasks: list[str]
List of task items.

Returns
-------
None

Raises
------
RuntimeError
If the 'task' does not match a task name.
"""
list_of_tasks_with_display_name = []
for task_name in list_of_tasks:
try:
list_of_tasks_with_display_name.append(
self._help_string_display_id_map[task_name]
)
except KeyError:
raise RuntimeError(
f"'{task_name}' is not an allowed task.\n"
"Use the 'get_available_task_names()' method to view a list of allowed tasks."
)

return self._workflow.CreateCompositeTask(
ListOfTasks=list_of_tasks_with_display_name
)


class ClassicWorkflow:
"""Wraps a meshing workflow object.
Expand Down
Loading
Loading