Skip to content

Commit

Permalink
Add complete flex integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardehrenfried committed Dec 3, 2021
1 parent 139824b commit 1d5778d
Show file tree
Hide file tree
Showing 10 changed files with 147 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
package org.opentripplanner.ext.flex;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.opentripplanner.graph_builder.module.FakeGraph.getFileForResource;
import static org.opentripplanner.routing.api.request.StreetMode.FLEXIBLE;

import java.io.File;
import java.net.URISyntaxException;
import java.time.Duration;
import java.time.ZonedDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.opentripplanner.ConstantsForTests;
import org.opentripplanner.graph_builder.model.GtfsBundle;
import org.opentripplanner.graph_builder.module.DirectTransferGenerator;
import org.opentripplanner.graph_builder.module.GtfsModule;
import org.opentripplanner.graph_builder.module.StreetLinkerModule;
import org.opentripplanner.model.GenericLocation;
import org.opentripplanner.model.calendar.ServiceDateInterval;
import org.opentripplanner.model.plan.Itinerary;
import org.opentripplanner.routing.RoutingService;
import org.opentripplanner.routing.api.request.RoutingRequest;
import org.opentripplanner.routing.core.TraverseMode;
import org.opentripplanner.routing.graph.Graph;
import org.opentripplanner.standalone.config.RouterConfig;
import org.opentripplanner.standalone.server.Router;
import org.opentripplanner.util.OTPFeature;

/**
* This test checks the combination of transit and flex works.
*/
public class FlexIntegrationTest {

static long dateTime = ZonedDateTime.parse("2021-12-02T12:00:00-05:00[America/New_York]")
.toInstant()
.getEpochSecond();

static Graph graph;
static RoutingService service;
static Router router;

@BeforeAll
static void setup() throws URISyntaxException {
OTPFeature.enableFeatures(Map.of(OTPFeature.FlexRouting, true));
var osmPath = getFileForResource(FlexTest.COBB_OSM).getAbsolutePath();
var gtfsPath = getFileForResource(FlexTest.COBB_BUS_30_GTFS).getAbsolutePath();
var flexGtfsPath = getFileForResource(FlexTest.COBB_FLEX_GTFS).getAbsolutePath();

graph = ConstantsForTests.buildOsmGraph(osmPath);
addGtfsToGraph(graph, List.of(gtfsPath, flexGtfsPath));
router = new Router(graph, RouterConfig.DEFAULT);
router.startup();

service = new RoutingService(graph);
}

@Test
public void shouldReturnARouteTransferringFromBusToFlex() {
var from = new GenericLocation(33.84329482265106, -84.583740234375);
var to = new GenericLocation(33.86701256815635, -84.61787939071655);

var itin = getItinerary(graph, from, to);

assertEquals(4, itin.legs.size());

var walkToBus = itin.legs.get(0);
assertEquals(TraverseMode.WALK, walkToBus.mode);

var bus = itin.legs.get(1);
assertEquals(TraverseMode.BUS, bus.mode);
assertEquals("30", bus.getRoute().getShortName());

var transfer = itin.legs.get(2);
assertEquals(TraverseMode.WALK, transfer.mode);

var flex = itin.legs.get(3);
assertEquals(TraverseMode.BUS, flex.mode);
assertEquals("Zone 2", flex.getRoute().getShortName());
assertTrue(flex.flexibleTrip);

}

private Itinerary getItinerary(Graph graph, GenericLocation from, GenericLocation to) {
RoutingRequest request = new RoutingRequest();
request.dateTime = dateTime;
request.from = from;
request.to = to;
request.numItineraries = 10;
request.searchWindow = Duration.ofHours(2);
request.setRoutingContext(graph);

request.modes.egressMode = FLEXIBLE;

var result = service.route(request, router);
var itineraries = result.getTripPlan().itineraries;

return itineraries.get(2);
}

private static void addGtfsToGraph(
Graph graph,
List<String> gtfsFiles
) {
var extra = new HashMap<Class<?>, Object>();

// GTFS
var gtfsBundles = gtfsFiles.stream()
.map(f -> new GtfsBundle(new File(f)))
.collect(Collectors.toList());
GtfsModule gtfsModule = new GtfsModule(gtfsBundles, ServiceDateInterval.unbounded());
gtfsModule.buildGraph(graph, extra);

// link stations to streets
StreetLinkerModule streetLinkerModule = new StreetLinkerModule();
streetLinkerModule.buildGraph(graph, extra);

// link flex locations to streets
var flexMapper = new FlexLocationsToStreetEdgesMapper();
flexMapper.buildGraph(graph, new HashMap<>());

// generate direct transfers
var req = new RoutingRequest();
var transfers = new DirectTransferGenerator(600, List.of(req));
transfers.buildGraph(graph, extra);

graph.index();
}


@AfterAll
static void teardown() {
OTPFeature.enableFeatures(Map.of(OTPFeature.FlexRouting, false));
}
}
5 changes: 5 additions & 0 deletions src/ext-test/java/org/opentripplanner/ext/flex/FlexTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@

