def list_edges(graph: pydot.Graph) -> Dict[str, str]: # Source node is stored as key, dest node as value edges = {} for edge in graph.get_edge_list(): edges[edge.get_source()] = edge.get_destination() return edges
def find_children_nodes(graph: pydot.Graph, traversed: List[str], node_name: str) -> List[str]: children = [] for edge in graph.get_edge(node_name): dest = edge.get_destination() if dest not in traversed: children.append(dest) return children
def get_nodes_and_edges(name='my_graph'): graph = Graph(cleanup(name)) edges = [] for orm_node in Node.objects.select_related(): graph.add_node(get_node(orm_node.name)) for parent in orm_node.parent.iterator(): edges.append((cleanup(parent.parent.name),cleanup(parent.child.name))) for child in orm_node.child.iterator(): edges.append((cleanup(child.parent.name),cleanup(child.child.name))) edges = list(set(edges)) # remove duplicates for edge in edges: e = Edge(edge[0],edge[1]) e.set_target('_parent') e.set_arrowhead('vee') graph.add_edge(e) return graph
def _parse_graph(graph: pydot.Graph) -> EFSM: """Build internal EFSM representation from specified DOT 'graph'.""" from .actions import Action from .efsm import EfsmBuilder nodes = [_extract(node.get_name()) for node in graph.get_nodes()] efsm = EfsmBuilder(nodes) for edge in graph.get_edge_list(): src = _extract(edge.get_source()) dst = _extract(edge.get_destination()) label = _extract(edge.get_label()) action = Action.parse(label, src, dst) action.add_to_efsm(efsm) return efsm.build()
def diff_graphs(origin_graph: pydot.Graph, updated_graph: pydot.Graph): origin_edges = list_edges(origin_graph) traversed = [] for node in updated_graph.get_nodes(): if node in traversed: continue node_name = node.get_name() children = find_children_nodes(updated_graph, traversed, node_name) to_color = [node_name] + children traversed += to_color if node_name in origin_edges: continue color_nodes(updated_graph, to_color) print(updated_graph.to_string())
def group_graph_nodes(group_attrs: Sequence[str], g: nx.DiGraph, pdot: pydot.Graph) -> None: """Groups nodes with matching attrs into single subgraph nodes """ # TODO(#53): remove duplicate edges and nodes between subgraph and graph if not group_attrs: return for g_attr, groups in get_graph_groups(group_attrs, g).items(): if not g_attr: continue for key, subgraph in groups.items(): if len(subgraph) < 2: continue log.debug( "adding subgraph for {} with {} nodes and {} edges".format( key, len(subgraph), len(subgraph.edges))) pdot.add_subgraph(to_pydot_subgraph(subgraph, 0)) for i, pdot_subgraph in enumerate(pdot.get_subgraphs()): # relabel subgraphs so they show up relabel_subgraph(pdot_subgraph, i)
def get_node_and_edges(name): graph = Graph(cleanup(name)) orm_node = Node.objects.select_related().get(name=name) other_nodes = [x for x in orm_node.parent.iterator()] other_nodes += [x for x in orm_node.child.iterator()] edges = [] node_check = [] # used to make sure we don't add the same node twice for other_node in other_nodes: if other_node.parent.name not in node_check: graph.add_node(get_node(other_node.parent.name)) node_check.append(other_node.parent.name) if other_node.child.name not in node_check: graph.add_node(get_node(other_node.child.name)) node_check.append(other_node.child.name) # edges are tuples edges.append((cleanup(other_node.parent.name),cleanup(other_node.child.name))) edges = list(set(edges)) # remove duplicates for edge in edges: e = Edge(edge[0],edge[1]) e.set_arrowhead('vee') graph.add_edge(e) return graph
def _recur_draw(graph: pydot.Graph, cache: defaultdict, node: Node, rank: int = 0): # cache[node.name] += 1 node_name = get_node_name(node, cache) # graph.add_node(pydot.Node(node_name, label=node_name, color="blue")) children: List[pydot.Node] = [] for child in node.children: cache[child.name] += 1 child_name = get_node_name(child, cache) child_label = get_node_label(child, cache) fillcolor = "turquoise" color = "red" if isinstance(child, syntree.List): fillcolor = "gray" color = "black" elif isinstance(child, syntree.IfStmt) or isinstance( child, syntree.ForStmt): fillcolor = "coral" color = "blue" elif isinstance(child, syntree.Identifier) or isinstance( child, syntree.QualifiedIdent): fillcolor = "lightpink" color = "red" elif isinstance(child, syntree.Function): fillcolor = "palegreen" color = "blue" elif isinstance(child, syntree.Literal): fillcolor = "thistle" color = "purple" elif isinstance(child, syntree.BinOp) or isinstance( child, syntree.UnaryOp): fillcolor = "lightyellow" color = "orange" elif isinstance(child, syntree.Keyword): fillcolor = "lightblue" color = "navy" elif isinstance(child, syntree.FunctionCall): fillcolor = "orange" color = "red" child_node = pydot.Node( child_name, label=child_label, group=node_name, fillcolor=fillcolor, color=color, ) graph.add_node(child_node) children.append(child_node) graph.add_edge(pydot.Edge(node_name, child_name, weight=1.5)) _recur_draw(graph, cache, child, rank + 1)
def recursive_draw(graph: pydot.Graph, node_cntr: defaultdict, node: Node, rank: int = 0): node_name = get_node_name(node, node_cntr) children: List[pydot.Node] = [] for child in node.children: node_cntr[child.name] += 1 child_name = get_node_name(child, node_cntr) child_label = get_node_label(child, node_cntr) fillcolor = "turquoise" color = "red" if isinstance(child, ast.List): fillcolor = "gray" color = "black" elif isinstance(child, ast.Foreach): fillcolor = "coral" color = "blue" elif isinstance(child, ast.Use): fillcolor = "yellow1" color = "yellow4" elif isinstance(child, ast.Decleration): fillcolor = "lightpink" color = "red" elif isinstance(child, ast.Until): fillcolor = "palegreen" color = "blue" elif isinstance(child, ast.Literal): fillcolor = "thistle" color = "purple" elif isinstance(child, ast.BinOP): fillcolor = "lightyellow" color = "orange" elif isinstance(child, ast.Print): fillcolor = "lightblue" color = "navy" child_node = pydot.Node( child_name, label=child_label, group=node_name, fillcolor=fillcolor, color=color, ) graph.add_node(child_node) children.append(child_node) graph.add_edge(pydot.Edge(node_name, child_name, weight=1.5)) recursive_draw(graph, node_cntr, child, rank + 1)
def color_nodes(graph: pydot.Graph, nodes: List[str]): for node_name in nodes: node = graph.get_node(node_name)[0] node.set_fillcolor("#5f5f5f5f") node.set_style("filled")
def create_graph(src, destination): print "module2pdf.py" print 'parsing files...' if False == os.path.exists(src): return modules = [] all_representations = [] # travel the path recursively down and create module representations for (path, dirs, files) in os.walk(src): for file_ in files: if file_.endswith('.h') or file_.endswith('.cpp') or file_.endswith('.cxx'): # path of the current file mod_path = os.path.join(path, file_) # create edges with open(mod_path) as f: module = get_module_info(f) if module: all_representations.extend(module.required) all_representations.extend(module.provided) all_representations.extend(module.recycled) modules.append(module) print "creating graph..." graph = Graph() # add nodes of representations for representation in set(all_representations): graph.add_node(Node(representation, style='filled', color='lightgrey', fontsize=11)) # add nodes of modules module_names = set([module.name for module in modules]) for name in module_names: graph.add_node(Node(name)) # add edges for module in modules: # require -> module for representation in module.required: graph.add_edge(Edge(representation, module.name)) # module -> provide for representation in module.provided: graph.add_edge(Edge(module.name, representation)) # module -> recycled for representation in module.recycled: edge = Edge(representation, module.name, color="red", dir="none") graph.add_edge(edge) # write .dot file dot_file = destination + ".dot" print 'writing dot file %s...' % dot_file g_txt = graph.to_string() #print g_txt with open(dot_file, 'w') as f: f.writelines(g_txt) # create pdf pdf_file = destination + ".pdf" print 'creating pdf file %s...' % pdf_file cmd = "dot -Tpdf %s -o %s" % (dot_file, pdf_file) os.system(cmd) print 'You can open %s now' % pdf_file print "DONE"
def strip_crate_and_package_attrs(pdot: pydot.Graph): """Remove crate and crate_package attrs from nodes, since it can break graphviz dot rendering""" for node in pdot.get_nodes(): del node.obj_dict["attributes"]["crate"] del node.obj_dict["attributes"]["crate_package"]