Exemplo n.º 1
0
def gv_layout(nodes, edges, mode="dot"):
    G = gv.graph("root")
    s = gv.graph(G, "test")
    for i in nodes:
        sg = "%02x %s" % (i, nodes[i][0])
        n = gv.node(s, sg)
        if nodes[i][0] in gv_colors:
            gv.setv(n, "color", gv_colors[nodes[i][0]])
            gv.setv(n, "style", "filled")

    for i in edges:
        if i[0] in nodes and i[1] in nodes:
            e = gv.edge(G, "%02x %s" % (i[0], nodes[i[0]][0]),
                        "%02x %s" % (i[1], nodes[i[1]][0]))
            gv.setv(e, "dir", "none")
    gv.layout(G, mode)
    gv.render(G)
    # for debugging purposes
    gv.render(G, 'svg', 'test.svg')
    devs = {}
    fn = gv.firstnode(G)
    try:
        devs[gv.nameof(fn)] = gv.getv(fn, "pos").split(",")
    except:
        print 'Failed in gv_render'
    for i in range(len(nodes) - 1):
        fn = gv.nextnode(G, fn)
        devs[gv.nameof(fn)] = gv.getv(fn, "pos").split(",")

    return devs
Exemplo n.º 2
0
def gv_layout(nodes,edges,mode="dot"):
	G = gv.graph("root")
	s = gv.graph(G,"test")
	for i in nodes:
		sg = "%02x %s"%(i,nodes[i][0])
		n = gv.node(s,sg)
		if nodes[i][0] in gv_colors:
			gv.setv(n,"color",gv_colors[nodes[i][0]])
			gv.setv(n,"style","filled")

	for i in edges:
		if i[0] in nodes and i[1] in nodes:
			e = gv.edge(G,"%02x %s"%(i[0],nodes[i[0]][0]),"%02x %s"%(i[1],nodes[i[1]][0]))
			gv.setv(e,"dir","none")
	gv.layout(G, mode)
	gv.render(G)
# for debugging purposes
	gv.render(G,'svg','test.svg')
	devs = {}
	fn = gv.firstnode(G)
	try:
		devs[gv.nameof(fn)] = gv.getv(fn,"pos").split(",")
	except:
		print 'Failed in gv_render'
	for i in range(len(nodes)-1):
		fn = gv.nextnode(G,fn)
		devs[gv.nameof(fn)] = gv.getv(fn,"pos").split(",")

	return devs
Exemplo n.º 3
0
	def initialise_nodes(self):
		# Bake in the node attributes from 'dot' layout
		gv.layout(self.gvo, 'dot')
		gv.render(self.gvo)
		
		# iterate over node attributes to get/set node positions
		# see gv.3python.pdf for more info
		# as well as https://mailman.research.att.com/pipermail/graphviz-interest/2006q1/003182.html
		n = gv.firstnode(self.gvo)
		
		#store min and max x and y
		minx = 0
		miny = 0
		maxx = None
		maxy = None
		
		# store the node label and position as reported by Dot layout
		nodepos = {} # {<node object>:(x,y)}
		
		while gv.ok(n) : # check that the iterator returned by firstnode is ok
			label = gv.nameof(n)
			
			spos = gv.getv(n,'pos').split(',') # list of strings 
			(xpos,ypos) = [float(i) for i in spos] # convert to float
			
			node = self.dag.get_node_from_label(label)
			pos = node.get_position()
			
			if pos != None:
				# Set xpos and ypos if they are already defined in node.get_position()
				(xpos,ypos) = pos				
			
			print xpos, ypos
			# set min and max values
			if minx > xpos:
				minx = xpos
			if maxx < xpos:
				maxx = xpos
			if miny > ypos:
				miny = ypos
			if maxy < ypos:
				maxy = ypos
			
			nodepos[node] = (xpos, ypos)
			
			#change node before iteration
			n = gv.nextnode(self.gvo, n)
			
		print "min", minx, miny
		print "max", maxx, maxy
			
		# Set the position in all nodes
		for node, pos in nodepos.iteritems():			
			node.set_position(pos)
Exemplo n.º 4
0
def mark():
    ok = False
    n = gv.firstnode(gr)
    while gv.ok(n):
        if gv.getv(n, 'color') == 'green':
            nh = gv.firsthead(n)
            while gv.ok(nh):
                if gv.getv(nh, 'color') != 'green':
                    gv.setv(nh, 'color', 'green')
                    ok = True
                nh = gv.nexthead(n, nh)
        n = gv.nextnode(gr, n)
    return ok
