-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPathParser.java
More file actions
186 lines (173 loc) · 6.07 KB
/
Copy pathPathParser.java
File metadata and controls
186 lines (173 loc) · 6.07 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
package hw8;
import hw8.Buildings;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
import hw5.Graph;
/**
* PathParser reads the building file and paths file and creates a Graph from it
* where end points/buildings are connected by distances between them.
*
*
* @author RibhavHora
*/
public class PathParser {
// This class does not represent an ADT.
/**
* A checked exception class for bad data files
*/
@SuppressWarnings("serial")
public static class MalformedDataException extends Exception {
public MalformedDataException() {
}
public MalformedDataException(String message) {
super(message);
}
public MalformedDataException(Throwable cause) {
super(cause);
}
public MalformedDataException(String message, Throwable cause) {
super(message, cause);
}
}
/**
* Reads the data from filename and stores it
*
* @param fileName
* the name of the file which contains the data about the
* buildings in short name, long name, x, y format separated by
* tabs.
* @param buildingLocations
* stores the buildings in fileName
* @param map
* stores a map from short names to their buildings objects.
*
* @effects Stores the data from fileName in buildingLocations and makes a
* map of abbreviated name to their respective buildings objects.
*/
public static void parseBuildingData(String filename, Set<Buildings> buildingLocations, Map<String, Buildings> map)
throws MalformedDataException {
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(filename));
String inputLine;
while ((inputLine = reader.readLine()) != null) {
// Add on - Ignore comment lines.
if (inputLine.startsWith("#")) {
continue;
}
String[] tokens = inputLine.split("\t"); // split of the line
// into four
if (tokens.length != 4) {
throw new MalformedDataException("Line should contain exactly three tabs: " + inputLine);
}
String shortName = tokens[0];
String longName = tokens[1];
Double x = Double.parseDouble(tokens[2]);
Double y = Double.parseDouble(tokens[3]);
buildingLocations.add(new Buildings(x, y, longName, shortName)); // the buildings objects
map.put(shortName, new Buildings(x, y, longName, shortName)); // the map from short name to its buildings object
}
} catch (IOException e) {
System.err.println(e.toString());
e.printStackTrace(System.err);
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
System.err.println(e.toString());
e.printStackTrace(System.err);
}
}
}
}
/**
* Reads the data from pathData and builds a graph
*
* @param buildingData
* the name of the file which contains the data about the
* buildings in short name, long name, x, y format separated by
* tabs.
* @param pathData
* the name of the file which contains the locations of all end points
* and to which points they are connected and the distance between them
* x,y
* \tx1,y1: dist
* x,y is connected to x1,y1 with a distance of dist
*
* @returns a graph of all the end points or buildings if the end point is a building.
* The graph has buildings objects connected by the distance between them.
*/
public static Graph<Buildings, Double> parsePathData(String buildingData, String pathData)
throws MalformedDataException {
BufferedReader reader = null;
Graph<Buildings, Double> graph = new Graph<Buildings, Double>();
try {
reader = new BufferedReader(new FileReader(pathData));
Set<Buildings> nameBuildings = new HashSet<Buildings>();
Map<String, Buildings> shorts = new HashMap<String, Buildings>();
PathParser.parseBuildingData(buildingData, nameBuildings, shorts);
String inputLine;
inputLine = reader.readLine();
while (inputLine != null) {
// Add on - Ignore comment lines.
if (inputLine.startsWith("#")) {
continue;
}
String[] mainLine = inputLine.split(",");
while ((inputLine = reader.readLine()) != null && inputLine.startsWith("\t")) {
String[] firstSplit = inputLine.split(": ");
String connectedPoint = firstSplit[0].replace("\t",""); // getting rid of the starting tab
String[] secondPoint = connectedPoint.split(",");
if (firstSplit.length != 2) {
throw new MalformedDataException("Line should contain exactly two values seperated by : ");
}
boolean ifAdded1 = false; // to check if the point is a building and was added as one
boolean ifAdded2 = false; // to check if the point is a building and was added as one
double x1 = Double.parseDouble(mainLine[0]); // firstLine x coordinate
double y1 = Double.parseDouble(mainLine[1]); // firstLine y coordinate
double x2 = Double.parseDouble(secondPoint[0]); // tab line x coordinate
double y2 = Double.parseDouble(secondPoint[1]); // tab line y coordinate
Buildings b1 = new Buildings();
Buildings b2 = new Buildings();
for (Buildings b : nameBuildings) { // to check if the point is a building so we add it as a building
if (b.getX() == x1 && b.getY() == y1) {
b1 = b;
graph.addSingleNode(b);
ifAdded1 = true;
}
if (b.getX() == x2 && b.getY() == y2) {
b2 = b;
graph.addSingleNode(b2);
ifAdded2 = true;
}
}
if (!ifAdded1) { // if its not a building
b1 = new Buildings(x1, y1, "", "");
graph.addSingleNode(b1);
}
if (!ifAdded2) { // if its not a building
b2 = new Buildings(x2, y2, "", "");
graph.addSingleNode(b2);
}
graph.addEdge(b1, b2, Double.parseDouble(firstSplit[1])); // creating the graph
}
}
} catch (IOException e) {
System.err.println(e.toString());
e.printStackTrace(System.err);
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
System.err.println(e.toString());
e.printStackTrace(System.err);
}
}
}
return graph;
}
}