Exemplo n.º 1
0
        private static Route.Stop[] ReadStops(dynamic token)
        {
            var dArray = token.First;
            var stops  = new Route.Stop[dArray.Count];

            for (var i = 0; i < dArray.Count; i++)
            {
                var dItem = dArray[i];
                stops[i] = new Route.Stop();
                foreach (var token1 in dItem)
                {
                    switch (token1.Name)
                    {
                    case "Shape":
                        stops[i].Shape = token1.Value;
                        break;

                    case "Attributes":
                        stops[i].Attributes = ReadAttributes(token1);
                        break;

                    case "Coordinates":
                        stops[i].Coordinate = ReadCoordinate(token1);
                        break;
                    }
                }
            }

            return(stops);
        }
Exemplo n.º 2
0
        public Task <RideInfo> MakeBestRide(UserRideOffer offer, IEnumerable <UserRideRequest> requestsToMatch)
        {
            Route.Stop[] stops = new Route.Stop[requestsToMatch.Count() * 2 + 2];

            stops[0] = new Route.Stop(offer.RideOffer.Trip.Source);

            int i = 1;

            foreach (UserRideRequest r in requestsToMatch)
            {
                stops[i++] = new Route.Stop(r.RideRequest.Trip.Source, r.User.UserInfo, true);
                stops[i++] = new Route.Stop(r.RideRequest.Trip.Destination, r.User.UserInfo, false);
            }

            stops[i] = new Route.Stop(offer.RideOffer.Trip.Destination);

            return(Task.FromResult(new RideInfo(offer.User.UserInfo.UserId, offer.RideOffer.Car, new Route(stops))));
        }
Exemplo n.º 3
0
        /// <summary>
        /// Executes the actual run of the algorithm.
        /// </summary>
        protected override void DoRun()
        {
            if (_path.Count == 0)
            { // an empty path.
                this.ErrorMessage = "Path was empty.";
                this.HasSucceeded = false;
                return;
            }

            // check source.
            var source = _path[0];

            if (source != Constants.NO_VERTEX &&
                !_source.IsVertex(_routerDb, source))
            {
                this.ErrorMessage = "The source is a vertex but the source is not a match.";
                this.HasSucceeded = false;
                return;
            }

            // check target.
            var target = _path[_path.Count - 1];

            if (target != Constants.NO_VERTEX &&
                !_target.IsVertex(_routerDb, target))
            {
                this.ErrorMessage = "The target is a vertex but the target is not a match.";
                this.HasSucceeded = false;
                return;
            }

            // build the route.
            _shape     = new List <Coordinate>();
            _shapeMeta = new List <Route.Meta>();
            _branches  = new List <Route.Branch>();

            // add source.
            lock (_routerDb)
            {
                this.AddSource();

                if (_path.Count == 1)
                { // there is only the source/target location.
                    this.HasSucceeded = true;
                }
                else
                { // there are at least two points.
                    var i = 0;
                    for (i = 0; i < _path.Count - 2; i++)
                    {
                        this.Add(_path[i], _path[i + 1], _path[i + 2]);
                    }
                    this.Add(_path[i], _path[i + 1]);
                    this.HasSucceeded = true;
                }
            }

            // set stops.
            var stops = new Route.Stop[]
            {
                new Route.Stop()
                {
                    Shape      = 0,
                    Attributes = new AttributeCollection(_source.Attributes),
                    Coordinate = _source.Location()
                },
                new Route.Stop()
                {
                    Shape      = _shape.Count - 1,
                    Attributes = new AttributeCollection(_target.Attributes),
                    Coordinate = _target.Location()
                }
            };

            stops[0].Distance = 0;
            stops[0].Time     = 0;
            stops[1].Distance = _shapeMeta.Last().Distance;
            stops[1].Time     = _shapeMeta.Last().Time;

            // build route.
            _route = new Route()
            {
                Shape         = _shape.ToArray(),
                ShapeMeta     = _shapeMeta.ToArray(),
                Stops         = stops,
                Branches      = _branches.ToArray(),
                TotalDistance = _shapeMeta.Last().Distance,
                TotalTime     = _shapeMeta.Last().Time,
                Profile       = _profile.FullName
            };
        }