Search in sources :

Example 36 with TripPattern

use of org.opentripplanner.model.TripPattern in project OpenTripPlanner by opentripplanner.

the class GtfsGraphBuilderModuleTest method testNoBikesByDefault.

@Test
public void testNoBikesByDefault() throws IOException {
    // We configure two trip: one with unknown bikes_allowed and the second with bikes
    // allowed.
    MockGtfs gtfs = getSimpleGtfs();
    gtfs.putTrips(2, "r0", "sid0", "bikes_allowed=0,1");
    gtfs.putStopTimes("t0,t1", "s0,s1");
    List<GtfsBundle> bundleList = getGtfsAsBundleList(gtfs);
    bundleList.get(0).setDefaultBikesAllowed(false);
    builder = new GtfsModule(bundleList, ServiceDateInterval.unbounded());
    Graph graph = new Graph();
    builder.buildGraph(graph, _extra);
    graph.index();
    // Feed id is used instead of the agency id for OBA entities.
    GtfsBundle gtfsBundle = bundleList.get(0);
    GtfsFeedId feedId = gtfsBundle.getFeedId();
    Trip trip = graph.index.getTripForId().get(new FeedScopedId(feedId.getId(), "t0"));
    TripPattern pattern = graph.index.getPatternForTrip().get(trip);
    List<Trip> trips = pattern.getTrips();
    assertEquals(BikeAccess.UNKNOWN, BikeAccess.fromTrip(withId(trips, new FeedScopedId(feedId.getId(), "t0"))));
    assertEquals(BikeAccess.ALLOWED, BikeAccess.fromTrip(withId(trips, new FeedScopedId(feedId.getId(), "t1"))));
}
Also used : Trip(org.opentripplanner.model.Trip) Graph(org.opentripplanner.routing.graph.Graph) GtfsBundle(org.opentripplanner.graph_builder.model.GtfsBundle) MockGtfs(org.opentripplanner.gtfs.MockGtfs) FeedScopedId(org.opentripplanner.model.FeedScopedId) TripPattern(org.opentripplanner.model.TripPattern) Test(org.junit.Test)

Example 37 with TripPattern

use of org.opentripplanner.model.TripPattern in project OpenTripPlanner by opentripplanner.

the class RoutingServiceTest method testPatternsCoherent.

/**
 * Check that bidirectional relationships between TripPatterns and Trips, Routes, and Stops are coherent.
 */
public void testPatternsCoherent() {
    for (Trip trip : graph.index.getTripForId().values()) {
        TripPattern pattern = graph.index.getPatternForTrip().get(trip);
        assertTrue(pattern.getTrips().contains(trip));
    }
    /* This one depends on a feed where each TripPattern appears on only one route. */
    for (Route route : graph.index.getAllRoutes()) {
        for (TripPattern pattern : graph.index.getPatternsForRoute().get(route)) {
            assertEquals(pattern.route, route);
        }
    }
    for (Stop stop : graph.index.getAllStops()) {
        for (TripPattern pattern : graph.index.getPatternsForStop(stop)) {
            assertTrue(pattern.stopPattern.containsStop(stop.getId().toString()));
        }
    }
}
Also used : Trip(org.opentripplanner.model.Trip) Stop(org.opentripplanner.model.Stop) TripPattern(org.opentripplanner.model.TripPattern) Route(org.opentripplanner.model.Route)

Example 38 with TripPattern

use of org.opentripplanner.model.TripPattern in project OpenTripPlanner by opentripplanner.

the class TimetableSnapshotSourceTest method testHandleAddedTrip.

