Example #1
0
        public IGraph <T> FormGraph(LocationIdMap <ILocation> locations, List <ITransport> transports)
        {
            var graph = new Graph <T>();

            //Don't want to use reflections, so use an empty constructor and add location later on
            foreach (var location in locations)
            {
                graph.Nodes.Add(location.Key, new T());
            }

            foreach (var location in locations)
            {
                var locationNode = graph.Nodes[location.Key];
                locationNode.Location = location.Value;
                var transits = new List <ITransit>();

                foreach (var transport in transports.Where(trans => trans.IsDestinationReachable(location.Value.Id) && trans.HasNextLocation(location.Value)))
                {
                    var transit = new Transit();
                    transit.StartNode = locationNode;
                    transit.EndNode   = graph.Nodes[transport.GetNextLocation(transit.StartNode.Location.Id).Id];
                    transit.Transport = transport;

                    transits.Add(transit);
                }
                locationNode.Transits.AddRange(transits);
            }

            return(graph);
        }
Example #2
0
        public List <ITransport> ParseTransports(LocationIdMap <ILocation> locations)
        {
            var trafiTransports = JsonConvert.DeserializeObject <TrafiApiScheduleJSON>(_transportAndScheduleJSON).Schedules;
            var transports      = new List <ITransport>();

            foreach (var trafiTransport in trafiTransports)
            {
                foreach (var track in trafiTransport.Tracks)
                {
                    var successiveDestinations = new List <ILocation>();
                    foreach (var stop in track.Stops)
                    {
                        successiveDestinations.Add(locations[stop.StopId]);
                    }

                    //For now pick the first timetable.
                    var scheduleParser = new TrafiApiScheduleParser();
                    var schedule       = scheduleParser.Parse(track.Timetables[0].Times, track.Timetables[0].Durations, track.Stops);

                    transports.Add(new PublicTransport(trafiTransport.Name, track.Name, successiveDestinations, schedule));
                }
            }

            return(transports);
        }
Example #3
0
 /// <summary>
 /// Get the RowId from a LocationGuid
 /// </summary>
 /// <param name="guid"></param>
 /// <param name="defaultValue"></param>
 /// <returns></returns>
 public int IdFor(Guid?guid, int defaultValue = 0)
 {
     if (!guid.HasValue)
     {
         return(defaultValue);
     }
     if (LocationIdMap.ContainsKey(guid.Value))
     {
         return(LocationIdMap[guid.Value]);
     }
     return(defaultValue);
 }
Example #4
0
        public LocationIdMap <ILocation> ParseLocationsWithIds()
        {
            var stops     = JsonConvert.DeserializeObject <List <TrafiApiStopJSON> >(_locationsJSON);
            var locations = new LocationIdMap <ILocation>();

            foreach (var stop in stops)
            {
                locations.Add(stop.Id, new Location(stop.Id, stop.Name, stop.Coordinate.Lng, stop.Coordinate.Lat));
            }

            return(locations);
        }
Example #5
0
        // Should probably find a nice library that will do a better job than this quick and messy thing.
        public string ReadStopID(LocationIdMap <ILocation> availableStops)
        {
            var stopName = ReadStop().ToLower();

            while (true)
            {
                var similarStops   = new LocationIdMap <ILocation>();
                var containedStops = new LocationIdMap <ILocation>();

                foreach (var availableStop in availableStops)
                {
                    var distance = Fastenshtein.Levenshtein.Distance(stopName, availableStop.Value.Alias.ToLower());

                    //Levenshtein distance is zero, thus the strings are equal
                    if (distance == 0)
                    {
                        return(availableStop.Key);
                    }

                    if (availableStop.Value.Alias.ToLower().Contains(stopName) || stopName.Contains(availableStop.Value.Alias.ToLower()))
                    {
                        containedStops.Add(availableStop.Key, availableStop.Value);
                    }
                    else if (distance <= 5)
                    {
                        similarStops.Add(availableStop.Key, availableStop.Value);
                    }
                }

                var potentialStopIDs = new List <string>(containedStops.Keys.ToList());
                potentialStopIDs.AddRange(similarStops
                                          .OrderBy(x => Fastenshtein.Levenshtein.Distance(stopName, x.Value.Alias))
                                          .Select(x => x.Key)
                                          .ToList());

                System.Console.WriteLine("Couldn't find the stop you entered.");
                System.Console.WriteLine("Here are some similar sounding stops.");
                for (int i = 0; i < potentialStopIDs.Count && i < STOP_CHOICE_AMOUNT_LIMIT; i++)
                {
                    System.Console.WriteLine(i + ". " + potentialStopIDs[i] + " " + availableStops[potentialStopIDs[i]].Alias);
                }
                System.Console.Write("Enter a number from the list above, or try again with a new stop name: ");

                stopName = System.Console.ReadLine();
                int stopNumber;
                if (Int32.TryParse(stopName, out stopNumber))
                {
                    return(potentialStopIDs[stopNumber]);
                }
            }
        }
Example #6
0
        public PublicTransport(string alias, string trackName, List <ILocation> successiveDestinations, Schedule schedule)
        {
            Alias     = alias;
            TrackName = trackName;
            _schedule = schedule;

            _successiveDestinations = successiveDestinations;
            _destinations           = new LocationIdMap <ILocation>();
            _destinationIndex       = new LocationIdMap <int>();

            for (int i = 0; i < _successiveDestinations.Count; i++)
            {
                _destinations.TryAdd(_successiveDestinations[i].Id, _successiveDestinations[i]);
                _destinationIndex.TryAdd(_successiveDestinations[i].Id, i);
            }
        }
Example #7
0
 public Graph()
 {
     Nodes = new LocationIdMap <T>();
 }