当前位置: 首页>>代码示例>>Python>>正文


Python Graph.add_node方法代码示例

本文整理汇总了Python中Graph.add_node方法的典型用法代码示例。如果您正苦于以下问题:Python Graph.add_node方法的具体用法?Python Graph.add_node怎么用?Python Graph.add_node使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Graph的用法示例。


在下文中一共展示了Graph.add_node方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: parse

# 需要导入模块: import Graph [as 别名]
# 或者: from Graph import add_node [as 别名]
    def parse(self, fname):
        """
        """

        dom = minidom.parse(open(fname, 'r'))
        root = dom.getElementsByTagName("graphml")[0]
        graph = root.getElementsByTagName("graph")[0]
        name = graph.getAttribute('id')

        g = Graph(name)

        # # Get attributes
        # attributes = []
        # for attr in root.getElementsByTagName("key"):
        #     attributes.append(attr)

        # Get nodes
        for node in graph.getElementsByTagName("node"):
            n = g.add_node(node.getAttribute('id'))

            for attr in node.getElementsByTagName("data"):
                if attr.firstChild:
                    n[attr.getAttribute("key")] = attr.firstChild.data
                else:
                    n[attr.getAttribute("key")] = ""

        # Get edges
        for edge in graph.getElementsByTagName("edge"):
            source = edge.getAttribute('source')
            dest = edge.getAttribute('target')
            e = g.add_edge_by_label(source, dest)

            for attr in edge.getElementsByTagName("data"):
                if attr.firstChild:
                    e[attr.getAttribute("key")] = attr.firstChild.data
                else:
                    e[attr.getAttribute("key")] = ""

        return g
开发者ID:codemaniac,项目名称:pygraphml,代码行数:41,代码来源:GraphMLParser.py

示例2: generate_cfg

# 需要导入模块: import Graph [as 别名]
# 或者: from Graph import add_node [as 别名]
    def generate_cfg(self, start_addr, ret_addr=None, start_clnum=0, end_clnum=0):
        if start_clnum == 0:
            start_clnum = self.t.get_minclnum() + 1

        if end_clnum == 0:
            end_clnum = self.t.get_maxclnum() - 1

        traces = []
        enter_call = 0
        enter_sub_call = 0

        for i in range(start_clnum, end_clnum + 1):
            pc = self.get_pc(i)
            asm = self.get_disasm(pc)
            if enter_call == 0:
                if pc == start_addr:
                    if ret_addr is None:
                        end_addr = self.get_ret_addr(i - 1)
                        print hex(end_addr)
                    else:
                        end_addr = ret_addr
                    enter_call = 1
                    trace = [(i, pc, asm)]
            else:
                if end_addr == pc:
                    print 'exit call'
                    enter_call = 0
                    traces.append(trace)
                    trace = []
                if enter_sub_call == 0:
                    trace.append((i, pc, asm))
                    if asm.startswith('call'):
                        enter_sub_call = 1
                        sub_call_ret = self.get_ret_addr(i)
                else:
                    if pc == sub_call_ret:
                        trace.append((i, pc, asm))
                        enter_sub_call = 0

        graph = Graph()

        pcs = []
        for trace in traces:
            print trace

        for trace in traces:
            exist_node = None
            exist_index = 1
            new_node = None
            for ins in trace:
                if ins[1] not in pcs:
                    pcs.append(ins[1])
                    if exist_node is None:
                        if new_node is None:
                            new_node = Node([Assemble(ins[1], ins[2])])
                            graph.add_node(new_node)
                        else:
                            new_node.add_asm(Assemble(ins[1], ins[2]))
                    else:
                        new_node = Node([Assemble(ins[1], ins[2])])
                        graph.add_node(new_node)
                        if len(exist_node.asm_seqs) == exist_index:
                            graph.add_edge(exist_node, new_node)
                        else:
                            node1, node2 = graph.split_node(exist_node, exist_index, count=exist_node.count - 1)
                            graph.add_edge(node1, new_node)
                        exist_node = None
                        exist_index = 0
                else:
                    if exist_node is None:
                        if new_node is None:
                            exist_node = graph.search_and_split(ins[1])
                            exist_node.add_count()
                            exist_index = 1
                        else:
                            node, index = graph.search_node(ins[1])
                            if index == 0:
                                graph.add_edge(new_node, node)
                                node2 = node
                            else:
                                node1, node2 = graph.split_node(node, index)
                                if node == new_node:
                                    graph.add_edge(node2, node2)
                                else:
                                    graph.add_edge(new_node, node2)
                            new_node = None
                            exist_node = node2
                            node2.add_count()
                            exist_index = 1
                    else:
                        if new_node is None:
                            if len(exist_node.asm_seqs) == exist_index:
                                node3 = graph.search_and_split(ins[1])
                                graph.add_edge(exist_node, node3)
                                exist_node = node3
                                node3.add_count()
                                exist_index = 1
                            else:
                                if exist_node.asm_seqs[exist_index].addr == ins[1]:
                                    exist_index += 1
