Search in sources :

Example 46 with TripPattern

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;
}
Also used : StopPattern(org.opentripplanner.model.StopPattern) Calendar(java.util.Calendar) ArrayList(java.util.ArrayList) BitSet(java.util.BitSet) TripPattern(org.opentripplanner.model.TripPattern) StopTimeUpdate(com.google.transit.realtime.GtfsRealtime.TripUpdate.StopTimeUpdate) TripTimes(org.opentripplanner.routing.trippattern.TripTimes) StopTime(org.opentripplanner.model.StopTime)

Example 47 with TripPattern

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;
}
Also used : Trip(org.opentripplanner.model.Trip) FeedScopedId(org.opentripplanner.model.FeedScopedId) TripTimes(org.opentripplanner.routing.trippattern.TripTimes) TripPattern(org.opentripplanner.model.TripPattern)

Example 48 with TripPattern

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());
}
Also used : ServiceCalendarDate(org.opentripplanner.model.calendar.ServiceCalendarDate) Trip(org.opentripplanner.model.Trip) ServiceDateInterval(org.opentripplanner.model.calendar.ServiceDateInterval) TripPattern(org.opentripplanner.model.TripPattern) ServiceCalendar(org.opentripplanner.model.calendar.ServiceCalendar) Test(org.junit.Test)

Example 49 with TripPattern

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());
}
Also used : StopTimeKey(org.opentripplanner.model.StopTimeKey) Trip(org.opentripplanner.model.Trip) MultiModalStation(org.opentripplanner.model.MultiModalStation) Multimap(com.google.common.collect.Multimap) OtpTransitServiceBuilder(org.opentripplanner.model.impl.OtpTransitServiceBuilder) ServiceDate(org.opentripplanner.model.calendar.ServiceDate) CalendarServiceData(org.opentripplanner.model.calendar.CalendarServiceData) ArrayList(java.util.ArrayList) TransitEntity(org.opentripplanner.model.TransitEntity) Map(java.util.Map) ConstantsForTests(org.opentripplanner.ConstantsForTests) DataImportIssueStore(org.opentripplanner.graph_builder.DataImportIssueStore) Assert.fail(org.junit.Assert.fail) BikeAccess(org.opentripplanner.model.BikeAccess) FeedScopedId(org.opentripplanner.model.FeedScopedId) Station(org.opentripplanner.model.Station) Operator(org.opentripplanner.model.Operator) TripPattern(org.opentripplanner.model.TripPattern) Assert.assertNotNull(org.junit.Assert.assertNotNull) Stop(org.opentripplanner.model.Stop) Collection(java.util.Collection) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Notice(org.opentripplanner.model.Notice) Agency(org.opentripplanner.model.Agency) Collectors(java.util.stream.Collectors) Serializable(java.io.Serializable) OtpTransitService(org.opentripplanner.model.OtpTransitService) List(java.util.List) Assert.assertNull(org.junit.Assert.assertNull) Deduplicator(org.opentripplanner.routing.trippattern.Deduplicator) Assert.assertEquals(org.junit.Assert.assertEquals) FeedScopedId(org.opentripplanner.model.FeedScopedId) TripPattern(org.opentripplanner.model.TripPattern)

Example 50 with TripPattern

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);
}
Also used : StopPattern(org.opentripplanner.model.StopPattern) GraphIndex(org.opentripplanner.routing.graph.GraphIndex) RoutingService(org.opentripplanner.routing.RoutingService) FeedScopedId(org.opentripplanner.model.FeedScopedId) TripPattern(org.opentripplanner.model.TripPattern) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

TripPattern (org.opentripplanner.model.TripPattern)94 Trip (org.opentripplanner.model.Trip)34 ArrayList (java.util.ArrayList)32 TripTimes (org.opentripplanner.routing.trippattern.TripTimes)30 FeedScopedId (org.opentripplanner.model.FeedScopedId)27 ServiceDate (org.opentripplanner.model.calendar.ServiceDate)21 Route (org.opentripplanner.model.Route)17 Stop (org.opentripplanner.model.Stop)17 List (java.util.List)16 Timetable (org.opentripplanner.model.Timetable)16 StopPattern (org.opentripplanner.model.StopPattern)15 RoutingService (org.opentripplanner.routing.RoutingService)14 Test (org.junit.Test)13 HashSet (java.util.HashSet)12 Collectors (java.util.stream.Collectors)10 StopLocation (org.opentripplanner.model.StopLocation)9 TimetableSnapshot (org.opentripplanner.model.TimetableSnapshot)9 Map (java.util.Map)8 GET (javax.ws.rs.GET)8 Path (javax.ws.rs.Path)8