1 package org.opentrafficsim.imb.demo;
2
3 import java.io.IOException;
4 import java.net.URISyntaxException;
5 import java.nio.file.Files;
6 import java.nio.file.Paths;
7 import java.util.ArrayList;
8 import java.util.HashMap;
9 import java.util.HashSet;
10 import java.util.List;
11 import java.util.Map;
12 import java.util.Set;
13
14 import nl.tudelft.simulation.language.io.URLResource;
15
16
17
18
19
20
21
22
23
24
25
26
27 public class Convert
28 {
29 private static List<String> nodesEB = new ArrayList<>();
30
31 private static List<String> nodesWB = new ArrayList<>();
32
33 private static Map<Coordinate, String> coordinateMap = new HashMap<>();
34
35 private static Map<String, Integer> nodeMaxLanesMap = new HashMap<>();
36
37 private static List<String> linksEB = new ArrayList<>();
38
39 private static Set<String> mainNodes = new HashSet<>();
40
41 private static List<String> linksWB = new ArrayList<>();
42
43 private static List<List<Object>> linesEB = new ArrayList<>();
44
45 private static List<List<Object>> linesWB = new ArrayList<>();
46
47 private static List<Object> parseLine(final String line)
48 {
49 String l = line;
50 l = l.replaceAll("\n", "");
51 l = l.replaceAll("\r", "");
52 List<Object> out = new ArrayList<>();
53 while (l.length() > 0)
54 {
55 if (l.charAt(0) == '[')
56 {
57 int p = l.indexOf("]", 1);
58 out.add(parseLine(l.substring(1, p)));
59 l = l.substring(p + 2).trim();
60 }
61 else if (l.charAt(0) == '"')
62 {
63 int p = l.indexOf("\"", 1);
64 out.add(l.substring(1, p));
65 l = l.substring(p + 2).trim();
66 }
67 else
68 {
69 int p = l.indexOf(",");
70 if (p == -1)
71 {
72 out.add(l.trim());
73 l = "";
74 }
75 else
76 {
77 out.add(l.substring(0, p));
78 l = l.substring(p + 1).trim();
79 }
80 }
81 }
82 return out;
83 }
84
85 private static void parseAllLines(final String[] lines, final String dir, final List<List<Object>> linesList)
86 {
87 boolean first = true;
88 for (String line : lines)
89 {
90 if (first)
91 {
92 first = false;
93 continue;
94 }
95 List<Object> list = parseLine(line);
96 linesList.add(list);
97 }
98 }
99
100 private static void writeNodes(final List<List<Object>> linesList, final String dir, final List<String> nodes)
101 {
102 for (List<Object> line : linesList)
103 {
104 writeNodes(line, dir, nodes, 0, 1);
105 if (line.get(5).toString().equalsIgnoreCase("true"))
106 {
107 writeNodes(line, dir, nodes, 11, 12);
108 }
109 if (line.get(6).toString().equalsIgnoreCase("true"))
110 {
111 writeNodes(line, dir, nodes, 16, 17);
112 }
113 }
114 System.out.println();
115 }
116
117 private static void writeNodes(final List<Object> line, final String dir, final List<String> nodes, final int xi,
118 final int yi)
119 {
120 List<String> xc = (List<String>) line.get(xi);
121 List<String> yc = (List<String>) line.get(yi);
122
123 Coordinate cf = new Coordinate(Double.parseDouble(xc.get(0)), Double.parseDouble(yc.get(0)));
124 String sf = "N" + (nodes.size() + 1) + dir;
125 if (!coordinateMap.keySet().contains(cf))
126 {
127 System.out.println("<NODE NAME=" + "\"" + sf + "\" COORDINATE=\"" + cf + "\" />");
128 nodes.add(sf);
129 coordinateMap.put(cf, sf);
130 }
131 if (!nodeMaxLanesMap.containsKey(sf))
132 {
133 nodeMaxLanesMap.put(sf, 0);
134 }
135
136 Coordinate cl = new Coordinate(Double.parseDouble(xc.get(xc.size() - 1)), Double.parseDouble(yc.get(yc.size() - 1)));
137 String sl = "N" + (nodes.size() + 1) + dir;
138 if (!coordinateMap.keySet().contains(cl))
139 {
140 System.out.println("<NODE NAME=" + "\"" + sl + "\" COORDINATE=\"" + cl + "\" />");
141 nodes.add(sl);
142 coordinateMap.put(cl, sl);
143 }
144 if (!nodeMaxLanesMap.containsKey(sl))
145 {
146 nodeMaxLanesMap.put(sl, 0);
147 }
148 }
149
150 private static void writeLinks(final List<List<Object>> linesList, final String dir, final List<String> links)
151 {
152 for (List<Object> line : linesList)
153 {
154 writeLinks(line, dir, links, 0, 1, 2, 0.0, 0.0);
155 if (line.get(5).toString().equalsIgnoreCase("true"))
156 {
157 writeLinks(line, dir, links, 11, 12, 14, 0.0, -3.5);
158 }
159 if (line.get(6).toString().equalsIgnoreCase("true"))
160 {
161 writeLinks(line, dir, links, 16, 17, 19, -3.5, 0.0);
162 }
163 }
164 }
165
166 private static void writeLinks(final List<Object> line, final String dir, final List<String> links, final int xi,
167 final int yi, final int li, final double startOffset, final double endOffset)
168 {
169 List<String> xc = (List<String>) line.get(xi);
170 List<String> yc = (List<String>) line.get(yi);
171
172 Coordinate cf = new Coordinate(Double.parseDouble(xc.get(0)), Double.parseDouble(yc.get(0)));
173 Coordinate cl = new Coordinate(Double.parseDouble(xc.get(xc.size() - 1)), Double.parseDouble(yc.get(yc.size() - 1)));
174
175 String nodef = coordinateMap.get(cf);
176 String nodel = coordinateMap.get(cl);
177
178 String linkName = "L" + (links.size() + 1) + dir;
179 int nrLanes = Integer.parseInt((String) line.get(li));
180 int offset = xi == 0 ? Integer.parseInt((String) line.get(li + 1)) : nrLanes;
181
182 nodeMaxLanesMap.put(nodef, Math.max(nodeMaxLanesMap.get(nodef), nrLanes));
183 nodeMaxLanesMap.put(nodel, Math.max(nodeMaxLanesMap.get(nodel), nrLanes));
184
185 if (xi == 0)
186 {
187 mainNodes.add(nodef);
188 mainNodes.add(nodel);
189 }
190
191 System.out.print("<LINK NAME=\"" + linkName + "\" NODESTART=\"" + nodef + "\" NODEEND=\"" + nodel + "\" ROADLAYOUT=\"");
192 if (nrLanes - offset == 0)
193 {
194 System.out.print((nrLanes == 1) ? "HW1\"" : (nrLanes == 2) ? "HW2\"" : (nrLanes == 3) ? "HW3\"" : "HW4\"");
195 }
196 else
197 {
198 System.out.print(
199 (nrLanes == 1) ? "HW1\"" : (nrLanes == 2) ? "HW2AFSLAG\"" : (nrLanes == 3) ? "HW3AFSLAG\"" : "HW4AFSLAG\"");
200 }
201 if (startOffset != 0.0)
202 {
203 System.out.print(" OFFSETSTART=\"" + startOffset + " m\"");
204 }
205 if (endOffset != 0.0)
206 {
207 System.out.print(" OFFSETEND=\"" + endOffset + " m\"");
208 }
209 System.out.println(">");
210 if (xc.size() > 2)
211 {
212 System.out.print("<POLYLINE INTERMEDIATEPOINTS=\"");
213 boolean first = true;
214 for (int i = 1; i < xc.size() - 1; i++)
215 {
216 System.out.print((first ? "" : " ") + "(" + xc.get(i) + "," + yc.get(i) + ")");
217 first = false;
218 }
219 System.out.println("\" />");
220 }
221 else
222 {
223 System.out.println("<STRAIGHT />");
224 }
225
226 if (linkName.equals("L2EB"))
227 {
228 System.out.println("<GENERATOR LANE=\"A1\" POSITION=\"20m\" GTU=\"CAR\" IAT=\"EXPO(5) s\" "
229 + "INITIALSPEED=\"CONST(0.0) m/s\" GTUCOLORER=\"ID\" ROUTE=\"RouteEB\" />");
230 System.out.println("<GENERATOR LANE=\"A2\" POSITION=\"20m\" GTU=\"CAR\" IAT=\"EXPO(5) s\" "
231 + "INITIALSPEED=\"CONST(0.0) m/s\" GTUCOLORER=\"ID\" ROUTE=\"RouteEB\" />");
232 }
233
234 if (linkName.equals("L36EB"))
235 {
236 System.out.println("<SINK LANE=\"A1\" POSITION=\"200m\" />");
237 System.out.println("<SINK LANE=\"A2\" POSITION=\"200m\" />");
238 }
239
240 if (linkName.equals("L2WB"))
241 {
242 System.out.println("<GENERATOR LANE=\"A1\" POSITION=\"20m\" GTU=\"CAR\" IAT=\"EXPO(5) s\" "
243 + "INITIALSPEED=\"CONST(0.0) m/s\" GTUCOLORER=\"ID\" ROUTE=\"RouteWB\" />");
244 System.out.println("<GENERATOR LANE=\"A2\" POSITION=\"20m\" GTU=\"CAR\" IAT=\"EXPO(5) s\" "
245 + "INITIALSPEED=\"CONST(0.0) m/s\" GTUCOLORER=\"ID\" ROUTE=\"RouteWB\" />");
246 }
247
248 if (linkName.equals("L34WB"))
249 {
250 System.out.println("<SINK LANE=\"A1\" POSITION=\"200m\" />");
251 System.out.println("<SINK LANE=\"A2\" POSITION=\"200m\" />");
252 }
253
254 System.out.println("</LINK>\n");
255 links.add(linkName);
256 }
257
258 private static void writeRoutes(final String routeName, final List<String> nodes, final boolean reverse)
259 {
260 System.out.print("<ROUTE NAME=\"" + routeName + "\" NODELIST=\"");
261 if (!reverse)
262 {
263 System.out.print(nodes.get(0));
264 for (int i = 1; i < nodes.size(); i++)
265 {
266 if (mainNodes.contains(nodes.get(i)))
267 {
268 System.out.print(" " + nodes.get(i));
269 }
270 }
271 }
272 else
273 {
274 System.out.print(nodes.get(nodes.size() - 1));
275 for (int i = nodes.size() - 2; i >= 0; --i)
276 {
277 if (mainNodes.contains(nodes.get(i)))
278 {
279 System.out.print(" " + nodes.get(i));
280 }
281 }
282 }
283 System.out.println("\" />");
284 }
285
286
287
288
289
290
291 public static void main(final String[] args) throws IOException, URISyntaxException
292 {
293 String a58EB = new String(Files.readAllBytes(Paths.get(URLResource.getResource("/A58_EB.txt").toURI())));
294 String[] eb = a58EB.split("\n");
295 String a58WB = new String(Files.readAllBytes(Paths.get(URLResource.getResource("/A58_WB.txt").toURI())));
296 String[] wb = a58WB.split("\n");
297
298 parseAllLines(eb, "EB", linesEB);
299 parseAllLines(wb, "WB", linesWB);
300
301 writeNodes(linesEB, "EB", nodesEB);
302 writeNodes(linesWB, "WB", nodesWB);
303
304 writeLinks(linesEB, "EB", linksEB);
305 writeLinks(linesWB, "WB", linksWB);
306
307 writeRoutes("RouteEB", nodesEB, false);
308 writeRoutes("RouteWB", nodesWB, false);
309 System.out.println();
310 }
311
312
313 private static class Coordinate
314 {
315 final double x, y;
316
317
318
319
320
321 public Coordinate(double x, double y)
322 {
323 super();
324 this.x = x;
325 this.y = y;
326 }
327
328
329 @Override
330 public int hashCode()
331 {
332 final int prime = 31;
333 int result = 1;
334 long temp;
335 temp = Double.doubleToLongBits(this.x);
336 result = prime * result + (int) (temp ^ (temp >>> 32));
337 temp = Double.doubleToLongBits(this.y);
338 result = prime * result + (int) (temp ^ (temp >>> 32));
339 return result;
340 }
341
342
343 @Override
344 public boolean equals(Object obj)
345 {
346 if (this == obj)
347 return true;
348 if (obj == null)
349 return false;
350 if (getClass() != obj.getClass())
351 return false;
352 Coordinate other = (Coordinate) obj;
353 if (Double.doubleToLongBits(this.x) != Double.doubleToLongBits(other.x))
354 return false;
355 if (Double.doubleToLongBits(this.y) != Double.doubleToLongBits(other.y))
356 return false;
357 return true;
358 }
359
360
361 @Override
362 public String toString()
363 {
364 return "(" + this.x + "," + this.y + ")";
365 }
366
367 }
368 }