-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5796 from ibi-group/flex-duration-factors
Implement GTFS Flex safe duration spec draft
- Loading branch information
Showing
25 changed files
with
444 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/ext-test/java/org/opentripplanner/ext/flex/FlexStopTimesForTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package org.opentripplanner.ext.flex; | ||
|
||
import static org.opentripplanner.model.StopTime.MISSING_VALUE; | ||
|
||
import org.opentripplanner._support.geometry.Polygons; | ||
import org.opentripplanner.framework.time.TimeUtils; | ||
import org.opentripplanner.model.StopTime; | ||
import org.opentripplanner.transit.model._data.TransitModelForTest; | ||
import org.opentripplanner.transit.model.site.RegularStop; | ||
import org.opentripplanner.transit.model.site.StopLocation; | ||
|
||
public class FlexStopTimesForTest { | ||
|
||
private static final TransitModelForTest TEST_MODEL = TransitModelForTest.of(); | ||
private static final StopLocation AREA_STOP = TEST_MODEL.areaStopForTest("area", Polygons.BERLIN); | ||
private static final RegularStop REGULAR_STOP = TEST_MODEL.stop("stop").build(); | ||
|
||
public static StopTime area(String startTime, String endTime) { | ||
return area(AREA_STOP, endTime, startTime); | ||
} | ||
|
||
public static StopTime area(StopLocation areaStop, String endTime, String startTime) { | ||
var stopTime = new StopTime(); | ||
stopTime.setStop(areaStop); | ||
stopTime.setFlexWindowStart(TimeUtils.time(startTime)); | ||
stopTime.setFlexWindowEnd(TimeUtils.time(endTime)); | ||
return stopTime; | ||
} | ||
|
||
public static StopTime regularArrival(String arrivalTime) { | ||
return regularStopTime(TimeUtils.time(arrivalTime), MISSING_VALUE); | ||
} | ||
|
||
public static StopTime regularStopTime(String arrivalTime, String departureTime) { | ||
return regularStopTime(TimeUtils.time(arrivalTime), TimeUtils.time(departureTime)); | ||
} | ||
|
||
public static StopTime regularStopTime(int arrivalTime, int departureTime) { | ||
var stopTime = new StopTime(); | ||
stopTime.setStop(REGULAR_STOP); | ||
stopTime.setArrivalTime(arrivalTime); | ||
stopTime.setDepartureTime(departureTime); | ||
return stopTime; | ||
} | ||
|
||
public static StopTime regularDeparture(String departureTime) { | ||
return regularStopTime(MISSING_VALUE, TimeUtils.time(departureTime)); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
src/ext-test/java/org/opentripplanner/ext/flex/flexpathcalculator/FlexPathTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package org.opentripplanner.ext.flex.flexpathcalculator; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import java.time.Duration; | ||
import java.util.List; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
import org.opentripplanner._support.geometry.LineStrings; | ||
import org.opentripplanner.routing.api.request.framework.TimePenalty; | ||
|
||
class FlexPathTest { | ||
|
||
private static final int THIRTY_MINS_IN_SECONDS = (int) Duration.ofMinutes(30).toSeconds(); | ||
private static final FlexPath PATH = new FlexPath( | ||
10_000, | ||
THIRTY_MINS_IN_SECONDS, | ||
() -> LineStrings.SIMPLE | ||
); | ||
|
||
static List<Arguments> cases() { | ||
return List.of( | ||
Arguments.of(TimePenalty.ZERO, THIRTY_MINS_IN_SECONDS), | ||
Arguments.of(TimePenalty.of(Duration.ofMinutes(10), 1), 2400), | ||
Arguments.of(TimePenalty.of(Duration.ofMinutes(10), 1.5f), 3300), | ||
Arguments.of(TimePenalty.of(Duration.ZERO, 3), 5400) | ||
); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("cases") | ||
void calculate(TimePenalty mod, int expectedSeconds) { | ||
var modified = PATH.withDurationModifier(mod); | ||
assertEquals(expectedSeconds, modified.durationSeconds); | ||
assertEquals(LineStrings.SIMPLE, modified.getGeometry()); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...java/org/opentripplanner/ext/flex/flexpathcalculator/ScheduledFlexPathCalculatorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package org.opentripplanner.ext.flex.flexpathcalculator; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.opentripplanner.ext.flex.FlexStopTimesForTest.area; | ||
import static org.opentripplanner.ext.flex.FlexStopTimesForTest.regularStopTime; | ||
import static org.opentripplanner.street.model._data.StreetModelForTest.V1; | ||
import static org.opentripplanner.street.model._data.StreetModelForTest.V2; | ||
import static org.opentripplanner.transit.model._data.TransitModelForTest.id; | ||
|
||
import java.time.Duration; | ||
import java.util.List; | ||
import org.junit.jupiter.api.Test; | ||
import org.opentripplanner._support.geometry.LineStrings; | ||
import org.opentripplanner.ext.flex.trip.ScheduledDeviatedTrip; | ||
|
||
class ScheduledFlexPathCalculatorTest { | ||
|
||
private static final ScheduledDeviatedTrip TRIP = ScheduledDeviatedTrip | ||
.of(id("123")) | ||
.withStopTimes( | ||
List.of( | ||
regularStopTime("10:00", "10:01"), | ||
area("10:10", "10:20"), | ||
regularStopTime("10:25", "10:26"), | ||
area("10:40", "10:50") | ||
) | ||
) | ||
.build(); | ||
|
||
@Test | ||
void calculateTime() { | ||
var c = (FlexPathCalculator) (fromv, tov, fromStopIndex, toStopIndex) -> | ||
new FlexPath(10_000, (int) Duration.ofMinutes(10).toSeconds(), () -> LineStrings.SIMPLE); | ||
var calc = new ScheduledFlexPathCalculator(c, TRIP); | ||
var path = calc.calculateFlexPath(V1, V2, 0, 1); | ||
assertEquals(Duration.ofMinutes(19), Duration.ofSeconds(path.durationSeconds)); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
...-test/java/org/opentripplanner/ext/flex/flexpathcalculator/TimePenaltyCalculatorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package org.opentripplanner.ext.flex.flexpathcalculator; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNull; | ||
|
||
import java.time.Duration; | ||
import org.junit.jupiter.api.Test; | ||
import org.opentripplanner._support.geometry.LineStrings; | ||
import org.opentripplanner.routing.api.request.framework.TimePenalty; | ||
import org.opentripplanner.street.model._data.StreetModelForTest; | ||
|
||
class TimePenaltyCalculatorTest { | ||
|
||
private static final int THIRTY_MINS_IN_SECONDS = (int) Duration.ofMinutes(30).toSeconds(); | ||
|
||
@Test | ||
void calculate() { | ||
FlexPathCalculator delegate = (fromv, tov, fromStopIndex, toStopIndex) -> | ||
new FlexPath(10_000, THIRTY_MINS_IN_SECONDS, () -> LineStrings.SIMPLE); | ||
|
||
var mod = TimePenalty.of(Duration.ofMinutes(10), 1.5f); | ||
var calc = new TimePenaltyCalculator(delegate, mod); | ||
var path = calc.calculateFlexPath(StreetModelForTest.V1, StreetModelForTest.V2, 0, 5); | ||
assertEquals(3300, path.durationSeconds); | ||
} | ||
|
||
@Test | ||
void nullValue() { | ||
FlexPathCalculator delegate = (fromv, tov, fromStopIndex, toStopIndex) -> null; | ||
var mod = TimePenalty.of(Duration.ofMinutes(10), 1.5f); | ||
var calc = new TimePenaltyCalculator(delegate, mod); | ||
var path = calc.calculateFlexPath(StreetModelForTest.V1, StreetModelForTest.V2, 0, 5); | ||
assertNull(path); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
src/ext-test/java/org/opentripplanner/ext/flex/trip/UnscheduledDrivingDurationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package org.opentripplanner.ext.flex.trip; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.opentripplanner.street.model._data.StreetModelForTest.V1; | ||
import static org.opentripplanner.street.model._data.StreetModelForTest.V2; | ||
import static org.opentripplanner.transit.model._data.TransitModelForTest.id; | ||
|
||
import java.time.Duration; | ||
import java.util.List; | ||
import org.junit.jupiter.api.Test; | ||
import org.opentripplanner._support.geometry.LineStrings; | ||
import org.opentripplanner.ext.flex.FlexStopTimesForTest; | ||
import org.opentripplanner.ext.flex.flexpathcalculator.FlexPath; | ||
import org.opentripplanner.ext.flex.flexpathcalculator.FlexPathCalculator; | ||
import org.opentripplanner.model.StopTime; | ||
import org.opentripplanner.routing.api.request.framework.TimePenalty; | ||
|
||
class UnscheduledDrivingDurationTest { | ||
|
||
static final FlexPathCalculator STATIC_CALCULATOR = (fromv, tov, fromStopIndex, toStopIndex) -> | ||
new FlexPath(10_000, (int) Duration.ofMinutes(10).toSeconds(), () -> LineStrings.SIMPLE); | ||
private static final StopTime STOP_TIME = FlexStopTimesForTest.area("10:00", "18:00"); | ||
|
||
@Test | ||
void noPenalty() { | ||
var trip = UnscheduledTrip.of(id("1")).withStopTimes(List.of(STOP_TIME)).build(); | ||
|
||
var calculator = trip.flexPathCalculator(STATIC_CALCULATOR); | ||
var path = calculator.calculateFlexPath(V1, V2, 0, 0); | ||
assertEquals(600, path.durationSeconds); | ||
} | ||
|
||
@Test | ||
void withPenalty() { | ||
var trip = UnscheduledTrip | ||
.of(id("1")) | ||
.withStopTimes(List.of(STOP_TIME)) | ||
.withTimePenalty(TimePenalty.of(Duration.ofMinutes(2), 1.5f)) | ||
.build(); | ||
|
||
var calculator = trip.flexPathCalculator(STATIC_CALCULATOR); | ||
var path = calculator.calculateFlexPath(V1, V2, 0, 0); | ||
assertEquals(1020, path.durationSeconds); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.