-
Notifications
You must be signed in to change notification settings - Fork 1
/
MineChangePatterns.java
180 lines (165 loc) · 6.61 KB
/
MineChangePatterns.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
package main;
import change.ChangeGraph;
import groum.GROUMGraph;
import mining.Miner;
import mining.Pattern;
import org.apache.commons.lang.SystemUtils;
import utils.FileIO;
import utils.NotifyingBlockingThreadPoolExecutor;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Scanner;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
public class MineChangePatterns {
private static int THREAD_POOL_SIZE = 1;
private static final Callable<Boolean> blockingTimeoutCallback = new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
return true; // keep waiting
}
};
private static NotifyingBlockingThreadPoolExecutor pool = new NotifyingBlockingThreadPoolExecutor(THREAD_POOL_SIZE, THREAD_POOL_SIZE, 15, TimeUnit.SECONDS, 200, TimeUnit.MILLISECONDS, blockingTimeoutCallback);
private static AtomicInteger numOfCommits = new AtomicInteger(0), numOfGraphs = new AtomicInteger(0);
private static String changesPath ="";
private static String reposPath = "";
public static void main(String[] args) {
long start = System.currentTimeMillis();
// changesPath = "contains a set of GitHub repos each of which is in the structure of username/reponame/*.dat."
// + "E.g. inPath = repos-junit should contains junit-team/junit/*.dat"
// + "or inPath = repos could contains junit-team/junit/*.dat and JetBrains/intellij-community/*.dat";
changesPath = "/Users/malinda/Documents/Research_Topic_2/CPatMiner/AtomicASTChangeMining/OUTPUT/";
reposPath = "/Users/malinda/Documents/Research_Topic_2/DATA_FOR_CPATMiner";
if (args.length > 0) {
Pattern.mode = Integer.parseInt(args[0]);
if (Pattern.mode == 0) {
THREAD_POOL_SIZE = Integer.parseInt(args[1]);
pool = new NotifyingBlockingThreadPoolExecutor(THREAD_POOL_SIZE, THREAD_POOL_SIZE, 15, TimeUnit.SECONDS, 200, TimeUnit.MILLISECONDS, blockingTimeoutCallback);
}
}
ArrayList<GROUMGraph> allGraphs = new ArrayList<>();
HashSet<String> projectNames = new HashSet<String>();
String content = null;
if(SystemUtils.IS_OS_MAC){
content = FileIO.readStringFromFile("/Users/malinda/Documents/Research_Topic_2/SemanticChangeGraphMiner/SemanticChangeGraphMiner/selected-repos.csv");
} else if (SystemUtils.IS_OS_LINUX) {
content = FileIO.readStringFromFile("/Users/malinda/Documents/Research_Topic_2/CPatMiner/AtomicASTChangeMining/selected-repos.csv");
} else if (SystemUtils.IS_OS_WINDOWS){
content = FileIO.readStringFromFile(new File(reposPath).getParentFile().getAbsolutePath() + "/" + new File(reposPath).getName() + ".csv");
}
Scanner sc = new Scanner(content);
while (sc.hasNextLine()) {
// if (projectNames.size() >= 8000)
// break;
String line = sc.nextLine();
int index = line.indexOf(',');
if (index == -1)
index = line.length();
final String name = line.substring(0, index);
System.out.println(name);
if (Pattern.mode == 0) {
if (new File("output/patterns/" + name.replace("/", "---")).exists())
continue;
new File("output/patterns/" + name.replace("/", "---")).mkdirs();
pool.execute(new Runnable() {
@Override
public void run() {
ArrayList<GROUMGraph> graphs = readGraphs(changesPath, name);
if (!graphs.isEmpty()) {
projectNames.add(name);
System.out.println("Project " + projectNames.size() + " " + name);
}
mine(graphs, 1, name.replace("/", "---"));
System.out.println("Done " + name);
}
});
}
else {
ArrayList<GROUMGraph> graphs = readGraphs(changesPath, name);
if (!graphs.isEmpty()) {
projectNames.add(name);
System.out.println("Project " + projectNames.size() + " " + name);
}
allGraphs.addAll(graphs);
}
}
sc.close();
System.out.println("Projects: " + projectNames.size());
System.out.println("Commits: " + numOfCommits);
System.out.println("Graphs: " + numOfGraphs);
if (Pattern.mode != 0) {
File currDir = new File("output/patterns/" + new File(changesPath).getName() + (Pattern.mode == -1 ? "-hybrid" : "-cross"));
currDir.mkdirs();
mine(allGraphs, 2, currDir.getName());
}
System.out.println("Projects: " + projectNames.size());
System.out.println("Commits: " + numOfCommits);
System.out.println("Graphs: " + numOfGraphs);
System.out.println("<td>"+projectNames.size()+"</td>");
System.out.println("<td>"+numOfCommits+"</td>");
System.out.println("<td>"+numOfGraphs+"</td>");
long end = System.currentTimeMillis();
System.out.println((end - start) / 1000 + " s.");
if (Pattern.mode == 0) {
try {
// pool.await(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
pool.shutdown();
pool.awaitTermination(1, TimeUnit.MINUTES);
} catch (final InterruptedException e) { }
}
}
private static ArrayList<GROUMGraph> mine (ArrayList<GROUMGraph> graphs, int level, String currDir) {
Miner miner = new Miner(level);
miner.setCurrDir(currDir);
if (level == 1) {
return miner.mine(graphs, reposPath);
} else {
miner.setReposPath(reposPath);
return miner.superMine(graphs);
}
}
public static ArrayList<GROUMGraph> readGraphs(String changesPath, String projectName) {
ArrayList<GROUMGraph> graphs = new ArrayList<>();
File dir = new File(changesPath + "/" + projectName);
System.out.println(changesPath + "/" + projectName);
if (!dir.exists())
return graphs;
File[] files = dir.listFiles();
if (files == null)
return graphs;
for (int i = 0; i < files.length; i++) {
File sub = files[i];
if (!sub.getName().endsWith(".dat")) continue;
numOfCommits.incrementAndGet();
//System.out.println("Commit " + i + ": " + file.getName());
@SuppressWarnings("unchecked")
HashMap<String, HashMap<String, ChangeGraph>> fileChangeGraphs = (HashMap<String, HashMap<String, ChangeGraph>>) FileIO.readObjectFromFile(sub.getAbsolutePath());
for (String fp : fileChangeGraphs.keySet()) {
// System.out.println(fp);
HashMap<String, ChangeGraph> cgs = fileChangeGraphs.get(fp);
for (String method : cgs.keySet()) {
//System.out.println(method);
int index = sub.getName().indexOf('.');
if (index < 0) {
index = sub.getName().length();
}
String name = FileIO.getSimpleFileName(sub.getName().substring(0, index)) + "," + fp + "," + method;
ChangeGraph cg = cgs.get(method);
if (cg.getNodes().size() <= 1) continue;
numOfGraphs.incrementAndGet();
GROUMGraph g = new GROUMGraph(cg, name);
// FIXME
g.pruneDoubleEdges();
g.setProject(projectName);
graphs.add(g);
}
}
}
return graphs;
}
}