Skip to content

Commit

Permalink
feat(plan query): add further query parameters
Browse files Browse the repository at this point in the history
add reluctance(car, bike, bikeWalking), banned, optimize, triangle parameters
  • Loading branch information
tobsesHub committed Jun 11, 2024
1 parent c985415 commit c32faab
Show file tree
Hide file tree
Showing 9 changed files with 375 additions and 5 deletions.
8 changes: 7 additions & 1 deletion src/main/java/org/opentripplanner/client/OtpApiClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,13 @@ public TripPlan plan(TripPlanParameters req) throws IOException {
req.searchDirection().isArriveBy(),
req.searchWindow().map(sw -> "searchWindow : %d".formatted(sw.toSeconds())).orElse(""),
req.walkReluctance(),
req.wheelchair());
req.carReluctance(),
req.bikeReluctance(),
req.bikeWalkingReluctance(),
req.wheelchair(),
req.banned().map(banned -> "banned : %s".formatted(banned)).orElse(""),
req.optimize(),
req.triangle().map(triangle -> "triangle : %s".formatted(triangle)).orElse(""));

final var jsonNode = sendRequest(formattedQuery);
return deserialize(jsonNode, "/data/plan", TripPlan.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package org.opentripplanner.client.parameters;

import java.util.Set;

public class InputBanned {

public InputBanned(
Set<String> routes,
Set<String> agencies,
Set<String> trips,
Set<String> stops,
Set<String> stopsHard) {
this.routes = routes;
this.agencies = agencies;
this.trips = trips;
this.stops = stops;
this.stopsHard = stopsHard;
}

Set<String> routes = Set.of();

Set<String> agencies = Set.of();

Set<String> trips = Set.of();

Set<String> stops = Set.of();

Set<String> stopsHard = Set.of();

public static InputBannedBuilder builder() {
return new InputBannedBuilder();
}

@Override
public String toString() {
String routesString =
routes.isEmpty() ? "" : String.format("routes: \"%s\"", String.join(",", routes));
String agenciesString =
agencies.isEmpty() ? "" : String.format("agencies: \"%s\"", String.join(",", agencies));
String tripsString =
trips.isEmpty() ? "" : String.format("trips: \"%s\"", String.join(",", trips));
String stopsString =
stops.isEmpty() ? "" : String.format("stops: \"%s\"", String.join(",", stops));
String stopsHardString =
stopsHard.isEmpty() ? "" : String.format("stopsHard: \"%s\"", String.join(",", stopsHard));

return String.format(
"{%s %s %s %s %s}",
routesString, agenciesString, tripsString, stopsString, stopsHardString);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package org.opentripplanner.client.parameters;

import java.util.Set;

public class InputBannedBuilder {
private Set<String> routes = Set.of();
private Set<String> agencies = Set.of();
private Set<String> trips = Set.of();
private Set<String> stops = Set.of();
private Set<String> stopsHard = Set.of();

public InputBannedBuilder withRoutes(Set<String> routes) {
this.routes = routes;
return this;
}

public InputBannedBuilder withAgencies(Set<String> agencies) {
this.agencies = agencies;
return this;
}

public InputBannedBuilder withTrips(Set<String> trips) {
this.trips = trips;
return this;
}

public InputBannedBuilder withStops(Set<String> stops) {
this.stops = stops;
return this;
}

public InputBannedBuilder withStopsHard(Set<String> stopsHard) {
this.stopsHard = stopsHard;
return this;
}

public InputBanned copy() {
return new InputBannedBuilder()
.withRoutes(this.routes)
.withAgencies(this.agencies)
.withTrips(this.trips)
.withStops(this.stops)
.withStopsHard(this.stopsHard)
.build();
}

public InputBanned build() {
return new InputBanned(routes, agencies, trips, stops, stopsHard);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.opentripplanner.client.parameters;

public class InputTriangle {
float safetyFactor;
float slopeFactor;
float timeFactor;

public InputTriangle(float safetyFactor, float slopeFactor, float timeFactor) {
this.safetyFactor = safetyFactor;
this.slopeFactor = slopeFactor;
this.timeFactor = timeFactor;
}

public static InputTriangleBuilder builder() {
return new InputTriangleBuilder();
}

@Override
public String toString() {
String safetyFactorString = String.format("safetyFactor: %f", safetyFactor);
String slopeFactorString = String.format("slopeFactor: %f", slopeFactor);
String timeFactorString = String.format("timeFactor: %f", timeFactor);

return String.format("{%s %s %s}", safetyFactorString, slopeFactorString, timeFactorString);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.opentripplanner.client.parameters;

public class InputTriangleBuilder {
private float safetyFactor;
private float slopeFactor;
private float timeFactor;

public InputTriangleBuilder withSafetyFactor(float safetyFactor) {
this.safetyFactor = safetyFactor;
return this;
}

public InputTriangleBuilder withSlopeFactor(float slopeFactor) {
this.slopeFactor = slopeFactor;
return this;
}

public InputTriangleBuilder withTimeFactor(float timeFactor) {
this.timeFactor = timeFactor;
return this;
}

public InputTriangleBuilder copy() {
return new InputTriangleBuilder()
.withSafetyFactor(this.safetyFactor)
.withSlopeFactor(this.slopeFactor)
.withTimeFactor(this.timeFactor);
}

public InputTriangle build() {
return new InputTriangle(safetyFactor, slopeFactor, timeFactor);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,13 @@ public final class TripPlanParameters {
private final SearchDirection searchDirection;
@Nullable private final Duration searchWindow;
private final float walkReluctance;
private final float carReluctance;
private final float bikeReluctance;
private final float bikeWalkingReluctance;
private final boolean wheelchair;
@Nullable private final InputBanned banned;
private final OptimizeType optimize;
@Nullable private final InputTriangle triangle;

public TripPlanParameters(
PlaceParameter fromPlace,
Expand All @@ -31,8 +37,13 @@ public TripPlanParameters(
SearchDirection searchDirection,
@Nullable Duration searchWindow,
float walkReluctance,
boolean wheelchair) {

float carReluctance,
float bikeReluctance,
float bikeWalkingReluctance,
boolean wheelchair,
@Nullable InputBanned banned,
OptimizeType optimize,
@Nullable InputTriangle triangle) {
this.fromPlace = Objects.requireNonNull(fromPlace);
this.toPlace = Objects.requireNonNull(toPlace);
this.time = Objects.requireNonNull(time);
Expand All @@ -41,7 +52,13 @@ public TripPlanParameters(
this.searchDirection = Objects.requireNonNull(searchDirection);
this.searchWindow = searchWindow;
this.walkReluctance = walkReluctance;
this.carReluctance = carReluctance;
this.bikeReluctance = bikeReluctance;
this.bikeWalkingReluctance = bikeWalkingReluctance;
this.wheelchair = wheelchair;
this.banned = banned;
this.optimize = optimize;
this.triangle = triangle;
}

public Optional<Duration> searchWindow() {
Expand All @@ -57,6 +74,14 @@ public boolean isArriveBy() {
}
}

public enum OptimizeType {
QUICK,
SAFE,
FLAT,
GREENWAYS,
TRIANGLE,
}

public static TripPlanParametersBuilder builder() {
return new TripPlanParametersBuilder();
}
Expand Down Expand Up @@ -89,10 +114,34 @@ public float walkReluctance() {
return walkReluctance;
}

public float carReluctance() {
return carReluctance;
}

public float bikeReluctance() {
return bikeReluctance;
}

public float bikeWalkingReluctance() {
return bikeWalkingReluctance;
}

public boolean wheelchair() {
return wheelchair;
}

public Optional<InputBanned> banned() {
return Optional.ofNullable(banned);
}

public OptimizeType optimize() {
return optimize;
}

public Optional<InputTriangle> triangle() {
return Optional.ofNullable(triangle);
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this)
Expand All @@ -104,7 +153,13 @@ public String toString() {
.add("searchDirection", searchDirection)
.add("searchWindow", searchWindow)
.add("walkReluctance", walkReluctance)
.add("carReluctance", carReluctance)
.add("bikeReluctance", bikeReluctance)
.add("bikeWalkReluctance", bikeWalkingReluctance)
.add("wheelchair", wheelchair)
.add("banned", banned)
.add("optimize", optimize)
.add("triangle", triangle)
.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.stream.Collectors;
import org.opentripplanner.client.model.PlaceParameter;
import org.opentripplanner.client.model.RequestMode;
import org.opentripplanner.client.parameters.TripPlanParameters.OptimizeType;
import org.opentripplanner.client.parameters.TripPlanParameters.SearchDirection;

public class TripPlanParametersBuilder {
Expand All @@ -17,8 +18,14 @@ public class TripPlanParametersBuilder {
private SearchDirection searchDirection = SearchDirection.DEPART_AT;
private Duration searchWindow;
private float walkReluctance = 1.4f;
private float carReluctance = 3.0f;
private float bikeReluctance = 2.0f;
private float bikeWalkingReluctance = 5.0f;
private OptimizeType optimize = OptimizeType.QUICK;
private InputTriangle triangle;
private int numItineraries = 5;
private boolean wheelchair = false;
private InputBanned banned;

public TripPlanParametersBuilder withFrom(PlaceParameter from) {
this.fromPlace = from;
Expand Down Expand Up @@ -60,6 +67,21 @@ public TripPlanParametersBuilder withWalkReluctance(float wr) {
return this;
}

public TripPlanParametersBuilder withCarReluctance(float cr) {
this.carReluctance = cr;
return this;
}

public TripPlanParametersBuilder withBikeReluctance(float br) {
this.bikeReluctance = br;
return this;
}

public TripPlanParametersBuilder withBikeWalkingReluctance(float bwr) {
this.bikeWalkingReluctance = bwr;
return this;
}

public TripPlanParametersBuilder withNumberOfItineraries(int ni) {
this.numItineraries = ni;
return this;
Expand All @@ -70,6 +92,21 @@ public TripPlanParametersBuilder withWheelchair(boolean wheelchair) {
return this;
}

public TripPlanParametersBuilder withBanned(InputBanned banned) {
this.banned = banned;
return this;
}

public TripPlanParametersBuilder withOptimize(OptimizeType optimize) {
this.optimize = optimize;
return this;
}

public TripPlanParametersBuilder withTriangle(InputTriangle triangle) {
this.triangle = triangle;
return this;
}

public TripPlanParametersBuilder copy() {
return TripPlanParameters.builder()
.withFrom(fromPlace)
Expand All @@ -79,8 +116,14 @@ public TripPlanParametersBuilder copy() {
.withSearchDirection(searchDirection)
.withSearchWindow(searchWindow)
.withWalkReluctance(walkReluctance)
.withCarReluctance(carReluctance)
.withBikeReluctance(bikeReluctance)
.withBikeWalkingReluctance(bikeWalkingReluctance)
.withNumberOfItineraries(numItineraries)
.withWheelchair(wheelchair);
.withWheelchair(wheelchair)
.withBanned(banned)
.withOptimize(optimize)
.withTriangle(triangle);
}

public TripPlanParameters build() {
Expand All @@ -93,6 +136,12 @@ public TripPlanParameters build() {
searchDirection,
searchWindow,
walkReluctance,
wheelchair);
carReluctance,
bikeReluctance,
bikeWalkingReluctance,
wheelchair,
banned,
optimize,
triangle);
}
}
6 changes: 6 additions & 0 deletions src/main/resources/queries/plan.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@ query {
arriveBy: %s
%s
walkReluctance: %s
carReluctance: %s
bikeReluctance: %s
bikeWalkingReluctance: %s
wheelchair: %s
%s
optimize: %s
%s
) {
itineraries {
startTime
Expand Down
Loading

0 comments on commit c32faab

Please sign in to comment.