private bool TryGetNode(List <RetStop> retStops, RETRoute cur, int j, out AStarNode result) { try { RetStop curRetStop = retStops.Find(rs => rs.Code == cur.Stops[j].Id); Stop nodeId = stops.Find(s => s.Name == curRetStop.Name); result = map_all.Nodes.Find(n => n.Id == nodeId); return(result != null); } catch { result = null; return(false); } }
private static RETRoute ReadRouteLine(string line) { CheckNullLine(line, "route"); LoggedException.RaiseIf(line[0] != '#' || line[2] != ',' || line[9] != ',' || line[13] != ',' || line[15] != ',' || line.Length != 21, nameof(PASReader), "Invalid route line format"); RETRoute result = new RETRoute { CompanyId = int.Parse(line.Substring(3, 5)), LineNum = int.Parse(line.Substring(10, 2)), Direction = (Direction)Convert.ToInt32(line[14]), RouteNum = int.Parse(line.Substring(16, 4)) }; result.SetTypeFromChar(line[1]); return(result); }
private void InitializeAStartMaps() { map_all = new AStarMap(); Log.Verbose(nameof(AStar), "Started generating map, this may take a while"); Stopwatch sw = Stopwatch.StartNew(); for (int i = 0; i < stationsCodesRotterdam.Count; i++) { Station cur = stations.Find(s => s.Code == stationsCodesRotterdam[i]); AStarNode node = new AStarNode(cur.Position) { Id = cur }; map_all.Nodes.Add(node); } for (int i = 0; i < stops.Count; i++) { Stop cur = stops[i]; AStarNode node = new AStarNode(cur.Position) { Id = cur }; map_all.Nodes.Add(node); } List <RETRoute> retRoutes = PASReader.ReadRoutesFromFile("RET.PAS"); List <RetStop> retStops = CSVReader.GetRetStopsFromFile("RET.HLT"); for (int i = 0; i < retRoutes.Count; i++) { RETRoute cur = retRoutes[i]; Log.Debug(nameof(AStar), $"Adding route: {cur}"); for (int j = 0; j < cur.Stops.Count; j++) { AStarNode curNode; if (!TryGetNode(retStops, cur, j, out curNode)) { continue; } if (j > 0) { AStarNode prevNode; if (!TryGetNode(retStops, cur, j - 1, out prevNode)) { continue; } if (!curNode.Adjason.Contains(prevNode)) { curNode.Adjason.Add(prevNode); } } if (j < cur.Stops.Count - 1) { AStarNode nextNode; if (!TryGetNode(retStops, cur, j + 1, out nextNode)) { continue; } if (!curNode.Adjason.Contains(nextNode)) { curNode.Adjason.Add(nextNode); } } } } map_all.Nodes.RemoveAll(n => n.Adjason.Count == 0); sw.Stop(); Log.Verbose(nameof(AStar), $"Finished generating map, took: {sw.Elapsed}"); }
public static List <RETRoute> ReadRoutesFromFile(string fileName) { fileName = $"{Settings.Default.DataDirectory}{fileName}"; List <RETRoute> result = new List <RETRoute>(); curLine = 0; LoggedException.RaiseIf(!fileName.EndsWith(".PAS"), nameof(PASReader), $"Cannot open file with extension {Path.GetExtension(fileName)}, supply .pas file"); try { using (StreamReader sr = new StreamReader(fileName)) { Log.Verbose(nameof(CSVReader), $"Started parsing file: '{Path.GetFileName(fileName)}'"); RETRoute cur = null; string line; while ((line = sr.ReadLine()) != null) { ++curLine; try { switch (line[0]) { case ('!'): CheckFormat(line); break; case ('&'): CheckValidity(line); break; case ('#'): cur = ReadRouteLine(line); break; case ('-'): Log.Debug(nameof(PASReader), "Skipped day code line (Useless)"); ReadDayCodeLine(line); break; case ('>'): if (cur == null) { Log.Error(nameof(PASReader), $"Floating stop at line: {curLine} (Ignored)"); } else if (cur.Stops.Count > 0) { Log.Error(nameof(PASReader), $"Second start stop at line: {curLine} (Ignored)"); } else { cur.Stops.Add(ReadStopStartLine(line)); } break; case ('.'): if (cur == null) { Log.Error(nameof(PASReader), $"Floating stop at line: {curLine} (Ignored)"); } else if (cur.Stops.Count < 1) { Log.Error(nameof(PASReader), $"Intermediate stop with no start at line: {curLine} (Ignored)"); } else { cur.Stops.Add(ReadStopLine(line)); } break; case ('+'): if (cur == null) { Log.Error(nameof(PASReader), $"Floating big stop at line: {curLine} (Ignored)"); } else if (cur.Stops.Count < 1) { Log.Error(nameof(PASReader), $"Big stop with no start at line: {curLine} (Ignored)"); } else { cur.Stops.Add(ReadBigStopLine(line)); } break; case ('<'): if (cur == null) { Log.Error(nameof(PASReader), $"Floating stop at line: {curLine} (Ignored)"); } else if (cur.Stops.Count < 1) { Log.Error(nameof(PASReader), $"End stop with no start at line: {curLine} (Ignored)"); } else { cur.Stops.Add(ReadStopEndLine(line)); } result.Add(cur); cur = null; break; default: Log.Warning(nameof(PASReader), $"Unknown line type at line: {curLine}"); break; } } catch (Exception e) { LoggedException.Raise(nameof(PASReader), $"An unhandled excpetion was raised at line: {curLine}", e); } } Log.Info(nameof(CSVReader), $"File contains {result.Count} readable entries"); } } catch (Exception e) { Log.Fatal(nameof(CSVReader), new FileLoadException($"An unhandled exception was raised during the processing of file: '{Path.GetFullPath(fileName)}'", e)); } Log.Verbose(nameof(CSVReader), $"Finished parsing file: '{Path.GetFileName(fileName)}'"); return(result); }