-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.java
118 lines (109 loc) · 4.57 KB
/
Main.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
import java.io.IOException;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Main class to test algorithms.
*/
class Main {
private static final Logger logger = Logger.getLogger("CZ2001 Project 2");
/**
* Runs the tests.
*
* @param args Default main parameter.
*/
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
GraphReader graphReader = new GraphReader();
Graph graph = new Graph();
int k;
// Read graph filename
while (true) {
try {
System.out.println("Enter graph filename:");
String graphFile = scanner.nextLine();
graphReader.readGraph(graph, graphFile);
break;
} catch (IOException ioException) {
logger.log(Level.SEVERE, ioException.getMessage());
System.out.println("Error reading file! Please use a valid file!");
} catch (Exception exception) {
logger.log(Level.SEVERE, exception.getMessage());
System.out.println("Problem parsing file! Please use a valid file");
}
}
// Read hospital filename
while (true) {
try {
System.out.println("Enter hospital filename:");
String hospitalFile = scanner.nextLine();
graphReader.readHospitals(graph, hospitalFile);
break;
} catch (IOException ioException) {
logger.log(Level.SEVERE, ioException.getMessage());
System.out.println("Error reading file! Please use a valid file!");
} catch (Exception exception) {
logger.log(Level.SEVERE, exception.getMessage());
System.out.println("Problem parsing file! Please use a valid file");
}
}
// Read node filename
while (true) {
try {
System.out.println("Enter node filename:");
String nodeFile = scanner.nextLine();
graphReader.readNodes(graph, nodeFile);
break;
} catch (IOException ioException) {
logger.log(Level.SEVERE, ioException.getMessage());
System.out.println("Error reading file! Please use a valid file!");
} catch (Exception exception) {
logger.log(Level.SEVERE, exception.getMessage());
System.out.println("Problem parsing file! Please use a valid file");
}
}
// Read k
while (true) {
try {
System.out.println("Enter value of k:");
k = scanner.nextInt();
if (k < 1) {
throw new Exception("Non-positive integer entered!");
}
break;
} catch (Exception exception) {
logger.log(Level.SEVERE, exception.getMessage());
System.out.println("Please enter a valid integer!");
}
}
// Run BFS algorithm and write to file
try {
BFS bfs = new BFS(graph);
logger.log(Level.INFO, "Running BFS...");
bfs.execute();
logger.log(Level.INFO, "Writing BFS results...");
new ResultWriter(bfs.getFileName()).writeShortestPath(graph.getNodes(), bfs.getPreviousNodes());
} catch (IOException ioException) {
logger.log(Level.SEVERE, ioException.getMessage());
System.out.println("Error writing BFS file!");
} catch (Exception exception) {
logger.log(Level.SEVERE, exception.getMessage());
System.out.println("Problem executing BFS algorithm!");
}
// Run modified BFS algorithm and write to file
try {
ModifiedBFS modifiedBFS = new ModifiedBFS(graph, k);
logger.log(Level.INFO, "Running modified BFS...");
modifiedBFS.execute();
logger.log(Level.INFO, "Writing modified BFS results...");
new ResultWriter(modifiedBFS.getFileName()).writeKShortestPaths(graph.getNodes(),
modifiedBFS.getVisitedOrder(), modifiedBFS.getPartialMinDistanceSpanningTrees());
} catch (IOException ioException) {
logger.log(Level.SEVERE, ioException.getMessage());
System.out.println("Error writing BFS file!");
} catch (Exception exception) {
logger.log(Level.SEVERE, exception.getMessage());
System.out.println("Problem executing BFS algorithm!");
}
}
}