forked from opentripplanner/OpenTripPlanner
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConstructApplication.java
More file actions
394 lines (340 loc) · 13.7 KB
/
ConstructApplication.java
File metadata and controls
394 lines (340 loc) · 13.7 KB
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
package org.opentripplanner.standalone.configure;
import jakarta.ws.rs.core.Application;
import javax.annotation.Nullable;
import org.opentripplanner.core.framework.deduplicator.DeduplicatorService;
import org.opentripplanner.datastore.api.DataSource;
import org.opentripplanner.ext.carpooling.CarpoolingRepository;
import org.opentripplanner.ext.emission.EmissionRepository;
import org.opentripplanner.ext.empiricaldelay.EmpiricalDelayRepository;
import org.opentripplanner.ext.stopconsolidation.StopConsolidationRepository;
import org.opentripplanner.framework.application.LogMDCSupport;
import org.opentripplanner.framework.application.OTPFeature;
import org.opentripplanner.graph_builder.GraphBuilder;
import org.opentripplanner.graph_builder.GraphBuilderDataSources;
import org.opentripplanner.graph_builder.issue.api.DataImportIssueSummary;
import org.opentripplanner.raptor.configure.RaptorConfig;
import org.opentripplanner.routing.algorithm.raptoradapter.transit.RaptorTransitData;
import org.opentripplanner.routing.algorithm.raptoradapter.transit.TransitTuningParameters;
import org.opentripplanner.routing.algorithm.raptoradapter.transit.TripSchedule;
import org.opentripplanner.routing.algorithm.raptoradapter.transit.mappers.RaptorTransitDataMapper;
import org.opentripplanner.routing.fares.FareServiceFactory;
import org.opentripplanner.routing.util.EllipsoidUtils;
import org.opentripplanner.service.osminfo.OsmInfoGraphBuildRepository;
import org.opentripplanner.service.realtimevehicles.RealtimeVehicleRepository;
import org.opentripplanner.service.streetdetails.StreetDetailsRepository;
import org.opentripplanner.service.vehicleparking.VehicleParkingRepository;
import org.opentripplanner.service.vehicleparking.VehicleParkingService;
import org.opentripplanner.service.vehiclerental.VehicleRentalRepository;
import org.opentripplanner.service.worldenvelope.WorldEnvelopeRepository;
import org.opentripplanner.standalone.api.OtpServerRequestContext;
import org.opentripplanner.standalone.config.BuildConfig;
import org.opentripplanner.standalone.config.CommandLineParameters;
import org.opentripplanner.standalone.config.ConfigModel;
import org.opentripplanner.standalone.config.DebugUiConfig;
import org.opentripplanner.standalone.config.OtpConfig;
import org.opentripplanner.standalone.config.RouterConfig;
import org.opentripplanner.standalone.server.GrizzlyServer;
import org.opentripplanner.standalone.server.OTPWebApplication;
import org.opentripplanner.street.StreetRepository;
import org.opentripplanner.street.graph.Graph;
import org.opentripplanner.street.linking.VertexLinker;
import org.opentripplanner.transfer.regular.TransferRepository;
import org.opentripplanner.transit.service.TimetableRepository;
import org.opentripplanner.updater.configure.UpdaterConfigurator;
import org.opentripplanner.updater.trip.TimetableSnapshotManager;
import org.opentripplanner.utils.logging.ProgressTracker;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* This class is responsible for creating the top level services like the {@link OTPWebApplication}
* and {@link GraphBuilder}. The purpose of this class is to wire the application, creating the
* necessary Services and modules and putting them together. It is NOT responsible for starting or
* running the application. The whole idea of this class is to separate application construction
* from running it.
* <p>
* The top level construction class(this class) may delegate to other construction classes
* to inject configuration and services into submodules. An instance of this class is created
* using the {@link LoadApplication} - An application is constructed AFTER config and input files
* are loaded.
* <p>
* THIS CLASS IS NOT THREAD SAFE - THE APPLICATION SHOULD BE CREATED IN ONE THREAD. This
* should be really fast, since the only IO operations are reading config files and logging. Loading
* transit or map data should NOT happen during this phase.
*/
public class ConstructApplication {
private static final Logger LOG = LoggerFactory.getLogger(ConstructApplication.class);
private final CommandLineParameters cli;
private final GraphBuilderDataSources graphBuilderDataSources;
/**
* The OSM Info is injected into the graph-builder, but not the web-server; Hence not part of
* the application context.
*/
private final OsmInfoGraphBuildRepository osmInfoGraphBuildRepository;
private final ConstructApplicationFactory factory;
/**
* Create a new OTP configuration instance for a given directory.
*/
ConstructApplication(
CommandLineParameters cli,
Graph graph,
OsmInfoGraphBuildRepository osmInfoGraphBuildRepository,
StreetDetailsRepository streetDetailsRepository,
TimetableRepository timetableRepository,
TransferRepository transferRepository,
WorldEnvelopeRepository worldEnvelopeRepository,
ConfigModel config,
GraphBuilderDataSources graphBuilderDataSources,
DataImportIssueSummary issueSummary,
EmissionRepository emissionRepository,
@Nullable EmpiricalDelayRepository empiricalDelayRepository,
VehicleParkingRepository vehicleParkingRepository,
@Nullable StopConsolidationRepository stopConsolidationRepository,
StreetRepository streetRepository,
FareServiceFactory fareServiceFactory
) {
this.cli = cli;
this.graphBuilderDataSources = graphBuilderDataSources;
this.osmInfoGraphBuildRepository = osmInfoGraphBuildRepository;
ConstructApplicationFactory.Builder builder = DaggerConstructApplicationFactory.builder();
this.factory = builder
.configModel(config)
.graph(graph)
.streetDetailsRepository(streetDetailsRepository)
.timetableRepository(timetableRepository)
.transferRepository(transferRepository)
.worldEnvelopeRepository(worldEnvelopeRepository)
.vehicleParkingRepository(vehicleParkingRepository)
.emissionRepository(emissionRepository)
.empiricalDelayRepository(empiricalDelayRepository)
.dataImportIssueSummary(issueSummary)
.stopConsolidationRepository(stopConsolidationRepository)
.streetStreetRepository(streetRepository)
.schema(config.routerConfig().routingRequestDefaults())
.fareServiceFactory(fareServiceFactory)
.build();
}
public ConstructApplicationFactory getFactory() {
return factory;
}
/**
* Create a new Grizzly server - call this method once, the new instance is created every time
* this method is called.
*/
public GrizzlyServer createGrizzlyServer() {
return new GrizzlyServer(
cli,
createApplication(),
routerConfig().server().apiProcessingTimeout()
);
}
/**
* Create the default graph builder.
*/
public GraphBuilder createGraphBuilder() {
LOG.info("Wiring up and configuring graph builder task.");
return GraphBuilder.create(
buildConfig(),
graphBuilderDataSources,
graph(),
osmInfoGraphBuildRepository,
factory.streetDetailsRepository(),
fareServiceFactory(),
factory.streetRepository(),
factory.timetableRepository(),
factory.transferRepository(),
factory.worldEnvelopeRepository(),
factory.vehicleParkingRepository(),
factory.emissionRepository(),
factory.empiricalDelayRepository(),
factory.stopConsolidationRepository(),
cli.doLoadStreetGraph(),
cli.doSaveStreetGraph()
);
}
/**
* The output data source to use for saving the serialized graph.
* <p>
* This method will return {@code null} if the graph should NOT be saved. The business logic to
* make that decision is in the {@link GraphBuilderDataSources}.
*/
@Nullable
public DataSource graphOutputDataSource() {
return graphBuilderDataSources.getOutputGraph();
}
public GraphBuilderDataSources graphBuilderDataSources() {
return graphBuilderDataSources;
}
private Application createApplication() {
LOG.info("Wiring up and configuring server.");
setupTransitRoutingServer();
return new OTPWebApplication(routerConfig().server(), this::createServerContext);
}
private void setupTransitRoutingServer() {
enableRequestTraceLogging();
createMetricsLogging();
createRaptorTransitData(
timetableRepository(),
transferRepository(),
routerConfig().transitTuningConfig()
);
/* Create updater modules from JSON config. */
UpdaterConfigurator.configure(
graph(),
deduplicatorService(),
vertexLinker(),
realtimeVehicleRepository(),
vehicleRentalRepository(),
vehicleParkingRepository(),
timetableRepository(),
carpoolingRepository(),
snapshotManager(),
routerConfig().updaterConfig()
);
// Start application warmup — runs routing queries to warm up the application
factory.warmupLauncher().start();
initEllipsoidToGeoidDifference();
initializeTransferCache(routerConfig().transitTuningConfig(), timetableRepository());
if (OTPFeature.SandboxAPIGeocoder.isOn()) {
LOG.info("Initializing geocoder");
// eagerly initialize the geocoder
this.factory.luceneIndex();
}
}
private void initEllipsoidToGeoidDifference() {
try {
var c = factory.worldEnvelopeService().envelope().orElseThrow().center();
double value = EllipsoidUtils.computeEllipsoidToGeoidDifference(c.latitude(), c.longitude());
graph().initEllipsoidToGeoidDifference(value, c.latitude(), c.longitude());
} catch (Exception e) {
LOG.error("Error computing ellipsoid/geoid difference");
}
}
/**
* Create transit layer for Raptor routing. Here we map the scheduled timetables.
*/
public static void createRaptorTransitData(
TimetableRepository timetableRepository,
TransferRepository transferRepository,
TransitTuningParameters tuningParameters
) {
if (!timetableRepository.hasTransit() || !timetableRepository.isIndexed()) {
LOG.warn(
"Cannot create Raptor data, that requires the graph to have transit data and be indexed."
);
}
LOG.info("Creating transit layer for Raptor routing.");
timetableRepository.setRaptorTransitData(
RaptorTransitDataMapper.map(tuningParameters, timetableRepository, transferRepository)
);
timetableRepository.setRealtimeRaptorTransitData(
new RaptorTransitData(timetableRepository.getRaptorTransitData())
);
}
public static void initializeTransferCache(
TransitTuningParameters transitTuningConfig,
TimetableRepository timetableRepository
) {
var transferCacheRequests = transitTuningConfig.transferCacheRequests();
if (!transferCacheRequests.isEmpty()) {
var progress = ProgressTracker.track(
"Creating initial raptor transfer cache",
1,
transferCacheRequests.size()
);
LOG.info(progress.startMessage());
transferCacheRequests.forEach(request -> {
timetableRepository.getRaptorTransitData().initTransferCacheForRequest(request);
//noinspection Convert2MethodRef
progress.step(s -> LOG.info(s));
});
LOG.info(progress.completeMessage());
}
}
public TimetableRepository timetableRepository() {
return factory.timetableRepository();
}
public TransferRepository transferRepository() {
return factory.transferRepository();
}
public CarpoolingRepository carpoolingRepository() {
return factory.carpoolingRepository();
}
public DataImportIssueSummary dataImportIssueSummary() {
return factory.dataImportIssueSummary();
}
public OsmInfoGraphBuildRepository osmInfoGraphBuildRepository() {
return osmInfoGraphBuildRepository;
}
public StopConsolidationRepository stopConsolidationRepository() {
return factory.stopConsolidationRepository();
}
public StreetRepository streetRepository() {
return factory.streetRepository();
}
public RealtimeVehicleRepository realtimeVehicleRepository() {
return factory.realtimeVehicleRepository();
}
public VehicleRentalRepository vehicleRentalRepository() {
return factory.vehicleRentalRepository();
}
private TimetableSnapshotManager snapshotManager() {
return factory.timetableSnapshotManager();
}
public VehicleParkingService vehicleParkingService() {
return factory.vehicleParkingService();
}
public VehicleParkingRepository vehicleParkingRepository() {
return factory.vehicleParkingRepository();
}
public Graph graph() {
return factory.graph();
}
public VertexLinker vertexLinker() {
return factory.vertexLinker();
}
public DeduplicatorService deduplicatorService() {
return factory.deduplicatorService();
}
public WorldEnvelopeRepository worldEnvelopeRepository() {
return factory.worldEnvelopeRepository();
}
public OtpConfig otpConfig() {
return factory.config().otpConfig();
}
public RouterConfig routerConfig() {
return factory.config().routerConfig();
}
public BuildConfig buildConfig() {
return factory.config().buildConfig();
}
public DebugUiConfig debugUiConfig() {
return factory.config().debugUiConfig();
}
public RaptorConfig<TripSchedule> raptorConfig() {
return factory.raptorConfig();
}
private OtpServerRequestContext createServerContext() {
return factory.createServerContext();
}
private void enableRequestTraceLogging() {
if (routerConfig().server().requestTraceLoggingEnabled()) {
LogMDCSupport.enable();
}
}
private void createMetricsLogging() {
factory.metricsLogging();
}
public EmissionRepository emissionRepository() {
return factory.emissionRepository();
}
public StreetDetailsRepository streetDetailsRepository() {
return factory.streetDetailsRepository();
}
@Nullable
public EmpiricalDelayRepository empiricalDelayRepository() {
return factory.empiricalDelayRepository();
}
public FareServiceFactory fareServiceFactory() {
return factory.fareServiceFactory();
}
}