-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.py
410 lines (291 loc) · 10.7 KB
/
search.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
# Copyright (c) 2011 Julian Wintermayr
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import visualize
class Searcher:
"""
Class for several search methods.
"""
def __init__(self, connector, filename, details):
self.filename = filename
self.noDetails = details
self.maxPrefix = 0
self.lcaMode = False
self.ancestorSet = set()
self.descendantSet = set()
self.LCAset = set()
self.paths = set()
self.allLCApaths = set()
self.connector = connector
self.connection = connector[0]
self.cursor = connector[1]
def saveDotFile(self, queryName, rootID, blackSet, redSet=None):
# Create DOT-file
visualizer = visualize.Visualizer(self.connector, self.noDetails)
dotFile = visualizer.generateDotFile(blackSet, redSet)
# Print DOT-file to user defined file
if self.filename is not None:
query = open(self.filename + str(rootID) + queryName + ".dot", "w")
query.write(dotFile)
query.close()
# Print DOT-file to std-out
else:
print(dotFile)
def createAdvisorSet(self, id):
# Get all advisors
self.cursor.execute("SELECT advisor FROM advised, dissertation WHERE student=dID AND author=?", (id,))
queryList = self.cursor.fetchall()
# Store advisors in a set
advisors = set()
for row in queryList:
advisors.add(row["advisor"])
return advisors
def createStudentSet(self, id):
# Get all students
self.cursor.execute("SELECT author FROM advised, dissertation WHERE student=dID AND advisor=?", (id,))
queryList = self.cursor.fetchall()
# Store students in a set
students = set()
for row in queryList:
students.add(row["author"])
return students
def allAncestors(self, id):
# 'id' is a list containing one item
id = id[0]
self.ancestorSet.add(id)
advisors = self.createAdvisorSet(id)
# Start grabbing ancestors recursively
if len(advisors) > 0:
self.recursiveAncestors(advisors)
# Only create DOT-file if the user only searches for all ancestors and not for the LCA
if not self.lcaMode:
# If there is only the start node in the set, then there are no ancestors
if len(self.ancestorSet) < 2:
print(u"There are no ancestors!".encode('utf-8'))
# Create DOT-file
else:
self.ancestorSet.remove(id)
print(u"The {} ancestor(s) of {} is/are {}".format(len(self.ancestorSet), id, self.ancestorSet).encode('utf-8'))
self.ancestorSet.add(id)
self.saveDotFile("All-Ancestors", id, self.ancestorSet)
# If searching for the LCA, then store this set
else:
self.LCAset = self.LCAset.union(self.ancestorSet)
# Delete set!
self.ancestorSet = set()
def allDescendants(self, id):
# 'id' is a list containing one item
id = id[0]
self.descendantSet.add(id)
students = self.createStudentSet(id)
# Start grabbing descendants recursively
if len(students) > 0:
self.recursiveDescendants(students)
# Only create DOT-file if the user only searches for all descendants and not for the LCA
if not self.lcaMode:
# If there is only the start node in the set, then there are no descendants
if len(self.descendantSet) < 2:
print(u"There are no descendants!".encode('utf-8'))
# Create DOT-file
else:
self.descendantSet.remove(id)
print(u"The {} descendant(s) of {} is/are {}".format(len(self.descendantSet), id, self.descendantSet).encode('utf-8'))
self.descendantSet.add(id)
self.saveDotFile("All-Descendants", id, self.descendantSet)
# If searching for the LCA, then store this set
else:
self.LCAset = self.LCAset.union(self.descendantSet)
# Delete set!
self.descendantSet = set()
def allAncestorsDescendants(self, id):
# 'id' is a list containing one item
id = id[0]
self.ancestorSet.add(id)
self.descendantSet.add(id)
advisors = self.createAdvisorSet(id)
# Start grabbing ancestors recursively
if len(advisors) > 0:
self.recursiveAncestors(advisors)
students = self.createStudentSet(id)
# Start grabbing descendants recursively
if len(students) > 0:
self.recursiveDescendants(students)
# If there is only the start node in the set, then there are no ancestors
if len(self.ancestorSet) < 2:
print(u"There are no ancestors!".encode('utf-8'))
else:
self.ancestorSet.remove(id)
print(u"The {} ancestor(s) of {} is/are {}".format(len(self.ancestorSet), id, self.ancestorSet).encode('utf-8'))
self.ancestorSet.add(id)
print(u"".encode('utf-8'))
# If there is only the start node in the set, then there are no descendants
if len(self.descendantSet) < 2:
print(u"There are no descendants!".encode('utf-8'))
else:
self.descendantSet.remove(id)
print(u"The {} descendant(s) of {} is/are {}".format(len(self.descendantSet), id, self.descendantSet).encode('utf-8'))
self.descendantSet.add(id)
# Create DOT-file
if len(self.ancestorSet) > 1 or len(self.descendantSet) > 1:
AncestorDescendantSet = self.ancestorSet.union(self.descendantSet)
self.saveDotFile("All-Ancestors-Descendants", id, AncestorDescendantSet)
# Delete sets!
self.ancestorSet = set()
self.descendantSet = set()
def recursiveAncestors(self, advisors):
for advisor in advisors:
self.ancestorSet.add(advisor)
nextAdvisors = self.createAdvisorSet(advisor)
if len(nextAdvisors) > 0:
self.recursiveAncestors(nextAdvisors.difference(self.ancestorSet))
def recursiveDescendants(self, students):
for student in students:
self.descendantSet.add(student)
nextStudents = self.createStudentSet(student)
if len(nextStudents) > 0:
self.recursiveDescendants(nextStudents.difference(self.descendantSet))
# Used by 'update.py'
def numberOfDescendants(self, students):
self.recursiveDescendants(students)
return len(self.descendantSet)
def lca(self, ids):
self.lcaMode = True
id1 = [0, ids[0]]
for i in range(len(ids)-1):
if len(id1) < 1:
break
else:
id1 = self.recursiveLCA(id1, ids[i+1])
if len(id1) < 1:
print(u"There is no LCA!".encode('utf-8'))
else:
for id in ids:
self.allAncestors([id])
self.allDescendants([id])
lca = id1
redLCAset = set()
for singleLCA in lca:
redLCAset.add(singleLCA)
for path in self.allLCApaths:
splitPath = path.split(str(singleLCA) + '.')
if len(splitPath) > 1 and not splitPath[1] == "":
redIDlist = splitPath[1].split('.')
for redID in redIDlist:
try:
intID = int(redID)
except ValueError:
continue
redLCAset.add(intID)
self.cursor.execute("SELECT name FROM person WHERE pID=?", (singleLCA,))
lcaName = self.cursor.fetchone()
print(u"The LCA with {} common ancestors is {}: {}".format(self.maxPrefix, singleLCA, lcaName["name"]).encode('utf-8'))
blackLCAset = self.LCAset.difference(redLCAset)
rootIDs = "-"
for id in ids:
rootIDs += str(id) + "-"
self.saveDotFile("LCA", rootIDs, blackLCAset, redLCAset)
self.lcaMode = False
def recursiveLCA(self, idSet, id2):
path2 = self.generatePathOf(id2)
lcaPath = set()
self.maxPrefix = 0
lca = []
for id1 in idSet:
if id1 == 0:
continue
path1 = self.generatePathOf(id1)
for row1 in path1:
splitPath1 = row1.split('.')
for row2 in path2:
prefix = 0
splitPath2 = row2.split('.')
if len(splitPath1) >= len(splitPath2):
longPathIter = iter(splitPath1)
shortPathIter = iter(splitPath2)
else:
longPathIter = iter(splitPath2)
shortPathIter = iter(splitPath1)
for singlePathID1 in shortPathIter:
singlePathID2 = next(longPathIter)
prefix += 1
if singlePathID1 == singlePathID2:
if prefix == self.maxPrefix:
if int(singlePathID1) not in lca:
lca.append(int(singlePathID1))
lcaPath.add(row1)
lcaPath.add(row2)
if prefix > self.maxPrefix:
lca = []
lcaPath = set()
lca.append(int(singlePathID1))
lcaPath.add(row1)
lcaPath.add(row2)
self.maxPrefix = prefix
else:
break
for row in lcaPath:
self.allLCApaths.add(row)
return lca
def generatePathOf(self, id):
print(u"Generating paths of #{}".format(id).encode('utf-8'))
nextAdvisors = self.createAdvisorSet(id)
if len(nextAdvisors) > 0:
self.recursiveAncestorsPath(nextAdvisors, str(id))
else:
self.paths.add(str(id) + ".")
print(str(id) + ".")
allPaths = self.paths
self.paths = set()
return allPaths
def recursiveAncestorsPath(self, advisors, treeString):
"""
Create all ancestors paths for the requested ID.
"""
for advisor in advisors:
nextAdvisors = self.createAdvisorSet(advisor)
treeString = str(advisor) + "." + treeString
if len(nextAdvisors) > 0:
self.recursiveAncestorsPath(nextAdvisors, treeString)
# We have to delete the last node from the string because we are following another path now
treeString = treeString.split(".", 1)[1]
else:
# If we reach the highest ancestor, then store this string!
self.paths.add(treeString + ".")
print(treeString + ".")
# We have to delete the last node from the string because we are following another path now
treeString = treeString.split(".", 1)[1]
# Function not used yet!
def recursiveDescendantsPath(self, students, treeString):
"""
Create all descendants paths for the requested ID.
"""
for student in students:
nextStudents = self.createStudentSet(student)
treeString = treeString + "." + str(student)
if len(nextStudents) > 0:
self.recursiveDescendantsPath(nextStudents, treeString)
# We have to delete the last node from the string because we are following another path now
treeString = treeString.rsplit(".", 1)[0]
else:
# If we reach the highest ancestor, then store this string!
self.paths.add(treeString + ".")
print(treeString + ".")
# We have to delete the last node from the string because we are following another path now
treeString = treeString.rsplit(".", 1)[0]