abstract public class FlexTest {

static final String ASPEN_GTFS = "/flex/aspen-flex-on-demand.gtfs.zip";
static final String COBB_FLEX_GTFS = "/flex/cobblinc-scheduled-deviated-flex.gtfs.zip";
static final String COBB_BUS_30_GTFS = "/flex/cobblinc-bus-30-only.gtfs.zip";
static final String COBB_OSM = "/flex/cobb-county.filtered.osm.pbf";

static final DirectFlexPathCalculator calculator = new DirectFlexPathCalculator(null);
static final ServiceDate serviceDate = new ServiceDate(2021, 4, 11);
static final int secondsSinceMidnight = LocalTime.of(10, 0).toSecondOfDay();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@
*/
public class ScheduledDeviatedTripTest extends FlexTest {

static final String COBB_COUNTY_GTFS = "/flex/cobblinc-scheduled-deviated-flex.gtfs.zip";

static Graph graph;

@Test
Expand Down Expand Up @@ -121,7 +119,7 @@ public void calculateDirectFare() {

@BeforeAll
static void setup() throws URISyntaxException {
graph = FlexTest.buildFlexGraph(COBB_COUNTY_GTFS);
graph = FlexTest.buildFlexGraph(COBB_FLEX_GTFS);
}

private static NearbyStop getNearbyStop(FlexTrip trip) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
*/
public class UnscheduledTripTest extends FlexTest {

static final String ASPEN_GTFS = "/flex/aspen-flex-on-demand.gtfs.zip";

static Graph graph;

@Test
Expand Down
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ public OptimizedPathTail<T> mutate() {
/** Start by adding the last transit leg with the egress leg attached. */
public OptimizedPathTail<T> addTransitTail(TransitPathLeg<T> leg) {
var next = leg.nextLeg();
// this could also be a transfer to a flex leg
if(next.isTransferLeg()) {
next = next.nextLeg();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ private void reportReadinessForUpdaters() {
while (true) {
try {
if (updaterList.stream().allMatch(GraphUpdater::isPrimed)) {
LOG.info("OTP UPDATERS INITIALIZED - OTP is read for routing!");
LOG.info("OTP UPDATERS INITIALIZED - OTP is ready for routing!");
return;
}
//noinspection BusyWait
Expand Down
12 changes: 0 additions & 12 deletions src/test/java/org/opentripplanner/gtfs/GtfsContextBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,18 +93,6 @@ public GtfsContextBuilder withDeduplicator(Deduplicator deduplicator) {
return this;
}

/**
* The {@link org.opentripplanner.graph_builder.module.GtfsModule} is responsible for repairing
* StopTimes for all trips and trip patterns generation, so turn this feature <b>off</b>
* when using GtfsModule to load data.
*
* This feature is turned <b>on</b> by <em>default</em>.
*/
public GtfsContextBuilder turnOffRepairStopTimesAndTripPatternsGeneration() {
this.repairStopTimesAndGenerateTripPatterns = false;
return this;
}

/**
* This method will:
* <ol>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@
import java.util.Arrays;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.opentripplanner.ext.flex.FlexAccessEgress;
import org.opentripplanner.model.Stop;
import org.opentripplanner.routing.algorithm.GraphRoutingTest;
import org.opentripplanner.routing.algorithm.raptor.transit.FlexAccessEgressAdapter;
import org.opentripplanner.routing.algorithm.raptor.transit.StopIndexForRaptor;
import org.opentripplanner.routing.algorithm.raptor.transit.TransitTuningParameters;
import org.opentripplanner.routing.algorithm.raptor.transit.cost.DefaultCostCalculator;
import org.opentripplanner.transit.raptor._data.RaptorTestConstants;
import org.opentripplanner.transit.raptor._data.transit.TestTransfer;
Expand Down

0 comments on commit 1d5778d

Please sign in to comment.