graph.py 文件源码

python
阅读 30 收藏 0 点赞 0 评论 0

项目:robograph 作者: csparpa 项目源码 文件源码
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)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号