Exemplo n.º 5
0
    def generate(self, filename):
        '''
        Displays the graph on the canvas
        Uses python-igraph fruchterman-reingold algorithm to decide about
        position of the nodes, then draw these nodes on the canvas and
        draw connections between them
        Author: Jan Vorcak <*****@*****.**>
        '''

        g = gv.readstring(self.source)
        gv.layout(g, 'dot')
        gv.render(g)

        context = CanvasContext().dictionary

        node = gv.firstnode(g)
        while node is not None:
            props = {
                    'filepath' : gv.getv(node, 'filepath'),
                    'title' : gv.getv(node, 'label'),
                    'lineno' : gv.getv(node, 'lineno'),
                    }
            pos = gv.getv(node, 'pos').split(',')
            width = gv.getv(node, 'width')
            height = gv.getv(node, 'height')
            x, y = map(int, pos)
            class_box = ClassBox(props, width, height)
            class_box.matrix.translate(x, y)
            self.view.canvas.add(class_box)
            context[(props['filepath'], props['title'])] = class_box
            node = gv.nextnode(g, node)

        edge = gv.firstedge(g)
        while edge is not None:
            props = {
                    'arrowhead' : gv.getv(edge, 'arrowhead'),
                    'arrowtail' : gv.getv(edge, 'arrowtail'),
                    }

            head = gv.headof(edge)
            tail = gv.tailof(edge)
            head_str = (gv.getv(head, 'filepath'), gv.getv(head, 'label'))
            tail_str = (gv.getv(tail, 'filepath'), gv.getv(tail, 'label'))
            context[head_str]
            context[tail_str]

            edge = gv.nextedge(g, edge)
            set_association(self.view.canvas, context[head_str], \
                    context[tail_str], props)
Exemplo n.º 6
0
def main():
    # create a new empty graph
    G = gv.digraph('G')
    # define a simple graph ( A->B )
    gv.edge(gv.node(G, 'A'), gv.node(G, 'B'))
    # compute a directed graph layout
    gv.layout(G, 'dot')
    # annotate the graph with the layout information
    gv.render(G)
    # do something with the layout
    n = gv.firstnode(G)
    while n:
        print 'node ' + gv.nameof(n) + ' is at ' + gv.getv(n, 'pos')
        e = gv.firstout(n)
        while e:
            print 'edge ' + gv.nameof(gv.tailof(e)) + '->' + gv.nameof(
                gv.headof(e)) + ' is at ' + gv.getv(e, 'pos')
            e = gv.nextout(n, e)
        n = gv.nextnode(G, n)
Exemplo n.º 7
0
def main():
    # create a new empty graph
    G = gv.digraph("G")
    # define a simple graph ( A->B )
    gv.edge(gv.node(G, "A"), gv.node(G, "B"))
    # compute a directed graph layout
    gv.layout(G, "dot")
    # annotate the graph with the layout information
    gv.render(G)
    # do something with the layout
    n = gv.firstnode(G)
    while n:
        print(f"node {gv.nameof(n)} is at {gv.getv(n, 'pos')}")
        e = gv.firstout(n)
        while e:
            print(
                f"edge {gv.nameof(gv.tailof(e))}->{gv.nameof(gv.headof(e))} is at {gv.getv(e, 'pos')}"
            )
            e = gv.nextout(n, e)
        n = gv.nextnode(G, n)
