-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathStructure.java
389 lines (343 loc) · 13.6 KB
/
Structure.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
package fr.inria.prophet4j.utility;
import com.google.gson.Gson;
import fr.inria.prophet4j.feature.Feature;
import fr.inria.prophet4j.feature.FeatureCross;
import fr.inria.prophet4j.feature.S4R.S4RFeature;
import fr.inria.prophet4j.feature.S4RO.S4ROFeature;
import fr.inria.prophet4j.feature.enhanced.EnhancedFeature;
import fr.inria.prophet4j.feature.extended.ExtendedFeature;
import fr.inria.prophet4j.feature.original.OriginalFeature;
import fr.inria.prophet4j.utility.Option.FeatureOption;
import org.apache.commons.io.FileUtils;
import spoon.reflect.declaration.CtElement;
import java.io.*;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
public interface Structure {
class Sample { // namely TrainingCase
private String filePath;
private List<FeatureMatrix> featureMatrices;
public Sample(String filePath) {
this.filePath = filePath;
this.featureMatrices = new ArrayList<>();
}
public List<FeatureMatrix> getFeatureMatrices() {
return featureMatrices;
}
public void loadFeatureMatrices() {
try {
FileInputStream fis = new FileInputStream(filePath);
ObjectInputStream ois = new ObjectInputStream(fis);
featureMatrices = (List<FeatureMatrix>) ois.readObject();
/*
// if we need to try on merged feature-vector
// however it usually not performances better
List<FeatureMatrix> tmpFeatureMatrices = new ArrayList<>();
for (FeatureMatrix featureMatrix: featureMatrices) {
FeatureVector tmpFeatureVector = new FeatureVector();
for (FeatureVector featureVector: featureMatrix.featureVectors) {
tmpFeatureVector.merge(featureVector);
}
List<FeatureVector> tmpFeatureVectors = new ArrayList<>();
tmpFeatureVectors.add(tmpFeatureVector);
tmpFeatureMatrices.add(new FeatureMatrix(featureMatrix.marked, featureMatrix.fileKey, tmpFeatureVectors));
}
featureMatrices = tmpFeatureMatrices;
*/
ois.close();
fis.close();
} catch (ClassNotFoundException | IOException e) {
e.printStackTrace();
}
}
public void saveFeatureMatrices(List<FeatureMatrix> featureMatrices) {
try {
File file = new File(filePath);
if (!file.exists()) {
file.getParentFile().mkdirs();
}
FileOutputStream fos = new FileOutputStream(filePath);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(featureMatrices);
oos.flush();
oos.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
// as we do not utilize json-format data, we have no loadJson method
// the root reason is that prophet4j owns sub-features
public void saveAsJson(FeatureOption featureOption) {
int arraySize = 0;
switch (featureOption) {
case ENHANCED:
arraySize = EnhancedFeature.FEATURE_SIZE;
break;
case EXTENDED:
arraySize = ExtendedFeature.FEATURE_SIZE;
break;
case ORIGINAL:
arraySize = OriginalFeature.FEATURE_SIZE;
break;
case S4R:
arraySize = S4RFeature.FEATURE_SIZE;
break;
case S4RO:
arraySize = S4ROFeature.FEATURE_SIZE;
break;
}
Map<String, List<double[]>> featureMatrixList = new HashMap<>();
for (FeatureMatrix featureMatrix : featureMatrices) {
List<double[]> featureVectorList = new ArrayList<>();
// always skip this loop when we meet invalid patches
for (FeatureVector featureVector : featureMatrix.getFeatureVectors()) {
double[] featureCrossArray = new double[arraySize];
for (FeatureCross featureCross : featureVector.getFeatureCrosses()) {
featureCrossArray[featureCross.getId()] = featureCross.getDegree();
}
featureVectorList.add(featureCrossArray);
}
featureMatrixList.put(featureMatrix.fileKey, featureVectorList);
}
String json = new Gson().toJson(featureMatrixList);
String jsonPath = filePath.replace("prophet4j/_BIN/", "prophet4j/_JSON/");
jsonPath = jsonPath.replace(".bin", ".json");
try {
File file = new File(jsonPath);
if (!file.exists()) {
file.getParentFile().mkdirs();
}
Files.write(Paths.get(jsonPath), json.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public String toString() {
return filePath;
}
}
// entity which contains feature-crosses to express characteristics of each diff-operation
class FeatureVector implements Serializable {
static final long serialVersionUID = 1L;
private Set<FeatureCross> featureCrosses;
public FeatureVector() {
this.featureCrosses = new HashSet<>();
}
public boolean containFeature(Feature feature) {
for (FeatureCross featureCross : featureCrosses) {
if (featureCross.containFeature(feature)) {
return true;
}
}
return false;
}
public void addFeatureCross(FeatureCross featureCross) {
featureCrosses.add(featureCross);
}
// if we return set directly, results would be unpredictable. DO NOT return set directly
public List<FeatureCross> getFeatureCrosses() {
List<FeatureCross> list = new ArrayList<>(featureCrosses);
list.sort(Comparator.comparingInt(FeatureCross::getId));
return list;
}
public List<FeatureCross> getNonSortedFeatureCrosses() {
List<FeatureCross> list = new ArrayList<>(featureCrosses);
return list;
}
public void merge(FeatureVector featureVector) {
this.featureCrosses.addAll(featureVector.getFeatureCrosses());
}
public double score(ParameterVector parameterVector) {
// scores means values of phi dotProduct theta
return parameterVector.dotProduct(this);
}
@Override
public String toString() {
return "FeatureCrosses: " + featureCrosses;
}
}
// entity which contains feature-vectors to express multi-diff-operations of each patch
class FeatureMatrix implements Serializable {
static final long serialVersionUID = 1L;
private boolean marked; // for human patch or not
private String fileKey;
private List<FeatureVector> featureVectors;
public FeatureMatrix(boolean marked, String fileKey, List<FeatureVector> featureVectors) {
this.marked = marked;
this.fileKey = fileKey;
this.featureVectors = featureVectors;
}
public boolean containFeature(Feature feature) {
for (FeatureVector featureVector : featureVectors) {
if (featureVector.containFeature(feature)) {
return true;
}
}
return false;
}
public List<FeatureVector> getFeatureVectors() {
return featureVectors;
}
public boolean isMarked() {
return this.marked;
}
// to mark generated patches false
public void correctMarked() {
this.marked = false;
}
public double score(ParameterVector parameterVector) {
double score = 0;
// we compute the sum as the whole score
for (FeatureVector featureVector : featureVectors) {
score += featureVector.score(parameterVector);
}
return score;
}
@Override
public String toString() {
return "FeatureVectors: " + featureVectors;
}
}
// entity which contains weights for all feature-crosses
class ParameterVector {
public double gamma = 0;
private int arraySize = 0;
private double[] parameterArray;
public ParameterVector(FeatureOption featureOption) {
switch (featureOption) {
case ENHANCED:
this.arraySize = EnhancedFeature.FEATURE_SIZE;
break;
case EXTENDED:
this.arraySize = ExtendedFeature.FEATURE_SIZE;
break;
case ORIGINAL:
this.arraySize = OriginalFeature.FEATURE_SIZE;
break;
case S4R:
this.arraySize = S4RFeature.FEATURE_SIZE;
break;
case S4RO:
this.arraySize = S4ROFeature.FEATURE_SIZE;
break;
}
this.parameterArray = new double[arraySize];
}
public int size() {
return parameterArray.length;
}
public double get(int index) {
return parameterArray[index];
}
// +=
public void inc(int index, double value) {
parameterArray[index] += value;
}
// -=
public void dec(int index, double value) {
parameterArray[index] -= value;
}
// /=
public void div(int index, double value) {
parameterArray[index] /= value;
}
public void clone(ParameterVector parameterVector) {
this.arraySize = parameterVector.arraySize;
this.parameterArray = parameterVector.parameterArray.clone();
}
public double dotProduct(FeatureVector featureVector) {
double res = 0;
for (FeatureCross featureCross : featureVector.getFeatureCrosses()) {
int featureCrossId = featureCross.getId();
res += parameterArray[featureCrossId];
}
return res;
}
public void load(String filePath) {
try {
File vectorFile = new File(filePath);
String string = FileUtils.readFileToString(vectorFile, Charset.defaultCharset());
String[] substrings = string.split(" ");
parameterArray = Arrays.stream(substrings).mapToDouble(Double::valueOf).toArray();
} catch (IOException e) {
e.printStackTrace();
}
}
public void save(String filePath) {
try {
StringJoiner stringJoiner = new StringJoiner(" ");
for (double parameter : parameterArray) {
stringJoiner.add(String.valueOf(parameter));
}
File vectorFile = new File(filePath);
FileUtils.writeStringToFile(vectorFile, stringJoiner.toString(), Charset.defaultCharset(), false);
} catch (IOException e) {
e.printStackTrace();
}
}
}
enum DiffType {
DeleteType,
InsertType,
UpdateType,
//Delete and Move
PartialDeleteType,
}
enum RepairKind { // implementation is at RepairGenerator.java
// INSERT_CONTROL_RF
IfExitKind, // genAddIfExit()
// INSERT_GUARD_RF
GuardKind, // genAddIfGuard()
SpecialGuardKind, // genAddIfGuard()
// INSERT_STMT_RF
AddInitKind, // Inapplicable to Java
AddAndReplaceKind, // genAddStatement()
// REPLACE_COND_RF
TightenConditionKind, // genTightCondition()
LoosenConditionKind, // genLooseCondition()
// REPLACE_STMT_RF
ReplaceKind, // genReplaceStmt()
ReplaceStringKind, // genReplaceStmt()
//code deletion related features
RemovePartialIFKind,
RemoveSTMTKind,
RemoveWholeIFKind,
RemoveWholeBlockKind,
}
class DiffEntry { // DiffResultEntry
// the reason why CtElement is used here is because clang::Expr isa clang::Stmt
public DiffType type;
public CtElement srcNode, dstNode;
public DiffEntry(DiffType type, CtElement srcNode, CtElement dstNode) {
this.type = type;
this.srcNode = srcNode;
this.dstNode = dstNode;
}
}
class Repair {
public RepairKind kind;
public boolean isReplace;
public CtElement srcElem, dstElem; // from RepairAction
public List<CtElement> atoms; // from RepairAction
public CtElement oldRExpr, newRExpr; // only for UpdateType
public Repair() {
this.kind = null;
this.isReplace = false;
this.srcElem = null;
this.dstElem = null;
this.atoms = new ArrayList<>();
this.oldRExpr = null;
this.newRExpr = null;
}
public Set<CtElement> getCandidateAtoms() {
Set<CtElement> ret = new HashSet<>();
ret.add(null); // ?
ret.addAll(atoms);
return ret;
}
}
}