-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTeste.java
More file actions
362 lines (336 loc) · 12.6 KB
/
Copy pathTeste.java
File metadata and controls
362 lines (336 loc) · 12.6 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
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
package br.ufcg.spg.transformation;
import br.ufcg.spg.bean.Tuple;
import br.ufcg.spg.cluster.Cluster;
import br.ufcg.spg.cluster.ClusterUnifier;
import br.ufcg.spg.config.TechniqueConfig;
import br.ufcg.spg.database.ClusterDao;
import br.ufcg.spg.database.TransformationDao;
import br.ufcg.spg.edit.Edit;
import br.ufcg.spg.ml.clustering.DbScanClustering;
import br.ufcg.spg.ml.editoperation.Script;
import br.ufcg.spg.ml.metric.ScriptDistanceMetric;
import br.ufcg.spg.refaster.RefasterTranslator;
import br.ufcg.spg.string.StringUtils;
import br.ufcg.spg.tree.RevisarTree;
import br.ufcg.spg.tree.RevisarTreeParser;
import br.ufcg.spg.util.PrintUtils;
import br.ufcg.spg.validator.ClusterValidator;
import br.ufcg.spg.validator.RenameChecker;
import br.ufcg.spg.validator.node.INodeChecker;
import de.jail.geometry.schemas.Point;
import de.jail.statistic.clustering.density.DBScan;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.apache.commons.io.FileUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
/**
* Utility class to perform transformations.
*/
public final class Teste {
/**
* Learned scripts.
*/
private static List<Point> scripts = new ArrayList<>();
/**
* Rename scripts.
*/
private static List<Script> renameScripts = new ArrayList<>();
/**
* Logger.
*/
private static final Logger logger = LogManager.getLogger(TransformationUtils.class.getName());
/**
* Field only for test purpose.
*/
private static int clusterIndex = 1;
private Teste () {
}
/**
* Computes the matches for all clusters.
*/
public static void transformations() {
final TransformationDao dao = TransformationDao.getInstance();
final Long clusterId = dao.getLastClusterId();
final List<Cluster> srcClusters = getClusters();
final List<Cluster> remainingClusters = new ArrayList<>();
if (clusterId == -1) {
transformations(srcClusters);
} else {
boolean include = false;
for (final Cluster cluster : srcClusters) {
if (include) {
remainingClusters.add(cluster);
}
if (cluster.getId().equals(clusterId)) {
include = true;
}
}
transformations(remainingClusters);
}
}
/**
* Computes the template for some cluster.
* @param clusterId label of the cluster
*/
public static void transformations(final String clusterId) {
final ClusterDao dao = ClusterDao.getInstance();
final List<Cluster> clusters = dao.getClusters(clusterId);
transformations(clusters);
}
/**
* Computes transformations for a set of clusters.
*/
public static void transformations(final List<Cluster> srcClusters) {
try {
for (int i = 0; i < srcClusters.size(); i++) {
logger.trace(((double) i) / srcClusters.size() + " % completed.");
final Cluster clusteri = srcClusters.get(i);
// Analyze clusters with two or more elements.
if (clusteri.getNodes().size() < 2) {
continue;
}
Edit edit = clusteri.getNodes().get(0);
Transformation transformation = tranformation(clusteri, edit);
//TransformationDao.getInstance().save(transformation);
saveTransformation(transformation);
}
} catch (final Exception e) {
logger.error(e.getStackTrace());
}
}
/**
* Computes the template for some cluster.
*/
public static void transformationsLargestClusters() {
final List<Cluster> clusters = getLargestClusters();
transformations(clusters);
}
/**
* Computes the template for some cluster.
*/
public static void transformationsMoreProjects(List<Cluster> clusters) {
transformations(clusters);
DBScan dbscan = new DBScan(0.01, 1, new ScriptDistanceMetric());
List<de.jail.statistic.clustering.Cluster> clusteres = dbscan.cluster(scripts);
int countCluster = 0;
List<Script> clusteredScriptsList = new ArrayList<>();
for (de.jail.statistic.clustering.Cluster list : clusteres) {
List<Script> ls = new ArrayList<>();
for (Point p : list.getAllPoints()) {
Script sc = (Script) p;
ls.add(sc);
}
clusteredScriptsList.addAll(ls);
saveCluster(++countCluster, ls);
}
if (!renameScripts.isEmpty()) {
saveCluster(++countCluster, renameScripts);
}
for (final Point point : scripts) {
Script sc = (Script) point;
Cluster clusteri = sc.getCluster();
Cluster clusterj = clusteri.getDst();
if (!clusteredScriptsList.contains(sc)) {
StringBuilder content = new StringBuilder("");
content.append(sc.getList()).append('\n');
content.append(formatHeader());
content.append(formatClusterContent(clusteri, clusterj));
content.append(formatHeader());
String counterFormated = String.format("%03d", ++ countCluster);
String path = "../Projects/cluster/clusters/" + counterFormated + ".txt";
final File clusterFile = new File(path);
try {
FileUtils.writeStringToFile(clusterFile, content.toString());
} catch (IOException e) {
logger.error(e.getStackTrace());
}
}
}
}
/**
* Computes the template for some cluster.
*/
public static void transformationsMoreProjects_Why_Distinct_Clusters() {
// final List<Cluster> clusters = getClusterMoreProjects();
// transformations(clusters);
// final List<ArrayList<Script>> clusteredScripts = DbScanClustering.cluster(scripts);
// final List<Edit> edits = new ArrayList<>();
// ArrayList<Script> list = clusteredScripts.get(0);
// list = new ArrayList<>(list.subList(2, 4));
// for (Script sc : list) {
// edits.addAll(sc.getCluster().getNodes());
// }
// Tuple<Tuple<List<Cluster>, List<Cluster>>, Tuple<Edit, Edit>> toAnalyze =
// ClusterUnifier.getInstance().clusterEditsAnalyzeInvalid(edits);
// Edit srcEdit = toAnalyze.getItem2().getItem1();
// Edit dstEdit = toAnalyze.getItem2().getItem2();
// List<Cluster> src = toAnalyze.getItem1().getItem1();
// List<Cluster> dst = toAnalyze.getItem1().getItem2();
// List<Tuple<Cluster, Double>> costs;
// costs = ClusterUnifier.getInstance().bestCluster(src, dst, srcEdit, dstEdit);
// Cluster valid = ClusterUnifier.getInstance().searchForValid(srcEdit, dstEdit, costs);
// logger.trace(valid);
}
private static void saveCluster(int countCluster, List<Script> list) {
StringBuilder content = new StringBuilder("NUMBER OF NODES IN THIS CLUSTER: "
+ list.size()).append("\n\n");
int count = 0;
for (Script sc : list) {
content.append(formatHeader());
content.append(sc.getList()).append('\n');
String cnumber = String.format("%03d", ++count);
content.append("CLUSTER ").append(cnumber).append('\n');
Cluster clusteri = sc.getCluster();
Cluster clusterj = clusteri.getDst();
content.append(formatClusterContent(clusteri, clusterj));
content.append(formatFooter());
}
String counterFormated = String.format("%03d", countCluster);
String path = "../Projects/cluster/clusters/" + counterFormated + ".txt";
final File clusterFile = new File(path);
try {
FileUtils.writeStringToFile(clusterFile, content.toString());
} catch (IOException e) {
logger.error(e.getStackTrace());
}
}
private static String formatHeader() {
StringBuilder content = new StringBuilder();
content.append("================================================================================\n");
content.append("=================================CLUSTER DATA===================================\n");
content.append("================================================================================\n");
return content.toString();
}
private static String formatFooter() {
StringBuilder content = new StringBuilder();
content.append("================================================================================\n");
content.append("==============================END OF CLUSTER DATA===============================\n");
content.append("================================================================================\n\n");
return content.toString();
}
/**
* Learns a transformation for a cluster.
*/
public static Transformation tranformation(final Cluster clusteri, final Edit srcEdit) {
try {
String refaster;
if (TechniqueConfig.getInstance().isCreateRule()) {
refaster = RefasterTranslator.translate(clusteri, srcEdit);
} else {
refaster = "";
}
final boolean isValid = ClusterValidator.isValidTrans(clusteri);
final Transformation trans = new Transformation();
trans.setTransformation(refaster);
trans.setCluster(clusteri);
trans.setValid(isValid);
return trans;
} catch (final Exception e) {
logger.error(e.getStackTrace());
}
return null;
}
/**
* Saves a transformation.
*/
public static void saveTransformation(final Transformation trans) throws IOException {
final String refaster = trans.getTransformation();
final Cluster clusteri = trans.getCluster();
final Cluster clusterj = clusteri.getDst();
StringBuilder content = new StringBuilder("");
content.append(formatHeader());
content.append(refaster).append('\n');
content.append(formatClusterContent(clusteri, clusterj));
content.append(formatHeader());
Script script = DbScanClustering.getCluster(clusteri);
if (isSameBeforeAfter(clusteri)) {
return;
}
scripts.add(script);
String path = "../Projects/cluster/" + trans.isValid() + '/' + clusterIndex++ + ".txt";
final File clusterFile = new File(path);
FileUtils.writeStringToFile(clusterFile, content.toString());
}
private static String formatClusterContent(final Cluster clusteri, final Cluster clusterj) {
StringBuilder content = new StringBuilder();
RevisarTree<String> tempBefore = RevisarTreeParser.parser(clusteri.getAu());
String before = PrintUtils.prettyPrint(tempBefore);
RevisarTree<String> tempAfter = RevisarTreeParser.parser(clusterj.getAu());
String after = PrintUtils.prettyPrint(tempAfter);
String addToLines = " ";
String afterFormated = formatOutput(after, addToLines);
String output = StringUtils.printStringSideBySide(before, afterFormated);
content.append(output);
content.append(formatStringNodes(clusteri.getNodes()));
return content.toString();
}
private static String formatStringNodes(final List<Edit> srcNodes) {
StringBuilder result = new StringBuilder();
result.append("\nEXAMPLES IN THIS CLUSTER ").append(srcNodes.size()).append(":\n\n");
StringBuilder beforeNodes = new StringBuilder();
StringBuilder afterNodes = new StringBuilder();
int count = 0;
for (final Edit node : srcNodes) {
beforeNodes.append(node.getText()).append('\n');
afterNodes.append(node.getDst().getText()).append('\n');
if (++count == 4) {
break;
}
}
String addToLines = " >> ";
String afterOutput = formatOutput(afterNodes.toString(), addToLines);
String output = StringUtils.printStringSideBySide(beforeNodes.toString(), afterOutput);
result.append(output);
result.append("...\n");
return result.toString();
}
/**
* Format output.
* @param pattern pattern
*/
private static String formatOutput(final String pattern, final String addToLines) {
String newPattern = "";
String addToMid = " >> ";
String[] lines = pattern.split("\n");
int mid = lines.length / 2;
for (int i = 0; i < lines.length; i++) {
if (i == mid) {
newPattern += addToMid + lines[i] + '\n';
} else {
newPattern += addToLines + lines[i] + '\n';
}
}
return newPattern;
}
private static List<Cluster> getClusters() {
final ClusterDao dao = ClusterDao.getInstance();
return dao.getSrcClusters();
}
private static boolean isSameBeforeAfter(final Cluster clusteri) {
for (Edit c : clusteri.getNodes()) {
Edit dstEdit = c.getDst();
if (!c.getText().equals(dstEdit.getText())) {
return false;
}
}
return true;
}
/**
* Get clusters with the largest number of nodes.
*/
public static List<Cluster> getLargestClusters() {
final ClusterDao dao = ClusterDao.getInstance();
return dao.getLargestClusters();
}
/**
* Get clusters with the largest number of examples.
*/
public static List<Cluster> getClusterMoreProjects() {
final ClusterDao dao = ClusterDao.getInstance();
return dao.getClusterMoreProjects(3);
}
}