-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathOutput.java
412 lines (355 loc) · 16 KB
/
Output.java
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
import java.nio.file.Paths;
import java.nio.file.Files;
import java.nio.charset.Charset;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.FileNotFoundException;
import java.io.UnsupportedEncodingException;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.List;
import java.util.ArrayList;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.io.ObjectInputStream;
import java.io.Serializable;
import java.util.Set;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import org.apache.commons.math3.stat.descriptive.rank.Median;
import org.apache.commons.math3.stat.descriptive.DescriptiveStatistics;
import java.util.Scanner;
public class Output {
//ArrayList<MatchGroup> matchGroupList = new ArrayList<MatchGroup>();
int algorithmMode;
boolean enableRepetitive;
boolean enableOneMethod;
int matchMode;
String outputDir;
int minNumberStatements;
boolean debug;
boolean enablePercentageMatching;
public Output (int alogrithm,
boolean enableRepetitiveIn,
boolean enableOneMethodIn,
int matchModeIn,
String outputDirIn,
int minNumberStatementsIn,
boolean debugIn,
boolean enablePercentageMatchingIn) {
enableRepetitive = enableRepetitiveIn;
enableOneMethod = enableOneMethodIn;
algorithmMode = alogrithm;
matchMode = matchModeIn;
outputDir = outputDirIn;
minNumberStatements = minNumberStatementsIn;
debug = debugIn;
enablePercentageMatching = enablePercentageMatchingIn;
}
HashMap<Integer,MatchGroup> matchGroupList = new HashMap<Integer,MatchGroup>();
// file coverage, start-end line
// statement hash number, start-end
// method line coverage, start-end line
public void addClone(String file1, int lineStart1, int lineEnd1,
String file2, int lineStart2, int lineEnd2, int length,
ArrayList<Statement> statementRaw1, int statementStart1, int statementEnd1,
ArrayList<Statement> statementRaw2, int statementStart2, int statementEnd2,
int totalHashValue) {
if (algorithmMode == 0) {
// check for hashing error during the group hash process
// only for non-gapped clones
boolean status = Analyze.hasHashError(
statementRaw1.subList(statementStart1, statementEnd1),
statementRaw2.subList(statementStart2, statementEnd2));
if (status == true) {
return;
}
// check for repetitive statements
if (enableRepetitive) {
if (Analyze.isRepetitive(statementRaw1.subList(statementStart1, statementEnd1)) == true) {
return;
}
}
// require at least one method call
if (enableOneMethod) {
if (Analyze.checkNumMethods(statementRaw1.subList(statementStart1, statementEnd1), 1) == false) {
return;
}
}
// check for valid scope
//if (Analyze.hasValidScope(statementRaw1.subList(statementStart1, statementEnd1)) == false) {
// return;
//}
} else {
// require at least one method call
if (minNumberStatements > 0) {
if (Analyze.checkNumMethods(statementRaw1.subList(statementStart1, statementEnd1), minNumberStatements) == false) {
if (debug) {
System.out.println("Removed from lack of method call");
System.out.println(file1);
System.out.println(file2);
System.out.println();
}
return;
}
if (Analyze.checkNumMethods(statementRaw2.subList(statementStart2, statementEnd2), minNumberStatements) == false) {
if (debug) {
System.out.println("Removed from lack of method call");
System.out.println(file1);
System.out.println(file2);
System.out.println();
}
return;
}
if (debug) {
System.out.println("Satisfied minimum method call threashold: " + minNumberStatements);
}
}
// ensure majority of the file is matched (only for SO snippets)
boolean isJavaCode = Utilities.checkIsJava(file2);
if (enablePercentageMatching && isJavaCode == false) {
int size = -2;
try {
BufferedReader br = new BufferedReader(new FileReader(file2));
String line = br.readLine();
while (line != null) {
line = br.readLine();
size = size + 1;
}
br.close();
} catch (Exception e) {
System.out.println("Error on % matching");
System.exit(0);
}
int sizeMatched = lineEnd2 - lineStart2 + 1;
float percentage = ((float)sizeMatched * 100 / (float)size);
//System.out.println(percentage + "%");
//System.out.println(file1);
//System.out.println(file2);
//System.out.println(lineEnd2 + " " + lineStart2 + "= " + sizeMatched + " X " + size);
if (percentage < 60) {
if (debug) {
System.out.println("Removed from lack of % matching: " + String.valueOf(percentage));
System.out.println(file1);
System.out.println(file2);
System.out.println();
}
return;
}
}
}
boolean added = false;
if (matchMode == 0) {
// between comparison
MatchGroup matchGroup = matchGroupList.get(totalHashValue);
if (matchGroup != null) {
boolean status1 = matchGroup.checkMatchExist(file1, lineStart1, lineEnd1, 0);
boolean status2 = matchGroup.checkMatchExist(file2, lineStart2, lineEnd2, 1);
if (status1 == true && status2 == false) {
// add as a clone
matchGroup.addMatch(1, file2, lineStart2, lineEnd2,
statementRaw2, statementStart2, statementEnd2, totalHashValue);
added = true;
} else if (status1 == false && status2 == true) {
// add as a master
matchGroup.addMatch(0, file1, lineStart1, lineEnd1,
statementRaw1, statementStart1, statementEnd1, totalHashValue);
added = true;
} else if (status1 == true && status2 == true) {
added = true;
}
} else {
// status 1 and 2 are false
if (added == false) {
MatchGroup newGroup = new MatchGroup(length);
newGroup.addMatch(0, file1, lineStart1, lineEnd1,
statementRaw1, statementStart1, statementEnd1, totalHashValue);
newGroup.addMatch(1, file2, lineStart2, lineEnd2,
statementRaw2, statementStart2, statementEnd2, totalHashValue);
matchGroupList.put(totalHashValue, newGroup);
}
}
} else {
// full mesh
MatchGroup matchGroup = matchGroupList.get(totalHashValue);
if (matchGroup != null) {
boolean status1 = matchGroup.checkMatchExist(file1, lineStart1, lineEnd1, 2);
boolean status2 = matchGroup.checkMatchExist(file2, lineStart2, lineEnd2, 2);
if (status1 == true && status2 == false) {
// add as a clone
matchGroup.addMatch(1, file2, lineStart2, lineEnd2,
statementRaw2, statementStart2, statementEnd2, totalHashValue);
added = true;
} else if (status1 == false && status2 == true) {
// add as a clone
matchGroup.addMatch(1, file1, lineStart1, lineEnd1,
statementRaw1, statementStart1, statementEnd1, totalHashValue);
added = true;
} else if (status1 == true && status2 == true) {
added = true;
}
} else {
// status 1 and 2 are false
if (added == false) {
MatchGroup newGroup = new MatchGroup(length);
newGroup.addMatch(0, file1, lineStart1, lineEnd1,
statementRaw1, statementStart1, statementEnd1, totalHashValue);
newGroup.addMatch(1, file2, lineStart2, lineEnd2,
statementRaw2, statementStart2, statementEnd2, totalHashValue);
matchGroupList.put(totalHashValue, newGroup);
}
}
}
}
public void saveResults(String path) {
try {
// Serialize file and write to file
FileOutputStream fout = new FileOutputStream(path);
ObjectOutputStream oos = new ObjectOutputStream(fout);
oos.writeObject(matchGroupList);
fout.close();
oos.close();
} catch (Exception e) {
System.out.println("Error while writing results\n" + e.getStackTrace());
System.exit(0);
}
}
public void loadResults(String path) {
try {
// Load serialized file
FileInputStream fin = new FileInputStream(path);
ObjectInputStream ois = new ObjectInputStream(fin);
matchGroupList = (HashMap<Integer,MatchGroup>) ois.readObject();
fin.close();
ois.close();
} catch (Exception e) {
System.out.println("Error while loading results\n" + e.getStackTrace());
System.exit(0);
}
}
public void processOutputTerms (FrequencyMap fMap) {
for (Integer key : matchGroupList.keySet()) {
MatchGroup thisMatchGroup = matchGroupList.get(key);
HashSet<String> listTerms = thisMatchGroup.dumpTerms();
fMap.addInstance(listTerms);
}
}
public void printResults(boolean saveEmpty,
int similarityRange,
boolean enableSimilarity,
int matchMode,
boolean debug,
ArrayList<String> banListSim) {
if (debug) {
System.out.println("Inside printResults()");
}
DescriptiveStatistics statsInternalClones = new DescriptiveStatistics();
DescriptiveStatistics statsExternalClones = new DescriptiveStatistics();
int sumInternalClones = 0;
int sumExternalClones = 0;
int sumExternalClonesComment = 0;
int numMatchesWithComment = 0;
int matchIndex = 0;
int outputIndex = 0;
int emptyIndex = 0;
PrintWriter writerMasterHasComment = null;
PrintWriter writerMasterNoComment = null;
try {
writerMasterHasComment = new PrintWriter(outputDir + "allComments.txt", "UTF-8");
writerMasterNoComment = new PrintWriter(outputDir + "noComments.txt", "UTF-8");
System.out.println("\n\n");
for (Integer key : matchGroupList.keySet()) {
if (debug) {
System.out.println("#####");
System.out.println("Printing one result");
}
MatchGroup thisMatchGroup = matchGroupList.get(key);
thisMatchGroup.mapCode2Comment();
thisMatchGroup.pruneComments(similarityRange, enableSimilarity, debug, banListSim);
thisMatchGroup.pruneDuplicateComments(debug);
boolean hasComment = thisMatchGroup.hasComment();
PrintWriter writerMaster;
if (hasComment) {
writerMaster = writerMasterHasComment;
} else {
writerMaster = writerMasterNoComment;
}
// no comment and remove empty is true
// simply export the ones without comment to a different filename
try {
String outputFileName = "";
if (hasComment == false) {
System.out.println("No comments");
outputFileName = outputDir + "empty-" + Integer.toString(emptyIndex) + ".txt";
emptyIndex = emptyIndex + 1;
} else {
System.out.println("Has comment(s)");
outputFileName = outputDir + "full-" + Integer.toString(outputIndex) + ".txt";
numMatchesWithComment++;
outputIndex = outputIndex + 1;
}
if (debug) {
System.out.println(outputFileName);
}
PrintWriter writerComment = new PrintWriter(outputFileName, "UTF-8");
String groupStr = "Match Group " + matchIndex + " of size " +
thisMatchGroup.getMasterSize() + "+" + thisMatchGroup.getCloneSize();
System.out.println(groupStr);
writerComment.println(groupStr);
writerMaster.println(groupStr);
thisMatchGroup.printAllMappings(saveEmpty, matchMode, 1, outputDir, writerComment, writerMaster);
// make sure similarity terms are already been gathered
// only can rank comment if there is a comment
if (enableSimilarity && hasComment == true) {
thisMatchGroup.printRankedComments(writerComment, writerMaster);
}
writerComment.close();
} catch (Exception e) {
System.out.println("Error in Output.java case 1");
System.out.println(e.getMessage());
e.printStackTrace();
}
writerMaster.println();
matchIndex++;
}
System.out.println("#####\n\n\n");
System.out.println(numMatchesWithComment + " comment groups has a comment");
} catch (FileNotFoundException e) {
System.out.println("Error while creating an output file");
} catch (UnsupportedEncodingException e) {
System.out.println("UnsupportedEncodingException error in print writer");
} finally {
if (writerMasterHasComment != null) {
writerMasterHasComment.close();
}
if (writerMasterNoComment != null) {
writerMasterNoComment.close();
}
}
}
public void search (String outputDir) {
String userInput;
while (true) {
System.out.print("Enter a list of terms seperated by space: ");
Scanner in = new Scanner(System.in);
userInput = in.nextLine();
if (userInput.equals("")) {
continue;
}
// break down the terms by camel case
String[] stringList = userInput.split("\\s");
HashSet<String> setSplittedString = new HashSet<String>();
for (String str : stringList) {
Set<String> splitSet = Utilities.splitCamelCaseSet(str);
setSplittedString.addAll(splitSet);
}
// search for a clone that contains all the terms
for (Integer key : matchGroupList.keySet()) {
MatchGroup thisMatchGroup = matchGroupList.get(key);
thisMatchGroup.findClones(setSplittedString, outputDir);
}
}
}
}