Ejemplo n.º 1
0
        /// <summary>
        /// Gets the routes.
        /// </summary>
        /// <param name="startedCityId">The started city identifier.</param>
        /// <param name="destinationCityId">The destination city identifier.</param>
        public List<string> BuildRoutes(int startedCityId, int destinationCityId)
        {
            _roads = new List<string>();

            if (startedCityId == destinationCityId || startedCityId == 0 || destinationCityId == 0)
            {
                return new List<string>();
            }

            var beginPointId = startedCityId < destinationCityId ? startedCityId : destinationCityId;

            var endPointId = destinationCityId > startedCityId ? destinationCityId : startedCityId;

            var pointTree = new PointTree
            {
                ParentId = beginPointId,
                ChildPointIds = _routeRepository.GetAllRelationsIds(beginPointId)
            };

            Build(string.Empty, ref pointTree, endPointId);

            return _roads;
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Build the specified trek.
        /// </summary>
        /// <param name="trek">The string formatted trek.</param>
        /// <param name="step">The <see cref="PointTree"/> instance.</param>
        /// <param name="destinationCityId">The destination city identifier.</param>
        private void Build(string trek, ref PointTree step, long destinationCityId)
        {
            trek = string.IsNullOrEmpty(trek) ? step.ParentId.ToString(CultureInfo.InvariantCulture) : string.Format("{0}-{1}", trek, step.ParentId);

            if (step.ParentId == destinationCityId)
            {
                if (_roads.Count(s => s == trek) == 0)
                {
                    _roads.Add(trek);

                    SaveRoute(trek);
                }
            }
            else
            {
                if (CheckTrekDepth(trek))
                {

                    while (step.ChildPointIds.Count != 0)
                    {
                        var nexPointId = step.ChildPointIds.First();

                        var newLevel = new PointTree
                        {
                            ParentId = nexPointId,
                            ChildPointIds = GetRelations(trek, nexPointId)
                        };

                        step.ChildPointIds.Remove(nexPointId);

                        Build(trek, ref newLevel, destinationCityId);
                    }
                }
            }
        }