Fhnw.Ecnf.RoutePlanner.RoutePlannerLib.Routes.ReadRoutes C# (CSharp) Method

ReadRoutes() public method

Reads a list of links from the given file. Reads only links where the cities exist.
public ReadRoutes ( string filename ) : int
filename string name of links file
return int
        public int ReadRoutes(string filename)
        {
            int routesRead = 0;

            // write log 
            traceSource.TraceEvent(TraceEventType.Information, 1, "ReadRoutes started");

            try
            {

                using (TextReader reader = new StreamReader(filename))
                {
                    foreach (string[] linkAsString in reader.GetSplittedLines('\t'))

                    {
                        City city1 = cities.FindCity(linkAsString[0]);
                        City city2 = cities.FindCity(linkAsString[1]);

                        // only add links, where the cities are found 
                        if ((city1 != null) && (city2 != null))
                        {
                            routes.Add(new Link(city1, city2, city1.Location.Distance(city2.Location),
                                                TransportModes.Rail));
                            routesRead++;
                        }
                    }
                    // write log 
                    traceSource.TraceEvent(TraceEventType.Information, 1, "ReadRoutes ended");
                }
            }
            catch (Exception e)
            {
                traceSource.TraceEvent(TraceEventType.Critical, 2, "FileNotFound: " + e.StackTrace.ToString());
            }
                
            return routesRead;

        }

Usage Example

        public async Task TestFindShortestRouteBetweenAsyncProgress()
        {
            var cities = new Cities();
            cities.ReadCities(CitiesTestFile);

            var routes = new Routes(cities);
            routes.ReadRoutes(LinksTestFile);

            // do synchronous execution
            var linksExpected = routes.FindShortestRouteBetween("Basel", "Zürich", TransportMode.Rail);

            // do asynchronous execution
            var messages = new List<string>();
            var progress = new Progress<string>(msg => messages.Add(msg));
            var linksActual = await routes.FindShortestRouteBetweenAsync("Basel", "Zürich", TransportMode.Rail, progress);

            // let pending tasks execute
            await Task.Yield();

            // ensure that at least 5 progress calls are made
            Assert.IsTrue(messages.Distinct().Count()>=5, "Less than 5 distinct progress messages");

            // ensure that all progress messages end with " done"
            Assert.IsTrue(messages.All(m => m.EndsWith(" done")),
                string.Format("Progress message \"{0}\" does not end with \" done\"",
                    messages.FirstOrDefault(m => !m.EndsWith(" done"))));
        }
All Usage Examples Of Fhnw.Ecnf.RoutePlanner.RoutePlannerLib.Routes::ReadRoutes