
org.opentripplanner.raptor.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
The newest version!
package org.opentripplanner.raptor.configure;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.function.IntPredicate;
import javax.annotation.Nullable;
import org.opentripplanner.framework.concurrent.OtpRequestThreadFactory;
import org.opentripplanner.raptor.api.model.RaptorTripSchedule;
import org.opentripplanner.raptor.api.request.RaptorRequest;
import org.opentripplanner.raptor.api.request.RaptorTuningParameters;
import org.opentripplanner.raptor.rangeraptor.DefaultRangeRaptorWorker;
import org.opentripplanner.raptor.rangeraptor.context.SearchContext;
import org.opentripplanner.raptor.rangeraptor.internalapi.Heuristics;
import org.opentripplanner.raptor.rangeraptor.internalapi.PassThroughPointsService;
import org.opentripplanner.raptor.rangeraptor.internalapi.RaptorWorker;
import org.opentripplanner.raptor.rangeraptor.internalapi.RaptorWorkerResult;
import org.opentripplanner.raptor.rangeraptor.internalapi.RaptorWorkerState;
import org.opentripplanner.raptor.rangeraptor.internalapi.RoutingStrategy;
import org.opentripplanner.raptor.rangeraptor.multicriteria.configure.McRangeRaptorConfig;
import org.opentripplanner.raptor.rangeraptor.standard.configure.StdRangeRaptorConfig;
import org.opentripplanner.raptor.rangeraptor.transit.RaptorSearchWindowCalculator;
import org.opentripplanner.raptor.spi.RaptorTransitDataProvider;
/**
* 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;
/** The service is not final, because it depends on the request. */
private PassThroughPointsService passThroughPointsService = null;
public RaptorConfig(RaptorTuningParameters tuningParameters) {
this.tuningParameters = tuningParameters;
this.threadPool = createNewThreadPool(tuningParameters.searchThreadPoolSize());
}
public static RaptorConfig defaultConfigForTest() {
return new RaptorConfig<>(new RaptorTuningParameters() {});
}
public SearchContext context(RaptorTransitDataProvider transit, RaptorRequest request) {
// The passThroughPointsService is needed to create the context, so we initialize it here.
this.passThroughPointsService = createPassThroughPointsService(request);
return new SearchContext<>(request, tuningParameters, transit, acceptC2AtDestination());
}
public RaptorWorker createStdWorker(
RaptorTransitDataProvider transitData,
RaptorRequest request
) {
var context = context(transitData, request);
var stdConfig = new StdRangeRaptorConfig<>(context);
return createWorker(context, stdConfig.state(), stdConfig.strategy());
}
public RaptorWorker createMcWorker(
RaptorTransitDataProvider transitData,
RaptorRequest request,
Heuristics heuristics
) {
final SearchContext context = context(transitData, request);
return new McRangeRaptorConfig<>(context, passThroughPointsService)
.createWorker(
heuristics,
(state, routingStrategy) -> createWorker(context, state, routingStrategy)
);
}
public RaptorWorker createHeuristicSearch(
RaptorTransitDataProvider transitData,
RaptorRequest request
) {
var context = context(transitData, request);
var stdConfig = new StdRangeRaptorConfig<>(context);
return createWorker(context, stdConfig.state(), stdConfig.strategy());
}
public Heuristics createHeuristic(
RaptorTransitDataProvider transitData,
RaptorRequest request,
RaptorWorkerResult results
) {
var context = context(transitData, request);
return new StdRangeRaptorConfig<>(context).createHeuristics(results);
}
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 static PassThroughPointsService createPassThroughPointsService(RaptorRequest> request) {
return McRangeRaptorConfig.passThroughPointsService(request.multiCriteria());
}
private RaptorWorker createWorker(
SearchContext ctx,
RaptorWorkerState workerState,
RoutingStrategy routingStrategy
) {
return new DefaultRangeRaptorWorker<>(
workerState,
routingStrategy,
ctx.transit(),
ctx.slackProvider(),
ctx.accessPaths(),
ctx.roundProvider(),
ctx.calculator(),
ctx.createLifeCyclePublisher(),
ctx.performanceTimers(),
ctx.useConstrainedTransfers()
);
}
private IntPredicate acceptC2AtDestination() {
return passThroughPointsService.isNoop()
? null
: passThroughPointsService.acceptC2AtDestination();
}
@Nullable
private ExecutorService createNewThreadPool(int size) {
return size > 0
? Executors.newFixedThreadPool(size, OtpRequestThreadFactory.of("raptor-%d"))
: null;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy