use of org.opentripplanner.routing.edgetype.TripPattern in project OpenTripPlanner by opentripplanner.
the class TimetableSnapshotSource method handleScheduledTrip.
private boolean handleScheduledTrip(final TripUpdate tripUpdate, final String feedId, final ServiceDate serviceDate) {
final TripDescriptor tripDescriptor = tripUpdate.getTrip();
// This does not include Agency ID or feed ID, trips are feed-unique and we currently assume a single static feed.
final String tripId = tripDescriptor.getTripId();
final TripPattern pattern = getPatternForTripId(feedId, tripId);
if (pattern == null) {
LOG.warn("No pattern found for tripId {}, skipping TripUpdate.", tripId);
return false;
}
if (tripUpdate.getStopTimeUpdateCount() < 1) {
LOG.warn("TripUpdate contains no updates, skipping.");
return false;
}
// Apply update on the *scheduled* time table and set the updated trip times in the buffer
final TripTimes updatedTripTimes = pattern.scheduledTimetable.createUpdatedTripTimes(tripUpdate, timeZone, serviceDate);
if (updatedTripTimes == null) {
return false;
}
// Make sure that updated trip times have the correct real time state
updatedTripTimes.setRealTimeState(RealTimeState.UPDATED);
final boolean success = buffer.update(feedId, pattern, updatedTripTimes, serviceDate);
return success;
}
use of org.opentripplanner.routing.edgetype.TripPattern in project OpenTripPlanner by opentripplanner.
the class TimetableSnapshotSource method cancelPreviouslyAddedTrip.
/**
* Cancel previously added trip from buffer if there is a previously added trip with given trip
* id (without agency id) on service date
*
* @param feedId feed id the trip id belongs to
* @param tripId trip id without agency id
* @param serviceDate service date
* @return true if a previously added trip was cancelled
*/
private boolean cancelPreviouslyAddedTrip(final String feedId, final String tripId, final ServiceDate serviceDate) {
boolean success = false;
final TripPattern pattern = buffer.getLastAddedTripPattern(feedId, tripId, serviceDate);
if (pattern != null) {
// Cancel trip times for this trip in this pattern
final Timetable timetable = buffer.resolve(pattern, serviceDate);
final int tripIndex = timetable.getTripIndex(tripId);
if (tripIndex == -1) {
LOG.warn("Could not cancel previously added trip {}", tripId);
} else {
final TripTimes newTripTimes = new TripTimes(timetable.getTripTimes(tripIndex));
newTripTimes.cancel();
buffer.update(feedId, pattern, newTripTimes, serviceDate);
success = true;
}
}
return success;
}
use of org.opentripplanner.routing.edgetype.TripPattern 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 route route of new trip pattern in case a new trip pattern will be created
* @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(final StopPattern stopPattern, final Route route, final Graph graph) {
// 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.code = generateUniqueTripPatternCode(tripPattern);
// Create an empty bitset for service codes (because the new pattern does not contain any trips)
tripPattern.setServiceCodes(graph.serviceCodes);
// Finish scheduled time table
tripPattern.scheduledTimetable.finish();
// Create vertices and edges for new TripPattern
// TODO: purge these vertices and edges once in a while?
tripPattern.makePatternVerticesAndEdges(graph, graph.index.stopVertexForStop);
// TODO: Add pattern to graph index?
// Add pattern to cache
cache.put(stopPattern, tripPattern);
}
return tripPattern;
}
use of org.opentripplanner.routing.edgetype.TripPattern in project OpenTripPlanner by opentripplanner.
the class TestTransfers method testTimedStopToStopTransfer.
public void testTimedStopToStopTransfer() throws Exception {
ServiceDate serviceDate = new ServiceDate(2009, 07, 11);
// Replace the transfer table with an empty table
TransferTable table = new TransferTable();
when(graph.getTransferTable()).thenReturn(table);
// Compute a normal path between two stops
Vertex origin = graph.getVertex(feedId + ":N");
Vertex destination = graph.getVertex(feedId + ":H");
// Set options like time and routing context
RoutingRequest options = new RoutingRequest();
options.dateTime = TestUtils.dateInSeconds("America/New_York", 2009, 7, 11, 11, 11, 0);
options.setRoutingContext(graph, origin, destination);
// Plan journey
GraphPath path;
List<Trip> trips;
path = planJourney(options);
trips = extractTrips(path);
// Validate result
assertEquals("8.1", trips.get(0).getId().getId());
assertEquals("4.2", trips.get(1).getId().getId());
// Add timed transfer to table
Stop stopK = new Stop();
stopK.setId(new AgencyAndId(feedId, "K"));
Stop stopF = new Stop();
stopF.setId(new AgencyAndId(feedId, "F"));
table.addTransferTime(stopK, stopF, null, null, null, null, StopTransfer.TIMED_TRANSFER);
// Don't forget to also add a TimedTransferEdge
Vertex fromVertex = graph.getVertex(feedId + ":K_arrive");
Vertex toVertex = graph.getVertex(feedId + ":F_depart");
TimedTransferEdge timedTransferEdge = new TimedTransferEdge(fromVertex, toVertex);
// Plan journey
path = planJourney(options);
trips = extractTrips(path);
// Check whether the trips are still the same
assertEquals("8.1", trips.get(0).getId().getId());
assertEquals("4.2", trips.get(1).getId().getId());
// Now apply a real-time update: let the to-trip be early by 27600 seconds,
// resulting in a transfer time of 0 seconds
Trip trip = graph.index.tripForId.get(new AgencyAndId("agency", "4.2"));
TripPattern pattern = graph.index.patternForTrip.get(trip);
applyUpdateToTripPattern(pattern, "4.2", "F", 1, 55200, 55200, ScheduleRelationship.SCHEDULED, 0, serviceDate);
// Plan journey
path = planJourney(options);
trips = extractTrips(path);
// Check whether the trips are still the same
assertEquals("8.1", trips.get(0).getId().getId());
assertEquals("4.2", trips.get(1).getId().getId());
// Now apply a real-time update: let the to-trip be early by 27601 seconds,
// resulting in a transfer time of -1 seconds
applyUpdateToTripPattern(pattern, "4.2", "F", 1, 55199, 55199, ScheduleRelationship.SCHEDULED, 0, serviceDate);
// Plan journey
path = planJourney(options);
trips = extractTrips(path);
// Check whether a later second trip was taken
assertEquals("8.1", trips.get(0).getId().getId());
assertEquals("4.3", trips.get(1).getId().getId());
// "Revert" the real-time update
applyUpdateToTripPattern(pattern, "4.2", "F", 1, 82800, 82800, ScheduleRelationship.SCHEDULED, 0, serviceDate);
// Remove the timed transfer from the graph
graph.removeEdge(timedTransferEdge);
// Revert the graph, thus using the original transfer table again
reset(graph);
}
use of org.opentripplanner.routing.edgetype.TripPattern in project OpenTripPlanner by opentripplanner.
the class OnBoardDepartServiceImplTest method testOnBoardDepartureTime.
@Test
public final void testOnBoardDepartureTime() {
Coordinate[] coordinates = new Coordinate[5];
coordinates[0] = new Coordinate(0.0, 0.0);
coordinates[1] = new Coordinate(0.0, 1.0);
coordinates[2] = new Coordinate(2.0, 1.0);
coordinates[3] = new Coordinate(5.0, 1.0);
coordinates[4] = new Coordinate(5.0, 5.0);
PatternDepartVertex depart = mock(PatternDepartVertex.class);
PatternArriveVertex dwell = mock(PatternArriveVertex.class);
PatternArriveVertex arrive = mock(PatternArriveVertex.class);
Graph graph = mock(Graph.class);
RoutingRequest routingRequest = mock(RoutingRequest.class);
ServiceDay serviceDay = mock(ServiceDay.class);
// You're probably not supposed to do this to mocks (access their fields directly)
// But I know of no other way to do this since the mock object has only action-free stub methods.
routingRequest.modes = new TraverseModeSet("WALK,TRANSIT");
when(graph.getTimeZone()).thenReturn(TimeZone.getTimeZone("GMT"));
GeometryFactory geometryFactory = GeometryUtils.getGeometryFactory();
CoordinateSequenceFactory coordinateSequenceFactory = geometryFactory.getCoordinateSequenceFactory();
CoordinateSequence coordinateSequence = coordinateSequenceFactory.create(coordinates);
LineString geometry = new LineString(coordinateSequence, geometryFactory);
ArrayList<Edge> hops = new ArrayList<Edge>(2);
RoutingContext routingContext = new RoutingContext(routingRequest, graph, null, arrive);
AgencyAndId agencyAndId = new AgencyAndId("Agency", "ID");
Agency agency = new Agency();
Route route = new Route();
ArrayList<StopTime> stopTimes = new ArrayList<StopTime>(3);
StopTime stopDepartTime = new StopTime();
StopTime stopDwellTime = new StopTime();
StopTime stopArriveTime = new StopTime();
Stop stopDepart = new Stop();
Stop stopDwell = new Stop();
Stop stopArrive = new Stop();
Trip trip = new Trip();
routingContext.serviceDays = new ArrayList<ServiceDay>(Collections.singletonList(serviceDay));
agency.setId(agencyAndId.getAgencyId());
route.setId(agencyAndId);
route.setAgency(agency);
stopDepart.setId(agencyAndId);
stopDwell.setId(agencyAndId);
stopArrive.setId(agencyAndId);
stopDepartTime.setStop(stopDepart);
stopDepartTime.setDepartureTime(0);
stopDwellTime.setArrivalTime(20);
stopDwellTime.setStop(stopDwell);
stopDwellTime.setDepartureTime(40);
stopArriveTime.setArrivalTime(60);
stopArriveTime.setStop(stopArrive);
stopTimes.add(stopDepartTime);
stopTimes.add(stopDwellTime);
stopTimes.add(stopArriveTime);
trip.setId(agencyAndId);
trip.setTripHeadsign("The right");
trip.setRoute(route);
TripTimes tripTimes = new TripTimes(trip, stopTimes, new Deduplicator());
StopPattern stopPattern = new StopPattern(stopTimes);
TripPattern tripPattern = new TripPattern(route, stopPattern);
TripPattern.generateUniqueIds(Arrays.asList(tripPattern));
when(depart.getTripPattern()).thenReturn(tripPattern);
when(dwell.getTripPattern()).thenReturn(tripPattern);
PatternHop patternHop0 = new PatternHop(depart, dwell, stopDepart, stopDwell, 0);
PatternHop patternHop1 = new PatternHop(dwell, arrive, stopDwell, stopArrive, 1);
hops.add(patternHop0);
hops.add(patternHop1);
when(graph.getEdges()).thenReturn(hops);
when(depart.getCoordinate()).thenReturn(new Coordinate(0, 0));
when(dwell.getCoordinate()).thenReturn(new Coordinate(0, 0));
when(arrive.getCoordinate()).thenReturn(new Coordinate(0, 0));
routingRequest.from = new GenericLocation();
routingRequest.startingTransitTripId = agencyAndId;
when(serviceDay.secondsSinceMidnight(anyInt())).thenReturn(9);
patternHop0.setGeometry(geometry);
tripPattern.add(tripTimes);
graph.index = new GraphIndex(graph);
coordinates = new Coordinate[3];
coordinates[0] = new Coordinate(3.5, 1.0);
coordinates[1] = new Coordinate(5.0, 1.0);
coordinates[2] = new Coordinate(5.0, 5.0);
coordinateSequence = coordinateSequenceFactory.create(coordinates);
geometry = new LineString(coordinateSequence, geometryFactory);
Vertex vertex = onBoardDepartServiceImpl.setupDepartOnBoard(routingContext);
Edge edge = vertex.getOutgoing().toArray(new Edge[1])[0];
assertEquals(vertex, edge.getFromVertex());
assertEquals(dwell, edge.getToVertex());
assertEquals("The right", edge.getDirection());
assertEquals(geometry, edge.getGeometry());
assertEquals(coordinates[0].x, vertex.getX(), 0.0);
assertEquals(coordinates[0].y, vertex.getY(), 0.0);
}
Aggregations