-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathgraphGeneration.py
More file actions
240 lines (195 loc) · 7.06 KB
/
Copy pathgraphGeneration.py
File metadata and controls
240 lines (195 loc) · 7.06 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
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
#!/usr/bin/env python3
# constants from our Java code
JAVA_TREE_NAME_SEPARATOR = "$$$"
JAVA_ID_LINE_NUMBER_OFFSET = 6 #16
JAVA_ID_DIFF_TYPE_OFFSET = 3 #8
JAVA_ID_DIFFLINE_FROM_OFFSET = 1
# export names
# DIR_SEPARATOR = "$"
DIR_SEPARATOR = "___"
COLORFUL_DELETED = '#A00000'
COLORFUL_INSERTED = '#00A000'
COLORFUL_MACRO = '#579'
# colour of a node shows diff type
DIFFTYPE_ADD_COLOR = 'green' #COLORFUL_INSERTED #
DIFFTYPE_REM_COLOR = 'red' #COLORFUL_DELETED #'#ff9129'
DIFFTYPE_NON_COLOR = '#d1d1e0' # light purple gray
# border colour of a node shows code type
CODE_TYPE_CODE_COLOR = 'black'
CODE_TYPE_OTHER_COLOR = '#3399ff'#COLORFUL_MACRO#
TYPE_BORDER_SIZE = (8.0 / 7.0)
# colour of edges
EDGE_ADD_COLOR = '#bbeb37'
EDGE_REM_COLOR = '#ff9129'
EDGE_NON_COLOR = 'black'
RELEASE_PATTERNS_CODE_PREFIX = "c"
RELEASE_PATTERNS_MACRO_PREFIX = "m"
# the same as string.find but in case the char c is not found, returns len(s) instead of -1
def findCharacterInStringGreedy(s, c):
index = s.find(c)
if index == -1:
index = len(s)
return index
def substringGraceful(s, fromIndex):
if fromIndex >= len(s):
return ""
return s[fromIndex:]
def lineNoOfNode(v):
# inverse of DiffNode::getID in our Java code
# ((1 + fromLine) << ID_LINE_NUMBER_OFFSET) + diffType.ordinal()
return (v >> JAVA_ID_LINE_NUMBER_OFFSET) - JAVA_ID_DIFFLINE_FROM_OFFSET
DIFFTYPE_ADD = "add"
DIFFTYPE_REM = "rem"
DIFFTYPE_NON = "non"
CODETYPE_CODE = "code"
CODETYPE_MACRO = "macro"
class Pattern:
def __init__(self, name, id, difftype):
self.name = name
self.id = id
self.difftype = difftype
class NodeData:
def __init__(self):
self.difftype = None
self.codetype = None
self.isroot = False
self.label = "undefined label"
_ALL_PATTERN_NAMES = [
"AddToPC", "AddWithMapping",
"RemFromPC", "RemWithMapping",
"Specialization", "Generalization", "Reconfiguration", "Refactoring", "Unchanged"
]
_ALL_PATTERN_DIFFTYPES = [
DIFFTYPE_ADD, DIFFTYPE_ADD,
DIFFTYPE_REM, DIFFTYPE_REM,
DIFFTYPE_NON, DIFFTYPE_NON, DIFFTYPE_NON, DIFFTYPE_NON, DIFFTYPE_NON
]
ALL_PATTERNS = [Pattern(name, i, _ALL_PATTERN_DIFFTYPES[i]) for i,name in enumerate(_ALL_PATTERN_NAMES)]
ADD_PATTERNS = list(filter(lambda pattern: pattern.difftype == DIFFTYPE_ADD, ALL_PATTERNS))
REM_PATTERNS = list(filter(lambda pattern: pattern.difftype == DIFFTYPE_REM, ALL_PATTERNS))
NON_PATTERNS = list(filter(lambda pattern: pattern.difftype == DIFFTYPE_NON, ALL_PATTERNS))
def getPatternThat(predicate):
matches = list(filter(predicate, ALL_PATTERNS))
if len(matches) < 1:
# raise Exception("There is no pattern with the name \"" + name + "\"!")
return None
if len(matches) > 1:
# raise Exception("There is more than one pattern with the name \"" + name + "\"!")
return None
return matches[0]
def getPatternFromName(name):
return getPatternThat(lambda pattern: pattern.name == name)
def getPatternFromId(id):
intId = int(id)
return getPatternThat(lambda pattern: pattern.id == intId)
def difftypeFromId(id):
if id == 0:
return DIFFTYPE_ADD
elif id == 1:
return DIFFTYPE_REM
elif id == 2:
return DIFFTYPE_NON
raise Exception("Cannot compute difftype from id " + id + "!")
def codetypeFromId(id):
if id == 0:
return "if"
elif id == 1:
return "endif"
elif id == 2:
return "else"
elif id == 3:
return "elif"
elif id == 4:
return "code"
elif id == 5:
return "ROOT"
raise Exception("Cannot compute difftype from id " + id + "!")
def parseNodeDefault(id, name):
nameWithoutDiffType = name[4:]
result = NodeData()
isCode = nameWithoutDiffType.startswith("CODE")
if name.startswith("NON"):
result.difftype = DIFFTYPE_NON
elif name.startswith("ADD"):
result.difftype = DIFFTYPE_ADD
elif name.startswith("REM"):
result.difftype = DIFFTYPE_REM
if isCode:
result.codetype = CODETYPE_CODE
else: # is if/else/elif
result.codetype = CODETYPE_MACRO
secondHyphenPos = findCharacterInStringGreedy(nameWithoutDiffType, '_')
codetype = nameWithoutDiffType[:secondHyphenPos]
isRoot = codetype.startswith("ROOT")
isMacro = not isRoot and not isCode
code = substringGraceful(nameWithoutDiffType, secondHyphenPos+1) # +1 to remove _ too
# print(code)
if len(code) > 0:
# remove parenthesis ""
code = code[1:len(code)-1]
# print(code)
if isMacro:
code = '#' + code
# prepend line number
code = str(lineNoOfNode(id)) + ("\n" + code if len(code) > 0 else "")
result.isroot = isRoot
result.label = "ROOT" if isRoot else code
return result
def parseNodeDebugAtomics(id, name):
nameWithoutDiffType = name[4:]
result = NodeData()
isCode = True
pattern = getPatternFromName(name)
if pattern is not None:
result.difftype = pattern.difftype
else:
isCode = False
if result.difftype is None:
if name.startswith("NON"):
result.difftype = DIFFTYPE_NON
elif name.startswith("ADD"):
result.difftype = DIFFTYPE_ADD
elif name.startswith("REM"):
result.difftype = DIFFTYPE_REM
if isCode:
result.codetype = CODETYPE_CODE
else: # is if/else/elif
result.codetype = CODETYPE_MACRO
secondHyphenPos = findCharacterInStringGreedy(nameWithoutDiffType, '_')
codetype = nameWithoutDiffType[:secondHyphenPos]
result.isroot = codetype.startswith("ROOT")
isMacro = not result.isroot and not isCode
result.label = "ROOT" if result.isroot else (codetype if isMacro else name)
return result
def parseNodeReleaseAtomics(id, name):
result = NodeData()
if name.startswith(RELEASE_PATTERNS_CODE_PREFIX):
# print("getPatternFromId(", name[len(RELEASE_PATTERNS_CODE_PREFIX):], ")")
pattern = getPatternFromId(name[len(RELEASE_PATTERNS_CODE_PREFIX):])
# print(name)
# print(name[len(RELEASE_PATTERNS_CODE_PREFIX):])
# print(pattern)
# print()
result.codetype = CODETYPE_CODE
result.difftype = pattern.difftype
result.label = pattern.name
elif name.startswith(RELEASE_PATTERNS_MACRO_PREFIX):
difftypeBegin = len(RELEASE_PATTERNS_MACRO_PREFIX)
codetypeBegin = difftypeBegin + 1
difftypeId = int(name[difftypeBegin:codetypeBegin])
codetypeId = int(name[codetypeBegin:codetypeBegin+1])
result.codetype = CODETYPE_MACRO
result.difftype = difftypeFromId(difftypeId)
result.label = codetypeFromId(codetypeId)
else:
raise Exception("Node " + name + " has unknown type. Expected prefix " + RELEASE_PATTERNS_CODE_PREFIX + " or " + RELEASE_PATTERNS_MACRO_PREFIX + " but was none.")
return result
def edgeColour(edge):
if edge.startswith("a"):
return EDGE_ADD_COLOR
elif edge.startswith("ba"):
return EDGE_NON_COLOR
elif edge.startswith("b"):
return EDGE_REM_COLOR
else:
raise Exception("Cannot parse edge label " + edge)