use of org.opentripplanner.model.TripPattern in project OpenTripPlanner by opentripplanner.
the class TimetableSnapshotSource method addTripToGraphAndBuffer.
/**
* Add a (new) trip to the graph and the buffer
*
* @param graph graph
* @param trip trip
* @param tripUpdate trip update containing stop time updates
* @param stops list of stops corresponding to stop time updates
* @param serviceDate service date of trip
* @param realTimeState real-time state of new trip
* @return true iff successful
*/
private boolean addTripToGraphAndBuffer(final Graph graph, final Trip trip, final TripUpdate tripUpdate, final List<StopLocation> stops, final ServiceDate serviceDate, final RealTimeState realTimeState) {
// Preconditions
Preconditions.checkNotNull(stops);
Preconditions.checkArgument(tripUpdate.getStopTimeUpdateCount() == stops.size(), "number of stop should match the number of stop time updates");
// Calculate seconds since epoch on GTFS midnight (noon minus 12h) of service date
final Calendar serviceCalendar = serviceDate.getAsCalendar(timeZone);
final long midnightSecondsSinceEpoch = serviceCalendar.getTimeInMillis() / MILLIS_PER_SECOND;
// Create StopTimes
final List<StopTime> stopTimes = new ArrayList<>(tripUpdate.getStopTimeUpdateCount());
for (int index = 0; index < tripUpdate.getStopTimeUpdateCount(); ++index) {
final StopTimeUpdate stopTimeUpdate = tripUpdate.getStopTimeUpdate(index);
final var stop = stops.get(index);
// Determine whether stop is skipped
final boolean skippedStop = isStopSkipped(stopTimeUpdate);
// Only create stop time for non-skipped stops
if (!skippedStop) {
// Create stop time
final StopTime stopTime = new StopTime();
stopTime.setTrip(trip);
stopTime.setStop(stop);
// Set arrival time
if (stopTimeUpdate.hasArrival() && stopTimeUpdate.getArrival().hasTime()) {
final long arrivalTime = stopTimeUpdate.getArrival().getTime() - midnightSecondsSinceEpoch;
if (arrivalTime < 0 || arrivalTime > MAX_ARRIVAL_DEPARTURE_TIME) {
LOG.warn("ADDED trip has invalid arrival time (compared to start date in " + "TripDescriptor), skipping.");
return false;
}
stopTime.setArrivalTime((int) arrivalTime);
}
// Set departure time
if (stopTimeUpdate.hasDeparture() && stopTimeUpdate.getDeparture().hasTime()) {
final long departureTime = stopTimeUpdate.getDeparture().getTime() - midnightSecondsSinceEpoch;
if (departureTime < 0 || departureTime > MAX_ARRIVAL_DEPARTURE_TIME) {
LOG.warn("ADDED trip has invalid departure time (compared to start date in " + "TripDescriptor), skipping.");
return false;
}
stopTime.setDepartureTime((int) departureTime);
}
// Exact time
stopTime.setTimepoint(1);
if (stopTimeUpdate.hasStopSequence()) {
stopTime.setStopSequence(stopTimeUpdate.getStopSequence());
}
// Set different pickup type for last stop
if (index == tripUpdate.getStopTimeUpdateCount() - 1) {
// No pickup available
stopTime.setPickupType(NONE);
} else {
// Regularly scheduled pickup
stopTime.setPickupType(SCHEDULED);
}
// Set different drop off type for first stop
if (index == 0) {
// No drop off available
stopTime.setDropOffType(NONE);
} else {
// Regularly scheduled drop off
stopTime.setDropOffType(SCHEDULED);
}
// Add stop time to list
stopTimes.add(stopTime);
}
}
// TODO: filter/interpolate stop times like in PatternHopFactory?
// Create StopPattern
final StopPattern stopPattern = new StopPattern(stopTimes);
// Get cached trip pattern or create one if it doesn't exist yet
final TripPattern pattern = tripPatternCache.getOrCreateTripPattern(stopPattern, trip, graph);
// Add service code to bitset of pattern if needed (using copy on write)
final int serviceCode = graph.getServiceCodes().get(trip.getServiceId());
if (!pattern.getServices().get(serviceCode)) {
final BitSet services = (BitSet) pattern.getServices().clone();
services.set(serviceCode);
pattern.setServices(services);
}
// Create new trip times
final TripTimes newTripTimes = new TripTimes(trip, stopTimes, graph.deduplicator);
// TODO: should we incorporate the delay field if present?
for (int stopIndex = 0; stopIndex < newTripTimes.getNumStops(); stopIndex++) {
newTripTimes.updateArrivalTime(stopIndex, newTripTimes.getScheduledArrivalTime(stopIndex));
newTripTimes.updateDepartureTime(stopIndex, newTripTimes.getScheduledDepartureTime(stopIndex));
}
// Set service code of new trip times
newTripTimes.setServiceCode(serviceCode);
// Make sure that updated trip times have the correct real time state
newTripTimes.setRealTimeState(realTimeState);
// Add new trip times to the buffer
final boolean success = buffer.update(pattern, newTripTimes, serviceDate);
return success;
}
use of org.opentripplanner.model.TripPattern in project OpenTripPlanner by opentripplanner.
the class OtpTransitServiceBuilderLimitPeriodTest method createTripPattern.
private TripPattern createTripPattern(String id, Collection<Trip> trips) {
FeedScopedId patternId = new FeedScopedId(FEED_ID, trips.stream().map(t -> t.getId().getId()).collect(Collectors.joining(":")));
TripPattern p = new TripPattern(patternId, route, STOP_PATTERN);
p.setName("Pattern");
for (Trip trip : trips) {
p.add(new TripTimes(trip, STOP_TIMES, DEDUPLICATOR));
}
return p;
}
use of org.opentripplanner.model.TripPattern in project OpenTripPlanner by opentripplanner.
the class OtpTransitServiceBuilderLimitPeriodTest method testLimitPeriod.
@Test
public void testLimitPeriod() {
// Assert the test is set up as expected
assertEquals(2, subject.getCalendars().size());
assertEquals(2, subject.getCalendarDates().size());
assertEquals(4, subject.getTripsById().size());
assertEquals(3, subject.getTripPatterns().get(STOP_PATTERN).size());
assertEquals(2, patternInT1.scheduledTripsAsStream().count());
assertEquals(2, patternInT1.getScheduledTimetable().getTripTimes().size());
assertEquals(1, patternInT2.scheduledTripsAsStream().count());
assertEquals(1, patternInT2.getScheduledTimetable().getTripTimes().size());
// Limit service to last half of month
subject.limitServiceDays(new ServiceDateInterval(D2, D3));
// Verify calendar
List<ServiceCalendar> calendars = subject.getCalendars();
assertEquals(calendars.toString(), 1, calendars.size());
assertEquals(calendars.toString(), SERVICE_C_IN, calendars.get(0).getServiceId());
// Verify calendar dates
List<ServiceCalendarDate> dates = subject.getCalendarDates();
assertEquals(dates.toString(), 1, dates.size());
assertEquals(dates.toString(), SERVICE_D_IN, dates.get(0).getServiceId());
// Verify trips
EntityById<Trip> trips = subject.getTripsById();
assertEquals(trips.toString(), 2, trips.size());
assertTrue(trips.toString(), trips.containsKey(tripCSIn.getId()));
assertTrue(trips.toString(), trips.containsKey(tripCSDIn.getId()));
// Verify patterns
Collection<TripPattern> patterns = subject.getTripPatterns().get(STOP_PATTERN);
assertEquals(2, patterns.size());
assertTrue(patterns.toString(), patterns.contains(patternInT1));
assertTrue(patterns.toString(), patterns.contains(patternInT2));
// Verify trips in pattern (one trip is removed from patternInT1)
assertEquals(1, patternInT1.scheduledTripsAsStream().count());
assertEquals(tripCSIn, patternInT1.scheduledTripsAsStream().findFirst().get());
// Verify trips in pattern is unchanged (one trip)
assertEquals(1, patternInT2.scheduledTripsAsStream().count());
// Verify scheduledTimetable trips (one trip is removed from patternInT1)
assertEquals(1, patternInT1.getScheduledTimetable().getTripTimes().size());
assertEquals(tripCSIn, patternInT1.getScheduledTimetable().getTripTimes().get(0).getTrip());
// Verify scheduledTimetable trips in pattern is unchanged (one trip)
assertEquals(1, patternInT2.getScheduledTimetable().getTripTimes().size());
}
use of org.opentripplanner.model.TripPattern in project OpenTripPlanner by opentripplanner.
the class NetexBundleSmokeTest method assertTripPatterns.
private void assertTripPatterns(Collection<TripPattern> patterns) {
Map<FeedScopedId, TripPattern> map = patterns.stream().collect(Collectors.toMap(TripPattern::getId, s -> s));
TripPattern p = map.get(fId("RUT:JourneyPattern:12-1"));
assertEquals("Jernbanetorget", p.getTripHeadsign());
assertEquals("RB", p.getFeedId());
assertEquals("[<Stop RB:NSR:Quay:7203>, <Stop RB:NSR:Quay:8027>]", p.getStops().toString());
assertEquals("[<Trip RB:RUT:ServiceJourney:12-101375-1000>]", p.scheduledTripsAsStream().collect(Collectors.toList()).toString());
// TODO OTP2 - Why?
assertNull(p.getServices());
assertEquals(4, patterns.size());
}
use of org.opentripplanner.model.TripPattern in project OpenTripPlanner by opentripplanner.
the class StreetGraphFinderTest method setUp.
@BeforeEach
protected void setUp() throws Exception {
var graph = graphOf(new Builder() {
@Override
public void build() {
var a = agency("Agency");
R1 = route("R1", TransitMode.BUS, a);
R2 = route("R2", TransitMode.TRAM, a);
S1 = stop("S1", 47.500, 19.001);
S2 = stop("S2", 47.510, 19.001);
S3 = stop("S3", 47.520, 19.001);
BR1 = vehicleRentalStation("BR1", 47.500, 18.999);
BR2 = vehicleRentalStation("BR2", 47.520, 18.999);
A = intersection("A", 47.500, 19.00);
B = intersection("B", 47.510, 19.00);
C = intersection("C", 47.520, 19.00);
D = intersection("D", 47.530, 19.00);
vehicleParking("BP1", 47.520, 18.999, true, false, List.of(vehicleParkingEntrance(C, "BP1 Entrance", false, true)));
vehicleParking("PR1", 47.510, 18.999, false, true, List.of(vehicleParkingEntrance(B, "PR1 Entrance", true, true)));
vehicleParking("PR2", 47.530, 18.999, false, true, List.of(vehicleParkingEntrance(D, "PR2 Entrance", true, true)));
biLink(A, S1);
biLink(A, BR1);
biLink(B, S2);
biLink(C, S3);
biLink(C, BR2);
street(A, B, 100, StreetTraversalPermission.ALL);
street(B, C, 100, StreetTraversalPermission.ALL);
street(C, D, 100, StreetTraversalPermission.ALL);
tripPattern(TP1 = new TripPattern(new FeedScopedId("F", "TP1"), R1, new StopPattern(List.of(st(S1), st(S2)))));
tripPattern(TP2 = new TripPattern(new FeedScopedId("F", "TP2"), R2, new StopPattern(List.of(st(S1), st(S3)))));
}
});
graph.index = new GraphIndex(graph);
routingService = new RoutingService(graph);
graphFinder = new StreetGraphFinder(graph);
}
Aggregations