use of org.opentripplanner.routing.graph.Edge in project OpenTripPlanner by opentripplanner.
the class TestUnconnectedAreas method testGeometricGraphWithClasspathFile.
/**
* We've written several OSM files that exhibit different situations but should show the same results. Test with this code.
*/
public List<String> testGeometricGraphWithClasspathFile(String fn, int prCount, int prlCount) {
Graph g = new Graph();
OpenStreetMapModule loader = new OpenStreetMapModule();
loader.setDefaultWayPropertySetSource(new DefaultWayPropertySetSource());
FileBasedOpenStreetMapProviderImpl provider = new FileBasedOpenStreetMapProviderImpl();
File file = new File(getClass().getResource(fn).getFile());
provider.setPath(file);
loader.setProvider(provider);
loader.buildGraph(g, new HashMap<Class<?>, Object>());
Vertex pr = null;
int nParkAndRide = 0;
int nParkAndRideLink = 0;
for (Vertex v : g.getVertices()) {
if (v instanceof ParkAndRideVertex) {
nParkAndRide++;
pr = v;
}
}
for (Edge e : g.getEdges()) {
if (e instanceof ParkAndRideLinkEdge) {
nParkAndRideLink++;
}
}
assertEquals(prCount, nParkAndRide);
assertEquals(prlCount, nParkAndRideLink);
assertNotNull(pr);
// make sure it is connected
List<String> connections = new ArrayList<String>();
for (Edge e : pr.getOutgoing()) {
if (e instanceof ParkAndRideEdge)
continue;
assertTrue(e instanceof ParkAndRideLinkEdge);
connections.add(e.getToVertex().getLabel());
}
// symmetry
for (Edge e : pr.getIncoming()) {
if (e instanceof ParkAndRideEdge)
continue;
assertTrue(e instanceof ParkAndRideLinkEdge);
assertTrue(connections.contains(e.getFromVertex().getLabel()));
}
return connections;
}
use of org.opentripplanner.routing.graph.Edge in project OpenTripPlanner by opentripplanner.
the class TestUnconnectedAreas method testCoincidentNodeUnconnectedParkAndRide.
/**
* This test ensures that if a Park and Ride has a node that is exactly atop a node on a way, the graph
* builder will not loop forever trying to split it. The hackett-pr.osm.gz file contains a park-and-ride lot in
* Hackettstown, NJ, which demonstrates this behavior. See discussion in ticket 1605.
*/
@Test
public void testCoincidentNodeUnconnectedParkAndRide() throws Exception {
Graph g = new Graph();
OpenStreetMapModule loader = new OpenStreetMapModule();
loader.setDefaultWayPropertySetSource(new DefaultWayPropertySetSource());
FileBasedOpenStreetMapProviderImpl provider = new FileBasedOpenStreetMapProviderImpl();
File file = new File(getClass().getResource("hackett_pr.osm.gz").getFile());
provider.setPath(file);
loader.setProvider(provider);
loader.buildGraph(g, new HashMap<Class<?>, Object>());
Vertex washTwp = null;
int nParkAndRide = 0;
int nParkAndRideLink = 0;
for (Vertex v : g.getVertices()) {
if (v instanceof ParkAndRideVertex) {
nParkAndRide++;
washTwp = v;
}
}
for (Edge e : g.getEdges()) {
if (e instanceof ParkAndRideLinkEdge) {
nParkAndRideLink++;
}
}
assertEquals(1, nParkAndRide);
// the P+R should get four connections, since the entrance road is duplicated as well, and crosses twice
// since there are in and out edges, that brings the total to 8 per P+R.
// Even though the park and rides get merged, duplicate edges remain from when they were separate.
// FIXME: we shouldn't have duplicate edges.
assertEquals(16, nParkAndRideLink);
assertNotNull(washTwp);
List<String> connections = new ArrayList<String>();
for (Edge e : washTwp.getOutgoing()) {
if (e instanceof ParkAndRideEdge)
continue;
assertTrue(e instanceof ParkAndRideLinkEdge);
connections.add(e.getToVertex().getLabel());
}
// symmetry
for (Edge e : washTwp.getIncoming()) {
if (e instanceof ParkAndRideEdge)
continue;
assertTrue(e instanceof ParkAndRideLinkEdge);
assertTrue(connections.contains(e.getFromVertex().getLabel()));
}
assertTrue(connections.contains("osm:node:3096570222"));
assertTrue(connections.contains("osm:node:3094264704"));
assertTrue(connections.contains("osm:node:3094264709"));
assertTrue(connections.contains("osm:node:3096570227"));
}
use of org.opentripplanner.routing.graph.Edge in project OpenTripPlanner by opentripplanner.
the class AlertPatchTest method testTimeRanges.
public void testTimeRanges() {
AlertPatch snp1 = new AlertPatch();
snp1.setFeedId(feedId);
LinkedList<TimePeriod> timePeriods = new LinkedList<TimePeriod>();
long breakTime = TestUtils.dateInSeconds("America/New_York", 2009, 8, 7, 0, 0, 0);
// until the beginning of the day
timePeriods.add(new TimePeriod(0, breakTime));
long secondPeriodStartTime = TestUtils.dateInSeconds("America/New_York", 2009, 8, 7, 7, 0, 0);
long secondPeriodEndTime = TestUtils.dateInSeconds("America/New_York", 2009, 8, 8, 0, 0, 0);
timePeriods.add(new TimePeriod(secondPeriodStartTime, secondPeriodEndTime));
snp1.setTimePeriods(timePeriods);
Alert note1 = Alert.createSimpleAlerts("The first note");
snp1.setAlert(note1);
snp1.setId("id1");
snp1.setStop(new AgencyAndId(feedId, "A"));
snp1.apply(graph);
Vertex stop_a = graph.getVertex(feedId + ":A");
Vertex stop_e = graph.getVertex(feedId + ":E_arrive");
ShortestPathTree spt;
GraphPath path;
options.dateTime = TestUtils.dateInSeconds("America/New_York", 2009, 8, 7, 0, 0, 0);
options.setRoutingContext(graph, stop_a, stop_e);
spt = aStar.getShortestPathTree(options);
path = spt.getPath(stop_e, true);
assertNotNull(path);
// expect no notes because we are during the break
State noAlertPatchesState = path.states.get(1);
Edge noAlertPatchesEdge = noAlertPatchesState.getBackEdge();
HashSet<Alert> noAlertPatchesAlerts = new HashSet<Alert>();
for (AlertPatch alertPatch : graph.getAlertPatches(noAlertPatchesEdge)) {
if (alertPatch.displayDuring(noAlertPatchesState)) {
noAlertPatchesAlerts.add(alertPatch.getAlert());
}
}
assertEquals(new HashSet<Alert>(), noAlertPatchesAlerts);
// now a trip during the second period
options.dateTime = TestUtils.dateInSeconds("America/New_York", 2009, 8, 7, 8, 0, 0);
options.setRoutingContext(graph, stop_a, stop_e);
spt = aStar.getShortestPathTree(options);
// do not optimize because we want the first trip
path = spt.getPath(stop_e, false);
assertNotNull(path);
HashSet<Alert> expectedNotes = new HashSet<Alert>();
expectedNotes.add(note1);
State oneAlertPatchState = path.states.get(1);
Edge oneAlertPatchEdge = oneAlertPatchState.getBackEdge();
HashSet<Alert> oneAlertPatchAlerts = new HashSet<Alert>();
for (AlertPatch alertPatch : graph.getAlertPatches(oneAlertPatchEdge)) {
if (alertPatch.displayDuring(oneAlertPatchState)) {
oneAlertPatchAlerts.add(alertPatch.getAlert());
}
}
assertEquals(expectedNotes, oneAlertPatchAlerts);
}
use of org.opentripplanner.routing.graph.Edge in project OpenTripPlanner by opentripplanner.
the class AlertPatchTest method testRouteNotePatch.
public void testRouteNotePatch() {
AlertPatch rnp1 = new AlertPatch();
rnp1.setFeedId(feedId);
rnp1.setTimePeriods(Collections.singletonList(new TimePeriod(0, // until ~1/1/2011
1000L * 60 * 60 * 24 * 365 * 40)));
Alert note1 = Alert.createSimpleAlerts("The route note");
rnp1.setAlert(note1);
rnp1.setId("id1");
// Routes isn't patched in tests through GtfsBundle, which is why we have have a reference to agency id here.
rnp1.setRoute(new AgencyAndId("agency", "1"));
rnp1.apply(graph);
Vertex stop_a = graph.getVertex(feedId + ":A");
Vertex stop_e = graph.getVertex(feedId + ":E_arrive");
ShortestPathTree spt;
GraphPath path;
options.setRoutingContext(graph, stop_a, stop_e);
spt = aStar.getShortestPathTree(options);
path = spt.getPath(stop_e, false);
assertNotNull(path);
HashSet<Alert> expectedAlerts = new HashSet<Alert>();
expectedAlerts.add(note1);
Edge actualEdge = path.states.get(2).getBackEdge();
HashSet<Alert> actualAlerts = new HashSet<Alert>();
for (AlertPatch alertPatch : graph.getAlertPatches(actualEdge)) {
actualAlerts.add(alertPatch.getAlert());
}
assertEquals(expectedAlerts, actualAlerts);
}
use of org.opentripplanner.routing.graph.Edge in project OpenTripPlanner by opentripplanner.
the class GraphPathToTripPlanConverter method sliceStates.
/**
* Slice a {@link State} array at the leg boundaries. Leg switches occur when:
* 1. A LEG_SWITCH mode (which itself isn't part of any leg) is seen
* 2. The mode changes otherwise, for instance from BICYCLE to WALK
* 3. A PatternInterlineDwell edge (i.e. interlining) is seen
*
* @param states The one-dimensional array of input states
* @return An array of arrays of states belonging to a single leg (i.e. a two-dimensional array)
*/
private static State[][] sliceStates(State[] states) {
boolean trivial = true;
for (State state : states) {
TraverseMode traverseMode = state.getBackMode();
if (traverseMode != null && traverseMode != TraverseMode.LEG_SWITCH) {
trivial = false;
break;
}
}
if (trivial) {
throw new TrivialPathException();
}
int[] legIndexPairs = { 0, states.length - 1 };
List<int[]> legsIndexes = new ArrayList<int[]>();
for (int i = 1; i < states.length - 1; i++) {
TraverseMode backMode = states[i].getBackMode();
TraverseMode forwardMode = states[i + 1].getBackMode();
if (backMode == null || forwardMode == null)
continue;
Edge edge = states[i + 1].getBackEdge();
if (backMode == TraverseMode.LEG_SWITCH || forwardMode == TraverseMode.LEG_SWITCH) {
if (backMode != TraverseMode.LEG_SWITCH) {
// Start of leg switch
legIndexPairs[1] = i;
} else if (forwardMode != TraverseMode.LEG_SWITCH) {
// End of leg switch
if (legIndexPairs[1] != states.length - 1) {
legsIndexes.add(legIndexPairs);
}
legIndexPairs = new int[] { i, states.length - 1 };
}
} else if (backMode != forwardMode) {
// Mode change => leg switch
legIndexPairs[1] = i;
legsIndexes.add(legIndexPairs);
legIndexPairs = new int[] { i, states.length - 1 };
} else if (edge instanceof PatternInterlineDwell) {
// Interlining => leg switch
legIndexPairs[1] = i;
legsIndexes.add(legIndexPairs);
legIndexPairs = new int[] { i + 1, states.length - 1 };
}
}
// Final leg
legsIndexes.add(legIndexPairs);
State[][] legsStates = new State[legsIndexes.size()][];
// Fill the two-dimensional array with states
for (int i = 0; i < legsStates.length; i++) {
legIndexPairs = legsIndexes.get(i);
legsStates[i] = new State[legIndexPairs[1] - legIndexPairs[0] + 1];
for (int j = 0; j <= legIndexPairs[1] - legIndexPairs[0]; j++) {
legsStates[i][j] = states[legIndexPairs[0] + j];
}
}
return legsStates;
}
Aggregations