org.opentripplanner.transit.raptor.RaptorService Maven / Gradle / Ivy
package org.opentripplanner.transit.raptor;
import java.util.stream.Collectors;
import org.opentripplanner.transit.raptor.api.path.Path;
import org.opentripplanner.transit.raptor.api.request.RaptorRequest;
import org.opentripplanner.transit.raptor.api.response.RaptorResponse;
import org.opentripplanner.transit.raptor.api.transit.RaptorTransitDataProvider;
import org.opentripplanner.transit.raptor.api.transit.RaptorTripSchedule;
import org.opentripplanner.transit.raptor.rangeraptor.configure.RaptorConfig;
import org.opentripplanner.transit.raptor.service.HeuristicSearchTask;
import org.opentripplanner.transit.raptor.service.RangeRaptorDynamicSearch;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Collection;
/**
* A service for performing Range Raptor routing request.
*
* @param The TripSchedule type defined by the user of the range raptor API.
*/
public class RaptorService {
private static final Logger LOG = LoggerFactory.getLogger(RaptorService.class);
private final RaptorConfig config;
public RaptorService(RaptorConfig config) {
this.config = config;
}
public RaptorResponse route(RaptorRequest request, RaptorTransitDataProvider transitData) {
LOG.debug("Original request: {}", request);
RaptorResponse response;
if(request.isDynamicSearch()) {
response = new RangeRaptorDynamicSearch<>(config, transitData, request).route();
}
else {
response = routeUsingStdWorker(transitData, request);
}
if(LOG.isDebugEnabled()) {
var pathsAsText = response.paths().stream()
.map(p -> "\t\n" + p.toString(transitData.stopNameResolver()))
.collect(Collectors.joining());
LOG.debug("Result: {}", pathsAsText);
}
return response;
}
public void compareHeuristics(
RaptorRequest r1,
RaptorRequest r2,
RaptorTransitDataProvider transitData
) {
HeuristicSearchTask fwdHeur = new HeuristicSearchTask<>(r1, config, transitData);
HeuristicSearchTask revHeur = new HeuristicSearchTask<>(r2, config, transitData);
fwdHeur.forceRun();
revHeur.forceRun();
fwdHeur.debugCompareResult(revHeur);
}
public void shutdown() {
config.shutdown();
}
/* private methods */
private RaptorResponse routeUsingStdWorker(RaptorTransitDataProvider transitData, RaptorRequest request) {
LOG.debug("Run query: {}", request);
Collection> paths = config.createStdWorker(transitData, request).route();
return new RaptorResponse<>(paths, request, request);
}
}