-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph.py
304 lines (236 loc) · 9.81 KB
/
graph.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
import type_origin as ty
import random
class Node:
def __init__(self, outgoing, typ = ty.Untype()):
self.outgoing = outgoing
self.typ = typ
def connect(self, node):
self.outgoing.append(Edge(node))
def __str__(self):
o = '->'
for i in range(len(self.outgoing)):
o = o + f'{self.outgoing[i].dest.name}'
return o + f'{id(self)}'
def recieve_type(self, incoming_type):
# self.typ = self.typ.create_union_type(incoming_type)
self.typ = ty.Union(self.typ, incoming_type).normalization()
def send_type(self):
for i in range(len(self.outgoing)):
self.outgoing[i].dest.recieve_type(self.typ)
def get_outgoing(self):
lst = []
for i in range(len(self.outgoing)):
lst.append(self.outgoing[i].dest)
return lst
class Source(Node):
def __init__(self, val, typ):
self.val = val
super().__init__(outgoing = [], typ=typ) #Sourceの型でtypを初期化
def __str__(self):
return f'{self.val} {super().__str__()}'
class Vertex(Node):
def __init__(self, name):
self.name = name
super().__init__(outgoing = []) #こうしないとインスタンス間でNodeクラスの初期リストが共有される
def __str__(self):
return f'{self.name} {super().__str__()}'
class Box(Node):
def __init__(self, name, num_args):
self.name = name
self.args = []
for i in range(num_args):
self.args.append(Vertex(f'{i}th Argument Vertex of {name}'))
self.ret = Vertex(f'return Vertex of {name}')
def get_arg(self, n):
return self.args[n]
def connect_arg(self, n, node):
self.args[n].connect(node)
def __str__(self):
o = ''
for i in range(len(self.args)):
o = '' + f'{self.args[i]}'
return f'{self.name} {super().__str__()}'
def recieve_type_arg(self, incoming_type, n):
self.args[n].recieve_type(incoming_type)
class Reciever(Vertex):
def __init__(self, name, method):
super().__init__(name)
self.method = method
class MBox(Box):
def __init__(self, name, num_args):
super().__init__(name, num_args)
self.reciever = Reciever(f'Reciever of {name}', self)
def __str__(self):
return f'メソッド{self.name}'
def get_reciever(self):
return self.reciever
def identify_class(self, classes):
target_classes = []
def sub_identify(name, target_class):
"""ある名前のクラスがtarget_classとのそsuperクラスにあるか特定し、target_classesに加える"""
if (target_class.name == name):
target_classes.append(target_class)
elif (target_class.super_class != None):
sub_identify(name, target_class.super_class)
#全ての候補となるクラスをtargetclassesに加える
if not(isinstance(self.reciever.typ, ty.Union)) and isinstance(self.reciever.typ, ty.NewType):
for target_class in classes:
sub_identify(self.reciever.typ.name, target_class)
elif (isinstance(self.reciever.typ, ty.Union)):
# TODO: レシーバの型がUnionのとき
...
self.identify_method(target_classes)
def identify_method(self, target_classes):
"""target_classesのそれぞれのクラス内でメソッドがあるか探し、あればメソッド定義側と呼び出し側のレシーバと引数を繋ぐ"""
def exist_method(target_class):
for method in target_class.method_list:
if (method.name == self.name):
print(f'{self}を{target_class}で発見')
return True, method
return False, None
def connect_reciever_and_args(method):
self.reciever.connect(method.reciever) #メソッド定義側と呼び出し側のレシーバを繋ぐ
for i in range(len(self.args)):
self.connect_arg(i, method.param_list[i]) #メソッド定義側と呼び出し側の引数を繋ぐ
print(f'{self}からグラフを再生成しました')
for target_class in target_classes:
result, found_method = exist_method(target_class)
if result: #自身で見つかった場合
connect_reciever_and_args(found_method)
for child_class in target_class.child_classes:
result_child , found_method_child = exist_method(child_class)
if result_child:
connect_reciever_and_args(found_method_child)
else: #自身で見つからなかった場合
result_parent, found_method_parent = exist_method(target_class.super_class)
if result_parent:#自身で見つからなかったが、親で見つかった場合
connect_reciever_and_args(found_method_parent)
for child_class in target_class.child_classes:
result_child , found_method_child = exist_method(child_class)
if result_child:
connect_reciever_and_args(found_method_child)
else: #自身と親両方で見つからなかった場合
result_child = False
for child_class in target_class.child_classes:
result_child , found_method_child = exist_method(child_class)
if result_child: break
if result_child: #自身と親両方で見つからなかったが、子クラスで見つかった場合
print('found method in sub class')
...
else: #ターゲットクラスとその親子クラスでは見つからなかった場合
print(f'Not found method {self.name} in {target_class} and related Class')
class Edge:
def __init__(self, dest, typ = 'untyped'):
self.dest = dest
# self.typ = typ
def __str__(self):
return f' {self.dest}'
class Graph:
def __init__(self):
self.node_list = []
self.work_list = []
def flow(self):
self.get_source_list0()
while (self.work_list != []):
target_node = self.work_list[0]
t = target_node.get_outgoing()
typlist = []
for i in range(len(t)):
typlist.append(t[i].typ)
target_node.send_type()
self.work_list.pop(0)
for i in range(len(typlist)):
if (typlist[i] != t[i].typ):
self.work_list.append(t[i])
return self
#グラフに含まれるピュアなSourceをリストとして取得
def get_source_list0(self):
for i in range(len(self.node_list)):
if (isinstance(self.node_list[i], Source)):
self.work_list.append(self.node_list[i])
def get_source_list(self):
source_list = []
for i in range(len(self.node_list)):
if (isinstance(self.node_list[i], Source)):
source_list.append(self.node_list[i])
return source_list
class FunctionDef:
def __init__(self, name, param_list):
self.body = Graph()
self.name = name
self.param_list = []
for i in range(len(param_list)):
r = random.randint(1, 10)
self.param_list.append(Vertex('FunctionDef Aragument Vertex: ' + param_list[i]))
self.ret = Vertex(f'return Vertex of {name}')
def get_source_list(self):
source_list = []
for i in range(len(self.body.node_list)):
if (isinstance(self.body.node_list[i], Source)):
source_list.append(self.body.node_list[i])
return source_list
def connect_arg(self, n, node):
self.param_list[n].connect(node)
class MethodDef(FunctionDef):
def __init__(self, name, reciever, param_list):
super().__init__(name=name, param_list=param_list )
self.reciever = RecieverDef(f"{reciever}: Reciever of {name}'s Definition")
class RecieverDef(Vertex):
def __init__(self, name):
super().__init__(name)
class Module:
def __init__(self, name): #関数もGrobalクラスのメソッドとして扱った方が良い?
self.name = name
self.body = Graph()
self.function_list = []
self.class_list = []
self.work_list = []
def __str__(self):
return f'モジュール{self.name}'
def flow(self, trace):
self.work_list = self.get_source_list()
while (self.work_list != []):
# print(f'test{self.work_list}') #デバッグ用
target_node = self.work_list[0]
t = target_node.get_outgoing()
typlist = []
for i in range(len(t)):
typlist.append(t[i].typ)
target_node.send_type()
self.work_list.pop(0)
for i in range(len(typlist)):
if (typlist[i] != t[i].typ):
self.work_list.append(t[i])
if (isinstance(t[i], Reciever)):
#もしレシーバの型が更新されたら更新された型に基づいてクラスを特定し、メソッドへエッジを引く
t[i].method.identify_class(self.class_list)
self.work_list.extend(t[i].method.args) # 引数を全てワークリストに加える
for i in range(len(self.work_list)):
print(self.work_list[i], self.work_list[i].typ) #デバッグ用
if (len(trace) > 0):
print('tracing:',trace[0].dest, trace[0].dest.typ)
print('--------------------------')
return self
def get_source_list(self):
source_list = []
source_list.extend(self.body.get_source_list())
for i in range(len(self.function_list)):
source_list.extend(self.function_list[i].get_source_list())
for i in range(len(self.class_list)):
source_list.extend(self.class_list[i].get_source_list())
return source_list
class ClassDef(Module):
def __init__(self, name, method_list, super_class):
super().__init__(name=name)
self.method_list = method_list
self.super_class = super_class
if(super_class != None):
self.super_class.child_classes.append(self)
self.child_classes = []
def __str__(self):
return f'クラス{self.name}'
def get_source_list(self):
source_list = []
for i in range(len(self.method_list)):
source_list.extend(self.method_list[i].get_source_list())
return source_list