Search in sources :

Example 21 with FeedScopedId

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

the class TimetableSnapshotSource method validateAndHandleModifiedTrip.

/**
 * Validate and handle GTFS-RT TripUpdate message containing a MODIFIED trip.
 *
 * @param graph graph to update
 * @param tripUpdate GTFS-RT TripUpdate message
 * @param feedId
 * @param serviceDate
 * @return true iff successful
 */
private boolean validateAndHandleModifiedTrip(final Graph graph, final TripUpdate tripUpdate, final String feedId, final ServiceDate serviceDate) {
    // Preconditions
    Preconditions.checkNotNull(graph);
    Preconditions.checkNotNull(tripUpdate);
    Preconditions.checkNotNull(serviceDate);
    // 
    // Validate modified trip
    // 
    // Check whether trip id of MODIFIED trip is available
    final TripDescriptor tripDescriptor = tripUpdate.getTrip();
    if (!tripDescriptor.hasTripId()) {
        LOG.warn("No trip id found for MODIFIED trip, skipping.");
        return false;
    }
    // Check whether trip id already exists in graph
    String tripId = tripDescriptor.getTripId();
    Trip trip = getTripForTripId(feedId, tripId);
    if (trip == null) {
        // TODO: should we support this and consider it an ADDED trip?
        LOG.warn("Graph does not contain trip id of MODIFIED trip, skipping.");
        return false;
    }
    // Check whether a start date exists
    if (!tripDescriptor.hasStartDate()) {
        // TODO: should we support this and apply update to all days?
        LOG.warn("MODIFIED trip doesn't have a start date in TripDescriptor, skipping.");
        return false;
    } else {
        // Check whether service date is served by trip
        final Set<FeedScopedId> serviceIds = graph.getCalendarService().getServiceIdsOnDate(serviceDate);
        if (!serviceIds.contains(trip.getServiceId())) {
            // TODO: should we support this and change service id of trip?
            LOG.warn("MODIFIED trip has a service date that is not served by trip, skipping.");
            return false;
        }
    }
    // Check whether at least two stop updates exist
    if (tripUpdate.getStopTimeUpdateCount() < 2) {
        LOG.warn("MODIFIED trip has less then two stops, skipping.");
        return false;
    }
    // Check whether all stop times are available and all stops exist
    List<Stop> stops = checkNewStopTimeUpdatesAndFindStops(feedId, tripUpdate);
    if (stops == null) {
        return false;
    }
    // 
    // Handle modified trip
    // 
    final boolean success = handleModifiedTrip(graph, trip, tripUpdate, stops, feedId, serviceDate);
    return success;
}
Also used : Trip(org.opentripplanner.model.Trip) TripDescriptor(com.google.transit.realtime.GtfsRealtime.TripDescriptor) Stop(org.opentripplanner.model.Stop) FeedScopedId(org.opentripplanner.model.FeedScopedId)

Example 22 with FeedScopedId

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

the class TimetableSnapshotSource method handleCanceledTrip.

private boolean handleCanceledTrip(final TripUpdate tripUpdate, final String feedId, final ServiceDate serviceDate) {
    boolean success = false;
    if (tripUpdate.getTrip().hasTripId()) {
        // Try to cancel scheduled trip
        final String tripId = tripUpdate.getTrip().getTripId();
        final boolean cancelScheduledSuccess = cancelScheduledTrip(feedId, tripId, serviceDate);
        // Try to cancel previously added trip
        final boolean cancelPreviouslyAddedSuccess = cancelPreviouslyAddedTrip(new FeedScopedId(feedId, tripId), serviceDate);
        if (cancelScheduledSuccess || cancelPreviouslyAddedSuccess) {
            success = true;
        } else {
            LOG.warn("No pattern found for tripId {}, skipping TripUpdate.", tripId);
        }
    } else {
        LOG.warn("No trip id in CANCELED trip update, skipping TripUpdate.");
    }
    return success;
}
Also used : FeedScopedId(org.opentripplanner.model.FeedScopedId)

Example 23 with FeedScopedId

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

the class TimetableSnapshotSource method getPatternForTripId.

/**
 * Retrieve a trip pattern given a feed id and trip id.
 *
 * @param feedId feed id for the trip id
 * @param tripId trip id without agency
 * @return trip pattern or null if no trip pattern was found
 */
private TripPattern getPatternForTripId(String feedId, String tripId) {
    Trip trip = routingService.getTripForId().get(new FeedScopedId(feedId, tripId));
    TripPattern pattern = routingService.getPatternForTrip().get(trip);
    return pattern;
}
Also used : Trip(org.opentripplanner.model.Trip) FeedScopedId(org.opentripplanner.model.FeedScopedId) TripPattern(org.opentripplanner.model.TripPattern)

Example 24 with FeedScopedId

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

the class TripPatternCache method getOrCreateTripPattern.

/**
 * Get cached trip pattern or create one if it doesn't exist yet. If a trip pattern is created, vertices
 * and edges for this trip pattern are also created in the graph.
 *
 * @param stopPattern stop pattern to retrieve/create trip pattern
 * @param trip the trip the new trip pattern will be created for
 * @param graph graph to add vertices and edges in case a new trip pattern will be created
 * @return cached or newly created trip pattern
 */
