public async Task <ProfileSearch> SearchAsync(string text, string userId, string id, FeedType type) { var result = new ProfileSearch(); result.SearchText = text ?? ""; // At the moment this is only getting called for people posting as an admin or a content source so we are showing all profiles if (string.IsNullOrEmpty(text)) { if (type == FeedType.School) { result.SchoolProfiles = await _repo.Schools.Where(m => m.IsActive && m.Id == id).Map <Profile>().ToListAsync(); result.SportProfile = new List <Profile>(); result.TeamProfiles = await _repo.Teams.Where(m => m.IsActive && m.SchoolId == id).ProjectTo <Profile>(MapObjects()).ToListAsync(); } else if (type == FeedType.User) { var teamId = _repo.Users.Where(m => m.Id == userId).SelectMany(m => m.Atheletes).FirstOrDefault().TeamId; result.SchoolProfiles = new List <Profile>(); result.SportProfile = new List <Profile>(); result.TeamProfiles = await _repo.Teams.Where(m => m.IsActive && m.Id == teamId).ProjectTo <Profile>(MapObjects()).ToListAsync(); } else if (type == FeedType.ContentSource) { result.SchoolProfiles = await _repo.Schools.Where(m => m.IsActive).Map <Profile>().ToListAsync(); result.SportProfile = await _repo.Sports.Where(m => m.IsActive).ProjectTo <Profile>(MapSports()).ToListAsync(); result.TeamProfiles = await _repo.Teams.Where(m => m.IsActive && m.School.DateDeletedUtc == null && m.Sport.DateDeletedUtc == null).ProjectTo <Profile>(MapObjects()).ToListAsync(); } } else { var lower = text.ToLower(); if (type == FeedType.School) { result.SchoolProfiles = await _repo.Schools.Where(m => m.IsActive && m.Name.Contains(lower) && m.Id == id).Map <Profile>().ToListAsync(); result.SportProfile = new List <Profile>(); result.TeamProfiles = await _repo.Teams.Where(m => m.IsActive && (m.School.Name + " " + m.Sport.Name).Contains(lower) && m.SchoolId == id).ProjectTo <Profile>(MapObjects()).ToListAsync(); } else if (type == FeedType.User) { var teamId = _repo.Users.Where(m => m.Id == userId).SelectMany(m => m.Atheletes).FirstOrDefault().TeamId; result.SchoolProfiles = new List <Profile>(); result.SportProfile = new List <Profile>(); result.TeamProfiles = await _repo.Teams.Where(m => m.IsActive && (m.School.Name + " " + m.Sport.Name).Contains(lower) && m.Id == teamId).ProjectTo <Profile>(MapObjects()).ToListAsync(); } else if (type == FeedType.ContentSource) { result.SchoolProfiles = await _repo.Schools.Where(m => m.IsActive && m.Name.Contains(lower)).Map <Profile>().ToListAsync(); result.SportProfile = await _repo.Sports.Where(m => m.IsActive && m.Name.Contains(lower)).ProjectTo <Profile>(MapSports()).ToListAsync(); result.TeamProfiles = await _repo.Teams.Where(m => m.IsActive && m.School.DateDeletedUtc == null && m.Sport.DateDeletedUtc == null && (m.School.Name + " " + m.Sport.Name).Contains(lower)).ProjectTo <Profile>(MapObjects()).ToListAsync(); } } return(result); }
/// <summary> /// Searches the specified search. /// </summary> /// <param name="search">The search.</param> /// <param name="totalRecords">The total records.</param> /// <returns></returns> public static MetaStorageCollectionBase <Account> Search(ProfileSearch search, out int totalRecords) { Guid searchGuid = Guid.NewGuid(); // Perform order search totalRecords = search.Search(searchGuid); // Load results and return them back return(LoadSearchResults(searchGuid)); }
/// <summary> /// Finds the organizations. /// </summary> /// <param name="parameters">The parameters.</param> /// <param name="options">The options.</param> /// <param name="totalRecords">The total records.</param> /// <returns></returns> public Organization[] FindOrganizations(ProfileSearchParameters parameters, ProfileSearchOptions options, out int totalRecords) { ProfileSearch search = new ProfileSearch(this); search.SearchOptions = options; search.SearchParameters = parameters; MetaStorageCollectionBase <Organization> orgs = Organization.Search(search, out totalRecords); return(orgs.ToArray()); }
/// <summary> /// Finds the accounts. /// </summary> /// <param name="parameters">The parameters.</param> /// <param name="options">The options.</param> /// <param name="totalRecords">The total records.</param> /// <returns></returns> public Account[] FindAccounts(ProfileSearchParameters parameters, ProfileSearchOptions options, out int totalRecords) { ProfileSearch search = new ProfileSearch(this); search.SearchOptions = options; search.SearchParameters = parameters; MetaStorageCollectionBase <Account> accounts = Account.Search(search, out totalRecords); return(accounts.ToArray()); }
private void timerBusqueda_Tick(object sender, EventArgs e) { if (sTextoActual.Trim() == sTextoAnterior.Trim()) { if (TimerTiempoBase.ElapsedMilliseconds > 1000) { frmPreloading frm2 = new frmPreloading(this); int iTotal = ProfileSearch.LoadProfileSearchResult(sTextoActual, sTipoBusqueda, true); frm2.Show(); TxtContadorRegistros.Visible = true; TxtContadorRegistros.Text = "Se han encontrado un total de " + iTotal + " registros en su busqueda"; TimerTiempoBase.Stop(); TimerTiempoBase = new Stopwatch(); } } else { TimerTiempoBase.Start(); sTextoAnterior = sTextoActual; } }
/// <summary> /// Tries to calculate an earliest arrival route from stop1 to stop2. /// </summary> public Result <Route> TryEarliestArrival(DateTime departureTime, uint stop1, uint stop2, Func <uint, bool> useAgency) { var tripEnumerator = _transitDb.GetTripsEnumerator(); var transfersDb = _transitDb.GetTransfersDb(_transferProfile); var profileSearch = new ProfileSearch(_transitDb, departureTime, transfersDb, (t, day) => { if (tripEnumerator.MoveTo(t)) { if (useAgency(tripEnumerator.AgencyId)) { return(true); } } return(false); }); profileSearch.SetSourceStop(stop1, (uint)(departureTime - departureTime.Date).TotalSeconds); profileSearch.SetTargetStop(stop2, 0); profileSearch.Run(); if (!profileSearch.HasSucceeded) { return(new Result <Route>(profileSearch.ErrorMessage, (message) => { return new RouteNotFoundException(message); })); } // generate route. var routeBuilder = new ProfileSearchRouteBuilder(profileSearch); routeBuilder.Run(); if (!routeBuilder.HasSucceeded) { return(new Result <Route>(routeBuilder.ErrorMessage, (message) => { return new RouteBuildFailedException(message); })); } return(new Result <Route>(routeBuilder.Route)); }
//---------------------------------------------------------------------- /// <summary> /// Search the SNAP system for matches to the provided criteria. /// </summary> public static ResultSet Do <ProfileType>( ITimer timerContext, IEnumerable <string> nameSearchNoiseWords, Dictionary <string, string> consumerProperties, Comparison <Result> sortOrder, AddressParserResult searchLocale, int desiredResultCount, int maxResultCount, List <int> externalPartyMembershipTypeIds, IntSet <PartyId> profileSpecificParties, IntSet <ProfileId> profileSpecificiProfiles, MembershipSearch membershipSearch, PartySearch partySearch, ProfileSearch <ProfileType> profileSearch, string featuredProfileTemplateLabel) where ProfileType : ISearch, new() { Timer t = new Timer(timerContext, "Search.Do() - implicit"); try { return(InnerSearch <ProfileType>(t, nameSearchNoiseWords, consumerProperties, sortOrder, searchLocale, desiredResultCount, maxResultCount, externalPartyMembershipTypeIds, profileSpecificParties, profileSpecificiProfiles, membershipSearch, partySearch, profileSearch, featuredProfileTemplateLabel)); } finally { t.Stop(); } }
public void TestOneHopUnsuccessful() { // build dummy db. var db = new TransitDb(); db.AddStop(0, 0, 0); db.AddStop(1, 1, 1); db.AddTrip(0, 0, 0); db.AddConnection(0, 1, 0, 8 * 3600, 8 * 3600 + 10 * 60); db.SortConnections(DefaultSorting.DepartureTime, null); // run algorithm. var algorithm = new ProfileSearch(db, new DateTime(2017, 05, 10, 08, 30, 00), (profileId, day) => true); algorithm.SetSourceStop(0, 08 * 3600 + 30 * 60); algorithm.SetTargetStop(1, 0); algorithm.Run(); // test results. Assert.IsTrue(algorithm.HasRun); Assert.IsFalse(algorithm.HasSucceeded); }
void GetData() { var feedType = Post.ContentSourceId != null ? FeedType.ContentSource : Post.TeamId != null ? FeedType.Team : Post.SchoolId != null ? FeedType.School : FeedType.User; var apiTask = new ServiceApi().SearchProfiles(txtSearch.Text, Post.TeamId ?? Post.SchoolId, feedType); apiTask.HandleError(LoadingScreen); apiTask.OnSucess(response => { if (response.Result.SearchText == txtSearch.Text) { result = response.Result; source.Items = response.Result.TeamProfiles.Union(response.Result.SchoolProfiles).Union(response.Result.SportProfile).ToList(); tvProfiles.ReloadData(); LoadingScreen.Hide(); } }); }
void GetData() { var feedType = Post.ContentSourceId != null ? FeedType.ContentSource : Post.TeamId != null ? FeedType.Team : Post.SchoolId != null ? FeedType.School : FeedType.User; var apiTask = new ServiceApi().SearchProfiles(txtSearch.Text, Post.TeamId ?? Post.SchoolId, feedType); apiTask.HandleError(this); apiTask.OnSucess(this, response => { if (response.Result.SearchText == txtSearch.Text) { result = response.Result; adapter.Items = response.Result.TeamProfiles.Union(response.Result.SchoolProfiles).Union(response.Result.SportProfile).ToList(); adapter.NotifyDataSetChanged(); HideProgressDialog(); } }); }
//---------------------------------------------------------------------- private static ResultSet InnerSearch <ProfileType>( Timer t, IEnumerable <string> nameSearchNoiseWords, Dictionary <string, string> consumerProperties, Comparison <Result> sortOrder, AddressParserResult searchLocale, int desiredResultCount, int maxResultCount, List <int> externalPartyMembershipTypeIds, IntSet <PartyId> profileSpecificParties, IntSet <ProfileId> profileSpecificProfiles, MembershipSearch membershipSearch, PartySearch partySearch, ProfileSearch <ProfileType> profileSearch, string featuredProfileTemplateLabel) where ProfileType : ISearch, new() { if (null == profileSpecificProfiles) { profileSpecificProfiles = IntSet <ProfileId> .Universe; } if (null == profileSpecificParties) { profileSpecificParties = IntSet <PartyId> .Universe; } //Console.WriteLine(Snap.Cache.Cache.ProfileTemplate.Count); ISearch profileType = new ProfileType(); IntSet <PartyId> matchesByProfileType = null; try { if (String.IsNullOrEmpty(profileType.ProfileTemplateLabel)) { matchesByProfileType = IntSet <PartyId> .Universe; } else { matchesByProfileType = Snap.Cache.Cache.ProfileTemplate[profileType.ProfileTemplateLabel]; } } catch (KeyNotFoundException exc) { throw new ApplicationException( "Unknown ProfileTemplateLabel: " + profileType.ProfileTemplateLabel, exc); } IntSet <PartyId> matchesByMembership = IntSet <PartyId> .Universe; t.Measure("Search by membership(s)", delegate() { if (!ListX.IsEmpty(externalPartyMembershipTypeIds)) { matchesByMembership = membershipSearch(externalPartyMembershipTypeIds); } }); matchesByMembership = IntSet <PartyId> .Intersection(matchesByProfileType, matchesByMembership); ResultSet matchesByProfile = null; t.Measure("Search by Profile ('advanced' search)", delegate() { matchesByProfile = profileSearch( t, consumerProperties, profileSpecificProfiles); }); double nonGeoMatchAbsDensity = NonGeoMatchAbsDensity( matchesByMembership, matchesByProfile); ResultSet matchesByParty = ResultSet.Universe; t.Measure("Search by Party (geography,name)", delegate() { if (searchLocale == null) { matchesByParty = ResultSet.From(null, null, matchesByMembership); } else { matchesByParty = partySearch( t, nameSearchNoiseWords, searchLocale, desiredResultCount, maxResultCount, matchesByMembership, profileSpecificParties, nonGeoMatchAbsDensity, ref sortOrder); } }); ResultSet matches = null; t.Measure("Combine matches-by-party and matches-by-profile", delegate() { matches = ResultSet.Intersection(matchesByParty, matchesByProfile); }); return(FinalizeResultSet( t, sortOrder, desiredResultCount, matches, featuredProfileTemplateLabel)); }
public void TestTwoHopsOneTransferCloseStopsSuccessfulSkippedPseudo() { // build dummy db. var db = new TransitDb(); db.AddStop(0, 0, 0); db.AddStop(1, 1, 1); db.AddStop(2, 2, 2); db.AddStop(3, 3, 3); db.AddTrip(0, 0, 0); db.AddTrip(0, 0, 0); db.AddTrip(0, 0, 0); db.AddConnection(0, 1, 0, 8 * 3600, 8 * 3600 + 10 * 60); db.AddConnection(1, 2, 0, 8 * 3600 + 10 * 60, 8 * 3600 + 15 * 60); db.AddConnection(2, 3, 0, 8 * 3600 + 15 * 60, 8 * 3600 + 25 * 60); db.SortConnections(DefaultSorting.DepartureTime, null); // build dummy transfers db. var transfersDb = new TransfersDb(1024); transfersDb.AddTransfer(1, 2, 60); // this leads to a transfer time faster than the actual connection. // run algorithm. var departureTime = new DateTime(2017, 05, 10, 07, 30, 00); var algorithm = new ProfileSearch(db, departureTime, transfersDb, (profileId, day) => true); algorithm.SetSourceStop(0, 07 * 3600 + 30 * 60); algorithm.SetTargetStop(3, 0); algorithm.Run(); // test results. Assert.IsTrue(algorithm.HasRun); Assert.IsTrue(algorithm.HasSucceeded); // check arrival profile(s). var arrivalStops = algorithm.ArrivalStops; Assert.AreEqual(3, arrivalStops.Count); Assert.AreEqual(3, arrivalStops[2]); var arrivalProfiles = algorithm.ArrivalProfiles; Assert.AreEqual(3, arrivalProfiles.Count); Assert.AreEqual(08 * 3600 + 25 * 60, arrivalProfiles[2].Seconds); Assert.AreEqual(55 * 60, algorithm.Duration(2)); Assert.AreEqual(new DateTime(2017, 05, 10, 08, 25, 00), algorithm.ArrivalTime(2)); // check stop profiles. var connections = db.GetConnectionsEnumerator(DefaultSorting.DepartureTime); var profiles = algorithm.GetStopProfiles(3); Assert.AreEqual(3, profiles.Count); var profile = profiles[0]; Assert.IsTrue(profile.IsEmpty); profile = profiles[1]; Assert.IsTrue(profile.IsEmpty); profile = profiles[2]; Assert.AreEqual(2, profile.PreviousConnectionId); Assert.AreEqual((int)(departureTime - departureTime.Date).TotalSeconds + 55 * 60, profile.Seconds); profiles = algorithm.GetStopProfiles(2); Assert.AreEqual(4, profiles.Count); profile = profiles[0]; Assert.IsTrue(profile.IsEmpty); profile = profiles[1]; Assert.IsTrue(profile.IsEmpty); profile = profiles[2]; Assert.AreEqual(1, profile.PreviousConnectionId); Assert.AreEqual((int)(departureTime - departureTime.Date).TotalSeconds + 45 * 60, profile.Seconds); profile = profiles[3]; Assert.IsTrue(profile.IsTransfer); Assert.AreEqual(1, profile.PreviousStopId); Assert.AreEqual((int)(departureTime - departureTime.Date).TotalSeconds + 40 * 60 + 60, profile.Seconds); profiles = algorithm.GetStopProfiles(1); Assert.AreEqual(3, profiles.Count); profile = profiles[0]; Assert.IsTrue(profile.IsEmpty); profile = profiles[1]; Assert.IsTrue(profile.IsEmpty); profile = profiles[2]; Assert.AreEqual(0, profile.PreviousConnectionId); Assert.AreEqual((int)(departureTime - departureTime.Date).TotalSeconds + 40 * 60, profile.Seconds); profiles = algorithm.GetStopProfiles(0); Assert.AreEqual(1, profiles.Count); profile = profiles[0]; Assert.AreEqual(Itinero.Transit.Constants.NoConnectionId, profile.PreviousConnectionId); Assert.AreEqual((int)(departureTime - departureTime.Date).TotalSeconds, profile.Seconds); // build dummy transfers db. transfersDb = new TransfersDb(1024); transfersDb.AddTransfer(1, 2, 6 * 60); // this leads to a transfer time slower than the actual connection. // run algorithm. departureTime = new DateTime(2017, 05, 10, 07, 30, 00); algorithm = new ProfileSearch(db, departureTime, transfersDb, (profileId, day) => true); algorithm.SetSourceStop(0, 07 * 3600 + 30 * 60); algorithm.SetTargetStop(3, 0); algorithm.Run(); // test results. Assert.IsTrue(algorithm.HasRun); Assert.IsTrue(algorithm.HasSucceeded); // check arrival profile(s). arrivalStops = algorithm.ArrivalStops; Assert.AreEqual(3, arrivalStops.Count); Assert.AreEqual(3, arrivalStops[2]); arrivalProfiles = algorithm.ArrivalProfiles; Assert.AreEqual(3, arrivalProfiles.Count); Assert.AreEqual(08 * 3600 + 25 * 60, arrivalProfiles[2].Seconds); Assert.AreEqual(55 * 60, algorithm.Duration(2)); Assert.AreEqual(new DateTime(2017, 05, 10, 08, 25, 00), algorithm.ArrivalTime(2)); // check stop profiles. connections = db.GetConnectionsEnumerator(DefaultSorting.DepartureTime); profiles = algorithm.GetStopProfiles(3); Assert.AreEqual(3, profiles.Count); profile = profiles[0]; Assert.IsTrue(profile.IsEmpty); profile = profiles[1]; Assert.IsTrue(profile.IsEmpty); profile = profiles[2]; Assert.AreEqual(2, profile.PreviousConnectionId); Assert.AreEqual((int)(departureTime - departureTime.Date).TotalSeconds + 55 * 60, profile.Seconds); profiles = algorithm.GetStopProfiles(2); Assert.AreEqual(3, profiles.Count); profile = profiles[0]; Assert.IsTrue(profile.IsEmpty); profile = profiles[1]; Assert.IsTrue(profile.IsEmpty); profile = profiles[2]; Assert.AreEqual(1, profile.PreviousConnectionId); Assert.AreEqual((int)(departureTime - departureTime.Date).TotalSeconds + 45 * 60, profile.Seconds); profiles = algorithm.GetStopProfiles(1); Assert.AreEqual(3, profiles.Count); profile = profiles[0]; Assert.IsTrue(profile.IsEmpty); profile = profiles[1]; Assert.IsTrue(profile.IsEmpty); profile = profiles[2]; Assert.AreEqual(0, profile.PreviousConnectionId); Assert.AreEqual((int)(departureTime - departureTime.Date).TotalSeconds + 40 * 60, profile.Seconds); profiles = algorithm.GetStopProfiles(0); Assert.AreEqual(1, profiles.Count); profile = profiles[0]; Assert.AreEqual(Itinero.Transit.Constants.NoConnectionId, profile.PreviousConnectionId); Assert.AreEqual((int)(departureTime - departureTime.Date).TotalSeconds, profile.Seconds); var tripStatus = algorithm.GetTripStatus(0); Assert.AreEqual(2, tripStatus.Transfers); Assert.AreEqual(0, tripStatus.StopId); Assert.AreEqual(08 * 3600 + 00 * 60, tripStatus.DepartureTime); }
public void TestOneHopScheduled() { // build dummy db. var db = new TransitDb(); var schedule = db.AddSchedule(); db.AddScheduleEntry(schedule, new DateTime(2017, 01, 01), new DateTime(2018, 01, 01), DayOfWeek.Monday); db.AddStop(0, 0, 0); db.AddStop(1, 1, 1); db.AddTrip(schedule, 0, 0); db.AddConnection(0, 1, 0, 3600, 3600 + 40 * 60); db.SortConnections(DefaultSorting.DepartureTime, null); // run algorithm. var departureTime = new DateTime(2017, 05, 08, 00, 50, 00); var algorithm = new ProfileSearch(db, departureTime, db.GetIsTripPossibleFunc()); algorithm.SetSourceStop(0, 50 * 60); algorithm.SetTargetStop(1, 0); algorithm.Run(); // test results. Assert.IsTrue(algorithm.HasRun); Assert.IsTrue(algorithm.HasSucceeded); // check arrival profile(s). var arrivalStops = algorithm.ArrivalStops; Assert.AreEqual(3, arrivalStops.Count); Assert.AreEqual(1, arrivalStops[2]); var arrivalProfiles = algorithm.ArrivalProfiles; Assert.AreEqual(3, arrivalProfiles.Count); Assert.AreEqual(3600 * 01 + 40 * 60, arrivalProfiles[2].Seconds); Assert.AreEqual(50 * 60, algorithm.Duration(2)); Assert.AreEqual(new DateTime(2017, 05, 08, 01, 40, 00), algorithm.ArrivalTime(2)); // check stop profiles. var connections = db.GetConnectionsEnumerator(DefaultSorting.DepartureTime); var profiles = algorithm.GetStopProfiles(1); var profile = profiles[0]; Assert.IsTrue(profile.IsEmpty); profile = profiles[1]; Assert.IsTrue(profile.IsEmpty); profile = profiles[2]; Assert.AreEqual(0, profile.PreviousConnectionId); Assert.AreEqual((int)(departureTime - departureTime.Date).TotalSeconds + 50 * 60, profile.Seconds); connections.MoveTo(profile.PreviousConnectionId); Assert.AreEqual(0, connections.TripId); Assert.AreEqual(0, connections.DepartureStop); profiles = algorithm.GetStopProfiles(0); profile = profiles[0]; Assert.AreEqual(Itinero.Transit.Constants.NoConnectionId, profile.PreviousConnectionId); Assert.AreEqual((int)(departureTime - departureTime.Date).TotalSeconds, profile.Seconds); var tripStatus = algorithm.GetTripStatus(0); Assert.AreEqual(2, tripStatus.Transfers); Assert.AreEqual(0, tripStatus.StopId); Assert.AreEqual(3600, tripStatus.DepartureTime); // run algorithm. departureTime = new DateTime(2017, 05, 10, 00, 50, 00); algorithm = new ProfileSearch(db, departureTime, db.GetIsTripPossibleFunc()); algorithm.SetSourceStop(0, 50 * 60); algorithm.SetTargetStop(1, 0); algorithm.Run(); Assert.IsTrue(algorithm.HasRun); Assert.IsFalse(algorithm.HasSucceeded); }
/// <summary> /// Tries to calculate an earliest arrival route from stop1 to stop2. /// </summary> public override Result <Route> TryEarliestArrival(DateTime departureTime, RouterPoint sourcePoint, Profile sourceProfile, RouterPoint targetPoint, Profile targetProfile, EarliestArrivalSettings settings) { // get the get factor function. var sourceGetFactor = _router.GetDefaultGetFactor(sourceProfile); var targetGetFactor = _router.GetDefaultGetFactor(targetProfile); // create the profile search. var tripEnumerator = _db.TransitDb.GetTripsEnumerator(); var transfersDb = _db.TransitDb.GetTransfersDb(_transferProfile); var profileSearch = new ProfileSearch(_db.TransitDb, departureTime, transfersDb, _db.TransitDb.GetIsTripPossibleFunc()); // search for sources. var departureTimeSeconds = (uint)(departureTime - departureTime.Date).TotalSeconds; var sourceSearch = new ClosestStopsSearch(_db, sourceProfile, sourceGetFactor, sourcePoint, settings.MaxSecondsSource, false); sourceSearch.StopFound = (s, t) => { profileSearch.SetSourceStop(s, departureTimeSeconds + (uint)t); return(false); }; // search for targets. var targetSearch = new ClosestStopsSearch(_db, targetProfile, targetGetFactor, targetPoint, settings.MaxSecondsTarget, true); targetSearch.StopFound = (s, t) => { profileSearch.SetTargetStop(s, (uint)t); return(false); }; // create bidirectional helper if possible. SearchHelper helper = null; BidirectionalSearchHelper bidirectionalHelper = null; if (sourceProfile.Name == targetProfile.Name) { // profiles are the same. bidirectionalHelper = new BidirectionalSearchHelper( sourceSearch.Search, targetSearch.Search); targetSearch.WasEdgeFound = bidirectionalHelper.TargetWasFound; } else { // profiles are different but the source can still reach the destination. helper = new SearchHelper(_router.Db, sourceSearch.Search, sourceProfile, targetPoint); sourceSearch.WasEdgeFound = helper.SourceWasFound; } // run source search. sourceSearch.Run(); if (!sourceSearch.HasRun || !sourceSearch.HasSucceeded) { return(new Result <Route>("Searching for source stops failed.")); } // run target search. targetSearch.Run(); if (!targetSearch.HasRun || !targetSearch.HasSucceeded) { return(new Result <Route>("Searching for target stops failed.")); } // run actual profile search. profileSearch.Run(); if (!profileSearch.HasRun || !profileSearch.HasSucceeded) { return(new Result <Route>("No route found.")); } // build routes. var profileSearchRouteBuilder = new ProfileSearchRouteBuilder(profileSearch); profileSearchRouteBuilder.Run(); if (!profileSearchRouteBuilder.HasRun || !profileSearchRouteBuilder.HasSucceeded) { return(new Result <Route>(string.Format("Route could not be built: {0}.", profileSearchRouteBuilder.ErrorMessage))); } var sourceWeight = sourceSearch.GetWeight(profileSearchRouteBuilder.Stops[0]); var targetWeight = targetSearch.GetWeight(profileSearchRouteBuilder.Stops[profileSearchRouteBuilder.Stops.Count - 1]); var transitWeight = sourceWeight + profileSearchRouteBuilder.Duration + targetWeight; if (bidirectionalHelper != null && bidirectionalHelper.HasSucceeded) { // there is a direct route to the target. if (transitWeight > bidirectionalHelper.BestWeight) { if (!this.PreferTransit || bidirectionalHelper.BestWeight < this.PreferTransitThreshold) { // transit it not preferred or belof threshold. var path = bidirectionalHelper.GetPath(); return(new Result <Route>( CompleteRouteBuilder.Build(_router.Db, sourceProfile, sourcePoint, targetPoint, path))); } } } else if (helper != null && helper.HasSucceeded) { // there is a direct route to the target. if (transitWeight > helper.BestWeight) { if (!this.PreferTransit || bidirectionalHelper.BestWeight < this.PreferTransitThreshold) { // transit it not preferred or belof threshold. var path = helper.GetPath(); return(new Result <Route>( CompleteRouteBuilder.Build(_router.Db, sourceProfile, sourcePoint, targetPoint, path))); } } } // build source/target routes. var sourceRoute = sourceSearch.GetRoute(profileSearchRouteBuilder.Stops[0]); var targetRoute = targetSearch.GetRoute(profileSearchRouteBuilder.Stops[profileSearchRouteBuilder.Stops.Count - 1]); // concatenate it all. var route = sourceRoute.Concatenate(profileSearchRouteBuilder.Route); route = route.Concatenate(targetRoute); return(new Result <Route>(route)); }
public void TestTwoHopsOneTransferCloseStopsSuccessful() { // build dummy db. var db = new TransitDb(); db.AddStop(0, 0, db.StopAttributes.Add(new Attribute("name", "stop1"))); db.AddStop(1, 1, db.StopAttributes.Add(new Attribute("name", "stop2"))); db.AddStop(2, 2, db.StopAttributes.Add(new Attribute("name", "stop3"))); db.AddStop(3, 3, db.StopAttributes.Add(new Attribute("name", "stop4"))); db.AddTrip(0, 0, db.TripAttributes.Add(new Attribute("name", "trip1"))); db.AddTrip(0, 0, db.TripAttributes.Add(new Attribute("name", "trip2"))); db.AddConnection(0, 1, 0, 8 * 3600, 8 * 3600 + 10 * 60); db.AddConnection(2, 3, 1, 8 * 3600 + 15 * 60, 8 * 3600 + 25 * 60); db.SortConnections(DefaultSorting.DepartureTime, null); // build dummy transfers db. var transfersDb = new TransfersDb(1024); transfersDb.AddTransfer(1, 2, 100); // run algorithm. var departureTime = new System.DateTime(2017, 05, 10, 07, 30, 00); var algorithm = new ProfileSearch(db, departureTime, transfersDb, (profileId, day) => true); algorithm.SetSourceStop(0, 07 * 3600 + 30 * 60); algorithm.SetTargetStop(3, 0); algorithm.Run(); // build route. var routeBuilder = new ProfileSearchRouteBuilder(algorithm, false); routeBuilder.Run(); var route = routeBuilder.Route; Assert.IsNotNull(route); Assert.IsNotNull(route.Shape); Assert.AreEqual(6, route.Shape.Length); Assert.AreEqual(0, route.Shape[0].Latitude); Assert.AreEqual(0, route.Shape[0].Longitude); Assert.AreEqual(0, route.Shape[1].Latitude); Assert.AreEqual(0, route.Shape[1].Longitude); Assert.AreEqual(1, route.Shape[2].Latitude); Assert.AreEqual(1, route.Shape[2].Longitude); Assert.AreEqual(2, route.Shape[3].Latitude); Assert.AreEqual(2, route.Shape[3].Longitude); Assert.AreEqual(2, route.Shape[4].Latitude); Assert.AreEqual(2, route.Shape[4].Longitude); Assert.AreEqual(3, route.Shape[5].Latitude); Assert.AreEqual(3, route.Shape[5].Longitude); Assert.IsNotNull(route.Stops); Assert.AreEqual(6, route.Stops.Length); var stop = route.Stops[0]; Assert.IsNotNull(stop.Attributes); Assert.AreEqual(1, stop.Attributes.Count); Assert.IsTrue(stop.Attributes.Contains("name", "stop1")); Assert.AreEqual(0, stop.Shape); Assert.AreEqual(0, stop.Coordinate.Latitude); Assert.AreEqual(0, stop.Coordinate.Longitude); stop = route.Stops[1]; Assert.IsNotNull(stop.Attributes); Assert.AreEqual(1, stop.Attributes.Count); Assert.IsTrue(stop.Attributes.Contains("name", "stop1")); Assert.AreEqual(1, stop.Shape); Assert.AreEqual(0, stop.Coordinate.Latitude); Assert.AreEqual(0, stop.Coordinate.Longitude); stop = route.Stops[2]; Assert.IsNotNull(stop.Attributes); Assert.AreEqual(1, stop.Attributes.Count); Assert.IsTrue(stop.Attributes.Contains("name", "stop2")); Assert.AreEqual(2, stop.Shape); Assert.AreEqual(1, stop.Coordinate.Latitude); Assert.AreEqual(1, stop.Coordinate.Longitude); stop = route.Stops[3]; Assert.IsNotNull(stop.Attributes); Assert.AreEqual(1, stop.Attributes.Count); Assert.IsTrue(stop.Attributes.Contains("name", "stop3")); Assert.AreEqual(3, stop.Shape); Assert.AreEqual(2, stop.Coordinate.Latitude); Assert.AreEqual(2, stop.Coordinate.Longitude); stop = route.Stops[4]; Assert.IsNotNull(stop.Attributes); Assert.AreEqual(1, stop.Attributes.Count); Assert.IsTrue(stop.Attributes.Contains("name", "stop3")); Assert.AreEqual(4, stop.Shape); Assert.AreEqual(2, stop.Coordinate.Latitude); Assert.AreEqual(2, stop.Coordinate.Longitude); stop = route.Stops[5]; Assert.IsNotNull(stop.Attributes); Assert.AreEqual(1, stop.Attributes.Count); Assert.IsTrue(stop.Attributes.Contains("name", "stop4")); Assert.AreEqual(5, stop.Shape); Assert.AreEqual(3, stop.Coordinate.Latitude); Assert.AreEqual(3, stop.Coordinate.Longitude); Assert.IsNotNull(route.ShapeMeta); Assert.AreEqual(6, route.ShapeMeta.Length); var meta = route.ShapeMeta[0]; Assert.AreEqual(0, meta.Shape); Assert.IsNotNull(meta.Attributes); Assert.IsTrue(meta.Attributes.Contains(Itinero.Transit.Constants.TimeOfDayKey, ((07 * 3600) + (30 * 60)).ToInvariantString())); Assert.AreEqual(0, meta.Time); meta = route.ShapeMeta[1]; Assert.AreEqual(1, meta.Shape); Assert.IsNotNull(meta.Attributes); Assert.IsTrue(meta.Attributes.Contains(Itinero.Transit.Constants.TimeOfDayKey, ((08 * 3600) + (00 * 60)).ToInvariantString())); Assert.AreEqual(meta.Time, 30 * 60); meta = route.ShapeMeta[2]; Assert.AreEqual(2, meta.Shape); Assert.IsNotNull(meta.Attributes); Assert.IsTrue(meta.Attributes.Contains(Itinero.Transit.Constants.TimeOfDayKey, ((08 * 3600) + (10 * 60)).ToInvariantString())); Assert.AreEqual(meta.Time, 40 * 60); meta = route.ShapeMeta[3]; Assert.AreEqual(3, meta.Shape); Assert.IsNotNull(meta.Attributes); Assert.IsTrue(meta.Attributes.Contains(Itinero.Transit.Constants.TimeOfDayKey, ((08 * 3600) + (10 * 60) + 100).ToInvariantString())); Assert.AreEqual(meta.Time, 40 * 60 + 100); meta = route.ShapeMeta[4]; Assert.AreEqual(4, meta.Shape); Assert.IsNotNull(meta.Attributes); Assert.IsTrue(meta.Attributes.Contains(Itinero.Transit.Constants.TimeOfDayKey, ((08 * 3600) + (15 * 60)).ToInvariantString())); Assert.AreEqual(meta.Time, 45 * 60); meta = route.ShapeMeta[5]; Assert.AreEqual(5, meta.Shape); Assert.IsNotNull(meta.Attributes); Assert.IsTrue(meta.Attributes.Contains(Itinero.Transit.Constants.TimeOfDayKey, ((08 * 3600) + (25 * 60)).ToInvariantString())); Assert.AreEqual(meta.Time, 55 * 60); }
public void TestOneHopWithWalkingAfter() { // build dummy db. var db = new TransitDb(); db.AddStop(0, 0, db.StopAttributes.Add(new Attribute("name", "stop1"))); db.AddStop(1, 1, db.StopAttributes.Add(new Attribute("name", "stop2"))); db.AddTrip(0, 0, db.TripAttributes.Add(new Attribute("name", "trip1"))); db.AddConnection(0, 1, 0, 8 * 3600, 8 * 3600 + 10 * 60); db.SortConnections(DefaultSorting.DepartureTime, null); // run algorithm. var departureTime = new System.DateTime(2017, 05, 10, 07, 30, 00); var algorithm = new ProfileSearch(db, departureTime, (profileId, day) => true); algorithm.SetSourceStop(0, 07 * 3600 + 30 * 60); // a 15 min walk. algorithm.SetTargetStop(1, 15 * 60); algorithm.Run(); // build route. var routeBuilder = new ProfileSearchRouteBuilder(algorithm, false); routeBuilder.Run(); var route = routeBuilder.Route; Assert.IsNotNull(route); Assert.IsNotNull(route.Shape); Assert.AreEqual(3, route.Shape.Length); Assert.AreEqual(0, route.Shape[0].Latitude); Assert.AreEqual(0, route.Shape[0].Longitude); Assert.AreEqual(0, route.Shape[1].Latitude); Assert.AreEqual(0, route.Shape[1].Longitude); Assert.AreEqual(1, route.Shape[2].Latitude); Assert.AreEqual(1, route.Shape[2].Longitude); Assert.IsNotNull(route.Stops); Assert.AreEqual(3, route.Stops.Length); var stop = route.Stops[0]; Assert.IsNotNull(stop.Attributes); Assert.AreEqual(1, stop.Attributes.Count); Assert.IsTrue(stop.Attributes.Contains("name", "stop1")); Assert.AreEqual(0, stop.Shape); Assert.AreEqual(0, stop.Coordinate.Latitude); Assert.AreEqual(0, stop.Coordinate.Longitude); stop = route.Stops[1]; Assert.IsNotNull(stop.Attributes); Assert.AreEqual(1, stop.Attributes.Count); Assert.IsTrue(stop.Attributes.Contains("name", "stop1")); Assert.AreEqual(1, stop.Shape); Assert.AreEqual(0, stop.Coordinate.Latitude); Assert.AreEqual(0, stop.Coordinate.Longitude); stop = route.Stops[2]; Assert.IsNotNull(stop.Attributes); Assert.AreEqual(1, stop.Attributes.Count); Assert.IsTrue(stop.Attributes.Contains("name", "stop2")); Assert.AreEqual(2, stop.Shape); Assert.AreEqual(1, stop.Coordinate.Latitude); Assert.AreEqual(1, stop.Coordinate.Longitude); Assert.IsNotNull(route.ShapeMeta); Assert.AreEqual(3, route.ShapeMeta.Length); var meta = route.ShapeMeta[0]; Assert.AreEqual(0, meta.Shape); Assert.IsNotNull(meta.Attributes); Assert.IsTrue(meta.Attributes.Contains(Itinero.Transit.Constants.TimeOfDayKey, ((07 * 3600) + (30 * 60)).ToInvariantString())); Assert.AreEqual(0, meta.Time); meta = route.ShapeMeta[1]; Assert.AreEqual(1, meta.Shape); Assert.IsNotNull(meta.Attributes); Assert.IsTrue(meta.Attributes.Contains(Itinero.Transit.Constants.TimeOfDayKey, ((08 * 3600) + (00 * 60)).ToInvariantString())); Assert.AreEqual(30 * 60, meta.Time); meta = route.ShapeMeta[2]; Assert.AreEqual(2, meta.Shape); Assert.IsNotNull(meta.Attributes); Assert.IsTrue(meta.Attributes.Contains(Itinero.Transit.Constants.TimeOfDayKey, ((08 * 3600) + (10 * 60)).ToInvariantString())); Assert.AreEqual(meta.Time, 40 * 60); }
public void TestTwoHopsOneTransferVersusOneHopSuccessful() { // build dummy db. var db = new TransitDb(); db.AddStop(0, 0, 0); db.AddStop(1, 1, 1); db.AddStop(2, 2, 2); db.AddTrip(0, 0, 0); db.AddTrip(0, 0, 0); db.AddTrip(0, 0, 0); db.AddConnection(0, 1, 0, 8 * 3600, 8 * 3600 + 10 * 60); db.AddConnection(1, 2, 1, 8 * 3600 + 15 * 60, 8 * 3600 + 25 * 60); db.AddConnection(0, 2, 2, 8 * 3600 + 16 * 60, 8 * 3600 + 25 * 60); db.SortConnections(DefaultSorting.DepartureTime, null); // run algorithm. var departureTime = new DateTime(2017, 05, 10, 07, 30, 00); var algorithm = new ProfileSearch(db, departureTime, (profileId, day) => true); algorithm.SetSourceStop(0, 07 * 3600 + 30 * 60); algorithm.SetTargetStop(2, 0); algorithm.Run(); // test results. Assert.IsTrue(algorithm.HasRun); Assert.IsTrue(algorithm.HasSucceeded); // check arrival profile(s). var arrivalStops = algorithm.ArrivalStops; Assert.AreEqual(3, arrivalStops.Count); Assert.AreEqual(2, arrivalStops[2]); var arrivalProfiles = algorithm.ArrivalProfiles; Assert.AreEqual(3, arrivalProfiles.Count); Assert.AreEqual(08 * 3600 + 25 * 60, arrivalProfiles[2].Seconds); Assert.AreEqual(55 * 60, algorithm.Duration(2)); Assert.AreEqual(new DateTime(2017, 05, 10, 08, 25, 00), algorithm.ArrivalTime(2)); // check stop profiles. var connections = db.GetConnectionsEnumerator(DefaultSorting.DepartureTime); var precedingStop = Itinero.Transit.Constants.NoStopId; var transfers = Itinero.Transit.Constants.NoTransfers; // get profiles at stop 2. var profiles = algorithm.GetStopProfiles(2); var profile = profiles[0]; Assert.IsTrue(profile.IsEmpty); profile = profiles[1]; Assert.IsTrue(profile.IsEmpty); profile = profiles[2]; Assert.AreEqual(2, profile.PreviousConnectionId); Assert.AreEqual((int)(departureTime - departureTime.Date).TotalSeconds + 55 * 60, profile.Seconds); connections.MoveTo(profile.PreviousConnectionId); Assert.AreEqual(0, connections.DepartureStop); Assert.AreEqual(2, connections.TripId); // get previous profile and check this is stop 0. profile = algorithm.GetPreceding(profiles, 2, out precedingStop, out transfers); profiles = algorithm.GetStopProfiles(0); profile = profiles[0]; Assert.AreEqual(Itinero.Transit.Constants.NoConnectionId, profile.PreviousConnectionId); Assert.AreEqual((int)(departureTime - departureTime.Date).TotalSeconds, profile.Seconds); // check the profiles at stop 1. profiles = algorithm.GetStopProfiles(1); profile = profiles[0]; Assert.IsTrue(profile.IsEmpty); profile = profiles[1]; Assert.IsTrue(profile.IsEmpty); profile = profiles[2]; Assert.AreEqual(0, profile.PreviousConnectionId); Assert.AreEqual((int)(departureTime - departureTime.Date).TotalSeconds + 40 * 60, profile.Seconds); connections.MoveTo(profile.PreviousConnectionId); Assert.AreEqual(0, connections.DepartureStop); Assert.AreEqual(0, connections.TripId); var tripStatus = algorithm.GetTripStatus(0); Assert.AreEqual(2, tripStatus.Transfers); Assert.AreEqual(0, tripStatus.StopId); Assert.AreEqual(08 * 3600 + 00 * 60, tripStatus.DepartureTime); tripStatus = algorithm.GetTripStatus(1); Assert.AreEqual(4, tripStatus.Transfers); Assert.AreEqual(1, tripStatus.StopId); Assert.AreEqual(08 * 3600 + 15 * 60, tripStatus.DepartureTime); tripStatus = algorithm.GetTripStatus(2); Assert.AreEqual(2, tripStatus.Transfers); Assert.AreEqual(0, tripStatus.StopId); Assert.AreEqual(08 * 3600 + 16 * 60, tripStatus.DepartureTime); }