Exemplo n.º 8
0
	def _getNodesFromDAG(self):
		# Get the dotfile from the DAG
		dot = self.dag.get_dot()
		gvo = gv.readstring(dot)
		
		# Bake in the node attributes from 'dot' layout
		gv.layout(gvo, 'dot')
		gv.render(gvo)
		
		# iterate over node attributes to get/set node positions
		# see gv.3python.pdf for more info
		# as well as https://mailman.research.att.com/pipermail/graphviz-interest/2006q1/003182.html
		n = gv.firstnode(gvo)
		
				#store min and max x and y
		minx = 0
		miny = 0
		maxx = None
		maxy = None
		
		# store the node label and position as reported by Dot layout
		nodepos = {} # {<node object>:(x,y)}
		
		while gv.ok(n) : # check that the iterator returned by firstnode is ok
			label = gv.nameof(n)
			
			spos = gv.getv(n,'pos').split(',') # list of strings 
			(xpos,ypos) = [float(i) for i in spos] # convert to float
			
			node = self.dag.get_node_from_label(label)
			pos = node.get_position()
			
			if pos != None:
				# Set xpos and ypos if they are already defined in node.get_position()
				(xpos,ypos) = pos				
			
			# set min and max values
			if minx > xpos:
				minx = xpos
			if maxx < xpos:
				maxx = xpos
			if miny > ypos:
				miny = ypos
			if maxy < ypos:
				maxy = ypos
			
			nodepos[node] = (xpos, ypos)
			
			#change node before iteration
			n = gv.nextnode(gvo, n)
						
		# Set the position in all nodes and add them to the graph
		for node, pos in nodepos.iteritems():			
			node.set_position(pos)
			label = self.dag.get_label_from_node(node)
			v_node = v_Node(label)
			v_node.setPos(*pos)
			self.graphview.add(v_node)
			
		bounding = self.graphview.scene().itemsBoundingRect()
		#self.graphview.fitInView(bounding, QtCore.Qt.IgnoreAspectRatio)
		self.graphview.centerOn(bounding.center())
Exemplo n.º 9
0
def get_node_list(graph_h):
    """Generator to iterate over all nodes of a graph"""
    handle = gv.firstnode(graph_h)
    while gv.ok(handle):
        yield handle
        handle = gv.nextnode(graph_h, handle)
Exemplo n.º 10
0
#!/usr/bin/python
import sys
import gv

# create a new empty graph
G = gv.digraph('G')

# define a simple graph ( A->B )
gv.edge(gv.node(G, 'A'), gv.node(G, 'B'))

# compute a directed graph layout
gv.layout(G, 'dot')

# annotate the graph with the layout information
gv.render(G)

# do something with the layout
n = gv.firstnode(G)
while n:
    print 'node ' + gv.nameof(n) + ' is at ' + gv.getv(n, 'pos')
    e = gv.firstout(n)
    while e:
        print 'edge ' + gv.nameof(gv.tailof(e)) + '->' + gv.nameof(
            gv.headof(e)) + ' is at ' + gv.getv(e, 'pos')
        e = gv.nextout(n, e)
    n = gv.nextnode(G, n)
Exemplo n.º 11
0
def get_node_list(graph_h):
    """Generator to iterate over all nodes of a graph"""
    handle = gv.firstnode(graph_h)
    while gv.ok(handle):
        yield handle
        handle = gv.nextnode(graph_h, handle)
Exemplo n.º 12
0
#!/usr/bin/python
import sys
import gv

# create a new empty graph 
G = gv.digraph('G')

# define a simple graph ( A->B )
gv.edge(gv.node(G, 'A'),gv.node(G, 'B'))

# compute a directed graph layout
gv.layout(G, 'dot')

# annotate the graph with the layout information
gv.render(G)

# do something with the layout
n = gv.firstnode(G)
while n :
    print 'node '+gv.nameof(n)+' is at '+gv.getv(n,'pos')
    e = gv.firstout(n)
    while e :
	print 'edge '+gv.nameof(gv.tailof(e))+'->'+gv.nameof(gv.headof(e))+' is at '+gv.getv(e,'pos')
	e = gv.nextout(n,e)
    n = gv.nextnode(G,n)
Exemplo n.º 13
0
    return ok

if __name__ == "__main__":
    name = sys.argv[1]
    if name[-4:] != '.dot':
        print "wrong name", name
        exit
    name = name[:-4]
    gr = gv.read(name + '.dot')
    m = gv.findnode(gr, 'main')
    gv.setv(m, 'color', 'green')
    while mark():
        pass
    n = gv.firstnode(gr)
    while gv.ok(n):
        if gv.getv(n, 'color') != 'green':
            gv.setv(n, 'fillcolor', 'red')
            gv.setv(n, 'style', 'filled')
        in_degree = 0
        e = gv.firstin(n)
        while gv.ok(e):
            in_degree += 1
            e = gv.nextin(n, e)
        if in_degree == 1:
            gv.setv(n, 'shape', 'diamond')
        n = gv.nextnode(gr, n)
    gv.write(gr, name + '-new.dot')
    gv.layout(gr, 'dot')
    gv.render(gr)
    gv.render(gr, 'fig', name + '.fig')