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"))));
}
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()));
}
}
}
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);
}
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;
}
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;
}
Aggregations