Skip to content

Commit

Permalink
Remove RoutingContext from RoutingRequest and extract TemporaryVertic…
Browse files Browse the repository at this point in the history
…esContainer
  • Loading branch information
hannesj committed Mar 31, 2022
1 parent 4c6f1e6 commit eaf3a05
Show file tree
Hide file tree
Showing 64 changed files with 763 additions and 754 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
import org.opentripplanner.routing.api.request.RoutingRequest;
import org.opentripplanner.routing.core.Fare.FareType;
import org.opentripplanner.routing.core.Money;
import org.opentripplanner.routing.core.RoutingContext;
import org.opentripplanner.routing.core.State;
import org.opentripplanner.routing.core.TemporaryVerticesContainer;
import org.opentripplanner.routing.core.WrappedCurrency;
import org.opentripplanner.routing.framework.DebugTimingAggregator;
import org.opentripplanner.routing.graph.Graph;
Expand Down Expand Up @@ -268,14 +270,17 @@ private static NearbyStop getNearbyStop(FlexTrip trip, String id) {
.findFirst()
.get();
var r = new RoutingRequest();
r.setRoutingContext(graph);
return new NearbyStop(
stopLocation,
0,
List.of(),
null,
new State(new StreetLocation(id, new Coordinate(0, 0), id), r)
);
try(var temporaryVertices = new TemporaryVerticesContainer(graph, r)) {
RoutingContext routingContext = new RoutingContext(r, graph, temporaryVertices);

return new NearbyStop(
stopLocation,
0,
List.of(),
null,
new State(new StreetLocation(id, new Coordinate(0, 0), id), r, routingContext)
);
}
}

