def execute(self, result_label="result"):
"""
Starts from the leaf nodes, calculates their outputs and feeds them as
inputs to their parent ones. The loop stops once the root node is reached.
Optionally, you can assign a custom label to the output of the root node.
:param result_label: str (optional)
:return:
"""
# Cannote execute graphs with isles
if self.has_isles():
raise exceptions.GraphExecutionError("Cannot execute graphs with "
"isolated nodes")
# Sort post-order (leaf nodes before, root node at then end)
ordered_nodes = nx.topological_sort(self._nxgraph, reverse=True)
# Assign a label to the output of the very last node to be executed:
# the root node!
self.root_node.set_output_label(result_label)
# Output of node N is input for its parent
try:
for n in ordered_nodes:
output = n.execute()
predecessors = self._nxgraph.predecessors(n)
if not predecessors:
return output
for parent in predecessors:
parent.input(output)
except exceptions.StopGraphExecutionSignal as e:
console.info(e.message)
return None
except Exception as e:
console.error(traceback.format_exc())
raise exceptions.GraphExecutionError(e.message)
评论列表
文章目录