diff --git a/src/graph.py b/src/graph.py index 99c98ccc4d3017ae88863a1820007f6f972845f2..fe13c4596e42b3665bd6121c0584f9d95bea20f7 100644 --- a/src/graph.py +++ b/src/graph.py @@ -194,105 +194,14 @@ class Graph(): self.dico_wo_operation = dico - def get_graph_wo_operations_mermaid(self): - dico_nodes = {} - def fill_dico_node(dico): - for node in dico["nodes"]: - dico_nodes[node["id"]] = node['name'] - for subworkflow in dico["subworkflows"]: - fill_dico_node(dico["subworkflows"][subworkflow]) - fill_dico_node(self.dico_wo_operation) - - #txt = "```mermaid\n\t" - txt= "graph TB;\n" - - def get_id(txt): - import re - for match in re.finditer(r"object at (\w+)>", txt): - return match.group(1) + with open(f"{self.get_output_dir()}/graphs/graph_wo_operations.json", 'w') as output_file : + json.dump(self.dico_wo_operation, output_file, indent=4) - def get_graph_wo_operations_mermaid_temp(dico, txt, count): - count+=1 - for node in dico["nodes"]: - tab= count*"\t" - txt+=f"{tab}{get_id(node['id'])}[{node['name']}];\n" - - for edge in dico["edges"]: - tab= count*"\t" - txt+=f"{tab}{get_id(edge['A'])}-->{get_id(edge['B'])};\n" - for subworkflow in dico["subworkflows"]: - tab= count*"\t" - txt += f"{tab}subgraph {subworkflow}\n{tab}\tdirection TB;\n" - count+=1 - txt = get_graph_wo_operations_mermaid_temp(dico["subworkflows"][subworkflow], txt, count) - count-=1 - txt += f"{tab}end\n" - return txt - txt = get_graph_wo_operations_mermaid_temp(self.dico_wo_operation, txt, 0) - #txt += """```""" - #print(txt) - with open(f"{self.get_output_dir()}/graphs/mermaid_wo_operations.md", "w") as text_file: - text_file.write(txt) - def render_graph_wo_operations(self, filename = "graph_wo_operations", render_graphs = True): - generate_graph(self.get_output_dir()/'graphs'/filename, self.dico_wo_operation, render_graphs = render_graphs) + generate_graph(self.get_output_dir()/'graphs'/filename, self.dico_wo_operation, render_graphs = render_graphs, label_edge=False, label_node=False) - #def get_graph_wo_branch_operations(self): - # self.intia_link_dico() - # nodes_in_graph = [] - # #Function that replicates the workflow's structure wo the operations in the nodes - # def replicate_dico_wo_branch_operations(dico_struct): - # dico = {} - # dico['nodes'] = [] - # dico['edges'] = [] - # dico['subworkflows'] = {} - # for node in dico_struct["nodes"]: - # if(get_type_node(node)!="Branch Operation"): - # dico['nodes'].append(node) - # nodes_in_graph.append(node['id']) - # for sub in dico_struct['subworkflows']: - # dico['subworkflows'][sub] = replicate_dico_wo_branch_operations(dico_struct['subworkflows'][sub]) - # return dico - # - # dico = replicate_dico_wo_branch_operations(self.full_dico) - # - # #Function that takes a node and gives all the nodes in which is it connected to (simplifying by the branch operations) - # def get_nodes_linked(element, already_searched = {}): - # try: - # temp = already_searched[element] - # except: - # already_searched[element] = [] - # tab = [] - # #It's possible the node wasn't added to link_dico - # try: - # gives = self.link_dico[element] - # except: - # gives = [] - # for ele in gives: - # if(ele in nodes_in_graph): - # tab.append(ele) - # else: - # if(ele!=element and ele not in already_searched[element]): - # already_searched[element].append(ele) - # tab += get_nodes_linked(ele, already_searched) - # return tab - # - # def add_edges(dico, links_added = []): - # for node in dico['nodes']: - # edges = get_nodes_linked(node['id']) - # for B in edges: - # link = f"{node['id']} -> {B}" - # if(link not in links_added): - # dico['edges'].append({'A': node['id'], 'B': B, 'label': ''}) - # links_added.append(link) - # for sub in dico['subworkflows']: - # add_edges(dico["subworkflows"][sub], links_added) - # - # add_edges(dico) - # self.dico_wo_branch_operation = dico - def get_graph_wo_branch_operations(self): self.intia_link_dico() nodes_in_graph = [] @@ -353,6 +262,9 @@ class Graph(): add_edges(dico) self.dico_wo_branch_operation = dico + + with open(f"{self.get_output_dir()}/graphs/graph_wo_branch_operations.json", 'w') as output_file : + json.dump(self.dico_wo_branch_operation, output_file, indent=4) def render_graph_wo_branch_operations(self, filename = "graph_wo_branch_operations", render_graphs = True): diff --git a/src/outils_graph.py b/src/outils_graph.py index ca99e61f467b27c5b3904e3759d7961fdcbb99ac..776d5a974370c0d8aa9795c68f62900840870827 100644 --- a/src/outils_graph.py +++ b/src/outils_graph.py @@ -31,13 +31,60 @@ def fill_dot(dot, dico, label_node = True, label_edge = True): fill_dot(c, dico["subworkflows"][sub], label_node, label_edge) c.attr(label=sub) -def generate_graph(filename, dico, label_node = True, label_edge = True, render_graphs = True): +def generate_graph_dot(filename, dico, label_node = True, label_edge = True, render_graphs = True): dot = graphviz.Digraph(filename=filename, format='png', comment="temp") fill_dot(dot, dico, label_node, label_edge) dot.save(filename=f'{filename}.dot') if(render_graphs): dot.render(filename=f'{filename}.dot', outfile=f'{filename}.png') +def generate_graph_mermaid(filename, dico, label_node = True, label_edge = True, render_graphs = True): + txt= "graph TB;\n" + + def get_id(txt): + import re + for match in re.finditer(r"object at (\w+)>", txt): + return match.group(1) + def get_graph_wo_operations_mermaid_temp(dico, txt, count): + count+=1 + for node in dico["nodes"]: + tab= count*"\t" + if(node['name']==''): + if(label_node): + txt+=f"{tab}{get_id(node['id'])}(({node['xlabel']}));\n" + else: + txt+=f"{tab}{get_id(node['id'])}(({' '}));\n" + else: + txt+=f"{tab}{get_id(node['id'])}({node['name']});\n" + + for edge in dico["edges"]: + tab= count*"\t" + if(label_edge): + txt+=f"{tab}{get_id(edge['A'])}--{edge['label']}-->{get_id(edge['B'])};\n" + else: + txt+=f"{tab}{get_id(edge['A'])}-->{get_id(edge['B'])};\n" + for subworkflow in dico["subworkflows"]: + tab= count*"\t" + txt += f"{tab}subgraph {subworkflow}\n{tab}\tdirection TB;\n" + count+=1 + txt = get_graph_wo_operations_mermaid_temp(dico["subworkflows"][subworkflow], txt, count) + count-=1 + txt += f"{tab}end\n" + return txt + txt = get_graph_wo_operations_mermaid_temp(dico, txt, 0) + + with open(f"{filename}.md", "w") as text_file: + text_file.write(txt) + + + +def generate_graph(filename, dico, label_node = True, label_edge = True, render_graphs = True, dot = True, mermaid = True): + if(dot): + generate_graph_dot(filename, dico, label_node, label_edge, render_graphs) + if(mermaid): + generate_graph_mermaid(filename, dico, label_node, label_edge, render_graphs) + + #Function that merges to dictionnaries def merge(x, y): return { key:list(set(x.get(key,[])+y.get(key,[]))) for key in set(list(x.keys())+list(y.keys())) }