Skip to content

Commit 1b6bfd6

Browse files
committed
Update for running
1 parent adb228c commit 1b6bfd6

File tree

6 files changed

+138
-88
lines changed

6 files changed

+138
-88
lines changed

src/ConstructGroum.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public Groum constructGroum(String groumPath,Map<String,String> apiMap){
1818
String id = strs[0];
1919
String api = strs[1].replace("label=\"","");
2020
api = api.substring(0,api.length() - 1);
21-
if(!api.startsWith("C:#Users#")){
21+
if(!api.startsWith("/home/x/mydisk/download_high_repos")){
2222
String originApi = api;
2323
if(originApi.contains("<")){
2424
if(originApi.contains("<init>")){
@@ -31,8 +31,8 @@ public Groum constructGroum(String groumPath,Map<String,String> apiMap){
3131
}
3232
}
3333

34-
if(apiMap.containsKey(api)){
35-
api = apiMap.get(api);
34+
if(apiMap.containsKey(originApi)){
35+
api = apiMap.get(originApi);
3636
}else{
3737
api = Integer.toString(-1);
3838
}
@@ -51,16 +51,22 @@ public Groum constructGroum(String groumPath,Map<String,String> apiMap){
5151
line = line.replace("[label=\"\"];","");
5252
line = line.trim();
5353
String[] strs = line.split(" -> ");
54-
String parentNodeId = strs[0];
55-
String childNodeId = strs[1];
56-
groum.addEdge(parentNodeId,childNodeId);
54+
if(strs.length == 2) {
55+
String parentNodeId = strs[0];
56+
String childNodeId = strs[1];
57+
if (groum.getNodeMap().containsKey(parentNodeId) && groum.getNodeMap().containsKey(childNodeId)) {
58+
groum.addEdge(parentNodeId, childNodeId);
59+
}
60+
}
5761
}
5862
}
5963
br.close();
6064
}catch(Exception e){
6165
e.printStackTrace();
66+
return null;
6267
}catch (Error e){
6368
e.printStackTrace();
69+
return null;
6470
}
6571
return groum;
6672
}

src/GetPath.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ private static void getForPath(List<String> successors) {
3333
getForPath(successorsCopy);
3434
}
3535
}
36+
3637
private static void getBackPath(List<String> predecessors) {
3738
if (predecessors.size() + 1 > threshold)
3839
return;
@@ -108,15 +109,21 @@ public static void convert(List<List<String>> pathResult, List<List<String>> sou
108109
Map<String, GroumNode> nodeMap = targetGraph.getNodeMap();
109110
for (List<String> sourcePath : source) {
110111
List<String> apiPath = new ArrayList<String>();
112+
boolean canAdd = true;
111113
for (String id : sourcePath) {
112114
if (id.equals("*")) {
113115
apiPath.add("*");
114116
}
115117
else {
116-
apiPath.add(nodeMap.get(id).getApi());
118+
String temp = nodeMap.get(id).getApi();
119+
if (!temp.equals("-1")) apiPath.add(temp);
120+
else {
121+
canAdd = false;
122+
break;
123+
}
117124
}
118125
}
119-
pathResult.add(apiPath);
126+
if (canAdd) pathResult.add(apiPath);
120127
}
121128
}
122129
public static List<List<String>> getAllPath(Groum groum, List<String> startList, int d) {

src/Main.java

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import java.io.File;
2+
import java.io.FileWriter;
23
import java.io.IOException;
4+
import java.io.PrintWriter;
35
import java.util.Map;
46

57
public class Main {
@@ -13,10 +15,46 @@ public static void main(String[] args) throws IOException {
1315
API2Index = ReadAPI2Index.getAPI2IndexMap(api2index);
1416
System.out.println("Finish Read API2Index");
1517
System.out.println("Write Path...");
16-
WritePathResult.ergodicDir(dotFile, allPathFile, API2Index);
18+
FileWriter fW = null;
19+
PrintWriter pW = null;
20+
21+
fW = getFileWriter(allPathFile);
22+
pW = getPrintWriter(fW);
23+
24+
WritePathResult.ergodicDir(dotFile, pW, API2Index);
25+
26+
ClosePWFW(pW, fW);
27+
1728
System.out.println("Finish Write Path");
1829
System.out.println("Read And Count PathResult...");
19-
ReadAndCountPathResult.readAndCount(allPathFile, countPathFile);
30+
31+
fW = getFileWriter(countPathFile);
32+
pW = getPrintWriter(fW);
33+
ReadAndCountPathResult.readAndCount(allPathFile, pW);
34+
35+
ClosePWFW(pW, fW);
36+
2037
System.out.println("Finish Read And Count PathResult");
2138
}
39+
40+
private static FileWriter getFileWriter(File file) throws IOException {
41+
if (file.exists()) {
42+
file.delete();
43+
}
44+
if (!file.exists()) {
45+
file.createNewFile();
46+
}
47+
return new FileWriter(file, true);
48+
}
49+
50+
private static PrintWriter getPrintWriter(FileWriter fW) {
51+
return new PrintWriter(fW);
52+
}
53+
54+
private static void ClosePWFW(PrintWriter pW, FileWriter fW) throws IOException {
55+
pW.flush();
56+
fW.flush();
57+
pW.close();
58+
fW.close();
59+
}
2260
}

src/ReadAndCountPathResult.java

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
public class ReadAndCountPathResult {
66
static Map<String, Integer> PathCounter;
7-
public static void readAndCount(File file, File targetFile) throws IOException {
7+
public static void readAndCount(File file, PrintWriter targetFilePW) throws IOException {
88
PathCounter = new HashMap<>();
99
FileReader fileReader = new FileReader(file);
1010
BufferedReader bufferedReader = new BufferedReader(fileReader);
@@ -19,20 +19,11 @@ public static void readAndCount(File file, File targetFile) throws IOException {
1919
}
2020
bufferedReader.close();
2121
fileReader.close();
22-
writePathAndCount(targetFile);
22+
writePathAndCount(targetFilePW);
2323
}
24-
private static void writePathAndCount(File file) throws IOException {
25-
if (file.exists()) {
26-
file.delete();
27-
}
28-
FileWriter fileWriter = new FileWriter(file, true);
29-
PrintWriter pw = new PrintWriter(fileWriter);
24+
private static void writePathAndCount(PrintWriter targetFilePW) throws IOException {
3025
for (String key : PathCounter.keySet()) {
31-
pw.println(key + " " + PathCounter.get(key));
26+
targetFilePW.println(key + " " + PathCounter.get(key));
3227
}
33-
pw.flush();
34-
fileWriter.flush();
35-
pw.close();
36-
fileWriter.close();
3728
}
3829
}

src/Test.java

Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,46 @@
1-
import java.io.File;
2-
import java.io.IOException;
3-
import java.util.ArrayList;
4-
import java.util.List;
5-
import java.util.Map;
6-
7-
public class Test {
8-
9-
static Map<String, String> API2Index;
10-
11-
/**
12-
* 单个Groum测试
13-
*/
14-
private static void test1() {
15-
ConstructGroum CG = new ConstructGroum();
16-
Groum groum = CG.constructGroum("C:\\Users\\RuI\\Desktop\\ZMR_RecRankEmersion\\RecRankJavaEmersion\\dotFile\\Demo.resizeArray.dot", API2Index);
17-
List<String> startList = new ArrayList<>();
18-
startList.add("2");
19-
List<List<String>> outList = GetPath.getAllPath(groum, startList, 6);
20-
for (List<String> path : outList) {
21-
System.out.println(path);
22-
}
23-
}
24-
25-
26-
27-
private static void test2() throws IOException {
28-
WritePathResult.ergodicDir(new File("C:\\Users\\RuI\\Desktop\\ZMR_RecRankEmersion\\RecRankJavaEmersion\\dotFile"), new File("C:\\Users\\RuI\\Desktop\\ZMR_RecRankEmersion\\RecRankJavaEmersion\\AllPath.txt"), API2Index);
29-
}
30-
31-
private static void test3() throws IOException {
32-
ReadAndCountPathResult.readAndCount(new File("C:\\Users\\RuI\\Desktop\\ZMR_RecRankEmersion\\RecRankJavaEmersion\\AllPath.txt"), new File("C:\\Users\\RuI\\Desktop\\ZMR_RecRankEmersion\\RecRankJavaEmersion\\PathCount.txt"));
33-
}
34-
35-
private static void test4() throws IOException {
36-
API2Index = ReadAPI2Index.getAPI2IndexMap(new File("C:\\Users\\RuI\\Desktop\\ZMR_RecRankEmersion\\RecRankJavaEmersion\\APIIndexMap.txt"));
37-
for (String keyStr : API2Index.keySet()) {
38-
System.out.println(keyStr + API2Index.get(keyStr));
39-
}
40-
}
41-
42-
public static void main(String[] args) throws IOException {
43-
test4(); // 获得APIIndexMap
44-
test3();
45-
}
46-
}
1+
//import java.io.File;
2+
//import java.io.IOException;
3+
//import java.util.ArrayList;
4+
//import java.util.List;
5+
//import java.util.Map;
6+
//
7+
//public class Test {
8+
//
9+
// static Map<String, String> API2Index;
10+
//
11+
// /**
12+
// * 单个Groum测试
13+
// */
14+
// private static void test1() {
15+
// ConstructGroum CG = new ConstructGroum();
16+
// Groum groum = CG.constructGroum("C:\\Users\\RuI\\Desktop\\ZMR_RecRankEmersion\\RecRankJavaEmersion\\dotFile\\Demo.resizeArray.dot", API2Index);
17+
// List<String> startList = new ArrayList<>();
18+
// startList.add("2");
19+
// List<List<String>> outList = GetPath.getAllPath(groum, startList, 6);
20+
// for (List<String> path : outList) {
21+
// System.out.println(path);
22+
// }
23+
// }
24+
//
25+
//
26+
//
27+
// private static void test2() throws IOException {
28+
// WritePathResult.ergodicDir(new File("C:\\Users\\RuI\\Desktop\\ZMR_RecRankEmersion\\RecRankJavaEmersion\\dotFile"), new File("C:\\Users\\RuI\\Desktop\\ZMR_RecRankEmersion\\RecRankJavaEmersion\\AllPath.txt"), API2Index);
29+
// }
30+
//
31+
// private static void test3() throws IOException {
32+
// ReadAndCountPathResult.readAndCount(new File("C:\\Users\\RuI\\Desktop\\ZMR_RecRankEmersion\\RecRankJavaEmersion\\AllPath.txt"), new File("C:\\Users\\RuI\\Desktop\\ZMR_RecRankEmersion\\RecRankJavaEmersion\\PathCount.txt"));
33+
// }
34+
//
35+
// private static void test4() throws IOException {
36+
// API2Index = ReadAPI2Index.getAPI2IndexMap(new File("C:\\Users\\RuI\\Desktop\\ZMR_RecRankEmersion\\RecRankJavaEmersion\\APIIndexMap.txt"));
37+
// for (String keyStr : API2Index.keySet()) {
38+
// System.out.println(keyStr + API2Index.get(keyStr));
39+
// }
40+
// }
41+
//
42+
// public static void main(String[] args) throws IOException {
43+
// test4(); // 获得APIIndexMap
44+
// test3();
45+
// }
46+
//}

src/WritePathResult.java

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,31 @@
44
import java.util.Map;
55

66
public class WritePathResult {
7+
private static int count = 0;
8+
9+
public static void ergodicDir(File dir, PrintWriter targetFilePW, Map<String, String> API2IndexMap) throws IOException {
710

8-
public static void ergodicDir(File dir, File targetFile, Map<String, String> API2IndexMap) throws IOException {
9-
if (targetFile.exists()) {
10-
targetFile.delete();
11-
}
1211
if(dir.isDirectory()){
1312
for(File file : dir.listFiles()){
1413
if(file.isDirectory()){
15-
ergodicDir(file, targetFile, API2IndexMap);
14+
System.out.println("ergodicDir...");
15+
ergodicDir(file, targetFilePW, API2IndexMap);
1616
}
1717
if(file.isFile() && file.getName().endsWith("dot")){
18-
writePath(file, targetFile, API2IndexMap);
18+
System.out.println("processing " + ++count + " data");
19+
writePath(file, targetFilePW, API2IndexMap);
1920
}
2021
}
2122
}
2223
else {
2324
if(dir.isFile() && dir.getName().endsWith("dot")){
24-
writePath(dir, targetFile, API2IndexMap);
25+
System.out.println("processing " + ++count + " data");
26+
writePath(dir, targetFilePW, API2IndexMap);
2527
}
2628
}
2729
}
2830

29-
public static void writePath(File groumFile, File targetFile, Map<String, String> API2IndexMap) throws IOException {
31+
private static void writePath(File groumFile, PrintWriter targetFilePW, Map<String, String> API2IndexMap) throws IOException {
3032
ConstructGroum CG = new ConstructGroum();
3133
Groum groum = CG.constructGroum(groumFile.getAbsolutePath(), API2IndexMap);
3234
Map<String, GroumNode> nodeMap = groum.getNodeMap();
@@ -35,22 +37,28 @@ public static void writePath(File groumFile, File targetFile, Map<String, String
3537
startList = new ArrayList<>();
3638
startList.add(id);
3739
List<List<String>> outList = GetPath.getAllPath(groum, startList, 4);
38-
writeFile(outList, targetFile);
40+
writeFile(outList, targetFilePW);
3941
}
4042
}
4143

42-
public static void writeFile(List<List<String>> outList, File target) throws IOException {
43-
if (!target.exists()) {
44-
target.createNewFile();
44+
private static String convertListToString(List<String> list) {
45+
StringBuilder sB = new StringBuilder();
46+
boolean ifStarted = false;
47+
for (String item : list) {
48+
if (ifStarted) {
49+
sB.append(",");
50+
}
51+
sB.append(item);
52+
if (!ifStarted) ifStarted = true;
4553
}
46-
FileWriter fileWriter = new FileWriter(target, true);
47-
PrintWriter pw = new PrintWriter(fileWriter);
54+
return sB.toString();
55+
}
56+
57+
private static void writeFile(List<List<String>> outList, PrintWriter targetFilePW) throws IOException {
58+
String tempStr = null;
4859
for (List<String> path : outList) {
49-
pw.println(path.toString());
60+
tempStr = convertListToString(path);
61+
targetFilePW.println(tempStr);
5062
}
51-
pw.flush();
52-
fileWriter.flush();
53-
pw.close();
54-
fileWriter.close();
5563
}
5664
}

0 commit comments

Comments
 (0)