@Test
public void testHandleAddedTrip() throws ParseException {
    // GIVEN
    // Get service date of today because old dates will be purged after applying updates
    final ServiceDate serviceDate = new ServiceDate(Calendar.getInstance());
    final String addedTripId = "added_trip";
    TripUpdate tripUpdate;
    {
        final TripDescriptor.Builder tripDescriptorBuilder = TripDescriptor.newBuilder();
        tripDescriptorBuilder.setTripId(addedTripId);
        tripDescriptorBuilder.setScheduleRelationship(TripDescriptor.ScheduleRelationship.ADDED);
        tripDescriptorBuilder.setStartDate(serviceDate.asCompactString());
        final Calendar calendar = serviceDate.getAsCalendar(graph.getTimeZone());
        final long midnightSecondsSinceEpoch = calendar.getTimeInMillis() / 1000;
        final TripUpdate.Builder tripUpdateBuilder = TripUpdate.newBuilder();
        tripUpdateBuilder.setTrip(tripDescriptorBuilder);
        {
            // Stop A
            final StopTimeUpdate.Builder stopTimeUpdateBuilder = tripUpdateBuilder.addStopTimeUpdateBuilder();
            stopTimeUpdateBuilder.setScheduleRelationship(StopTimeUpdate.ScheduleRelationship.SCHEDULED);
            stopTimeUpdateBuilder.setStopId("A");
            {
                // Arrival
                final StopTimeEvent.Builder arrivalBuilder = stopTimeUpdateBuilder.getArrivalBuilder();
                arrivalBuilder.setTime(midnightSecondsSinceEpoch + (8 * 3600) + (30 * 60));
                arrivalBuilder.setDelay(0);
            }
            {
                // Departure
                final StopTimeEvent.Builder departureBuilder = stopTimeUpdateBuilder.getDepartureBuilder();
                departureBuilder.setTime(midnightSecondsSinceEpoch + (8 * 3600) + (30 * 60));
                departureBuilder.setDelay(0);
            }
        }
        {
            // Stop C
            final StopTimeUpdate.Builder stopTimeUpdateBuilder = tripUpdateBuilder.addStopTimeUpdateBuilder();
            stopTimeUpdateBuilder.setScheduleRelationship(StopTimeUpdate.ScheduleRelationship.SCHEDULED);
            stopTimeUpdateBuilder.setStopId("C");
            {
                // Arrival
                final StopTimeEvent.Builder arrivalBuilder = stopTimeUpdateBuilder.getArrivalBuilder();
                arrivalBuilder.setTime(midnightSecondsSinceEpoch + (8 * 3600) + (40 * 60));
                arrivalBuilder.setDelay(0);
            }
            {
                // Departure
                final StopTimeEvent.Builder departureBuilder = stopTimeUpdateBuilder.getDepartureBuilder();
                departureBuilder.setTime(midnightSecondsSinceEpoch + (8 * 3600) + (45 * 60));
                departureBuilder.setDelay(0);
            }
        }
        {
            // Stop E
            final StopTimeUpdate.Builder stopTimeUpdateBuilder = tripUpdateBuilder.addStopTimeUpdateBuilder();
            stopTimeUpdateBuilder.setScheduleRelationship(StopTimeUpdate.ScheduleRelationship.SCHEDULED);
            stopTimeUpdateBuilder.setStopId("E");
            {
                // Arrival
                final StopTimeEvent.Builder arrivalBuilder = stopTimeUpdateBuilder.getArrivalBuilder();
                arrivalBuilder.setTime(midnightSecondsSinceEpoch + (8 * 3600) + (55 * 60));
                arrivalBuilder.setDelay(0);
            }
            {
                // Departure
                final StopTimeEvent.Builder departureBuilder = stopTimeUpdateBuilder.getDepartureBuilder();
                departureBuilder.setTime(midnightSecondsSinceEpoch + (8 * 3600) + (55 * 60));
                departureBuilder.setDelay(0);
            }
        }
        tripUpdate = tripUpdateBuilder.build();
    }
    // WHEN
    updater.applyTripUpdates(graph, fullDataset, Arrays.asList(tripUpdate), feedId);
    // THEN
    // Find new pattern in graph starting from stop A
    Stop stopA = graph.index.getStopForId(new FeedScopedId(feedId, "A"));
    // Get trip pattern of last (most recently added) outgoing edge
    // FIXME create a new test to see that add-trip realtime updates work
    TripPattern tripPattern = null;
    assertNotNull("Added trip pattern should be found", tripPattern);
    final TimetableSnapshot snapshot = updater.getTimetableSnapshot();
    final Timetable forToday = snapshot.resolve(tripPattern, serviceDate);
    final Timetable schedule = snapshot.resolve(tripPattern, null);
    assertNotSame(forToday, schedule);
    final int forTodayAddedTripIndex = forToday.getTripIndex(addedTripId);
    assertTrue("Added trip should be found in time table for service date", forTodayAddedTripIndex > -1);
    assertEquals(RealTimeState.ADDED, forToday.getTripTimes(forTodayAddedTripIndex).getRealTimeState());
    final int scheduleTripIndex = schedule.getTripIndex(addedTripId);
    assertEquals("Added trip should not be found in scheduled time table", -1, scheduleTripIndex);
}
Also used : Timetable(org.opentripplanner.model.Timetable) TripUpdate(com.google.transit.realtime.GtfsRealtime.TripUpdate) Stop(org.opentripplanner.model.Stop) GtfsContextBuilder.contextBuilder(org.opentripplanner.gtfs.GtfsContextBuilder.contextBuilder) Calendar(java.util.Calendar) TimetableSnapshot(org.opentripplanner.model.TimetableSnapshot) TripPattern(org.opentripplanner.model.TripPattern) ServiceDate(org.opentripplanner.model.calendar.ServiceDate) StopTimeUpdate(com.google.transit.realtime.GtfsRealtime.TripUpdate.StopTimeUpdate) FeedScopedId(org.opentripplanner.model.FeedScopedId) Test(org.junit.Test)

