-
Notifications
You must be signed in to change notification settings - Fork 5
/
process_data_ast_parallel.py
443 lines (378 loc) · 16.5 KB
/
process_data_ast_parallel.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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
import json
import sys
import numpy as np
from tqdm import tqdm
import pickle
import traceback
from copy import deepcopy
import signal
import time
import os
import javalang
from get_ast_root_action import Node, get_ast_root, ActionNode, get_ast_action
MODIFIERS = ['abstract', 'default', 'final', 'native', 'private',
'protected', 'public', 'static', 'strictfp',
'transient', 'volatile']
WASTE_TIME = json.load(open('WASTE_TIME'))
CHANGE_SINGLE = json.load(open('CHANGE_SINGLE'))
def process_bracket(tokens):
if tokens[0] == '}':
tokens.pop(0)
stack = []
for token in tokens:
if token == '{':
stack.append('{')
elif token == '}':
if stack and stack[-1] == '{':
stack.pop()
else:
stack.append('}')
l_num = stack.count('{')
r_num = stack.count('}')
tokens = ['{'] * r_num + tokens + ['}'] * l_num
return tokens
def get_ast(code, file_name):
if code in CHANGE_SINGLE[0]:
code = CHANGE_SINGLE[1][CHANGE_SINGLE[0].index(code)]
text = ' '.join(code)
text = text.replace('COMMENT', ' ')
text = text.replace('SINGLE', ' ')
text = text.replace('<nl>', ' ')
text = text.replace('<nb>', ' ')
if len(text) == 0:
return None, -1
try:
tokens_ori = list(javalang.tokenizer.tokenize(text))
except:
return None, -1
codes_ori = [x.value for x in tokens_ori]
if len(codes_ori) == 0:
return None, -1
if 'implement' in codes_ori:
codes_ori.remove('implement')
if codes_ori[-1] == 'implements':
codes_ori.remove('implements')
if len(codes_ori) == 0:
return None, -1
if len(codes_ori) >= 4 and 'class' in codes_ori and codes_ori[-2] == '<' and codes_ori[-1] != '>':
codes_ori += '>'
codes_ori = process_bracket(codes_ori)
if len(codes_ori) == 0:
return None, -1
ori_start_token = ' '.join(codes_ori)
if codes_ori[0] == 'import':
pass
elif codes_ori[0] == 'package':
pass
elif codes_ori[0] == '@':
if 'class' in codes_ori: # definition of class
pass
else: # definition of method
codes_ori = ['class', 'pad_pad_class', '{'] + codes_ori + ['}']
# gumtree can only parse class, so a padding class needs to be inserted
elif codes_ori[0] in MODIFIERS:
if 'class' in codes_ori: # definition of class
if codes_ori[-1] == '}':
pass
elif codes_ori[-1] == '{':
raise
else:
codes_ori += ['{', '}']
elif '(' in codes_ori and ')' in codes_ori and ('=' not in codes_ori or ('=' in codes_ori and codes_ori.index('(') < codes_ori.index('=') and codes_ori.index(')') < codes_ori.index('='))): # definition of method
if codes_ori[-1] == '}':
pass
elif codes_ori[-1] == '{':
raise
elif codes_ori[-1] != ';':
codes_ori += ['{', '}']
codes_ori = ['class', 'pad_pad_class', '{'] + codes_ori + ['}']
else: # definition of field
codes_ori = ['class', 'pad_pad_class', '{', '{'] + codes_ori + ['}', '}']
elif codes_ori[0] == '{':
codes_ori = ['class', 'pad_pad_class', '{'] + codes_ori + ['}']
else:
if codes_ori[0] == 'if':
if codes_ori[-1] == '}':
pass
elif codes_ori[-1] == '{':
raise
elif codes_ori[-1] == ')':
codes_ori += ['{', '}']
codes_ori = ['class', 'pad_pad_class', '{', '{'] + codes_ori + ['}', '}']
text = ' '.join(codes_ori)
start_code_pos = text.index(ori_start_token)
assert start_code_pos != -1
tokens = list(javalang.tokenizer.tokenize(text))
if [x.value for x in tokens] in WASTE_TIME:
return None, -1
open(tem_path + '/%s.java'%file_name, 'w').write(text)
root = get_ast_root(file_name, tem_path)
return root, start_code_pos
def get_edge_ast_code(root, codes, start_code_pos):
edge_ast_code = []
edge_ast = []
dmap_ast = {}
nodes = root.get_all_nodes()
ast_tokens = []
start_index = {}
pos_index = {}
dmap_node_code = {}
for node in nodes:
if node.pos < start_code_pos:
continue
if node.pos == start_code_pos and (node.typeLabel == "CompilationUnit" or node.typeLabel == "Block"):
continue
if len(node.children) == 0 and node.typeLabel != 'Block' :
name = node.label
if name not in start_index:
start_cur = -1
else:
start_cur = start_index[name]
if pos_index[name] >= node.pos:
continue
if name not in codes:
continue
if m == 70 and name == 'nextParent' and start_cur == -1:
code_no = codes.index('nextParent:', start_cur + 1)
else:
try:
code_no = codes.index(name, start_cur + 1)
except:
continue
dmap_node_code[node.ori_id] = code_no
start_index[name] = code_no
pos_index[name] = node.pos
ast_no = dmap_ast[node.father.ori_id]
edge_ast_code.append((ast_no, code_no))
else:
dmap_ast[node.ori_id] = len(ast_tokens)
ast_tokens.append(node.typeLabel)
if node.father.pos < start_code_pos:
continue
if node.father.pos == start_code_pos and (node.father.typeLabel == "CompilationUnit" or node.father.typeLabel == "Block"):
continue
edge_ast.append((dmap_ast[node.father.ori_id], dmap_ast[node.ori_id]))
verify_only = []
for node in dmap_node_code:
assert dmap_node_code[node] not in verify_only
verify_only.append(dmap_node_code[node])
return edge_ast_code, edge_ast, ast_tokens, dmap_ast, dmap_node_code
def get_edge_update(codes_old, codes_new):
edge_ast_code_old = []
edge_ast_old = []
ast_tokens_old = []
edge_ast_code_new = []
edge_ast_new = []
ast_tokens_new = []
edge_change_code_old = []
edge_change_ast_old = []
edge_change_code_new = []
edge_change_ast_new = []
change = []
global time_stamp
root_old, start_code_pos_old = get_ast(codes_old, 'old_%d'%time_stamp)
if root_old == None:
pass
else:
edge_ast_code_old, edge_ast_old, ast_tokens_old, dmap_ast_old, dmap_node_code_old = get_edge_ast_code(root_old, codes_old, start_code_pos_old)
root_new, start_code_pos_new = get_ast(codes_new, 'new_%d'%time_stamp)
if root_new == None:
pass
else:
edge_ast_code_new, edge_ast_new, ast_tokens_new, dmap_ast_new, dmap_node_code_new = get_edge_ast_code(root_new, codes_new, start_code_pos_new)
if root_old == None or root_new == None:
return edge_ast_code_old, edge_ast_old, ast_tokens_old, \
edge_ast_code_new, edge_ast_new, ast_tokens_new, \
edge_change_code_old, edge_change_ast_old, \
edge_change_code_new, edge_change_ast_new, change
all_match_new, all_delete, all_add = get_ast_action('old_%d'%time_stamp, 'new_%d'%time_stamp, root_old, root_new, tem_path)
all_nodes1 = root_old.get_all_nodes()
all_nodes2 = root_new.get_all_nodes()
map1 = {}
map2 = {}
for i, node in enumerate(all_nodes1):
map1[node.ori_id] = node.idx
assert i == node.idx
for i, node in enumerate(all_nodes2):
map2[node.ori_id] = node.idx
assert i == node.idx
change = []
edge_change_code_old = []
edge_change_code_new = []
edge_change_ast_old = []
edge_change_ast_new = []
for cur_match in all_match_new:
start_change = len(change)
kind = cur_match[0]
old_node = cur_match[1]
new_node = cur_match[2]
old_idx = old_node.idx
assert all_nodes1[map1[old_idx]].label == old_node.name
assert all_nodes1[map1[old_idx]].typeLabel == old_node.typ
new_idx = new_node.idx
assert all_nodes2[map2[new_idx]].label == new_node.name
assert all_nodes2[map2[new_idx]].typeLabel == new_node.typ
if old_idx in dmap_node_code_old:
if new_idx not in dmap_node_code_new:
continue
edge_change_code_old.append((start_change, dmap_node_code_old[old_idx]))
edge_change_code_new.append((start_change, dmap_node_code_new[new_idx]))
change.append(kind)
elif old_idx in dmap_ast_old:
if new_idx not in dmap_ast_new:
continue
edge_change_ast_old.append((start_change, dmap_ast_old[old_idx]))
edge_change_ast_new.append((start_change, dmap_ast_new[new_idx]))
change.append(kind)
for old_node in all_delete:
start_change = len(change)
old_idx = old_node.idx
assert all_nodes1[map1[old_idx]].label == old_node.name
assert all_nodes1[map1[old_idx]].typeLabel == old_node.typ
if old_idx in dmap_node_code_old:
edge_change_code_old.append((start_change, dmap_node_code_old[old_idx]))
change.append('delete')
elif old_idx in dmap_ast_old:
edge_change_ast_old.append((start_change, dmap_ast_old[old_idx]))
change.append('delete')
for cur_add in all_add:
start_change = len(change)
new_node = cur_add[0]
new_idx = new_node.idx
assert all_nodes2[map2[new_idx]].label == new_node.name
assert all_nodes2[map2[new_idx]].typeLabel == new_node.typ
if new_idx in dmap_node_code_new:
edge_change_code_new.append((start_change, dmap_node_code_new[new_idx]))
change.append('add')
elif new_idx in dmap_ast_new:
edge_change_ast_new.append((start_change, dmap_ast_new[new_idx]))
change.append('add')
if root_old:
os.system('rm %s/old_%d.java'%(tem_path, time_stamp))
os.system('rm %s/old_%d.ast'%(tem_path, time_stamp))
if root_new:
os.system('rm %s/new_%d.java'%(tem_path, time_stamp))
os.system('rm %s/new_%d.ast'%(tem_path, time_stamp))
time_stamp += 1
return edge_ast_code_old, edge_ast_old, ast_tokens_old, \
edge_ast_code_new, edge_ast_new, ast_tokens_new, \
edge_change_code_old, edge_change_ast_old, \
edge_change_code_new, edge_change_ast_new, change
def get_edge_normal(codes_old):
edge_ast_code_old = []
edge_ast_old = []
ast_tokens_old = []
global time_stamp
root_old, start_code_pos_old = get_ast(codes_old, 'old_%d'%time_stamp)
if root_old == None:
pass
else:
edge_ast_code_old, edge_ast_old, ast_tokens_old, dmap_ast_old, dmap_node_code_old = get_edge_ast_code(root_old, codes_old, start_code_pos_old)
if root_old:
os.system('rm %s/old_%d.java'%(tem_path, time_stamp))
os.system('rm %s/old_%d.ast'%(tem_path, time_stamp))
time_stamp += 1
return edge_ast_code_old, edge_ast_old, ast_tokens_old
if __name__ == '__main__':
begin = int(sys.argv[1])
end = int(sys.argv[2])
postfix = '_%d_%d'%(begin, end)
tem_path = 'TEMPORARY_FILES/TEMPORARY_FILES%s'%postfix
if not os.path.exists(tem_path):
os.makedirs(tem_path)
try:
time_stamp = 0
difftokens = json.load(open('../DataSet/difftoken.json'))
processed_tokens = pickle.load(open('processed_tokens.pkl', 'rb'))
processed_types = pickle.load(open('processed_types.pkl', 'rb'))
total_change = []
total_ast = []
total_edge_change_code = []
total_edge_change_ast = []
total_edge_ast_code = []
total_edge_ast = []
for m, lines_tokens in enumerate(tqdm(processed_tokens[begin:end])):
m += begin
all_change = []
all_ast = []
all_edge_change_code = []
all_edge_change_ast = []
all_edge_ast_code = []
all_edge_ast = []
all_token = []
types = processed_types[m]
for k, tokens in enumerate(lines_tokens):
code_old = []
code_new = []
ast_old = []
ast_new = []
change = []
edge_change_code = []
edge_change_ast = []
edge_ast_code = []
edge_ast = []
all_start_code = len(all_token)
all_start_change = len(all_change)
all_start_ast = len(all_ast)
if types[k] == 100:
edge_ast_code_old, edge_ast_old, ast_old, \
edge_ast_code_new, edge_ast_new, ast_new, \
edge_change_code_old, edge_change_ast_old, \
edge_change_code_new, edge_change_ast_new, change = get_edge_update(tokens[0], tokens[1])
for edge in edge_ast_code_old:
edge_ast_code.append((all_start_ast + edge[0], all_start_code + edge[1]))
for edge in edge_ast_old:
edge_ast.append((all_start_ast + edge[0], all_start_ast + edge[1]))
for edge in edge_change_code_old:
edge_change_code.append((all_start_change + edge[0], all_start_code + edge[1]))
for edge in edge_change_ast_old:
edge_change_ast.append((all_start_change + edge[0], all_start_ast + edge[1]))
for edge in edge_ast_code_new:
edge_ast_code.append((all_start_ast + len(ast_old) + edge[0], all_start_code + len(tokens[0]) + edge[1]))
for edge in edge_ast_new:
edge_ast.append((all_start_ast + len(ast_old) + edge[0], all_start_ast + len(ast_old) + edge[1]))
for edge in edge_change_code_new:
edge_change_code.append((all_start_change + edge[0], all_start_code + len(tokens[0]) + edge[1]))
for edge in edge_change_ast_new:
edge_change_ast.append((all_start_change + edge[0], all_start_ast + len(ast_old) + edge[1]))
code_old = tokens[0]
code_new = tokens[1]
else:
assert types[k] == -1 or types[k] == 1 or types[k] == 0
edge_ast_code_old, edge_ast_old, ast_old = get_edge_normal(tokens)
for edge in edge_ast_code_old:
edge_ast_code.append((all_start_ast + edge[0], all_start_code + edge[1]))
for edge in edge_ast_old:
edge_ast.append((all_start_ast + edge[0], all_start_ast + edge[1]))
code_old = tokens
assert len(code_old) > 0
all_token += code_old
all_token += code_new
all_ast += ast_old
all_ast += ast_new
all_change += change
all_edge_change_code += edge_change_code
all_edge_change_ast += edge_change_ast
all_edge_ast_code += edge_ast_code
all_edge_ast += edge_ast
assert all_token == difftokens[m]
total_change.append(all_change)
total_ast.append(all_ast)
total_edge_change_code.append(all_edge_change_code)
total_edge_change_ast.append(all_edge_change_ast)
total_edge_ast_code.append(all_edge_ast_code)
total_edge_ast.append(all_edge_ast)
output_path = 'DataSet_temporary/DataSet_%s'%postfix
if not os.path.exists(output_path):
os.makedirs(output_path)
json.dump(total_change, open(output_path + '/change.json', 'w'))
json.dump(total_ast, open(output_path + '/ast.json', 'w'))
json.dump(total_edge_change_code, open(output_path + '/edge_change_code.json', 'w'))
json.dump(total_edge_change_ast, open(output_path + '/edge_change_ast.json', 'w'))
json.dump(total_edge_ast_code, open(output_path + '/edge_ast_code.json', 'w'))
json.dump(total_edge_ast, open(output_path + '/edge_ast.json', 'w'))
except:
if not os.path.exists('ERROR'):
os.makedirs('ERROR')
error = traceback.format_exc()
open('ERROR/error%s'%postfix, 'w').write(error + '\n' + str(m))