-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathutilities.js
More file actions
283 lines (252 loc) · 9.36 KB
/
utilities.js
File metadata and controls
283 lines (252 loc) · 9.36 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
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
/**
* Created by saharmehrpour on 9/8/17.
*/
import {defaultXML, webSocketSendMessage} from "./coreConstants";
class Utilities {
static BREAK_LINE = 9000;
/**
* send the message to the server
* @param ws web socket
* @param command
* @param data
*/
static sendToServer(ws, command, data) {
let messageJson = {command: command};
let secondMessageJson = null;
if (ws) {
switch (command) {
case webSocketSendMessage.modified_rule_msg:
messageJson.data = {
ruleID: data.index,
ruleInfo: data
};
break;
case webSocketSendMessage.modified_tag_msg:
messageJson.data = {
tagID: data.ID,
tagInfo: data
};
break;
case webSocketSendMessage.send_llm_snippet_msg:
console.log("In command");
console.log(data);
messageJson.data={
code:data.suggestedSnippet,
explanation:data.snippetExplanation
}
break;
case webSocketSendMessage.send_info_for_edit_fix:
messageJson.data={
filePathOfSuggestedFix:data.data.fileToChange,
filePathOfViolation:data.data.filePath,
modifiedFileContent:data.data.modifiedFileContent
}
break;
case webSocketSendMessage.llm_modified_file_content:
if (!data.llmModifiedFileContent) {
console.warn('No LLM modified file content to send.');
return;
}
messageJson.data = {
filePath: data.llmModifiedFileContent.data.filePath,
explanation: data.llmModifiedFileContent.data.explanation,
fileToChange: data.llmModifiedFileContent.data.fileToChange,
modifiedFileContent: data.llmModifiedFileContent.data.modifiedFileContent,
originalFileContent: data.originalFileContent || data.llmModifiedFileContent.data.originalFileContent || '',
}
break;
case webSocketSendMessage.snippet_xml_msg:
messageJson.data = {
fileName: data.fileName,
xml: data.xml
};
secondMessageJson = {
command: webSocketSendMessage.converted_java_snippet_msg,
data: {
fileName: data.fileName,
convertedJava: Utilities.removeSrcmlAnnotations(data.xml)
}
};
break;
case webSocketSendMessage.code_to_xml_msg:
messageJson.data = {
codeText: data.codeText,
messageID: data.messageID
};
break;
case webSocketSendMessage.new_rule_msg:
messageJson.data = {
ruleID: data.index.toString(),
ruleInfo: data
};
break;
case webSocketSendMessage.new_tag_msg:
messageJson.data = {
tagID: data.ID,
tagInfo: data
};
break;
/* mining rules */
case webSocketSendMessage.learn_design_rules_databases_msg:
case webSocketSendMessage.learn_design_rules_features_msg:
case webSocketSendMessage.learn_design_rules_helper_files_msg:
if (data.content.length > this.BREAK_LINE) {
this.sendChunkedData(messageJson, data.content.slice(0), data.fileName, ws);
return;
}
messageJson.data = [[data.fileName, data.content]];
break;
case webSocketSendMessage.mine_design_rules_msg:
messageJson.data = {
parameters : data.parameters, // should be an array
algorithm: data.algorithm // selectedAlgorithm from allAlgorithms
};
break;
case webSocketSendMessage.open_file_msg:
messageJson.command = webSocketSendMessage.snippet_xml_msg; // there is no separate command in the server
messageJson.data = {
fileName: data,
xml: defaultXML
};
break;
default:
break;
}
console.log("Sending Message: ", messageJson);
ws.send(JSON.stringify(messageJson));
if (secondMessageJson){
console.log("Sending Second Message: ",secondMessageJson);
ws.send(JSON.stringify(secondMessageJson))
}
}
}
/**
* websocket cannot send data larger than 64KB. Here the function
* break the data to smaller pieces for transfer
* @param messageJson data: [[fileName, initData]]
* @param initData
* @param fileName
* @param ws
*/
static sendChunkedData (messageJson, initData, fileName, ws) {
messageJson.command += "_APPEND";
let start = 0; let cnt = 0;
while (start < initData.length) {
messageJson.data = [[fileName, initData.substring(start, Math.min(start + this.BREAK_LINE, initData.length))]];
messageJson.part = cnt; // only for debugging
ws.send(JSON.stringify(messageJson));
start += this.BREAK_LINE;
cnt ++;
}
}
/**
* check whether two arrays are equals
* @param array1
* @param array2
* @returns {boolean}
*/
static ResultArraysEqual(array1, array2) {
let arr1 = array1.slice(0);
let arr2 = array2.slice(0);
if (arr1.length !== arr2.length)
return false;
for (let i = arr2.length; i--;) {
let item = arr1.filter((d) => d.name === arr2[i].name);
if (item.length === 0)
return false;
// only remove one occurrence
let removed = false;
arr1 = arr1.filter((d) => {
if (!removed && d.name === arr2[i].name) {
removed = !removed;
return false;
}
return true;
});
}
return true;
}
/**
* deep copy of an xml data
* @param xml
* @returns {Document}
*/
static cloneXML(xml) {
let newDocument = xml.implementation.createDocument(
xml.namespaceURI, //namespace to use
"", //name of the root element (or for empty document)
null //doctype (null for XML)
);
let newNode = newDocument.importNode(
xml.documentElement, //node to import
true //clone its descendants
);
newDocument.appendChild(newNode);
return newDocument;
}
/**
* move an array element from old_index to new_index
* @param arr array of elements
* @param old_index origin
* @param new_index destination
* @returns {*}
*/
static arrayMove(arr, old_index, new_index) {
while (old_index < 0) {
old_index += arr.length;
}
while (new_index < 0) {
new_index += arr.length;
}
if (new_index >= arr.length) {
let k = new_index - arr.length + 1;
while (k--) {
arr.push(undefined);
}
}
arr.splice(new_index, 0, arr.splice(old_index, 1)[0]);
return arr; // for testing purposes
};
/**
* a method to parse strings
* @param stringJson
* @param dataName
* @param defaultValue
* @return {any}
*/
static parseJson(stringJson, dataName, defaultValue) {
try {
return JSON.parse(stringJson);
} catch (e) {
console.log(`Failed to parse ${dataName}`);
return defaultValue;
}
}
/**
* find all indices of an element in an array
* @param arr
* @param element
* @return {*[]}
*/
static findAllIndices(arr, element) {
let indices = [];
for (let i = 0; i < arr.length; i++) {
if (arr[i] === element) {
indices.push(i);
}
}
return indices;
}
static removeSrcmlAnnotations(xmlString) {
// removes srcml tags
xmlString = xmlString.replace(/<[^>]*>/g, "");
// replaces < with <
xmlString = xmlString.replace(/</g, "<");
// replaces > with >
xmlString = xmlString.replace(/>/g, ">");
// replaces & with &
xmlString = xmlString.replace(/&/g, "&");
return xmlString;
}
}
export default Utilities;