Skip to content

Commit

Permalink
Allow SIRI and GTFS at the same time
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardehrenfried committed Dec 22, 2024
1 parent 4198bb6 commit 7f55cca
Show file tree
Hide file tree
Showing 19 changed files with 99 additions and 160 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import org.opentripplanner.routing.algorithm.raptoradapter.transit.TransitTuningParameters;
import org.opentripplanner.routing.algorithm.raptoradapter.transit.TripSchedule;
import org.opentripplanner.routing.algorithm.raptoradapter.transit.mappers.TransitLayerMapper;
import org.opentripplanner.routing.algorithm.raptoradapter.transit.mappers.TransitLayerUpdater;
import org.opentripplanner.routing.graph.Graph;
import org.opentripplanner.service.realtimevehicles.RealtimeVehicleRepository;
import org.opentripplanner.service.vehicleparking.VehicleParkingRepository;
Expand All @@ -34,7 +33,6 @@
import org.opentripplanner.standalone.server.OTPWebApplication;
import org.opentripplanner.street.model.StreetLimitationParameters;
import org.opentripplanner.street.model.elevation.ElevationUtils;
import org.opentripplanner.transit.service.DefaultTransitService;
import org.opentripplanner.transit.service.TimetableRepository;
import org.opentripplanner.updater.configure.UpdaterConfigurator;
import org.opentripplanner.utils.logging.ProgressTracker;
Expand Down Expand Up @@ -162,7 +160,10 @@ private void setupTransitRoutingServer() {
enableRequestTraceLogging();
createMetricsLogging();

creatTransitLayerForRaptor(timetableRepository(), routerConfig().transitTuningConfig());
createTransitLayerForRaptor(
timetableRepository(),
routerConfig().transitTuningConfig()
);

/* Create updater modules from JSON config. */
UpdaterConfigurator.configure(
Expand All @@ -171,6 +172,7 @@ private void setupTransitRoutingServer() {
vehicleRentalRepository(),
vehicleParkingRepository(),
timetableRepository(),
factory.timetableSnapshotManager(),
routerConfig().updaterConfig()
);

Expand Down Expand Up @@ -207,7 +209,7 @@ private void initEllipsoidToGeoidDifference() {
/**
* Create transit layer for Raptor routing. Here we map the scheduled timetables.
*/
public static void creatTransitLayerForRaptor(
public static void createTransitLayerForRaptor(
TimetableRepository timetableRepository,
TransitTuningParameters tuningParameters
) {
Expand All @@ -223,9 +225,6 @@ public static void creatTransitLayerForRaptor(
timetableRepository.setRealtimeTransitLayer(
new TransitLayer(timetableRepository.getTransitLayer())
);
timetableRepository.setTransitLayerUpdater(
new TransitLayerUpdater(new DefaultTransitService(timetableRepository))
);
}

public static void initializeTransferCache(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.opentripplanner.transit.configure.TransitModule;
import org.opentripplanner.transit.service.TimetableRepository;
import org.opentripplanner.transit.service.TransitService;
import org.opentripplanner.updater.trip.TimetableSnapshotManager;
import org.opentripplanner.visualizer.GraphVisualizer;

/**
Expand Down Expand Up @@ -81,6 +82,7 @@ public interface ConstructApplicationFactory {
VehicleRentalService vehicleRentalService();
VehicleParkingRepository vehicleParkingRepository();
VehicleParkingService vehicleParkingService();
TimetableSnapshotManager timetableSnapshotManager();
DataImportIssueSummary dataImportIssueSummary();

@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,39 @@

import dagger.Binds;
import dagger.Module;
import dagger.Provides;
import jakarta.inject.Singleton;
import java.time.LocalDate;
import org.opentripplanner.model.TimetableSnapshot;
import org.opentripplanner.routing.algorithm.raptoradapter.transit.mappers.TransitLayerUpdater;
import org.opentripplanner.standalone.api.HttpRequestScoped;
import org.opentripplanner.standalone.config.ConfigModel;
import org.opentripplanner.transit.service.DefaultTransitService;
import org.opentripplanner.transit.service.TransitService;
import org.opentripplanner.updater.trip.TimetableSnapshotManager;

@Module
public abstract class TransitModule {

@Binds
@HttpRequestScoped
abstract TransitService bind(DefaultTransitService service);

@Provides
@Singleton
public static TimetableSnapshotManager timetableSnapshotManager(TransitLayerUpdater transitLayerUpdater, ConfigModel config) {
return new TimetableSnapshotManager(transitLayerUpdater, config.routerConfig().updaterConfig().timetableSnapshotParameters(), () -> LocalDate.now());
}

@Provides
@Singleton
public static TransitLayerUpdater transitLayerUpdater(DefaultTransitService service) {
return new TransitLayerUpdater(service);
}

@Provides
public static TimetableSnapshot timetableSnapshot(TimetableSnapshotManager manager) {
return manager.getTimetableSnapshot();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
import org.opentripplanner.transit.model.timetable.TripIdAndServiceDate;
import org.opentripplanner.transit.model.timetable.TripOnServiceDate;
import org.opentripplanner.updater.GraphUpdaterStatus;
import org.opentripplanner.updater.trip.TimetableSnapshotManager;
import org.opentripplanner.utils.collection.CollectionsView;

/**
Expand Down Expand Up @@ -531,9 +532,6 @@ public boolean hasNewTripPatternsForModifiedTrips() {
*/
@Nullable
private TimetableSnapshot lazyGetTimeTableSnapShot() {
if (this.timetableSnapshot == null) {
timetableSnapshot = timetableRepository.getTimetableSnapshot();
}
return this.timetableSnapshot;
}

Expand Down Expand Up @@ -652,11 +650,6 @@ public TransitLayer getRealtimeTransitLayer() {
return this.timetableRepository.getRealtimeTransitLayer();
}

@Override
public void setTransitLayer(TransitLayer transitLayer) {
this.timetableRepository.setTransitLayer(transitLayer);
}

@Override
public void setRealtimeTransitLayer(TransitLayer realtimeTransitLayer) {
timetableRepository.setRealtimeTransitLayer(realtimeTransitLayer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
import org.opentripplanner.graph_builder.issues.NoFutureDates;
import org.opentripplanner.model.FeedInfo;
import org.opentripplanner.model.PathTransfer;
import org.opentripplanner.model.TimetableSnapshot;
import org.opentripplanner.model.TimetableSnapshotProvider;
import org.opentripplanner.model.calendar.CalendarService;
import org.opentripplanner.model.calendar.CalendarServiceData;
import org.opentripplanner.model.calendar.impl.CalendarServiceImpl;
Expand All @@ -54,7 +52,9 @@
import org.opentripplanner.transit.model.site.StopLocation;
import org.opentripplanner.transit.model.timetable.TripOnServiceDate;
import org.opentripplanner.updater.GraphUpdaterManager;
import org.opentripplanner.updater.TimetableSnapshotSourceParameters;
import org.opentripplanner.updater.configure.UpdaterConfigurator;
import org.opentripplanner.updater.trip.TimetableSnapshotManager;
import org.opentripplanner.utils.lang.ObjectUtils;
import org.opentripplanner.utils.time.ServiceDateUtils;
import org.slf4j.Logger;
Expand Down Expand Up @@ -112,15 +112,6 @@ public class TimetableRepository implements Serializable {
*/
private transient TransitLayer transitLayer;

/**
* This updater applies realtime changes queued up for the next TimetableSnapshot such that
* this TimetableRepository.realtimeSnapshot remains aligned with the service represented in
* (this TimetableRepository instance + that next TimetableSnapshot). This is a way of keeping the
* TransitLayer up to date without repeatedly deriving it from scratch every few seconds. The
* same incremental changes are applied to both sets of data and they are published together.
*/
private transient TransitLayerUpdater transitLayerUpdater;

/**
* An optionally present second TransitLayer representing the contents of this TimetableRepository plus
* the results of realtime updates in the latest TimetableSnapshot.
Expand All @@ -132,7 +123,6 @@ public class TimetableRepository implements Serializable {
private final CalendarServiceData calendarServiceData = new CalendarServiceData();

private transient TimetableRepositoryIndex index;
private transient TimetableSnapshotProvider timetableSnapshotProvider = null;
private ZoneId timeZone = null;
private boolean timeZoneExplicitlySet = false;

Expand Down Expand Up @@ -174,24 +164,6 @@ public void index() {
}
}

@Nullable
public TimetableSnapshot getTimetableSnapshot() {
return timetableSnapshotProvider == null
? null
: timetableSnapshotProvider.getTimetableSnapshot();
}

public void initTimetableSnapshotProvider(TimetableSnapshotProvider timetableSnapshotProvider) {
if (this.timetableSnapshotProvider != null) {
throw new IllegalArgumentException(
"We support only one timetableSnapshotSource, there are two implementation; one for " +
"GTFS and one for Netex/Siri. They need to be refactored to work together. This cast " +
"will fail if updaters try setup both."
);
}
this.timetableSnapshotProvider = timetableSnapshotProvider;
}

/** Data model for Raptor routing, with realtime updates applied (if any). */
public TransitLayer getTransitLayer() {
return transitLayer;
Expand Down Expand Up @@ -234,7 +206,7 @@ public void addTransitMode(TransitMode mode) {
transitModes.add(mode);
}

/** List of transit modes that are availible in GTFS data used in this graph **/
/** List of transit modes that are available in GTFS data used in this graph **/
public HashSet<TransitMode> getTransitModes() {
return transitModes;
}
Expand Down Expand Up @@ -479,10 +451,6 @@ public GraphUpdaterManager getUpdaterManager() {
return updaterManager;
}

public TransitLayerUpdater getTransitLayerUpdater() {
return transitLayerUpdater;
}

public Deduplicator getDeduplicator() {
return deduplicator;
}
Expand All @@ -508,10 +476,6 @@ private void updateHasTransit(boolean hasTransit) {
this.hasTransit = this.hasTransit || hasTransit;
}

public void setTransitLayerUpdater(TransitLayerUpdater transitLayerUpdater) {
this.transitLayerUpdater = transitLayerUpdater;
}

/**
* Updating the site repository is only allowed during graph build
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,6 @@ public interface TransitEditorService extends TransitService {
@Nullable
Trip getScheduledTrip(FeedScopedId id);

/**
* Set the original, immutable, transit layer,
* based on scheduled data (not real-time data).
*/
void setTransitLayer(TransitLayer transitLayer);

/**
* Return true if a real-time transit layer is present.
* The real-time transit layer is optional,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.opentripplanner.updater.configure;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -27,6 +28,7 @@
import org.opentripplanner.updater.spi.TimetableSnapshotFlush;
import org.opentripplanner.updater.trip.MqttGtfsRealtimeUpdater;
import org.opentripplanner.updater.trip.PollingTripUpdater;
import org.opentripplanner.updater.trip.TimetableSnapshotManager;
import org.opentripplanner.updater.trip.TimetableSnapshotSource;
import org.opentripplanner.updater.vehicle_parking.AvailabilityDatasourceFactory;
import org.opentripplanner.updater.vehicle_parking.VehicleParkingAvailabilityUpdater;
Expand All @@ -51,15 +53,15 @@ public class UpdaterConfigurator {
private final RealtimeVehicleRepository realtimeVehicleRepository;
private final VehicleRentalRepository vehicleRentalRepository;
private final VehicleParkingRepository parkingRepository;
private SiriTimetableSnapshotSource siriTimetableSnapshotSource = null;
private TimetableSnapshotSource gtfsTimetableSnapshotSource = null;
private final TimetableSnapshotManager snapshotManager;

private UpdaterConfigurator(
Graph graph,
RealtimeVehicleRepository realtimeVehicleRepository,
VehicleRentalRepository vehicleRentalRepository,
VehicleParkingRepository parkingRepository,
TimetableRepository timetableRepository,
TimetableSnapshotManager snapshotManager,
UpdatersParameters updatersParameters
) {
this.graph = graph;
Expand All @@ -68,6 +70,7 @@ private UpdaterConfigurator(
this.timetableRepository = timetableRepository;
this.updatersParameters = updatersParameters;
this.parkingRepository = parkingRepository;
this.snapshotManager = snapshotManager;
}

public static void configure(
Expand All @@ -76,6 +79,7 @@ public static void configure(
VehicleRentalRepository vehicleRentalRepository,
VehicleParkingRepository parkingRepository,
TimetableRepository timetableRepository,
TimetableSnapshotManager snapshotManager,
UpdatersParameters updatersParameters
) {
new UpdaterConfigurator(
Expand All @@ -84,6 +88,7 @@ public static void configure(
vehicleRentalRepository,
parkingRepository,
timetableRepository,
snapshotManager,
updatersParameters
)
.configure();
Expand All @@ -101,18 +106,13 @@ private void configure() {
)
);

TimetableSnapshot timetableSnapshotBuffer = null;
if (siriTimetableSnapshotSource != null) {
timetableSnapshotBuffer = siriTimetableSnapshotSource.getTimetableSnapshotBuffer();
} else if (gtfsTimetableSnapshotSource != null) {
timetableSnapshotBuffer = gtfsTimetableSnapshotSource.getTimetableSnapshotBuffer();
}
TimetableSnapshot timetableSnapshotBuffer = snapshotManager.getTimetableSnapshotBuffer();
GraphUpdaterManager updaterManager = new GraphUpdaterManager(
new DefaultRealTimeUpdateContext(graph, timetableRepository, timetableSnapshotBuffer),
updaters
);

configureTimetableSnapshotFlush(updaterManager);
configureTimetableSnapshotFlush(updaterManager, snapshotManager);

updaterManager.startUpdaters();

Expand Down Expand Up @@ -223,42 +223,30 @@ private List<GraphUpdater> createUpdatersFromConfig() {
}

private SiriTimetableSnapshotSource provideSiriTimetableSnapshot() {
if (siriTimetableSnapshotSource == null) {
this.siriTimetableSnapshotSource =
new SiriTimetableSnapshotSource(
updatersParameters.timetableSnapshotParameters(),
timetableRepository
return new SiriTimetableSnapshotSource(
timetableRepository, snapshotManager
);
}

return siriTimetableSnapshotSource;
}

private TimetableSnapshotSource provideGtfsTimetableSnapshot() {
if (gtfsTimetableSnapshotSource == null) {
this.gtfsTimetableSnapshotSource =
new TimetableSnapshotSource(
updatersParameters.timetableSnapshotParameters(),
timetableRepository
return new TimetableSnapshotSource(
timetableRepository,snapshotManager, () -> LocalDate.now(timetableRepository.getTimeZone())
);
}
return gtfsTimetableSnapshotSource;
}

/**
* If SIRI or GTFS real-time updaters are in use, configure a periodic flush of the timetable
* snapshot.
*/
private void configureTimetableSnapshotFlush(GraphUpdaterManager updaterManager) {
if (siriTimetableSnapshotSource != null || gtfsTimetableSnapshotSource != null) {
private void configureTimetableSnapshotFlush(GraphUpdaterManager updaterManager, TimetableSnapshotManager snapshotManager) {
updaterManager
.getScheduler()
.scheduleWithFixedDelay(
new TimetableSnapshotFlush(siriTimetableSnapshotSource, gtfsTimetableSnapshotSource),
new TimetableSnapshotFlush(snapshotManager),
0,
updatersParameters.timetableSnapshotParameters().maxSnapshotFrequency().toSeconds(),
TimeUnit.SECONDS
);
}
}
}
Loading

0 comments on commit 7f55cca

Please sign in to comment.