#.........这里部分代码省略.........
开发者ID:MatrixLing,项目名称:stuff,代码行数:103,代码来源:Tracer.py

示例3: __init__

# 需要导入模块: import Graph [as 别名]
# 或者: from Graph import add_node [as 别名]
class VisibilityGraph:
    def __init__(self):
        self.graph = Graph()
        self.start_node_index = None
        self.goal_node_index = None
        self.obstacles = None
        self.shortest_path = None
        self.wall = None

    '''
    start_point: object of class Point
    goal_point: object of class Point
    obstacles: list of objects of class Obstacle
    '''
    def construct_visibility_graph(self, start_point, goal_point, obstacles, wall):
        self.obstacles = obstacles
        self.wall = wall

        # if start point or goal point is inside obstacles,
        # then we certainly can not find a collision-free path from start point to end point
        # here we set current_obstacle_index to -1 because start_point and end_point are not vertices of any obstacles
        # what if start_point or end_point is vertex of some obstacle? (answered)
        #     answer: no difference. no effect
        if start_point.is_in_obstacles(obstacles, -1) == True:
            print('error. start point is inside an obstacle')
            exit(1)
        if goal_point.is_in_obstacles(obstacles, -1) == True:
            print('error. goal point is inside an obstacle')
            exit(1)


        start_point.set_to_be_visible()
        goal_point.set_to_be_visible()

        node_index = -2 # index of nodes, auxiliary variable to help construct graph
        node_index += 1
        start_point.set_index(node_index)
        start_node = Node(start_point.get_index(), start_point.get_x_coord(), start_point.get_y_coord())
        self.start_node_index = node_index
        self.graph.add_node(start_node) # add start_node to graph
        node_index += 1
        goal_point.set_index(node_index)
        goal_node = Node(goal_point.get_index(), goal_point.get_x_coord(), goal_point.get_y_coord())
        self.goal_node_index = node_index
        self.graph.add_node(goal_node) # add goal_node to graph

        # add all visible vertices of obstacles into visibility graph
        # for a vertex of a obstacle, we should judge whether or not it inside other obstacles
        # previous: judge whether of not it is inside one obstacle (considering all obstacles)
        # previous method is unreasonable. (bug fixed)
        # add variable current_obstacle_index to keep track of the obstacle that the vertex comes from
        current_obstacle_index = 0 # the index of obstacle in obstacles (a list of object of class Obstacle)
        for obstacle in obstacles:
            for vertex in obstacle.get_all_vertices():
                node_index += 1
                vertex.set_index(node_index)
                if vertex.is_in_obstacles(obstacles, current_obstacle_index) == False:
                    vertex.set_to_be_visible()
                    vertex_node = Node(vertex.get_index(), vertex.get_x_coord(), vertex.get_y_coord())
                    self.graph.add_node(vertex_node)
            current_obstacle_index += 1

        # add all visible edges of obstacles into visibility graph
        current_obstacle_index = 0
        for obstacle in obstacles:
            for edge in obstacle.get_all_edges():
                seg_start_point =  edge.get_seg_start_point()
                seg_end_point = edge.get_seg_end_point()
                if (seg_start_point.is_visible() == True) and (seg_end_point.is_visible() == True):
                    if edge.is_intersected_with_an_obstacle(self.wall):
                        continue
                    # TODO
                    copy_obstacle_index = 0
                    copy_obstacles = []
                    for copy_obstacle in obstacles:
                        if copy_obstacle_index == current_obstacle_index:
                            copy_obstacle_index += 1
                            continue
                        copy_obstacles.append(copy_obstacle)
                        copy_obstacle_index += 1
                    if (edge.is_in_some_obstacle(copy_obstacles) == False):
                        for other_obstacle in copy_obstacles:
                            if edge.is_intersected_with_an_obstacle(other_obstacle):
                                continue
                        self.graph.add_edge(self.graph.get_node_by_index(seg_start_point.get_index()),
                                            self.graph.get_node_by_index(seg_end_point.get_index()))
            current_obstacle_index += 1

        start_to_goal_segment = Segment(start_point, goal_point)
        if start_to_goal_segment.is_intersected_with_obstacles(obstacles, -1, -1, -1, -1, self.wall) == False:
            self.graph.add_edge(self.graph.get_node_by_index(start_point.get_index()),
                                self.graph.get_node_by_index(goal_point.get_index()))

        # add all visible edges connecting start_point to vertices of obstacles into visibility graph
        obstacle_index = 0
        for obstacle in obstacles:
            vertex_index = 0
            for vertex in obstacle.get_all_vertices():
                if vertex.is_visible() == False:
                    vertex_index += 1
#.........这里部分代码省略.........
开发者ID:Xin-Yang15,项目名称:robotics_hw4,代码行数:103,代码来源:VisibilityGraph.py


注:本文中的Graph.add_node方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。