This repository has been archived by the owner on May 27, 2024. It is now read-only.
forked from opentripplanner/OpenTripPlanner
-
Notifications
You must be signed in to change notification settings - Fork 1
/
RoutingService.java
179 lines (163 loc) · 7.39 KB
/
RoutingService.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package org.opentripplanner.routing;
import java.util.Calendar;
import java.util.Collection;
import java.util.List;
import lombok.experimental.Delegate;
import org.opentripplanner.model.Stop;
import org.opentripplanner.model.StopLocation;
import org.opentripplanner.model.StopTimesInPattern;
import org.opentripplanner.model.Timetable;
import org.opentripplanner.model.TimetableSnapshot;
import org.opentripplanner.model.Trip;
import org.opentripplanner.model.TripPattern;
import org.opentripplanner.model.TripTimeOnDate;
import org.opentripplanner.model.calendar.ServiceDate;
import org.opentripplanner.routing.algorithm.RoutingWorker;
import org.opentripplanner.routing.api.request.RoutingRequest;
import org.opentripplanner.routing.api.response.RoutingResponse;
import org.opentripplanner.routing.graph.Graph;
import org.opentripplanner.routing.graph.GraphIndex;
import org.opentripplanner.routing.graphfinder.GraphFinder;
import org.opentripplanner.routing.stoptimes.ArrivalDeparture;
import org.opentripplanner.routing.stoptimes.StopTimesHelper;
import org.opentripplanner.standalone.server.Router;
/**
* This is the entry point of all API requests towards the OTP graph. A new instance of this class
* should be created for each request. This ensures that the same TimetableSnapshot is used for the
* duration of the request (which may involve several method calls).
*/
public class RoutingService {
@Delegate(types = Graph.class)
private final Graph graph;
@Delegate(types = GraphIndex.class)
private final GraphIndex graphIndex;
@Delegate(types = GraphFinder.class)
private final GraphFinder graphFinder;
/**
* This should only be accessed through the getTimetableSnapshot method.
*/
private TimetableSnapshot timetableSnapshot;
public RoutingService(Graph graph) {
this.graph = graph;
this.graphIndex = graph.index;
this.graphFinder = GraphFinder.getInstance(graph);
}
// TODO We should probably not have the Router as a parameter here
public RoutingResponse route(RoutingRequest request, Router router) {
try {
var zoneId = graph.getTimeZone().toZoneId();
RoutingWorker worker = new RoutingWorker(router, request, zoneId);
return worker.route();
} finally {
if (request != null) {
request.cleanup();
}
}
}
/**
* Fetch upcoming vehicle departures from a stop. It goes though all patterns passing the stop
* for the previous, current and next service date. It uses a priority queue to keep track of
* the next departures. The queue is shared between all dates, as services from the previous
* service date can visit the stop later than the current service date's services. This happens
* eg. with sleeper trains.
* <p>
* TODO: Add frequency based trips
*
* @param stop Stop object to perform the search for
* @param startTime Start time for the search. Seconds from UNIX epoch
* @param timeRange Searches forward for timeRange seconds from startTime
* @param numberOfDepartures Number of departures to fetch per pattern
* @param arrivalDeparture Filter by arrivals, departures, or both
* @param includeCancelledTrips If true, cancelled trips will also be included in result.
*/
public List<StopTimesInPattern> stopTimesForStop(
StopLocation stop, long startTime, int timeRange, int numberOfDepartures, ArrivalDeparture arrivalDeparture, boolean includeCancelledTrips
) {
return StopTimesHelper.stopTimesForStop(
this,
lazyGetTimeTableSnapShot(),
stop,
startTime,
timeRange,
numberOfDepartures,
arrivalDeparture,
includeCancelledTrips
);
}
/**
* Get a list of all trips that pass through a stop during a single ServiceDate. Useful when
* creating complete stop timetables for a single day.
*
* @param stop Stop object to perform the search for
* @param serviceDate Return all departures for the specified date
*/
public List<StopTimesInPattern> getStopTimesForStop(
StopLocation stop, ServiceDate serviceDate, ArrivalDeparture arrivalDeparture
) {
return StopTimesHelper.stopTimesForStop(this, stop, serviceDate, arrivalDeparture);
}
/**
* Fetch upcoming vehicle departures from a stop for a specific pattern, passing the stop
* for the previous, current and next service date. It uses a priority queue to keep track of
* the next departures. The queue is shared between all dates, as services from the previous
* service date can visit the stop later than the current service date's services.
* <p>
* TODO: Add frequency based trips
*
* @param stop Stop object to perform the search for
* @param pattern Pattern object to perform the search for
* @param startTime Start time for the search. Seconds from UNIX epoch
* @param timeRange Searches forward for timeRange seconds from startTime
* @param numberOfDepartures Number of departures to fetch per pattern
* @param arrivalDeparture Filter by arrivals, departures, or both
*/
public List<TripTimeOnDate> stopTimesForPatternAtStop(
StopLocation stop, TripPattern pattern, long startTime, int timeRange, int numberOfDepartures, ArrivalDeparture arrivalDeparture
) {
return StopTimesHelper.stopTimesForPatternAtStop(
this,
lazyGetTimeTableSnapShot(),
stop,
pattern,
startTime,
timeRange,
numberOfDepartures,
arrivalDeparture
);
}
/**
* Returns all the patterns for a specific stop. If includeRealtimeUpdates is set, new patterns
* added by realtime updates are added to the collection.
*/
public Collection<TripPattern> getPatternsForStop(StopLocation stop, boolean includeRealtimeUpdates) {
return graph.index.getPatternsForStop(stop,
includeRealtimeUpdates ? lazyGetTimeTableSnapShot() : null
);
}
/**
* Get the most up-to-date timetable for the given TripPattern, as of right now. There should
* probably be a less awkward way to do this that just gets the latest entry from the resolver
* without making a fake routing request.
*/
public Timetable getTimetableForTripPattern(TripPattern tripPattern) {
TimetableSnapshot timetableSnapshot = lazyGetTimeTableSnapShot();
return timetableSnapshot != null ? timetableSnapshot.resolve(
tripPattern,
new ServiceDate(Calendar.getInstance().getTime())
) : tripPattern.getScheduledTimetable();
}
public List<TripTimeOnDate> getTripTimesShort(Trip trip, ServiceDate serviceDate) {
return TripTimesShortHelper.getTripTimesShort(this, trip, serviceDate);
}
/**
* Lazy-initialization of TimetableSnapshot
*
* @return The same TimetableSnapshot is returned throughout the lifecycle of this object.
*/
private TimetableSnapshot lazyGetTimeTableSnapShot() {
if (this.timetableSnapshot == null) {
timetableSnapshot = graph.getTimetableSnapshot();
}
return this.timetableSnapshot;
}
}