Skip to content

Commit

Permalink
Merge pull request #165 from pyiron/root_node
Browse files Browse the repository at this point in the history
Expose root node
  • Loading branch information
liamhuber committed Jan 18, 2024
2 parents 2f41641 + a08e11b commit dadc69a
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
6 changes: 6 additions & 0 deletions pyiron_workflow/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ class Node(HasToDict, ABC, metaclass=AbstractHasPost):
owning this, if any.
ready (bool): Whether the inputs are all ready and the node is neither
already running nor already failed.
graph_root (Node): The parent-most node in this graph.
run_args (dict): **Abstract** the argmuments to use for actually running the
node. Must be specified in child classes.
running (bool): Whether the node has called :meth:`run` and has not yet
Expand Down Expand Up @@ -316,6 +317,11 @@ def parent(self, new_parent: Composite | None) -> None:
"parent"
)

@property
def graph_root(self) -> Node:
"""The parent-most node in this graph."""
return self if self.parent is None else self.parent.graph_root

@property
def readiness_report(self) -> str:
input_readiness = "\n".join(
Expand Down
28 changes: 28 additions & 0 deletions tests/unit/test_composite.py
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,34 @@ def test_de_activate_strict_connections(self):
msg="Activating should propagate to children"
)

def test_root(self):
top = AComposite("topmost")
top.middle_composite = AComposite("middle_composite")
top.middle_composite.deep_node = Composite.create.SingleValue(plus_one)
top.middle_function = Composite.create.SingleValue(plus_one)

self.assertIs(
top,
top.graph_root,
msg="The parent-most node should be its own graph_root."
)
self.assertIs(
top,
top.middle_composite.graph_root,
msg="The parent-most node should be the graph_root."
)
self.assertIs(
top,
top.middle_function.graph_root,
msg="The parent-most node should be the graph_root."
)
self.assertIs(
top,
top.middle_composite.deep_node.graph_root,
msg="The parent-most node should be the graph_root, recursively accessible "
"from all depths."
)


if __name__ == '__main__':
unittest.main()
9 changes: 9 additions & 0 deletions tests/unit/test_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,15 @@ def test_run_after_init(self):
msg="With run_after_init, the node should run right away"
)

def test_root(self):
n = ANode("n")
self.assertIs(
n,
n.graph_root,
msg="Lone nodes should be their own graph_root, as there is no parent "
"above."
)


if __name__ == '__main__':
unittest.main()

0 comments on commit dadc69a

Please sign in to comment.