Example #1
0
        public void Run()
        {
            try
            {
                List <RouteViewModel> routesViewModel = ReadAndSerializer <RouteViewModel>(GetPathFile("trechos.txt"));
                List <OrderViewModel> ordersViewModel = ReadAndSerializer <OrderViewModel>(GetPathFile("encomendas.txt"));

                var routes = _routeService.CreateListRoutes(routesViewModel);

                var paths = ordersViewModel.Select(order =>
                {
                    var cityOrigin      = _cityService.FindByCode(order.CityOrigin);
                    var cityDestination = _cityService.FindByCode(order.CityDestination);
                    return(_shortestPathFinder.FindShortestPath(cityOrigin, cityDestination, s => GetNeighbors(s, routes)));
                });

                var textPaths = _textSerializer.SerializeList(paths);
                _fileService.WriteAllText(GetPathFile("rotas.txt"), textPaths);

                _logger.LogDebug("{paths}", textPaths);
            }
            catch (Exception e)
            {
                _logger.LogError(e, e.Message);
                _fileService.WriteAllText(GetPathFile("rotas.txt"), "Could not calculate directions. " + e.Message);
            }
        }
Example #2
0
        private BestRouteInfo BestRoute(string from, string to, string fileName = null)
        {
            var airports    = string.IsNullOrEmpty(fileName) ? _airportRepository.GetAll() : _airportRepository.GetAllFromFile(fileName);
            var airportFrom = airports.SingleOrDefault(a => a.Name.Equals(from));
            var airportTo   = airports.SingleOrDefault(a => a.Name.Equals(to));

            if (airportFrom == null || airportTo == null)
            {
                throw new AirportsFromOrToNotExist();
            }
            return(_businessLogic.FindShortestPath(airportFrom, airportTo));
        }
        public string[] ListPath(string startWord, string endWord, string[] dictionary)
        {
            var inputWordLength = startWord.Length;

            _graphBuilder.BuildWordGraph(inputWordLength, dictionary);

            var startWordGraphNode = _graphBuilder.GetNodeByWord(startWord);
            var endWordGraphNode   = _graphBuilder.GetNodeByWord(endWord);

            var path = _shortestPathFinder.FindShortestPath(startWordGraphNode, endWordGraphNode);

            return(path.ToArray());
        }
Example #4
0
        /// <summary>
        /// Process the files, load the RoadSystem then find the shortest path and put the result into ResultItem
        /// If there is any error then the ResultItem.HasError will be true and ResultItem.Result will be error message
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="finder"></param>
        /// <returns></returns>
        private ResultItem ProcessFile(string filePath, IShortestPathFinder finder)
        {
            ResultItem item = null;
            string filename = Path.GetFileName(filePath);
            try
            {
                // Load RoadSystem
                RoadSystem system = RoadSystem.LoadFromXml(filePath);

                // Find the shortest path
                var result = finder.FindShortestPath(system);

                // Create ResultItem from the result
                if (result.Count() == 0)
                {
                    item = new ResultItem
                    {
                        Filename = filename,
                        Result = "No path found"
                    };
                }
                else
                {
                    item = new ResultItem
                    {
                        Filename = filename,
                        Result = String.Join(", ", result.Select(node => node.ID.ToString()).ToArray())
                    };
                }
            }
            catch (LoadRoadSystemException ex)
            {
                // Create ResultItem from error
                item = new ResultItem
                {
                    Filename = filename,
                    HasError = true,
                    Result = ex.Message
                };
            }

            // Return ResultItem
            return item;
        }