Skip to content

Commit

Permalink
Add comments and refactor transfer generation.
Browse files Browse the repository at this point in the history
  • Loading branch information
VillePihlava committed Jan 7, 2025
1 parent 4f84804 commit 4d599a2
Showing 1 changed file with 31 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@
import com.google.common.collect.Multimaps;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Collections;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import org.opentripplanner.framework.application.OTPFeature;
import org.opentripplanner.graph_builder.issue.api.DataImportIssueStore;
import org.opentripplanner.graph_builder.issues.StopNotLinkedForTransfers;
Expand All @@ -27,7 +25,6 @@
import org.opentripplanner.routing.graphfinder.NearbyStop;
import org.opentripplanner.street.model.edge.Edge;
import org.opentripplanner.street.model.vertex.TransitStopVertex;
import org.opentripplanner.street.model.vertex.Vertex;
import org.opentripplanner.transit.model.site.RegularStop;
import org.opentripplanner.transit.model.site.StopLocation;
import org.opentripplanner.transit.service.DefaultTransitService;
Expand Down Expand Up @@ -56,6 +53,9 @@ public class DirectTransferGenerator implements GraphBuilderModule {
private final TimetableRepository timetableRepository;
private final DataImportIssueStore issueStore;

/**
* Constructor used in tests. This initializes transferParametersForMode as an empty map.
*/
public DirectTransferGenerator(
Graph graph,
TimetableRepository timetableRepository,
Expand All @@ -68,7 +68,7 @@ public DirectTransferGenerator(
this.issueStore = issueStore;
this.defaultMaxTransferDuration = defaultMaxTransferDuration;
this.transferRequests = transferRequests;
this.transferParametersForMode = Collections.emptyMap();
this.transferParametersForMode = Map.of();
}

public DirectTransferGenerator(
Expand Down Expand Up @@ -155,18 +155,7 @@ public void buildGraph() {
if (sd.stop.transfersNotAllowed()) {
continue;
}
TransferKey transferKey = new TransferKey(stop, sd.stop, sd.edges);
PathTransfer pathTransfer = distinctTransfers.get(transferKey);
if (pathTransfer == null) {
// If the PathTransfer can't be found, it is created.
distinctTransfers.put(
transferKey,
new PathTransfer(stop, sd.stop, sd.distance, sd.edges, EnumSet.of(mode))
);
} else {
// If the PathTransfer is found, a new PathTransfer with the added mode is created.
distinctTransfers.put(transferKey, pathTransfer.withAddedMode(mode));
}
createPathTransfer(stop, sd.stop, sd, distinctTransfers, mode);
}
}
// Calculate flex transfers if flex routing is enabled.
Expand All @@ -187,18 +176,7 @@ public void buildGraph() {
continue;
}
// The TransferKey and PathTransfer are created differently for flex routing.
TransferKey transferKey = new TransferKey(sd.stop, stop, sd.edges);
PathTransfer pathTransfer = distinctTransfers.get(transferKey);
if (pathTransfer == null) {
// If the PathTransfer can't be found, it is created.
distinctTransfers.put(
transferKey,
new PathTransfer(sd.stop, stop, sd.distance, sd.edges, EnumSet.of(mode))
);
} else {
// If the PathTransfer is found, a new PathTransfer with the added mode is created.
distinctTransfers.put(transferKey, pathTransfer.withAddedMode(mode));
}
createPathTransfer(sd.stop, stop, sd, distinctTransfers, mode);
}
}
// Calculate transfers between stops that are visited by trips that allow cars, if configured.
Expand All @@ -220,18 +198,7 @@ public void buildGraph() {
if (!carsAllowedStops.contains(sd.stop)) {
continue;
}
TransferKey transferKey = new TransferKey(stop, sd.stop, sd.edges);
PathTransfer pathTransfer = distinctTransfers.get(transferKey);
if (pathTransfer == null) {
// If the PathTransfer can't be found, it is created.
distinctTransfers.put(
transferKey,
new PathTransfer(stop, sd.stop, sd.distance, sd.edges, EnumSet.of(mode))
);
} else {
// If the PathTransfer is found, a new PathTransfer with the added mode is created.
distinctTransfers.put(transferKey, pathTransfer.withAddedMode(mode));
}
createPathTransfer(stop, sd.stop, sd, distinctTransfers, mode);
}
}
}
Expand Down Expand Up @@ -302,6 +269,30 @@ private NearbyStopFinder createNearbyStopFinder(Duration radiusByDuration) {
}
}

private void createPathTransfer(
StopLocation from,
StopLocation to,
NearbyStop sd,
Map<TransferKey, PathTransfer> distinctTransfers,
StreetMode mode
) {
TransferKey transferKey = new TransferKey(from, to, sd.edges);
PathTransfer pathTransfer = distinctTransfers.get(transferKey);
if (pathTransfer == null) {
// If the PathTransfer can't be found, it is created.
distinctTransfers.put(
transferKey,
new PathTransfer(from, to, sd.distance, sd.edges, EnumSet.of(mode))
);
} else {
// If the PathTransfer is found, a new PathTransfer with the added mode is created.
distinctTransfers.put(transferKey, pathTransfer.withAddedMode(mode));
}
}

/**
* This method parses the given transfer parameters into a transfer configuration and checks for invalid input.
*/
private TransferConfiguration parseTransferParameters(NearbyStopFinder nearbyStopFinder) {
List<RouteRequest> defaultTransferRequests = new ArrayList<>();
List<RouteRequest> carsAllowedStopTransferRequests = new ArrayList<>();
Expand Down

0 comments on commit 4d599a2

Please sign in to comment.