org.opentripplanner.transit.raptor.rangeraptor.configure.RaptorConfig Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of otp Show documentation
Show all versions of otp Show documentation
The OpenTripPlanner multimodal journey planning system
package org.opentripplanner.transit.raptor.rangeraptor.configure;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Metrics;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import javax.annotation.Nullable;
import org.opentripplanner.transit.raptor.api.request.RaptorRequest;
import org.opentripplanner.transit.raptor.api.request.RaptorTuningParameters;
import org.opentripplanner.transit.raptor.api.transit.RaptorTransitDataProvider;
import org.opentripplanner.transit.raptor.api.transit.RaptorTripSchedule;
import org.opentripplanner.transit.raptor.api.view.Heuristics;
import org.opentripplanner.transit.raptor.api.view.Worker;
import org.opentripplanner.transit.raptor.rangeraptor.RangeRaptorWorker;
import org.opentripplanner.transit.raptor.rangeraptor.RoutingStrategy;
import org.opentripplanner.transit.raptor.rangeraptor.WorkerState;
import org.opentripplanner.transit.raptor.rangeraptor.debug.WorkerPerformanceTimers;
import org.opentripplanner.transit.raptor.rangeraptor.multicriteria.configure.McRangeRaptorConfig;
import org.opentripplanner.transit.raptor.rangeraptor.standard.configure.StdRangeRaptorConfig;
import org.opentripplanner.transit.raptor.rangeraptor.standard.heuristics.HeuristicSearch;
import org.opentripplanner.transit.raptor.rangeraptor.transit.SearchContext;
import org.opentripplanner.transit.raptor.service.RaptorSearchWindowCalculator;
import org.opentripplanner.transit.raptor.service.RequestAlias;
/**
* This class is responsible for creating a new search and holding
* application scoped Raptor state.
*
* This class should have APPLICATION scope. It manage a threadPool,
* and hold a reference to the application tuning parameters.
*
* @param The TripSchedule type defined by the user of the raptor API.
*/
public class RaptorConfig {
private final ExecutorService threadPool;
private final RaptorTuningParameters tuningParameters;
private final MeterRegistry registry;
public RaptorConfig(
RaptorTuningParameters tuningParameters,
MeterRegistry registry
) {
this.tuningParameters = tuningParameters;
this.threadPool = createNewThreadPool(tuningParameters.searchThreadPoolSize());
this.registry = registry;
}
public static RaptorConfig defaultConfigForTest() {
return new RaptorConfig<>(new RaptorTuningParameters() {}, Metrics.globalRegistry);
}
public SearchContext context(RaptorTransitDataProvider transit, RaptorRequest request) {
return new SearchContext<>(
request,
tuningParameters,
transit,
new WorkerPerformanceTimers(RequestAlias.alias(request, isMultiThreaded()), registry)
);
}
public Worker createStdWorker(RaptorTransitDataProvider transitData, RaptorRequest request) {
SearchContext context = context(transitData, request);
return new StdRangeRaptorConfig<>(context).createSearch((s, w) -> createWorker(context, s, w));
}
public Worker createMcWorker(RaptorTransitDataProvider transitData, RaptorRequest request, Heuristics heuristics) {
final SearchContext context = context(transitData, request);
return new McRangeRaptorConfig<>(context).createWorker(heuristics, (s, w) -> createWorker(context, s, w));
}
public HeuristicSearch createHeuristicSearch(
RaptorTransitDataProvider transitData,
RaptorRequest request
) {
SearchContext context = context(transitData, request);
return new StdRangeRaptorConfig<>(context)
.createHeuristicSearch((s, w) -> createWorker(context, s, w));
}
public boolean isMultiThreaded() {
return threadPool != null;
}
public ExecutorService threadPool() {
return threadPool;
}
public void shutdown() {
if (threadPool != null) {
threadPool.shutdown();
}
}
public RaptorSearchWindowCalculator searchWindowCalculator() {
return new RaptorSearchWindowCalculator(tuningParameters.dynamicSearchWindowCoefficients());
}
/* private factory methods */
private Worker createWorker(
SearchContext ctx,
WorkerState workerState,
RoutingStrategy routingStrategy
) {
return new RangeRaptorWorker<>(
workerState,
routingStrategy,
ctx.transit(),
ctx.slackProvider(),
ctx.accessPaths(),
ctx.roundProvider(),
ctx.calculator(),
ctx.createLifeCyclePublisher(),
ctx.timers(),
ctx.enableConstrainedTransfers()
);
}
@Nullable
private ExecutorService createNewThreadPool(int size) {
return size > 0 ? Executors.newFixedThreadPool(size) : null;
}
}