-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgraph2vec.py
executable file
·409 lines (345 loc) · 17.8 KB
/
graph2vec.py
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
import os
import json
import numpy as np
from vec2onehot import vec2onehot
"""
S, W, C nips_features: Node nips_features + Edge nips_features + Var nips_features;
Node self property + Incoming Var + Outgoing Var + Incoming Edge + Outgoing Edge
"""
dict_AC = {"NULL": 0, "LimitedAC": 1, "NoLimit": 2}
dict_NodeName = {"NULL": 0, "VAR0": 1, "VAR1": 2, "VAR2": 3, "VAR3": 4, "VAR4": 5, "VAR5": 6, "S": 7, "W0": 8,
"W1": 9, "W2": 10, "W3": 11, "W4": 12, "C0": 13, "C1": 14, "C2": 15, "C3": 16, "C4": 17}
dict_VarOpName = {"NULL": 0, "BOOL": 1, "ASSIGN": 2}
dict_EdgeOpName = {"NULL": 0, "FW": 1, "IF": 2, "GB": 3, "GN": 4, "WHILE": 5, "FOR": 6, "RE": 7, "AH": 8, "RG": 9,
"RH": 10, "IT": 11}
dict_AllOpName = {"NULL": 0, "FW": 1, "ASSIGN": 2, "BOOL": 3, "IF": 4, "GB": 5, "GN": 6, "WHILE": 7, "FOR": 8, "RE": 9,
"AH": 10, "RG": 11, "RH": 12, "IT": 13}
dict_NodeOpName = {"NULL": 0, "MSG": 1, "INNADD": 2}
dict_ConName = {"NULL": 0, "ARG1": 1, "ARG2": 2, "ARG3": 3, "CON1": 4, "CON2": 5, "CON3": 6, "CNS1": 7, "CNS2": 8,
"CNS3": 9}
node_convert = {"S": 0, "W0": 1, "C0": 2, "W1": 3, "C1": 4, "W2": 5, "C2": 6, "W3": 7, "C3": 8, "W4": 9, "C4": 10,
"VAR0": 0, "VAR1": 1, "VAR2": "VAR2", "VAR3": "VAR3", "VAR4": "VAR4", "VAR5": "VAR5"}
v2o = vec2onehot() # create the one-bot dicts
# extract the nips_features of each node from input file #
def extract_node_features(nodeFile):
nodeNum = 0
node_list = []
node_attribute_list = []
f = open(nodeFile)
lines = f.readlines()
f.close()
for line in lines:
node = list(map(str, line.split()))
verExist = False
for i in range(0, len(node_list)):
if node[1] == node_list[i]:
verExist = True
else:
continue
if verExist is False:
node_list.append(node[1])
nodeNum += 1
node_attribute_list.append(node)
return nodeNum, node_list, node_attribute_list
# elimination procedure for sub_graph Start here #
def elimination_node(node_attribute_list):
main_point = ['S', 'W0', 'W1', 'W2', 'W3', 'W4', 'C0', 'C1', 'C2', 'C3', 'C4']
extra_var_list = [] # extract var with low priority
for i in range(0, len(node_attribute_list)):
if node_attribute_list[i][1] not in main_point:
if i + 1 < len(node_attribute_list):
if node_attribute_list[i][1] == node_attribute_list[i + 1][1]:
loc1 = int(node_attribute_list[i][3]) # relative location
op1 = node_attribute_list[i][4] # operation
loc2 = int(node_attribute_list[i + 1][3])
op2 = node_attribute_list[i + 1][4]
if loc2 - loc1 == 1:
op1_index = dict_VarOpName[op1]
op2_index = dict_VarOpName[op2]
# extract node attribute based on priority
if op1_index < op2_index:
extra_var_list.append(node_attribute_list.pop(i))
else:
extra_var_list.append(node_attribute_list.pop(i + 1))
return node_attribute_list, extra_var_list
def embedding_node(node_attribute_list):
# embedding each node after elimination #
node_encode = []
var_encode = []
node_embedding = []
var_embedding = []
main_point = ['S', 'W0', 'W1', 'W2', 'W3', 'W4', 'C0', 'C1', 'C2', 'C3', 'C4']
for j in range(0, len(node_attribute_list)):
v = node_attribute_list[j][0]
if v in main_point:
vf0 = node_attribute_list[j][0]
vf1 = dict_NodeName[node_attribute_list[j][1]]
vfm1 = v2o.node2vecEmbedding(node_attribute_list[j][1])
vf2 = dict_AC[node_attribute_list[j][2]]
vfm2 = v2o.nodeAC2vecEmbedding(node_attribute_list[j][2])
result = node_attribute_list[j][3].split(",")
for call_vec in range(len(result)):
if call_vec + 1 < len(result):
tmp_vf = str(dict_NodeName[result[call_vec]]) + "," + str(dict_NodeName[result[call_vec + 1]])
tmp_vfm = np.array(list(v2o.node2vecEmbedding(result[call_vec]))) ^ np.array(
list(v2o.node2vecEmbedding(result[call_vec + 1])))
elif len(result) == 1:
tmp_vf = dict_NodeName[result[call_vec]]
tmp_vfm = v2o.node2vecEmbedding(result[call_vec])
vf3 = tmp_vf
vfm3 = tmp_vfm
vf4 = int(node_attribute_list[j][4])
vfm4 = v2o.sn2vecEmbedding(node_attribute_list[j][4])
vf5 = dict_NodeOpName[node_attribute_list[j][5]]
vfm5 = v2o.nodeOP2vecEmbedding(node_attribute_list[j][5])
nodeEmbedding = vfm1.tolist() + vfm2.tolist() + vfm3.tolist() + vfm4.tolist() + vfm5.tolist()
node_embedding.append([vf0, np.array(nodeEmbedding)])
temp = [vf1, vf2, vf3, vf4, vf5]
node_encode.append([vf0, temp])
else:
vf0 = node_attribute_list[j][0]
vf1 = dict_NodeName[node_attribute_list[j][1]]
vfm1 = v2o.node2vecEmbedding(node_attribute_list[j][1])
vf2 = dict_NodeName[node_attribute_list[j][2]]
vfm2 = v2o.node2vecEmbedding(node_attribute_list[j][2])
vf3 = int(node_attribute_list[j][3])
vfm3 = v2o.sn2vecEmbedding(node_attribute_list[j][3])
vf4 = dict_VarOpName[node_attribute_list[j][4]]
vfm4 = v2o.varOP2vecEmbedding(node_attribute_list[j][4])
vf5 = int(dict_NodeOpName['NULL'])
vfm5 = v2o.nodeOP2vecEmbedding('NULL')
varEmbedding = vfm1.tolist() + vfm2.tolist() + vfm3.tolist() + vfm4.tolist() + vfm5.tolist()
var_embedding.append([vf0, np.array(varEmbedding)])
temp = [vf1, vf2, vf3, vf4, vf5]
var_encode.append([vf0, temp])
return node_encode, var_encode, node_embedding, var_embedding
def elimination_edge(edgeFile):
# eliminate edge #
edge_list = [] # all edge
extra_edge_list = [] # eliminated edge
f = open(edgeFile)
lines = f.readlines()
f.close()
for line in lines:
edge = list(map(str, line.split()))
edge_list.append(edge)
# The ablation of multiple edge between two nodes, taking the edge with the edge_operation priority
for k in range(0, len(edge_list)):
if k + 1 < len(edge_list):
start1 = edge_list[k][0] # start node
end1 = edge_list[k][1] # end node
op1 = edge_list[k][4]
start2 = edge_list[k + 1][0]
end2 = edge_list[k + 1][1]
op2 = edge_list[k + 1][4]
if start1 == start2 and end1 == end2:
op1_index = dict_EdgeOpName[op1]
op2_index = dict_EdgeOpName[op2]
# extract edge attribute based on priority
if op1_index < op2_index:
extra_edge_list.append(edge_list.pop(k))
else:
extra_edge_list.append(edge_list.pop(k + 1))
return edge_list, extra_edge_list
def embedding_edge(edge_list):
# extract & embedding the nips_features of each edge from input file #
edge_encode = []
edge_embedding = []
for k in range(len(edge_list)):
start = edge_list[k][0] # start node
end = edge_list[k][1] # end node
a, b, c = edge_list[k][2], edge_list[k][3], edge_list[k][4] # origin info
ef1 = dict_NodeName[a]
ef2 = int(b)
ef3 = dict_EdgeOpName[c]
ef_temp = [ef1, ef2, ef3]
edge_encode.append([start, end, ef_temp])
efm1 = v2o.node2vecEmbedding(a)
efm2 = v2o.sn2vecEmbedding(b)
efm3 = v2o.edgeOP2vecEmbedding(c)
efm_temp = efm1.tolist() + efm2.tolist() + efm3.tolist()
edge_embedding.append([start, end, np.array(efm_temp)])
return edge_encode, edge_embedding
def construct_vec(edge_list, node_embedding, var_embedding, edge_embedding, edge_encode):
# Vec: Node self property + Incoming Var + Outgoing Var + Incoming Edge + Outgoing Edge
print("Start constructing node vector...")
var_in_node = []
var_in = []
var_out_node = []
var_out = []
edge_in_node = []
edge_in = []
edge_out_node = []
edge_out = []
node_vec = []
F_point = ['F']
S_point = ['S']
W_point = ['W0', 'W1', 'W2', 'W3', 'W4']
C_point = ['C0', 'C1', 'C2', 'C3', 'C4']
main_point = ['S', 'W0', 'W1', 'W2', 'W3', 'W4', 'C0', 'C1', 'C2', 'C3', 'C4']
node_embedding_dim_without_edge = 250
if len(var_embedding) > 0:
for k in range(len(edge_embedding)):
if edge_list[k][0] in F_point:
for i in range(len(var_embedding)):
if str(var_embedding[i][0]) == str(edge_embedding[k][1]):
var_out.append([edge_embedding[k][0], var_embedding[i][1]])
edge_out.append([edge_embedding[k][0], edge_embedding[k][2]])
elif edge_list[k][1] in F_point:
for i in range(len(var_embedding)):
if str(var_embedding[i][0]) == str(edge_embedding[k][0]):
var_in.append([edge_embedding[k][1], var_embedding[i][1]])
edge_in.append([edge_embedding[k][1], edge_embedding[k][2]])
if edge_list[k][0] in C_point:
for i in range(len(var_embedding)):
if str(var_embedding[i][0]) == str(edge_embedding[k][1]):
var_out.append([edge_embedding[k][0], var_embedding[i][1]])
edge_out.append([edge_embedding[k][0], edge_embedding[k][2]])
elif edge_list[k][1] in C_point:
for i in range(len(var_embedding)):
if str(var_embedding[i][0]) == str(edge_embedding[k][0]):
var_in.append([edge_embedding[k][1], var_embedding[i][1]])
edge_in.append([edge_embedding[k][1], edge_embedding[k][2]])
elif edge_list[k][0] in W_point:
for i in range(len(var_embedding)):
if str(var_embedding[i][0]) == str(edge_embedding[k][1]):
var_out.append([edge_embedding[k][0], var_embedding[i][1]])
edge_out.append([edge_embedding[k][0], edge_embedding[k][2]])
break
elif edge_list[k][1] in W_point:
for i in range(len(var_embedding)):
if str(var_embedding[i][0]) == str(edge_embedding[k][0]):
var_in.append([edge_embedding[k][1], var_embedding[i][1]])
edge_in.append([edge_embedding[k][1], edge_embedding[k][2]])
elif edge_list[k][0] in S_point:
S_OUT = []
S_OUT_Flag = 0
for i in range(len(var_embedding)):
if str(var_embedding[i][0]) == str(edge_embedding[k][1]):
S_OUT.append(var_embedding[i][1])
S_OUT_Flag = 1
if S_OUT_Flag != 1:
S_OUT.append(np.zeros(len(var_embedding[0][1]), dtype=int))
var_out.append([edge_embedding[k][0], S_OUT[0]])
edge_out.append([edge_embedding[k][0], edge_embedding[k][2]])
elif edge_list[k][1] in S_point:
for i in range(len(var_embedding)):
if str(var_embedding[i][0]) == str(edge_embedding[k][0]):
var_in.append([edge_embedding[k][1], var_embedding[i][1]])
edge_in.append([edge_embedding[k][1], edge_embedding[k][2]])
break
else:
print("Edge from node %s to node %s: edgeFeature: %s" % (
edge_embedding[k][0], edge_embedding[k][1], edge_embedding[k][2]))
else:
for k in range(len(edge_embedding)):
if edge_list[k][0] in F_point:
edge_out.append([edge_embedding[k][0], edge_embedding[k][2]])
elif edge_list[k][1] in F_point:
edge_in.append([edge_embedding[k][1], edge_embedding[k][2]])
if edge_list[k][0] in C_point:
edge_out.append([edge_embedding[k][0], edge_embedding[k][2]])
elif edge_list[k][1] in C_point:
edge_in.append([edge_embedding[k][1], edge_embedding[k][2]])
elif edge_list[k][0] in W_point:
edge_out.append([edge_embedding[k][0], edge_embedding[k][2]])
elif edge_list[k][1] in W_point:
edge_in.append([edge_embedding[k][1], edge_embedding[k][2]])
elif edge_list[k][0] in S_point:
edge_out.append([edge_embedding[k][0], edge_embedding[k][2]])
elif edge_list[k][1] in S_point:
edge_in.append([edge_embedding[k][1], edge_embedding[k][2]])
edge_vec_length = 44
var_vec_length = 61
for i in range(len(var_in)):
var_in_node.append(var_in[i][0])
for i in range(len(var_out)):
var_out_node.append(var_out[i][0])
for i in range(len(edge_in)):
edge_in_node.append(edge_in[i][0])
for i in range(len(edge_out)):
edge_out_node.append(edge_out[i][0])
for i in range(len(main_point)):
if main_point[i] not in var_in_node:
var_in.append([main_point[i], np.zeros(var_vec_length, dtype=int)])
if main_point[i] not in var_out_node:
var_out.append([main_point[i], np.zeros(var_vec_length, dtype=int)])
if main_point[i] not in edge_out_node:
edge_out.append([main_point[i], np.zeros(edge_vec_length, dtype=int)])
if main_point[i] not in edge_in_node:
edge_in.append([main_point[i], np.zeros(edge_vec_length, dtype=int)])
varIn_dict = dict(var_in)
varOut_dict = dict(var_out)
edgeIn_dict = dict(edge_in)
edgeOut_dict = dict(edge_out)
for i in range(len(node_embedding)):
vec = np.zeros(node_embedding_dim_without_edge, dtype=int)
if node_embedding[i][0] in F_point:
node_feature = node_embedding[i][1].tolist() + np.array(varIn_dict[node_embedding[i][0]]).tolist() + \
np.array(varOut_dict[node_embedding[i][0]]).tolist()
vec[0:len(np.array(node_feature))] = np.array(node_feature)
node_vec.append([node_embedding[i][0], vec])
elif node_embedding[i][0] in S_point:
node_feature = node_embedding[i][1].tolist() + np.array(varIn_dict[node_embedding[i][0]]).tolist() + \
np.array(varOut_dict[node_embedding[i][0]]).tolist()
vec[0:len(np.array(node_feature))] = np.array(node_feature)
node_vec.append([node_embedding[i][0], vec])
elif node_embedding[i][0] in W_point:
node_feature = node_embedding[i][1].tolist() + np.array(varIn_dict[node_embedding[i][0]]).tolist() + \
np.array(varOut_dict[node_embedding[i][0]]).tolist()
vec[0:len(np.array(node_feature))] = np.array(node_feature)
node_vec.append([node_embedding[i][0], vec])
elif node_embedding[i][0] in C_point:
node_feature = node_embedding[i][1].tolist() + np.array(varIn_dict[node_embedding[i][0]]).tolist() + \
np.array(varOut_dict[node_embedding[i][0]]).tolist()
vec[0:len(np.array(node_feature))] = np.array(node_feature)
node_vec.append([node_embedding[i][0], vec])
for i in range(len(node_vec)):
node_vec[i][1] = node_vec[i][1].tolist()
print("Node Vec:")
for i in range(len(node_vec)):
node_vec[i][0] = node_convert[node_vec[i][0]]
print(node_vec[i][0], node_vec[i][1])
for i in range(len(edge_embedding)):
edge_embedding[i][2] = edge_embedding[i][2].tolist()
# "S" -> 0, W0 -> 1, C0 -> 2
if len(edge_encode) == 2:
end = edge_encode[len(edge_encode) - 2][1]
start = edge_encode[len(edge_encode) - 1][0]
flag = edge_encode[len(edge_encode) - 1][1]
if end == start and ('VAR' in flag or 'MSG' in flag):
edge_encode[len(edge_encode) - 1][1] = edge_encode[len(edge_encode) - 2][0]
if len(edge_encode) > 2:
end1 = edge_encode[len(edge_encode) - 1][1]
start2 = edge_encode[len(edge_encode) - 2][0]
if end1 == start2 and ('VAR' in end1 or 'MSG' in end1):
edge_encode[len(edge_encode) - 1][1] = edge_encode[len(edge_encode) - 3][0]
for i in range(len(edge_encode)):
if i + 1 < len(edge_encode):
start1 = edge_encode[i][0]
end1 = edge_encode[i][1]
start2 = edge_encode[i + 1][0]
if end1 == start2 and ('VAR' in end1 or 'MSG' in end1):
edge_encode[i][1] = edge_encode[i + 1][1]
edge_encode[i + 1][0] = edge_encode[i][0]
elif 'W' in start1 and 'VAR' in end1:
edge_encode[i][1] = 'S'
print("Edge Vec:")
for i in range(len(edge_encode)):
edge_encode[i][0] = node_convert[edge_encode[i][0]]
edge_encode[i][1] = node_convert[edge_encode[i][1]]
print(edge_encode[i][0], edge_encode[i][1], edge_encode[i][2])
graph_edge = []
for i in range(len(edge_encode)):
graph_edge.append([edge_encode[i][0], edge_encode[i][2][2], edge_encode[i][1]])
print(graph_edge)
return node_vec, graph_edge
if __name__ == "__main__":
node = "./graph_data/node/SimpleDAO.sol"
edge = "./graph_data/edge/SimpleDAO.sol"
nodeNum, node_list, node_attribute_list = extract_node_features(node)
node_attribute_list, extra_var_list = elimination_node(node_attribute_list)
node_encode, var_encode, node_embedding, var_embedding = embedding_node(node_attribute_list)
edge_list, extra_edge_list = elimination_edge(edge)
edge_encode, edge_embedding = embedding_edge(edge_list)
node_vec, graph_edge = construct_vec(edge_list, node_embedding, var_embedding, edge_embedding, edge_encode)