public synchronized TripPattern getOrCreateTripPattern(@NotNull final StopPattern stopPattern, @NotNull final Trip trip, @NotNull final Graph graph) {
    Route route = trip.getRoute();
    // Check cache for trip pattern
    TripPattern tripPattern = cache.get(stopPattern);
    // Create TripPattern if it doesn't exist yet
    if (tripPattern == null) {
        tripPattern = new TripPattern(route, stopPattern);
        // Generate unique code for trip pattern
        tripPattern.setId(new FeedScopedId(tripPattern.getFeedId(), generateUniqueTripPatternCode(tripPattern)));
        // Create an empty bitset for service codes (because the new pattern does not contain any trips)
        tripPattern.setServiceCodes(graph.getServiceCodes());
        // Finish scheduled time table
        tripPattern.scheduledTimetable.finish();
        TripPattern originalTripPattern = graph.index.getPatternForTrip().get(trip);
        // Copy information from the TripPattern this is replacing
        if (originalTripPattern != null) {
            tripPattern.setHopGeometriesFromPattern(originalTripPattern);
        }
        // Add pattern to cache
        cache.put(stopPattern, tripPattern);
    }
    return tripPattern;
}
Also used : FeedScopedId(org.opentripplanner.model.FeedScopedId) Route(org.opentripplanner.model.Route) TripPattern(org.opentripplanner.model.TripPattern)

Example 25 with FeedScopedId

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

the class TestSpecificTransfer method testSpecificTransfer.

/**
 * Test different specific transfers
 */
public void testSpecificTransfer() {
    // Setup from trip with route
    Route fromRoute = new Route();
    fromRoute.setId(new FeedScopedId("A1", "R1"));
    Trip fromTrip = new Trip();
    fromTrip.setId(new FeedScopedId("A1", "T1"));
    fromTrip.setRoute(fromRoute);
    // Setup to trip with route
    Route toRoute = new Route();
    toRoute.setId(new FeedScopedId("A1", "R2"));
    Trip toTrip = new Trip();
    toTrip.setId(new FeedScopedId("A1", "T2"));
    toTrip.setRoute(toRoute);
    // Create full SpecificTransfer
    SpecificTransfer s1 = new SpecificTransfer(fromRoute.getId(), toRoute.getId(), fromTrip.getId(), toTrip.getId(), 1);
    assertTrue(s1.matches(fromTrip, toTrip));
    assertTrue(s1.getSpecificity() == SpecificTransfer.MAX_SPECIFICITY);
    assertTrue(s1.transferTime == 1);
    // Create empty SpecificTransfer
    SpecificTransfer s2 = new SpecificTransfer((FeedScopedId) null, null, null, null, 2);
    assertTrue(s2.matches(fromTrip, toTrip));
    assertTrue(s2.getSpecificity() == SpecificTransfer.MIN_SPECIFICITY);
    assertTrue(s2.transferTime == 2);
    // Create SpecificTransfer one trip missing
    SpecificTransfer s3 = new SpecificTransfer(fromRoute.getId(), toRoute.getId(), null, toTrip.getId(), 3);
    assertTrue(s3.matches(fromTrip, toTrip));
    assertTrue(s3.getSpecificity() == 3);
    assertTrue(s3.transferTime == 3);
    // Create SpecificTransfer one trip different
    SpecificTransfer s4 = new SpecificTransfer(fromRoute.getId(), toRoute.getId(), new FeedScopedId("A1", "T3"), toTrip.getId(), 4);
    assertFalse(s4.matches(fromTrip, toTrip));
    assertTrue(s4.getSpecificity() == SpecificTransfer.MAX_SPECIFICITY);
    assertTrue(s4.transferTime == 4);
    // Create SpecificTransfer one trip and route missing
    SpecificTransfer s5 = new SpecificTransfer(null, toRoute.getId(), null, toTrip.getId(), 5);
    assertTrue(s5.matches(fromTrip, toTrip));
    assertTrue(s5.getSpecificity() == 2);
    assertTrue(s5.transferTime == 5);
    // Create SpecificTransfer one trip only
    SpecificTransfer s6 = new SpecificTransfer(null, null, null, toTrip.getId(), 6);
    assertTrue(s6.matches(fromTrip, toTrip));
    assertTrue(s6.getSpecificity() == 2);
    assertTrue(s6.transferTime == 6);
    // Create SpecificTransfer one route only
    SpecificTransfer s7 = new SpecificTransfer(fromRoute.getId(), null, null, null, 7);
    assertTrue(s7.matches(fromTrip, toTrip));
    assertTrue(s7.getSpecificity() == 1);
    assertTrue(s7.transferTime == 7);
}
Also used : Trip(org.opentripplanner.model.Trip) FeedScopedId(org.opentripplanner.model.FeedScopedId) Route(org.opentripplanner.model.Route)

Aggregations

FeedScopedId (org.opentripplanner.model.FeedScopedId)117 Trip (org.opentripplanner.model.Trip)32 Test (org.junit.Test)29 Stop (org.opentripplanner.model.Stop)25 TripPattern (org.opentripplanner.model.TripPattern)25 ServiceDate (org.opentripplanner.model.calendar.ServiceDate)21 ArrayList (java.util.ArrayList)20 Route (org.opentripplanner.model.Route)15 HashSet (java.util.HashSet)11 List (java.util.List)11 Agency (org.opentripplanner.model.Agency)11 Collection (java.util.Collection)9 Map (java.util.Map)9 Collectors (java.util.stream.Collectors)9 ZonedDateTime (java.time.ZonedDateTime)8 TransitEntity (org.opentripplanner.model.TransitEntity)8 RoutingService (org.opentripplanner.routing.RoutingService)8 TripTimes (org.opentripplanner.routing.trippattern.TripTimes)8 Notice (org.opentripplanner.model.Notice)7 Station (org.opentripplanner.model.Station)7