Skip to content

Commit

Permalink
Start work on splitting transfer requests per mode.
Browse files Browse the repository at this point in the history
  • Loading branch information
VillePihlava committed Nov 19, 2024
1 parent 93b49d4 commit 4b6bbc4
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package org.opentripplanner.graph_builder.module;

import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
import com.google.common.collect.Multimaps;
import java.time.Duration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.ConcurrentHashMap;
import org.opentripplanner.framework.application.OTPFeature;
import org.opentripplanner.graph_builder.issue.api.DataImportIssueStore;
import org.opentripplanner.graph_builder.issues.StopNotLinkedForTransfers;
Expand All @@ -17,6 +19,7 @@
import org.opentripplanner.graph_builder.module.nearbystops.StreetNearbyStopFinder;
import org.opentripplanner.model.PathTransfer;
import org.opentripplanner.routing.api.request.RouteRequest;
import org.opentripplanner.routing.api.request.StreetMode;
import org.opentripplanner.routing.graph.Graph;
import org.opentripplanner.routing.graphfinder.NearbyStop;
import org.opentripplanner.street.model.edge.Edge;
Expand Down Expand Up @@ -81,27 +84,35 @@ public void buildGraph() {
AtomicInteger nTransfersTotal = new AtomicInteger();
AtomicInteger nLinkedStops = new AtomicInteger();

// This is a synchronizedMultimap so that a parallel stream may be used to insert elements.
var transfersByStop = Multimaps.<StopLocation, PathTransfer>synchronizedMultimap(
HashMultimap.create()
);
Map<StreetMode, Multimap<StopLocation, PathTransfer>> transfersByStopForMode =
new ConcurrentHashMap<>();
for (RouteRequest transferProfile : transferRequests) {
StreetMode mode = transferProfile.journey().transfer().mode();
// This is a synchronizedMultimap so that a parallel stream may be used to insert elements.
transfersByStopForMode.put(mode, Multimaps.<StopLocation, PathTransfer>synchronizedMultimap(
HashMultimap.create()
));
}

stops
.stream()
.parallel()
.forEach(ts0 -> {
/* Make transfers to each nearby stop that has lowest weight on some trip pattern.
* Use map based on the list of edges, so that only distinct transfers are stored. */
Map<TransferKey, PathTransfer> distinctTransfers = new HashMap<>();
RegularStop stop = ts0.getStop();

if (stop.transfersNotAllowed()) {
return;
}

LOG.debug("Linking stop '{}' {}", stop, ts0);

for (RouteRequest transferProfile : transferRequests) {
StreetMode mode = transferProfile.journey().transfer().mode();

/* Make transfers to each nearby stop that has lowest weight on some trip pattern.
* Use map based on the list of edges, so that only distinct transfers are stored. */
Map<TransferKey, PathTransfer> distinctTransfers = new HashMap<>();

LOG.debug("Linking stop '{}' {} for mode {}.", stop, ts0, mode);

for (NearbyStop sd : nearbyStopFinder.findNearbyStops(
ts0,
transferProfile,
Expand Down Expand Up @@ -142,29 +153,30 @@ public void buildGraph() {
);
}
}
}

LOG.debug(
"Linked stop {} with {} transfers to stops with different patterns.",
stop,
distinctTransfers.size()
);
if (distinctTransfers.isEmpty()) {
issueStore.add(new StopNotLinkedForTransfers(ts0));
} else {
distinctTransfers
.values()
.forEach(transfer -> transfersByStop.put(transfer.from, transfer));
nLinkedStops.incrementAndGet();
nTransfersTotal.addAndGet(distinctTransfers.size());
}
LOG.debug(
"Linked stop {} with {} transfers to stops with different patterns for mode {}.",
stop,
distinctTransfers.size(),
mode
);
if (distinctTransfers.isEmpty()) {
issueStore.add(new StopNotLinkedForTransfers(ts0));
} else {
distinctTransfers
.values()
.forEach(transfer -> transfersByStopForMode.get(mode).put(transfer.from, transfer));
nLinkedStops.incrementAndGet();
nTransfersTotal.addAndGet(distinctTransfers.size());
}

//Keep lambda! A method-ref would causes incorrect class and line number to be logged
//noinspection Convert2MethodRef
progress.step(m -> LOG.info(m));
//Keep lambda! A method-ref would causes incorrect class and line number to be logged
//noinspection Convert2MethodRef
progress.step(m -> LOG.info(m));
}
});

timetableRepository.addAllTransfersByStops(transfersByStop);
timetableRepository.addAllTransfersByStops(transfersByStopForMode);

LOG.info(progress.completeMessage());
LOG.info(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.opentripplanner.model.transfer.DefaultTransferService;
import org.opentripplanner.routing.algorithm.raptoradapter.transit.TransitLayer;
import org.opentripplanner.routing.algorithm.raptoradapter.transit.mappers.TransitLayerUpdater;
import org.opentripplanner.routing.api.request.StreetMode;
import org.opentripplanner.routing.impl.DelegatingTransitAlertServiceImpl;
import org.opentripplanner.routing.services.TransitAlertService;
import org.opentripplanner.routing.util.ConcurrentPublished;
Expand Down Expand Up @@ -98,7 +99,7 @@ public class TimetableRepository implements Serializable {

private final Map<FeedScopedId, Integer> serviceCodes = new HashMap<>();

private final Multimap<StopLocation, PathTransfer> transfersByStop = HashMultimap.create();
private final Map<StreetMode, Multimap<StopLocation, PathTransfer>> transfersByStopForMode = new HashMap<>();

private SiteRepository siteRepository;
private ZonedDateTime transitServiceStarts = LocalDate.MAX.atStartOfDay(ZoneId.systemDefault());
Expand Down Expand Up @@ -430,8 +431,11 @@ public Map<FeedScopedId, Integer> getServiceCodes() {
}

/** Pre-generated transfers between all stops. */
public Collection<PathTransfer> getTransfersByStop(StopLocation stop) {
return transfersByStop.get(stop);
public Collection<PathTransfer> getTransfersByStop(StreetMode mode, StopLocation stop) {
if (transfersByStopForMode.containsKey(mode)) {
return transfersByStopForMode.get(mode).get(stop);
}
return Collections.<PathTransfer>emptyList();
}

public SiteRepository getSiteRepository() {
Expand Down Expand Up @@ -519,9 +523,12 @@ public void setUpdaterManager(GraphUpdaterManager updaterManager) {
this.updaterManager = updaterManager;
}

public void addAllTransfersByStops(Multimap<StopLocation, PathTransfer> transfersByStop) {
public void addAllTransfersByStops(Map<StreetMode, Multimap<StopLocation, PathTransfer>> transfersByStopForMode) {
invalidateIndex();
this.transfersByStop.putAll(transfersByStop);
transfersByStopForMode
.forEach((mode, transfersByStop) -> {
this.transfersByStopForMode.put(mode, HashMultimap.create(transfersByStop));
});
}

/**
Expand Down

0 comments on commit 4b6bbc4

Please sign in to comment.