forked from test-v1/uob-summer-project
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgraph_visualisation.py
More file actions
150 lines (118 loc) · 4.42 KB
/
graph_visualisation.py
File metadata and controls
150 lines (118 loc) · 4.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
"""
Module with utility functions for generating visualisations of code
property graphs.
"""
import os
import subprocess
import tempfile
import numpy as np
import pandas as pd
from collections import defaultdict
from IPython.display import Image
def prolog_rule_to_dot(prolog_rule):
"""
TODO: given a prolog rule (as a string), parse it and return the
graphviz source of the code property graph it represents.
"""
# Make rule a single line without messing up the splits we use later.
prolog_rule = prolog_rule.replace("), \n\t", "), ")
prolog_rule = prolog_rule.replace("),\n\t", "), ")
prolog_rule = prolog_rule.replace(",\n\t", ",")
# Remove whitespace and . at end
prolog_rule = prolog_rule.strip().strip(".")
head, body = prolog_rule.split(" :- ")
goals = body.split(', ')
ast_edges = []
cfg_edges = []
ref_edges = []
ancestor_edges = []
runs_before_edges = []
node_properties = defaultdict(list) # {node_name: [node_properties]}
for goal in goals:
*functors, arguments = goal.strip(")").split("(")
if functors == ['ast']:
start, end = arguments.split(',')
ast_edges.append((start, end))
elif functors == ['ancestor']:
start, end = arguments.split(',')
ancestor_edges.append((start, end))
elif functors == ['cfg']:
start, end = arguments.split(',')
cfg_edges.append((start, end))
elif functors == ['runs_before']:
start, end = arguments.split(',')
runs_before_edges.append((start, end))
elif functors == ['ref']:
start, end = arguments.split(',')
ref_edges.append((start,end))
else:
[node_name] = arguments.split(',')
node_property = '◦'.join(functors)
node_properties[node_name].append(node_property)
def make_dot_edge(edge):
start, end = edge
return start + " -> " + end
cfg_dot_edgelist = '\n'.join(map(make_dot_edge, cfg_edges))
ast_dot_edgelist = '\n'.join(map(make_dot_edge, ast_edges))
ancestor_dot_edgelist = '\n'.join(map(make_dot_edge, ancestor_edges))
runs_before_dot_edgelist = '\n'.join(map(make_dot_edge, runs_before_edges))
ref_dot_edgelist = '\n'.join(map(make_dot_edge, ref_edges))
node_labels = ''
for name, properties in node_properties.items():
node_labels += name + ' [label="' + ", ".join(properties) + ' : ' + name +'"] \n'
return """
digraph g {
{ # NODE LABELS
node[shape=box]
""" + node_labels + """
}
{ # AST
edge[color=green3, constraint=true]
""" + ast_dot_edgelist + """
}
{ # ANCESTOR
edge[color=green3, constraint=true, style=dashed]
""" + ancestor_dot_edgelist + """
}
{ # CFG
edge[color=red3, constraint=false]
""" + cfg_dot_edgelist + """
}
{ # RUNS BEFORE
edge[color=red3, constraint=false, style=dashed]
""" + runs_before_dot_edgelist + """
}
{ # REF
edge[color=blue3, constraint=false]
""" + ref_dot_edgelist + """
}
}
"""
def c_source_to_dot(joern_rule):
"""
TODO: given a prolog rule (as a string), parse it and return the
graphviz source of the code property graph it represents.
"""
pass
def render_graph(filename, filetype='png'):
"""
Render the dot/graphviz source at <filename> into an image of
format <filetype>, outputting to the file <filename.filetype>.
"""
dot_process = subprocess.run(["dot", "-O", "-T" + filetype, filename])
def jupyter_display_graph(dot_source, filetype='png'):
"""
Given some dot/graphviz source code, render and display the graph
in the Jupyter notebook.
Example:
import graph_visualisation
graph_visualisation.jupyter_display_graph("digraph g {a -> b -> c -> d -> a}")
<picture of graph displayed in notebook>
"""
tmp_filename = "/tmp/jupyter_tmp_image"
tmp_file = open(tmp_filename, "w", encoding="utf-8")
tmp_file.write(dot_source)
tmp_file.close()
render_graph(tmp_filename, filetype=filetype)
image = Image(filename=tmp_filename + "." + filetype, embed=True, format=filetype)
return image