Skip to content

Commit

Permalink
Merge pull request #5953 from entur/use_transit_editor_service_in_sir…
Browse files Browse the repository at this point in the history
…i_timetable_snapshot_source

Use transit editor service in SIRI timetable snapshot source
  • Loading branch information
vpaturet authored Jul 16, 2024
2 parents dffd3d8 + d880072 commit baa490c
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.opentripplanner.transit.model.timetable.TripIdAndServiceDate;
import org.opentripplanner.transit.service.DefaultTransitService;
import org.opentripplanner.transit.service.StopModel;
import org.opentripplanner.transit.service.TransitEditorService;
import org.opentripplanner.transit.service.TransitModel;
import org.opentripplanner.updater.spi.UpdateError;
import uk.org.siri.siri20.VehicleModesEnumeration;
Expand Down Expand Up @@ -77,6 +78,7 @@ class AddedTripBuilderTest {

private final Deduplicator DEDUPLICATOR = new Deduplicator();
private final TransitModel TRANSIT_MODEL = new TransitModel(STOP_MODEL, DEDUPLICATOR);
private TransitEditorService transitService;
private EntityResolver ENTITY_RESOLVER;

@BeforeEach
Expand All @@ -101,6 +103,7 @@ void setUp() {

// Create transit model index
TRANSIT_MODEL.index();
transitService = new DefaultTransitService(TRANSIT_MODEL);

// Create the entity resolver only after the model has been indexed
ENTITY_RESOLVER =
Expand All @@ -110,7 +113,7 @@ void setUp() {
@Test
void testAddedTrip() {
var addedTrip = new AddedTripBuilder(
TRANSIT_MODEL,
transitService,
ENTITY_RESOLVER,
AbstractTransitEntity::getId,
TRIP_ID,
Expand Down Expand Up @@ -239,7 +242,7 @@ void testAddedTrip() {
@Test
void testAddedTripOnAddedRoute() {
var firstAddedTrip = new AddedTripBuilder(
TRANSIT_MODEL,
transitService,
ENTITY_RESOLVER,
AbstractTransitEntity::getId,
TRIP_ID,
Expand All @@ -265,7 +268,7 @@ void testAddedTripOnAddedRoute() {
var tripId2 = TransitModelForTest.id("TRIP_ID_2");

var secondAddedTrip = new AddedTripBuilder(
TRANSIT_MODEL,
transitService,
ENTITY_RESOLVER,
AbstractTransitEntity::getId,
tripId2,
Expand Down Expand Up @@ -316,7 +319,7 @@ void testAddedTripOnAddedRoute() {
@Test
void testAddedTripOnExistingRoute() {
var addedTrip = new AddedTripBuilder(
TRANSIT_MODEL,
transitService,
ENTITY_RESOLVER,
AbstractTransitEntity::getId,
TRIP_ID,
Expand Down Expand Up @@ -347,7 +350,7 @@ void testAddedTripOnExistingRoute() {
@Test
void testAddedTripWithoutReplacedRoute() {
var addedTrip = new AddedTripBuilder(
TRANSIT_MODEL,
transitService,
ENTITY_RESOLVER,
AbstractTransitEntity::getId,
TRIP_ID,
Expand Down Expand Up @@ -390,7 +393,7 @@ void testAddedTripWithoutReplacedRoute() {
@Test
void testAddedTripFailOnMissingServiceId() {
var addedTrip = new AddedTripBuilder(
TRANSIT_MODEL,
transitService,
ENTITY_RESOLVER,
AbstractTransitEntity::getId,
TRIP_ID,
Expand Down Expand Up @@ -445,7 +448,7 @@ void testAddedTripFailOnNonIncreasingDwellTime() {
);

var addedTrip = new AddedTripBuilder(
TRANSIT_MODEL,
transitService,
ENTITY_RESOLVER,
AbstractTransitEntity::getId,
TRIP_ID,
Expand Down Expand Up @@ -484,7 +487,7 @@ void testAddedTripFailOnTooFewCalls() {
.build()
);
var addedTrip = new AddedTripBuilder(
TRANSIT_MODEL,
transitService,
ENTITY_RESOLVER,
AbstractTransitEntity::getId,
TRIP_ID,
Expand Down Expand Up @@ -531,7 +534,7 @@ void testAddedTripFailOnUnknownStop() {
.build()
);
var addedTrip = new AddedTripBuilder(
TRANSIT_MODEL,
transitService,
ENTITY_RESOLVER,
AbstractTransitEntity::getId,
TRIP_ID,
Expand Down
46 changes: 21 additions & 25 deletions src/ext/java/org/opentripplanner/ext/siri/AddedTripBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
import org.opentripplanner.transit.model.timetable.TripIdAndServiceDate;
import org.opentripplanner.transit.model.timetable.TripOnServiceDate;
import org.opentripplanner.transit.model.timetable.TripTimesFactory;
import org.opentripplanner.transit.service.TransitModel;
import org.opentripplanner.transit.service.TransitEditorService;
import org.opentripplanner.updater.spi.DataValidationExceptionMapper;
import org.opentripplanner.updater.spi.UpdateError;
import org.rutebanken.netex.model.BusSubmodeEnumeration;
Expand All @@ -52,7 +52,7 @@
class AddedTripBuilder {

private static final Logger LOG = LoggerFactory.getLogger(AddedTripBuilder.class);
private final TransitModel transitModel;
private final TransitEditorService transitService;
private final EntityResolver entityResolver;
private final ZoneId timeZone;
private final Function<Trip, FeedScopedId> getTripPatternId;
Expand All @@ -73,7 +73,7 @@ class AddedTripBuilder {

AddedTripBuilder(
EstimatedVehicleJourney estimatedVehicleJourney,
TransitModel transitModel,
TransitEditorService transitService,
EntityResolver entityResolver,
Function<Trip, FeedScopedId> getTripPatternId
) {
Expand Down Expand Up @@ -112,16 +112,16 @@ class AddedTripBuilder {

calls = CallWrapper.of(estimatedVehicleJourney);

this.transitModel = transitModel;
this.transitService = transitService;
this.entityResolver = entityResolver;
this.getTripPatternId = getTripPatternId;
timeZone = transitModel.getTimeZone();
timeZone = transitService.getTimeZone();

replacedTrips = getReplacedVehicleJourneys(estimatedVehicleJourney);
}

AddedTripBuilder(
TransitModel transitModel,
TransitEditorService transitService,
EntityResolver entityResolver,
Function<Trip, FeedScopedId> getTripPatternId,
FeedScopedId tripId,
Expand All @@ -139,9 +139,9 @@ class AddedTripBuilder {
String headsign,
List<TripOnServiceDate> replacedTrips
) {
this.transitModel = transitModel;
this.transitService = transitService;
this.entityResolver = entityResolver;
this.timeZone = transitModel.getTimeZone();
this.timeZone = transitService.getTimeZone();
this.getTripPatternId = getTripPatternId;
this.tripId = tripId;
this.operator = operator;
Expand All @@ -168,7 +168,7 @@ Result<TripUpdate, UpdateError> build() {
return UpdateError.result(tripId, NO_START_DATE);
}

FeedScopedId calServiceId = transitModel.getOrCreateServiceIdForDate(serviceDate);
FeedScopedId calServiceId = transitService.getOrCreateServiceIdForDate(serviceDate);
if (calServiceId == null) {
return UpdateError.result(tripId, NO_START_DATE);
}
Expand All @@ -181,7 +181,7 @@ Result<TripUpdate, UpdateError> build() {
}
route = createRoute(agency);
LOG.info("Adding route {} to transitModel.", route);
transitModel.getTransitModelIndex().addRoutes(route);
transitService.addRoutes(route);
}

Trip trip = createTrip(route, calServiceId);
Expand Down Expand Up @@ -221,14 +221,14 @@ Result<TripUpdate, UpdateError> build() {
RealTimeTripTimes tripTimes = TripTimesFactory.tripTimes(
trip,
aimedStopTimes,
transitModel.getDeduplicator()
transitService.getDeduplicator()
);
// validate the scheduled trip times
// they are in general superseded by real-time trip times
// but in case of trip cancellation, OTP will fall back to scheduled trip times
// therefore they must be valid
tripTimes.validateNonIncreasingTimes();
tripTimes.setServiceCode(transitModel.getServiceCodes().get(trip.getServiceId()));
tripTimes.setServiceCode(transitService.getServiceCodeForId(trip.getServiceId()));
pattern.add(tripTimes);
RealTimeTripTimes updatedTripTimes = tripTimes.copyScheduledTimes();

Expand Down Expand Up @@ -267,17 +267,14 @@ Result<TripUpdate, UpdateError> build() {

// Adding trip to index necessary to include values in graphql-queries
// TODO - SIRI: should more data be added to index?
transitModel.getTransitModelIndex().getTripForId().put(tripId, trip);
transitModel.getTransitModelIndex().getPatternForTrip().put(trip, pattern);
transitModel.getTransitModelIndex().getPatternsForRoute().put(route, pattern);
transitModel
.getTransitModelIndex()
.getTripOnServiceDateById()
.put(tripOnServiceDate.getId(), tripOnServiceDate);
transitModel
.getTransitModelIndex()
.getTripOnServiceDateForTripAndDay()
.put(new TripIdAndServiceDate(tripId, serviceDate), tripOnServiceDate);
transitService.addTripForId(tripId, trip);
transitService.addPatternForTrip(trip, pattern);
transitService.addPatternsForRoute(route, pattern);
transitService.addTripOnServiceDateById(tripOnServiceDate.getId(), tripOnServiceDate);
transitService.addTripOnServiceDateForTripAndDay(
new TripIdAndServiceDate(tripId, serviceDate),
tripOnServiceDate
);

return Result.success(new TripUpdate(stopPattern, updatedTripTimes, serviceDate));
}
Expand Down Expand Up @@ -312,8 +309,7 @@ private Route createRoute(Agency agency) {
*/
@Nullable
private Agency resolveAgency() {
return transitModel
.getTransitModelIndex()
return transitService
.getAllRoutes()
.stream()
.filter(r -> r != null && r.getOperator() != null && r.getOperator().equals(operator))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
import org.opentripplanner.transit.model.timetable.Trip;
import org.opentripplanner.transit.model.timetable.TripTimes;
import org.opentripplanner.transit.service.DefaultTransitService;
import org.opentripplanner.transit.service.TransitEditorService;
import org.opentripplanner.transit.service.TransitModel;
import org.opentripplanner.transit.service.TransitService;
import org.opentripplanner.updater.TimetableSnapshotSourceParameters;
import org.opentripplanner.updater.spi.DataValidationExceptionMapper;
import org.opentripplanner.updater.spi.UpdateError;
Expand Down Expand Up @@ -56,9 +56,8 @@ public class SiriTimetableSnapshotSource implements TimetableSnapshotProvider {
* messages.
*/
private final SiriTripPatternCache tripPatternCache;
private final TransitModel transitModel;

private final TransitService transitService;
private final TransitEditorService transitService;

private final TimetableSnapshotManager snapshotManager;

Expand All @@ -72,7 +71,6 @@ public SiriTimetableSnapshotSource(
parameters,
() -> LocalDate.now(transitModel.getTimeZone())
);
this.transitModel = transitModel;
this.transitService = new DefaultTransitService(transitModel);
this.tripPatternCache =
new SiriTripPatternCache(tripPatternIdGenerator, transitService::getPatternForTrip);
Expand Down Expand Up @@ -115,7 +113,7 @@ public UpdateResult applyEstimatedTimetable(
var journeys = estimatedJourneyVersion.getEstimatedVehicleJourneies();
LOG.debug("Handling {} EstimatedVehicleJourneys.", journeys.size());
for (EstimatedVehicleJourney journey : journeys) {
results.add(apply(journey, transitModel, fuzzyTripMatcher, entityResolver));
results.add(apply(journey, transitService, fuzzyTripMatcher, entityResolver));
}
}
}
Expand All @@ -135,7 +133,7 @@ public TimetableSnapshot getTimetableSnapshot() {

private Result<UpdateSuccess, UpdateError> apply(
EstimatedVehicleJourney journey,
TransitModel transitModel,
TransitEditorService transitService,
@Nullable SiriFuzzyTripMatcher fuzzyTripMatcher,
EntityResolver entityResolver
) {
Expand All @@ -147,7 +145,7 @@ private Result<UpdateSuccess, UpdateError> apply(
result =
new AddedTripBuilder(
journey,
transitModel,
transitService,
entityResolver,
tripPatternIdGenerator::generateUniqueTripPatternId
)
Expand Down Expand Up @@ -265,7 +263,7 @@ private Result<TripUpdate, UpdateError> handleModifiedTrip(
pattern,
estimatedVehicleJourney,
serviceDate,
transitModel.getTimeZone(),
transitService.getTimeZone(),
entityResolver
)
.build();
Expand Down
Loading

0 comments on commit baa490c

Please sign in to comment.