/// <summary> /// Performs a search to find the optimal path between two points according to the search option /// </summary> /// <param name="origin">The origin point</param> /// <param name="destination">The destination point</param> /// <param name="searchParameters">The options needed by the search algorithm</param> /// <param name="ct">Cancellation token to stop the delete of the route</param> /// <returns></returns> public async Task<Results<Point>> SearchRoutePointsAsync(Guid origin, Guid destination, SearchAlgorithmParameters searchParameters, CancellationToken ct) { if (origin == Guid.Empty) { throw new ArgumentException(Resources.InvalidOriginPointId); } if (destination == Guid.Empty) { throw new ArgumentException(Resources.InvalidDestinationPointId); } var taskResult = await Task.Run(() => SearchShortestPath(origin, destination, searchParameters), ct); if (taskResult != null) { var totalDistance = taskResult.TotalDistance; var items = new List<PointEntity>(); if (taskResult.RouteVertex != null && taskResult.RouteVertex.Any()) { items = taskResult.RouteVertex.Select(vertex => _context.Points.FirstOrDefault(point => point.Id == vertex.NodeId)).ToList(); } return new Results<Point> { Items = Mapper.Map<IEnumerable<Point>>(items), TotalSize = totalDistance }; } return null; }
private SearchAlgorithmResult SearchShortestPath(Guid origin, Guid destination, SearchAlgorithmParameters searchParameters) { var graph = new Graph(_context); graph.PopulateGraph(searchParameters.SearchOptions); var search = SearchAlgorithmFactory.GetSearchAlgorithm(searchParameters.SearchAlgorithm); return search.Search(graph, origin, destination, searchParameters.SearchOptionPath); }
public async Task <IActionResult> SearchPathRouteAsync(Guid originPointId, Guid destinationPointId, SearchAlgorithmParameters searchParameters, CancellationToken ct) { if (originPointId == Guid.Empty) { return(BadRequest(new ApiError(Resources.InvalidParameters, Resources.InvalidOriginPointId))); } if (destinationPointId == Guid.Empty) { return(BadRequest(new ApiError(Resources.InvalidParameters, Resources.InvalidDestinationPointId))); } if (!ModelState.IsValid) { return(BadRequest(new ApiError(ModelState))); } var originPoint = await _pointsService.GetPointByIdAsync(originPointId, ct); if (originPoint == null) { return(BadRequest(new ApiError(Resources.InvalidParameters, Resources.InvalidOriginPointId))); } var destinationPoint = await _pointsService.GetPointByIdAsync(destinationPointId, ct); if (destinationPoint == null) { return(BadRequest(new ApiError(Resources.InvalidParameters, Resources.InvalidDestinationPointId))); } var points = await _routesService.SearchRoutePointsAsync(originPointId, destinationPointId, searchParameters, ct); if (points?.Items == null || !points.Items.Any()) { return(NotFound()); } var collection = Collection <Point> .Create <PointsResponse>(Link.ToCollection(nameof(PointsController.GetPointsAsync)), points.Items.ToArray(), points.TotalSize); return(Ok(collection)); }