Example 39 with TripPattern

use of org.opentripplanner.model.TripPattern in project OpenTripPlanner by opentripplanner.

the class SiriTimetableSnapshotSource method getPatternsForTrip.

private Set<TripPattern> getPatternsForTrip(Set<Trip> matches, VehicleActivityStructure.MonitoredVehicleJourney monitoredVehicleJourney) {
    if (monitoredVehicleJourney.getOriginRef() == null) {
        return null;
    }
    ZonedDateTime date = monitoredVehicleJourney.getOriginAimedDepartureTime();
    if (date == null) {
        // If no date is set - assume Realtime-data is reported for 'today'.
        date = ZonedDateTime.now();
    }
    ServiceDate realTimeReportedServiceDate = new ServiceDate(date.getYear(), date.getMonthValue(), date.getDayOfMonth());
    Set<TripPattern> patterns = new HashSet<>();
    for (Iterator<Trip> iterator = matches.iterator(); iterator.hasNext(); ) {
        Trip currentTrip = iterator.next();
        TripPattern tripPattern = routingService.getPatternForTrip().get(currentTrip);
        Set<ServiceDate> serviceDates = routingService.getCalendarService().getServiceDatesForServiceId(currentTrip.getServiceId());
        if (!serviceDates.contains(realTimeReportedServiceDate)) {
            // Current trip has no service on the date of the 'MonitoredVehicleJourney'
            continue;
        }
        Stop firstStop = tripPattern.getStop(0);
        Stop lastStop = tripPattern.getStop(tripPattern.getStops().size() - 1);
        String siriOriginRef = monitoredVehicleJourney.getOriginRef().getValue();
        if (monitoredVehicleJourney.getDestinationRef() != null) {
            String siriDestinationRef = monitoredVehicleJourney.getDestinationRef().getValue();
            boolean firstStopIsMatch = firstStop.getId().getId().equals(siriOriginRef);
            boolean lastStopIsMatch = lastStop.getId().getId().equals(siriDestinationRef);
            if (!firstStopIsMatch && firstStop.isPartOfStation()) {
                Stop otherFirstStop = routingService.getStopForId(new FeedScopedId(firstStop.getId().getFeedId(), siriOriginRef));
                firstStopIsMatch = firstStop.isPartOfSameStationAs(otherFirstStop);
            }
            if (!lastStopIsMatch && lastStop.isPartOfStation()) {
                Stop otherLastStop = routingService.getStopForId(new FeedScopedId(lastStop.getId().getFeedId(), siriDestinationRef));
                lastStopIsMatch = lastStop.isPartOfSameStationAs(otherLastStop);
            }
            if (firstStopIsMatch & lastStopIsMatch) {
                // Origin and destination matches
                TripPattern lastAddedTripPattern = buffer.getLastAddedTripPattern(currentTrip.getId(), realTimeReportedServiceDate);
                if (lastAddedTripPattern != null) {
                    patterns.add(lastAddedTripPattern);
                } else {
                    patterns.add(tripPattern);
                }
            }
        } else {
            // Match origin only - since destination is not defined
            if (firstStop.getId().getId().equals(siriOriginRef)) {
                // TODO does this line do anything?
                tripPattern.scheduledTimetable.tripTimes.get(0).getDepartureTime(0);
                patterns.add(tripPattern);
            }
        }
    }
    return patterns;
}
Also used : ServiceDate(org.opentripplanner.model.calendar.ServiceDate) Trip(org.opentripplanner.model.Trip) ZonedDateTime(java.time.ZonedDateTime) Stop(org.opentripplanner.model.Stop) FeedScopedId(org.opentripplanner.model.FeedScopedId) TripPattern(org.opentripplanner.model.TripPattern) HashSet(java.util.HashSet)

