org.opentripplanner.ext.transmodelapi.TransmodelGraphQLPlanner Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of otp Show documentation
Show all versions of otp Show documentation
The OpenTripPlanner multimodal journey planning system
package org.opentripplanner.ext.transmodelapi;
import graphql.schema.DataFetchingEnvironment;
import org.opentripplanner.api.common.Message;
import org.opentripplanner.api.common.ParameterException;
import org.opentripplanner.api.mapping.PlannerErrorMapper;
import org.opentripplanner.api.model.error.PlannerError;
import org.opentripplanner.ext.transmodelapi.mapping.TransitIdMapper;
import org.opentripplanner.ext.transmodelapi.model.PlanResponse;
import org.opentripplanner.ext.transmodelapi.model.TransportModeSlack;
import org.opentripplanner.ext.transmodelapi.support.DataFetcherDecorator;
import org.opentripplanner.ext.transmodelapi.support.GqlUtil;
import org.opentripplanner.model.FeedScopedId;
import org.opentripplanner.model.GenericLocation;
import org.opentripplanner.model.TransitMode;
import org.opentripplanner.routing.api.request.BannedStopSet;
import org.opentripplanner.routing.api.request.RequestModes;
import org.opentripplanner.routing.api.request.RoutingRequest;
import org.opentripplanner.routing.api.request.StreetMode;
import org.opentripplanner.routing.api.response.RoutingError;
import org.opentripplanner.routing.api.response.RoutingResponse;
import org.opentripplanner.routing.core.BicycleOptimizeType;
import org.opentripplanner.standalone.server.Router;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.function.DoubleFunction;
import java.util.function.Function;
import java.util.stream.Collectors;
import static org.opentripplanner.ext.transmodelapi.mapping.TransitIdMapper.mapIDsToDomain;
public class TransmodelGraphQLPlanner {
private static final Logger LOG = LoggerFactory.getLogger(TransmodelGraphQLPlanner.class);
public PlanResponse plan(DataFetchingEnvironment environment) {
PlanResponse response = new PlanResponse();
RoutingRequest request = null;
try {
TransmodelRequestContext ctx = environment.getContext();
Router router = ctx.getRouter();
request = createRequest(environment);
RoutingResponse res = ctx.getRoutingService().route(request, router);
response.plan = res.getTripPlan();
response.metadata = res.getMetadata();
for (RoutingError routingError : res.getRoutingErrors()) {
response.messages.add(PlannerErrorMapper.mapMessage(routingError).message);
}
response.debugOutput = res.getDebugAggregator().finishedRendering();
}
catch (Exception e) {
LOG.warn("System error");
LOG.error("Root cause: " + e.getMessage(), e);
PlannerError error = new PlannerError();
error.setMsg(Message.SYSTEM_ERROR);
response.messages.add(error.message);
}
return response;
}
private GenericLocation toGenericLocation(Map m) {
Map coordinates = (Map) m.get("coordinates");
Double lat = null;
Double lon = null;
if (coordinates != null) {
lat = (Double) coordinates.get("latitude");
lon = (Double) coordinates.get("longitude");
}
String placeRef = (String) m.get("place");
FeedScopedId stopId = placeRef == null ? null : TransitIdMapper.mapIDToDomain(placeRef);
String name = (String) m.get("name");
name = name == null ? "" : name;
return new GenericLocation(name, stopId, lat, lon);
}
private RoutingRequest createRequest(DataFetchingEnvironment environment) {
TransmodelRequestContext context = environment.getContext();
Router router = context.getRouter();
RoutingRequest request = router.defaultRoutingRequest.clone();
DataFetcherDecorator callWith = new DataFetcherDecorator(environment);
callWith.argument("locale", (String v) -> request.locale = Locale.forLanguageTag(v));
callWith.argument("from", (Map v) -> request.from = toGenericLocation(v));
callWith.argument("to", (Map v) -> request.to = toGenericLocation(v));
callWith.argument("dateTime", millisSinceEpoch -> request.setDateTime(new Date((long) millisSinceEpoch)), Date::new);
callWith.argument("searchWindow", (Integer m) -> request.searchWindow = Duration.ofMinutes(m));
callWith.argument("wheelchair", request::setWheelchairAccessible);
callWith.argument("numTripPatterns", request::setNumItineraries);
callWith.argument("transitGeneralizedCostLimit", (DoubleFunction it) -> request.transitGeneralizedCostLimit = it);
callWith.argument("maximumWalkDistance", request::setMaxWalkDistance);
// callWith.argument("maxTransferWalkDistance", request::setMaxTransferWalkDistance);
callWith.argument("maxPreTransitTime", request::setMaxPreTransitTime);
// callWith.argument("preTransitReluctance", (Double v) -> request.setPreTransitReluctance(v));
// callWith.argument("maxPreTransitWalkDistance", (Double v) -> request.setMaxPreTransitWalkDistance(v));
callWith.argument("walkBoardCost", request::setWalkBoardCost);
callWith.argument("walkReluctance", request::setWalkReluctance);
callWith.argument("waitReluctance", request::setWaitReluctance);
callWith.argument("walkBoardCost", request::setWalkBoardCost);
// callWith.argument("walkOnStreetReluctance", request::setWalkOnStreetReluctance);
callWith.argument("waitReluctance", request::setWaitReluctance);
callWith.argument("waitAtBeginningFactor", request::setWaitAtBeginningFactor);
callWith.argument("walkSpeed", (Double v) -> request.walkSpeed = v);
callWith.argument("bikeSpeed", (Double v) -> request.bikeSpeed = v);
callWith.argument("bikeSwitchTime", (Integer v) -> request.bikeSwitchTime = v);
callWith.argument("bikeSwitchCost", (Integer v) -> request.bikeSwitchCost = v);
// callWith.argument("transitDistanceReluctance", (Double v) -> request.transitDistanceReluctance = v);
BicycleOptimizeType optimize = environment.getArgument("optimize");
if (optimize == BicycleOptimizeType.TRIANGLE) {
try {
RoutingRequest.assertTriangleParameters(
request.bikeTriangleSafetyFactor,
request.bikeTriangleTimeFactor,
request.bikeTriangleSlopeFactor
);
callWith.argument("triangle.safetyFactor", request::setBikeTriangleSafetyFactor);
callWith.argument("triangle.slopeFactor", request::setBikeTriangleSlopeFactor);
callWith.argument("triangle.timeFactor", request::setBikeTriangleTimeFactor);
} catch (ParameterException e) {
throw new RuntimeException(e);
}
}
callWith.argument("arriveBy", request::setArriveBy);
request.showIntermediateStops = true;
callWith.argument("vias", (List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy