Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Take realtime patterns into account when storing realtime vehicles #5994

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import javax.annotation.Nonnull;
import org.opentripplanner.service.realtimevehicles.RealtimeVehicleRepository;
Expand All @@ -33,8 +32,16 @@ public DefaultRealtimeVehicleService(TransitService transitService) {
this.transitService = transitService;
}

/**
* Stores the relationship between a list of realtime vehicles with a pattern. If the pattern is
* a realtime-added one, then the original (scheduled) one is used as the key for the map storing
* the information.
*/
@Override
public void setRealtimeVehicles(TripPattern pattern, List<RealtimeVehicle> updates) {
if (pattern.getOriginalTripPattern() != null) {
pattern = pattern.getOriginalTripPattern();
}
vehicles.put(pattern, List.copyOf(updates));
}

Expand All @@ -43,8 +50,18 @@ public void clearRealtimeVehicles(TripPattern pattern) {
vehicles.remove(pattern);
}

/**
* Gets the realtime vehicles for a given pattern. If the pattern is a realtime-added one
* then the original (scheduled) one is used for the lookup instead, so you receive the correct
* result no matter if you use the realtime or static information.
*
* @see DefaultRealtimeVehicleService#setRealtimeVehicles(TripPattern, List)
*/
@Override
public List<RealtimeVehicle> getRealtimeVehicles(@Nonnull TripPattern pattern) {
if (pattern.getOriginalTripPattern() != null) {
pattern = pattern.getOriginalTripPattern();
}
// the list is made immutable during insertion, so we can safely return them
return vehicles.getOrDefault(pattern, List.of());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package org.opentripplanner.service.realtimevehicles.internal;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.opentripplanner.framework.geometry.WgsCoordinate.GREENWICH;
import static org.opentripplanner.transit.model._data.TransitModelForTest.route;
import static org.opentripplanner.transit.model._data.TransitModelForTest.tripPattern;

import java.time.Instant;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.opentripplanner.service.realtimevehicles.model.RealtimeVehicle;
import org.opentripplanner.transit.model._data.TransitModelForTest;
import org.opentripplanner.transit.model.network.Route;
import org.opentripplanner.transit.model.network.StopPattern;
import org.opentripplanner.transit.model.network.TripPattern;
import org.opentripplanner.transit.service.DefaultTransitService;
import org.opentripplanner.transit.service.TransitModel;

class DefaultRealtimeVehicleServiceTest {

private static final Route ROUTE = route("r1").build();
private static final TransitModelForTest MODEL = TransitModelForTest.of();
private static final StopPattern STOP_PATTERN = TransitModelForTest.stopPattern(
MODEL.stop("1").build(),
MODEL.stop("2").build()
);
private static final TripPattern ORIGINAL = tripPattern("original", ROUTE)
.withStopPattern(STOP_PATTERN)
.build();
private static final Instant TIME = Instant.ofEpochSecond(1000);
private static final List<RealtimeVehicle> VEHICLES = List.of(
RealtimeVehicle.builder().withTime(TIME).withCoordinates(GREENWICH).build()
);

@Test
void originalPattern() {
var service = new DefaultRealtimeVehicleService(new DefaultTransitService(new TransitModel()));
service.setRealtimeVehicles(ORIGINAL, VEHICLES);
var updates = service.getRealtimeVehicles(ORIGINAL);
assertEquals(VEHICLES, updates);
}

@Test
void realtimeAddedPattern() {
var service = new DefaultRealtimeVehicleService(new DefaultTransitService(new TransitModel()));
var realtimePattern = tripPattern("realtime-added", ROUTE)
.withStopPattern(STOP_PATTERN)
.withOriginalTripPattern(ORIGINAL)
.withCreatedByRealtimeUpdater(true)
.build();
service.setRealtimeVehicles(realtimePattern, VEHICLES);
var updates = service.getRealtimeVehicles(ORIGINAL);
assertEquals(VEHICLES, updates);
}
}
Loading