private static FlexTrip getFlexTrip() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.Map;
import org.opentripplanner.routing.algorithm.astar.AStarBuilder;
import org.opentripplanner.routing.api.request.RoutingRequest;
import org.opentripplanner.routing.core.RoutingContext;
import org.opentripplanner.routing.core.TraverseMode;
import org.opentripplanner.routing.graph.Graph;
import org.opentripplanner.routing.graph.Vertex;
Expand Down Expand Up @@ -72,19 +73,17 @@ public FlexPath calculateFlexPath(Vertex fromv, Vertex tov, int fromStopIndex, i
private ShortestPathTree routeToMany(Vertex vertex) {
RoutingRequest routingRequest = new RoutingRequest(TraverseMode.CAR);
routingRequest.arriveBy = reverseDirection;
RoutingContext rctx;
if (reverseDirection) {
routingRequest.setRoutingContext(graph, null, vertex);
rctx = new RoutingContext(routingRequest, graph, null, vertex);
} else {
routingRequest.setRoutingContext(graph, vertex, null);
rctx = new RoutingContext(routingRequest, graph, vertex, null);
}
routingRequest.dominanceFunction = new DominanceFunction.EarliestArrival();

ShortestPathTree spt = AStarBuilder
return AStarBuilder
.allDirectionsMaxDuration(MAX_FLEX_TRIP_DURATION)
.setRoutingRequest(routingRequest)
.setContext(rctx)
.getShortestPathTree();

routingRequest.cleanup();
return spt;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.opentripplanner.routing.algorithm.astar.strategies.SkipEdgeStrategy;
import org.opentripplanner.routing.api.request.RoutingRequest;
import org.opentripplanner.routing.api.request.StreetMode;
import org.opentripplanner.routing.core.RoutingContext;
import org.opentripplanner.routing.core.State;
import org.opentripplanner.routing.core.TraverseMode;
import org.opentripplanner.routing.edgetype.StreetEdge;
Expand Down Expand Up @@ -133,7 +134,7 @@ public List<NearbyStop> findNearbyStops(
Vertex vertex, RoutingRequest routingRequest, boolean reverseDirection
) {
if (useStreets) {
return findNearbyStopsViaStreets(Set.of(vertex), reverseDirection, true, routingRequest);
return findNearbyStopsViaStreets(Set.of(vertex), reverseDirection, routingRequest);
}
// It make sense for the directGraphFinder to use meters as a limit, so we convert first
double limitMeters = durationLimit.toSeconds() * new RoutingRequest(TraverseMode.WALK).walkSpeed;
Expand All @@ -149,20 +150,24 @@ public List<NearbyStop> findNearbyStops(
* @param originVertices the origin point of the street search
* @param reverseDirection if true the paths returned instead originate at the nearby stops and have the
* originVertex as the destination
* @param removeTempEdges after creating a new routing request and routing context, remove all the temporary
* edges that are part of that context. NOTE: this will remove _all_ temporary edges
* coming out of the origin and destination vertices, including those in any other
* RoutingContext referencing them, making routing from/to them totally impossible.
* This is a stopgap solution until we rethink the lifecycle of RoutingContext.
*/
public List<NearbyStop> findNearbyStopsViaStreets (
Set<Vertex> originVertices,
boolean reverseDirection,
boolean removeTempEdges,
RoutingRequest routingRequest
) {
List<NearbyStop> stopsFound = Lists.newArrayList();

routingRequest.setArriveBy(reverseDirection);
routingRequest.dominanceFunction = new DominanceFunction.MinimumWeight();

RoutingContext routingContext;
if (!reverseDirection) {
routingContext = new RoutingContext(routingRequest, graph, originVertices, null);
} else {
routingContext = new RoutingContext(routingRequest, graph, null, originVertices);
}

/* Add the origin vertices if they are stops */
for (Vertex vertex : originVertices) {
if (vertex instanceof TransitStopVertex) {
Expand All @@ -171,26 +176,18 @@ public List<NearbyStop> findNearbyStopsViaStreets (
(TransitStopVertex) vertex,
0,
Collections.emptyList(),
new State(vertex, routingRequest)
new State(vertex, routingRequest, routingContext)
));
}
}

// Return only the origin vertices if there are no valid street modes
if (!routingRequest.streetSubRequestModes.isValid()) { return stopsFound; }

routingRequest.setArriveBy(reverseDirection);
if (!reverseDirection) {
routingRequest.setRoutingContext(graph, originVertices, null);
} else {
routingRequest.setRoutingContext(graph, null, originVertices);
}
routingRequest.dominanceFunction = new DominanceFunction.MinimumWeight();

var skipEdgeStrategy = getSkipEdgeStrategy(reverseDirection, routingRequest);
ShortestPathTree spt = AStarBuilder
.allDirections(skipEdgeStrategy)
.setRoutingRequest(routingRequest)
.setContext(routingContext)
.getShortestPathTree();

// Only used if OTPFeature.FlexRouting.isOn()
Expand Down Expand Up @@ -237,9 +234,6 @@ public List<NearbyStop> findNearbyStopsViaStreets (
}
}

if (removeTempEdges) {
routingRequest.cleanup();
}
return stopsFound;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ private static void collectNeighbourVertices(
if (!(gv instanceof StreetVertex)) {
continue;
}
State s0 = new State(gv, options);
State s0 = new State(gv, options, null);
for (Edge e : gv.getOutgoing()) {
if (!(
e instanceof StreetEdge || e instanceof StreetTransitStopLink ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public double getTotalError() {

protected boolean carsCanTraverse(Edge edge) {
// should be done with a method on edge (canTraverse already exists on turnEdge)
State s0 = new State(edge.getFromVertex(), traverseOptions);
State s0 = new State(edge.getFromVertex(), traverseOptions, null);
State s1 = edge.traverse(s0);
return s1 != null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.opentripplanner.routing.algorithm.astar.AStarBuilder;
import org.opentripplanner.routing.algorithm.astar.strategies.SkipEdgeStrategy;
import org.opentripplanner.routing.api.request.RoutingRequest;
import org.opentripplanner.routing.core.RoutingContext;
import org.opentripplanner.routing.core.State;
import org.opentripplanner.routing.core.TraverseMode;
import org.opentripplanner.routing.edgetype.AreaEdge;
Expand Down Expand Up @@ -365,10 +366,9 @@ private void pruneAreaEdges(Collection<Vertex> startingVertices, Set<Edge> edges
options.dominanceFunction = new DominanceFunction.EarliestArrival();
Set<Edge> usedEdges = new HashSet<>();
for (Vertex vertex : startingVertices) {
options.setRoutingContext(graph, vertex, null);
ShortestPathTree spt = AStarBuilder
.allDirections(new ListedEdgesOnly(edges))
.setRoutingRequest(options)
.setContext(new RoutingContext(options, graph, vertex, null))
.getShortestPathTree();

for (Vertex endVertex : startingVertices) {
Expand Down
12 changes: 3 additions & 9 deletions src/main/java/org/opentripplanner/routing/RoutingService.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,9 @@ public RoutingService(Graph graph) {

// TODO We should probably not have the Router as a parameter here
public RoutingResponse route(RoutingRequest request, Router router) {
try {
var zoneId = graph.getTimeZone().toZoneId();
RoutingWorker worker = new RoutingWorker(router, request, zoneId);
return worker.route();
} finally {
if (request != null) {
request.cleanup();
}
}
var zoneId = graph.getTimeZone().toZoneId();
RoutingWorker worker = new RoutingWorker(router, request, zoneId);
return worker.route();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import org.opentripplanner.routing.algorithm.astar.strategies.RemainingWeightHeuristic;
import org.opentripplanner.routing.algorithm.astar.strategies.SearchTerminationStrategy;
import org.opentripplanner.routing.algorithm.astar.strategies.SkipEdgeStrategy;
import org.opentripplanner.routing.api.request.RoutingRequest;
import org.opentripplanner.routing.core.RoutingContext;
import org.opentripplanner.routing.core.State;
import org.opentripplanner.routing.graph.Edge;
Expand Down Expand Up @@ -49,20 +48,20 @@ protected AStar(
RemainingWeightHeuristic heuristic,
SkipEdgeStrategy skipEdgeStrategy,
TraverseVisitor traverseVisitor,
RoutingRequest options,
RoutingContext rctx,
SearchTerminationStrategy terminationStrategy,
Duration timeout,
Edge originBackEdge
) {
this.heuristic = heuristic;
this.skipEdgeStrategy = skipEdgeStrategy;
this.traverseVisitor = traverseVisitor;
this.arriveBy = options.arriveBy;
this.arriveBy = rctx.opt.arriveBy;
this.terminationStrategy = terminationStrategy;
this.timeout = timeout;

this.rctx = options.getRoutingContext();
this.spt = options.getNewShortestPathTree();
this.rctx = rctx;
this.spt = rctx.opt.getNewShortestPathTree();
this.heuristic.initialize(rctx);

// Priority Queue.
Expand All @@ -75,7 +74,7 @@ protected AStar(
this.nVisited = 0;
this.targetAcceptedStates = Lists.newArrayList();

for (State initialState : State.getInitialStates(options)) {
for (State initialState : State.getInitialStates(rctx)) {
if (originBackEdge != null) {
initialState.backEdge = originBackEdge;
}
Expand Down Expand Up @@ -147,8 +146,6 @@ private boolean iterate(){

double remaining_w = heuristic.estimateRemainingWeight(v);

// LOG.info("{} {}", v, remaining_w);

if (remaining_w < 0 || Double.isInfinite(remaining_w) ) {
continue;
}
Expand All @@ -167,7 +164,6 @@ private boolean iterate(){
if (traverseVisitor != null) {
traverseVisitor.visitEnqueue();
}
//LOG.info("u.w={} v.w={} h={}", runState.u.weight, v.weight, remaining_w);
pq.insert(v, estimate);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@

import java.time.Duration;
import java.util.List;
import java.util.stream.Collectors;
import org.opentripplanner.routing.algorithm.astar.strategies.DurationSkipEdgeStrategy;
import org.opentripplanner.routing.algorithm.astar.strategies.EuclideanRemainingWeightHeuristic;
import org.opentripplanner.routing.algorithm.astar.strategies.RemainingWeightHeuristic;
import org.opentripplanner.routing.algorithm.astar.strategies.SearchTerminationStrategy;
import org.opentripplanner.routing.algorithm.astar.strategies.SkipEdgeStrategy;
import org.opentripplanner.routing.algorithm.astar.strategies.TrivialRemainingWeightHeuristic;
import org.opentripplanner.routing.api.request.RoutingRequest;
import org.opentripplanner.routing.core.State;
import org.opentripplanner.routing.core.RoutingContext;
import org.opentripplanner.routing.graph.Edge;
import org.opentripplanner.routing.spt.GraphPath;
import org.opentripplanner.routing.spt.ShortestPathTree;
Expand All @@ -20,7 +18,7 @@ public class AStarBuilder {
private final RemainingWeightHeuristic heuristic;
private final SkipEdgeStrategy skipEdgeStrategy;
private TraverseVisitor traverseVisitor;
private RoutingRequest options;
private RoutingContext routingContext;
private SearchTerminationStrategy terminationStrategy;
private Duration timeout;
private Edge originBackEdge;
Expand Down Expand Up @@ -57,8 +55,8 @@ public AStarBuilder setTraverseVisitor(TraverseVisitor traverseVisitor) {
return this;
}

public AStarBuilder setRoutingRequest(RoutingRequest options) {
this.options = options;
public AStarBuilder setContext(RoutingContext routingContext) {
this.routingContext = routingContext;
return this;
}

Expand All @@ -82,7 +80,7 @@ private AStar build() {
heuristic,
skipEdgeStrategy,
traverseVisitor,
options,
routingContext,
terminationStrategy,
timeout,
originBackEdge
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.List;
import org.opentripplanner.model.plan.Itinerary;
import org.opentripplanner.routing.api.request.RoutingRequest;
import org.opentripplanner.routing.core.RoutingContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -11,12 +12,12 @@ public class ItinerariesHelper {

public static void decorateItinerariesWithRequestData(
List<Itinerary> itineraries,
RoutingRequest request
RoutingContext routingContext
) {
for (Itinerary it : itineraries) {
// Communicate the fact that the only way we were able to get a response
// was by removing a slope limit.
it.tooSloped = request.getRoutingContext().slopeRestrictionRemoved;
it.tooSloped = routingContext.slopeRestrictionRemoved;
}
}
}
Loading

0 comments on commit eaf3a05

Please sign in to comment.