forked from HSLdevcom/OpenTripPlanner
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix: Add back walk-reluctance in Transmodel API
- Loading branch information
Showing
10 changed files
with
211 additions
and
7 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
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
21 changes: 21 additions & 0 deletions
21
.../java/org/opentripplanner/ext/transmodelapi/mapping/preferences/CarPreferencesMapper.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,21 @@ | ||
package org.opentripplanner.ext.transmodelapi.mapping.preferences; | ||
|
||
import org.opentripplanner.ext.transmodelapi.support.DataFetcherDecorator; | ||
import org.opentripplanner.routing.api.request.preference.CarPreferences; | ||
|
||
public class CarPreferencesMapper { | ||
|
||
public static void mapCarPreferences(CarPreferences.Builder car, DataFetcherDecorator callWith) { | ||
// Walk reluctance is used for backward compatibility | ||
callWith.argument("walkReluctance", car::withReluctance); | ||
// UNSUPPORTED PARAMETERS | ||
|
||
// Override WALK reluctance with CAR reluctance | ||
// callWith.argument("car.reluctance", car::withReluctance); | ||
// callWith.argument("car.speed", car::withSpeed); | ||
// callWith.argument("car.park.cost", car::withParkCost); | ||
// callWith.argument("car.park.time", car::withParkTime); | ||
// callWith.argument("car.pickup.cost", car::withPickupCost); | ||
// callWith.argument("car.pickup.time", car::withPickupTime); | ||
} | ||
} |
6 changes: 3 additions & 3 deletions
6
...java/org/opentripplanner/ext/transmodelapi/mapping/preferences/WalkPreferencesMapper.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 |
---|---|---|
@@ -1,17 +1,17 @@ | ||
package org.opentripplanner.ext.transmodelapi.mapping.preferences; | ||
|
||
import graphql.schema.DataFetchingEnvironment; | ||
import org.opentripplanner.ext.transmodelapi.support.DataFetcherDecorator; | ||
import org.opentripplanner.routing.api.request.preference.WalkPreferences; | ||
|
||
public class WalkPreferencesMapper { | ||
|
||
public static void mapWalkPreferences( | ||
WalkPreferences.Builder walk, | ||
DataFetchingEnvironment environment, | ||
DataFetcherDecorator callWith | ||
) { | ||
callWith.argument("walkBoardCost", walk::withBoardCost); | ||
// This is not part of API | ||
// callWith.argument("walkBoardCost", walk::withBoardCost); | ||
callWith.argument("walkSpeed", walk::withSpeed); | ||
callWith.argument("walkReluctance", walk::withReluctance); | ||
} | ||
} |
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
30 changes: 30 additions & 0 deletions
30
src/test/java/org/opentripplanner/ext/transmodelapi/_support/TestDataFetcherDecorator.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,30 @@ | ||
package org.opentripplanner.ext.transmodelapi._support; | ||
|
||
import java.util.Map; | ||
import java.util.function.Consumer; | ||
import org.opentripplanner.ext.transmodelapi.support.DataFetcherDecorator; | ||
|
||
public class TestDataFetcherDecorator extends DataFetcherDecorator { | ||
|
||
private final Map<String, ?> data; | ||
|
||
private TestDataFetcherDecorator(Map<String, ?> data) { | ||
super(null); | ||
this.data = data; | ||
} | ||
|
||
public static TestDataFetcherDecorator of(String fieldName, Object value) { | ||
return new TestDataFetcherDecorator(Map.of(fieldName, value)); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
@Override | ||
public <T> void argument(String name, Consumer<T> consumer) { | ||
T value = (T) data.get(name); | ||
if (value != null) { | ||
consumer.accept(value); | ||
} else { | ||
System.out.println("No mapping for: " + name); | ||
} | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
.../org/opentripplanner/ext/transmodelapi/mapping/preferences/BikePreferencesMapperTest.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,74 @@ | ||
package org.opentripplanner.ext.transmodelapi.mapping.preferences; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.opentripplanner.ext.transmodelapi.mapping.preferences.BikePreferencesMapper.mapBikePreferences; | ||
|
||
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.ext.transmodelapi._support.TestDataFetcherDecorator; | ||
import org.opentripplanner.routing.api.request.preference.BikePreferences; | ||
import org.opentripplanner.routing.core.BicycleOptimizeType; | ||
|
||
class BikePreferencesMapperTest { | ||
|
||
static List<Arguments> mapBikePreferencesTestCases() { | ||
return List.of( | ||
Arguments.of( | ||
"walkReluctance", | ||
10.0, | ||
"BikePreferences{reluctance: 10.0, walkingReluctance: 27.0}" | ||
), | ||
Arguments.of("bikeSpeed", 10.0, "BikePreferences{speed: 10.0}"), | ||
Arguments.of( | ||
"bicycleOptimisationMethod", | ||
BicycleOptimizeType.TRIANGLE, | ||
"BikePreferences{optimizeType: TRIANGLE}" | ||
), | ||
// No effect unless BicycleOptimize is TRIANGLE | ||
Arguments.of("triangleFactors.time", 0.17, "BikePreferences{}"), | ||
Arguments.of("triangleFactors.slope", 0.12, "BikePreferences{}"), | ||
Arguments.of("triangleFactors.safety", 0.13, "BikePreferences{}") | ||
); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("mapBikePreferencesTestCases") | ||
void testMapBikePreferences(String field, Object value, String expected) { | ||
var preferences = BikePreferences.of(); | ||
mapBikePreferences(preferences, TestDataFetcherDecorator.of(field, value)); | ||
assertEquals(expected, preferences.build().toString()); | ||
} | ||
|
||
static List<Arguments> mapBikePreferencesOptimizeTriangleTestCases() { | ||
return List.of( | ||
Arguments.of( | ||
"triangleFactors.time", | ||
0.17, | ||
"TimeSlopeSafetyTriangle[time=1.0, slope=0.0, safety=0.0]" | ||
), | ||
Arguments.of( | ||
"triangleFactors.slope", | ||
0.12, | ||
"TimeSlopeSafetyTriangle[time=0.0, slope=1.0, safety=0.0]" | ||
), | ||
Arguments.of( | ||
"triangleFactors.safety", | ||
0.13, | ||
"TimeSlopeSafetyTriangle[time=0.0, slope=0.0, safety=1.0]" | ||
) | ||
); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("mapBikePreferencesOptimizeTriangleTestCases") | ||
void testMapBikePreferencesOptimizeTriangle(String field, Object value, String expected) { | ||
var preferences = BikePreferences.of().withOptimizeType(BicycleOptimizeType.TRIANGLE); | ||
mapBikePreferences(preferences, TestDataFetcherDecorator.of(field, value)); | ||
assertEquals( | ||
"BikePreferences{optimizeType: TRIANGLE, optimizeTriangle: " + expected + "}", | ||
preferences.build().toString() | ||
); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...a/org/opentripplanner/ext/transmodelapi/mapping/preferences/CarPreferencesMapperTest.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,25 @@ | ||
package org.opentripplanner.ext.transmodelapi.mapping.preferences; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
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.ext.transmodelapi._support.TestDataFetcherDecorator; | ||
import org.opentripplanner.routing.api.request.preference.CarPreferences; | ||
|
||
class CarPreferencesMapperTest { | ||
|
||
static List<Arguments> mapCarPreferencesTestCases() { | ||
return List.of(Arguments.of("walkReluctance", 7.5, "CarPreferences{reluctance: 7.5}")); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("mapCarPreferencesTestCases") | ||
void mapCarPreferences(String field, Object value, String expected) { | ||
var preferences = CarPreferences.of(); | ||
CarPreferencesMapper.mapCarPreferences(preferences, TestDataFetcherDecorator.of(field, value)); | ||
assertEquals(expected, preferences.build().toString()); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
.../org/opentripplanner/ext/transmodelapi/mapping/preferences/WalkPreferencesMapperTest.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,31 @@ | ||
package org.opentripplanner.ext.transmodelapi.mapping.preferences; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
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.ext.transmodelapi._support.TestDataFetcherDecorator; | ||
import org.opentripplanner.routing.api.request.preference.WalkPreferences; | ||
|
||
class WalkPreferencesMapperTest { | ||
|
||
static List<Arguments> mapWalkPreferencesTestCases() { | ||
return List.of( | ||
Arguments.of("walkReluctance", 7.5, "WalkPreferences{reluctance: 7.5}"), | ||
Arguments.of("walkSpeed", 3.2, "WalkPreferences{speed: 3.2}") | ||
); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("mapWalkPreferencesTestCases") | ||
void mapWalkPreferences(String field, Object value, String expected) { | ||
var preferences = WalkPreferences.of(); | ||
WalkPreferencesMapper.mapWalkPreferences( | ||
preferences, | ||
TestDataFetcherDecorator.of(field, value) | ||
); | ||
assertEquals(expected, preferences.build().toString()); | ||
} | ||
} |