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

handle empty zone #272

Merged
merged 2 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
51 changes: 33 additions & 18 deletions aiida_workgraph/engine/workgraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -1114,29 +1114,44 @@ def run_tasks(self, names: t.List[str], continue_workgraph: bool = True) -> None
self.set_task_state_info(name, "process", process)
self.to_context(**{name: process})
elif task["metadata"]["node_type"].upper() in ["WHILE"]:
# check the conditions of the while task
should_run = self.should_run_while_task(name)
if not should_run:
self.set_task_state_info(name, "state", "FINISHED")
self.set_tasks_state(self.ctx._tasks[name]["children"], "SKIPPED")
self.update_parent_task_state(name)
self.report(
f"While Task {name}: Condition not fullilled, task finished. Skip all its children."
)
# TODO refactor this for while, if and zone
# in case of an empty zone, it will finish immediately
if self.are_childen_finished(name)[0]:
self.update_while_task_state(name)
else:
task["execution_count"] += 1
self.set_task_state_info(name, "state", "RUNNING")
self.continue_workgraph()
# check the conditions of the while task
should_run = self.should_run_while_task(name)
if not should_run:
self.set_task_state_info(name, "state", "FINISHED")
self.set_tasks_state(
self.ctx._tasks[name]["children"], "SKIPPED"
)
self.update_parent_task_state(name)
self.report(
f"While Task {name}: Condition not fullilled, task finished. Skip all its children."
)
else:
task["execution_count"] += 1
self.set_task_state_info(name, "state", "RUNNING")
self.continue_workgraph()
elif task["metadata"]["node_type"].upper() in ["IF"]:
should_run = self.should_run_if_task(name)
if should_run:
self.set_task_state_info(name, "state", "RUNNING")
else:
self.set_tasks_state(task["children"], "SKIPPED")
# in case of an empty zone, it will finish immediately
if self.are_childen_finished(name)[0]:
self.update_zone_task_state(name)
else:
should_run = self.should_run_if_task(name)
if should_run:
self.set_task_state_info(name, "state", "RUNNING")
else:
self.set_tasks_state(task["children"], "SKIPPED")
self.update_zone_task_state(name)
self.continue_workgraph()
elif task["metadata"]["node_type"].upper() in ["ZONE"]:
self.set_task_state_info(name, "state", "RUNNING")
# in case of an empty zone, it will finish immediately
if self.are_childen_finished(name)[0]:
self.update_zone_task_state(name)
else:
self.set_task_state_info(name, "state", "RUNNING")
self.continue_workgraph()
elif task["metadata"]["node_type"].upper() in ["FROM_CONTEXT"]:
# get the results from the context
Expand Down
9 changes: 9 additions & 0 deletions tests/test_if.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,12 @@ def test_if_task(decorated_add, decorated_multiply, decorated_compare):
add3 = wg.add_task(decorated_add, name="add3", x=select1.outputs["result"], y=1)
wg.run()
assert add3.outputs["result"].value == 5


def test_empty_if_task():
"""Test the If task with no children."""

wg = WorkGraph("test_empty_if")
wg.add_task("If", name="if_true")
wg.run()
assert wg.state == "FINISHED"
Loading