Example 40 with TripPattern

use of org.opentripplanner.model.TripPattern in project OpenTripPlanner by opentripplanner.

the class SiriTimetableSnapshotSource method handleModifiedTrip.

private boolean handleModifiedTrip(Graph graph, String feedId, VehicleActivityStructure activity, ServiceDate serviceDate) {
    if (activity.getValidUntilTime().isBefore(ZonedDateTime.now())) {
        // Activity has expired
        return false;
    }
    if (activity.getMonitoredVehicleJourney() == null || activity.getMonitoredVehicleJourney().getVehicleRef() == null || activity.getMonitoredVehicleJourney().getLineRef() == null) {
        // No vehicle reference or line reference
        return false;
    }
    Boolean isMonitored = activity.getMonitoredVehicleJourney().isMonitored();
    if (isMonitored != null && !isMonitored) {
        // Vehicle is reported as NOT monitored
        return false;
    }
    Set<Trip> trips = siriFuzzyTripMatcher.match(activity);
    if (trips == null || trips.isEmpty()) {
        if (keepLogging) {
            String lineRef = (activity.getMonitoredVehicleJourney().getLineRef() != null ? activity.getMonitoredVehicleJourney().getLineRef().getValue() : null);
            String vehicleRef = (activity.getMonitoredVehicleJourney().getVehicleRef() != null ? activity.getMonitoredVehicleJourney().getVehicleRef().getValue() : null);
            String tripId = (activity.getMonitoredVehicleJourney().getCourseOfJourneyRef() != null ? activity.getMonitoredVehicleJourney().getCourseOfJourneyRef().getValue() : null);
            LOG.debug("No trip found for [isMonitored={}, lineRef={}, vehicleRef={}, tripId={}], skipping VehicleActivity.", isMonitored, lineRef, vehicleRef, tripId);
        }
        return false;
    }
    // Find the trip that best corresponds to MonitoredVehicleJourney
    Trip trip = getTripForJourney(trips, activity.getMonitoredVehicleJourney());
    if (trip == null) {
        return false;
    }
    final Set<TripPattern> patterns = getPatternsForTrip(trips, activity.getMonitoredVehicleJourney());
    if (patterns == null) {
        return false;
    }
    boolean success = false;
    for (TripPattern pattern : patterns) {
        if (handleTripPatternUpdate(graph, pattern, activity, trip, serviceDate)) {
            success = true;
        }
    }
    if (!success) {
        LOG.info("Pattern not updated for trip " + trip.getId());
    }
    return success;
}
Also used : Trip(org.opentripplanner.model.Trip) TripPattern(org.opentripplanner.model.TripPattern)

Aggregations

TripPattern (org.opentripplanner.model.TripPattern)61 Trip (org.opentripplanner.model.Trip)26 FeedScopedId (org.opentripplanner.model.FeedScopedId)23 Stop (org.opentripplanner.model.Stop)23 ArrayList (java.util.ArrayList)19 TripTimes (org.opentripplanner.routing.trippattern.TripTimes)19 Timetable (org.opentripplanner.model.Timetable)13 ServiceDate (org.opentripplanner.model.calendar.ServiceDate)13 Route (org.opentripplanner.model.Route)12 Test (org.junit.Test)11 HashSet (java.util.HashSet)8 RoutingService (org.opentripplanner.routing.RoutingService)8 List (java.util.List)7 StopPattern (org.opentripplanner.model.StopPattern)7 TimetableSnapshot (org.opentripplanner.model.TimetableSnapshot)7 GET (javax.ws.rs.GET)6 Path (javax.ws.rs.Path)6 StopTime (org.opentripplanner.model.StopTime)5 TripUpdate (com.google.transit.realtime.GtfsRealtime.TripUpdate)4 StopTimeUpdate (com.google.transit.realtime.GtfsRealtime.TripUpdate.StopTimeUpdate)4