org.opentripplanner.transit.raptor.rangeraptor.debug.AbstractDebugHandlerAdapter 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.debug;
import org.opentripplanner.transit.raptor.api.debug.DebugEvent;
import org.opentripplanner.transit.raptor.api.request.DebugRequest;
import org.opentripplanner.transit.raptor.rangeraptor.WorkerLifeCycle;
import org.opentripplanner.transit.raptor.rangeraptor.view.DebugHandler;
import java.util.Iterator;
import java.util.List;
import java.util.function.Consumer;
/**
* Generic abstract implementation of the {@link DebugHandler} interface. The main purpose
* is provide a common logic for the adapters between the Range Raptor domain and the
* outside client - the event listeners and the request API.
*
* @param The TripSchedule type defined by the user of the raptor API.
*/
abstract class AbstractDebugHandlerAdapter implements DebugHandler {
private final List stops;
private final List path;
private final int pathStartAtStopIndex;
private final Consumer> eventListener;
private int iterationDepartureTime = -1;
AbstractDebugHandlerAdapter(
DebugRequest debugRequest,
Consumer> eventListener,
WorkerLifeCycle lifeCycle
) {
this.stops = debugRequest.stops();
this.path = debugRequest.path();
this.pathStartAtStopIndex = debugRequest.debugPathFromStopIndex();
this.eventListener = eventListener;
// Attach debugger to RR life cycle to receive iteration setup events
lifeCycle.onSetupIteration(this::setupIteration);
}
@Override
public boolean isDebug(int stop) {
return stops.contains(stop) || isDebugTrip(stop);
}
@Override
public void accept(T element) {
// The "if" is needed because this is the first time we are able to check trip paths
if (isDebugStopOrTripPath(element)) {
eventListener.accept(DebugEvent.accept(iterationDepartureTime, element));
}
}
@Override
public void reject(T element, T rejectedByElement, String reason) {
// The "if" is needed because this is the first time we are able to check trip paths
if (isDebugStopOrTripPath(element)) {
eventListener.accept(DebugEvent.reject(iterationDepartureTime, element, rejectedByElement, reason));
}
}
@Override
public void drop(T element, T droppedByElement, String reason) {
// The "if" is needed because this is the first time we are able to check trip paths
if (isDebugStopOrTripPath(element)) {
eventListener.accept(DebugEvent.drop(iterationDepartureTime, element, droppedByElement, reason));
}
}
abstract protected int stop(T arrival);
abstract protected Iterable stopsVisited(T arrival);
/* private members */
private void setupIteration ( int iterationDepartureTime){
this.iterationDepartureTime = iterationDepartureTime;
}
/**
* Check if a stop exist among the trip path stops witch should be debugged.
*/
private boolean isDebugTrip(int stop) {
return pathStartAtStopIndex <= path.indexOf(stop);
}
private boolean isDebugStopOrTripPath(T arrival) {
return stops.contains(stop(arrival)) || isDebugTripPath(arrival);
}
private boolean isDebugTripPath(T arrival) {
if (!isDebugTrip(stop(arrival))) {
return false;
}
Iterator stopsVisited = stopsVisited(arrival).iterator();
Iterator pathStops = path.iterator();
while (stopsVisited.hasNext()) {
if (!pathStops.hasNext()) {
return false;
}
Integer visitedStop = stopsVisited.next();
Integer pathStop = pathStops.next();
if (!visitedStop.equals(pathStop)) {
return false;
}
}
return true;
}
}