Skip to content

Commit

Permalink
Merge remote-tracking branch 'entur/via' into gtfs-pass-through-thomas
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardehrenfried committed Sep 26, 2024
2 parents 66ccaa6 + 68e8867 commit dfe1f12
Show file tree
Hide file tree
Showing 157 changed files with 4,504 additions and 1,054 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.opentripplanner.framework.concurrent.OtpRequestThreadFactory;
import org.opentripplanner.framework.lang.ObjectUtils;
import org.opentripplanner.standalone.api.OtpServerRequestContext;
import org.opentripplanner.transit.model.framework.EntityNotFoundException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -72,7 +73,7 @@ Response executeGraphQL(
return ExecutionResultMapper.timeoutResponse();
} catch (ResponseTooLargeException rtle) {
return ExecutionResultMapper.tooLargeResponse(rtle.getMessage());
} catch (CoercingParseValueException | UnknownOperationException e) {
} catch (EntityNotFoundException | CoercingParseValueException | UnknownOperationException e) {
return ExecutionResultMapper.badRequestResponse(e.getMessage());
} catch (Exception systemError) {
LOG.error(systemError.getMessage(), systemError);
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import java.util.ArrayList;
import java.util.List;
import org.opentripplanner.apis.transmodel.model.util.EncodedPolylineBeanWithStops;
import org.opentripplanner.apis.transmodel.model.framework.EncodedPolylineBeanWithStops;
import org.opentripplanner.framework.geometry.EncodedPolyline;
import org.opentripplanner.transit.model.network.TripPattern;

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
import java.util.Locale;
import java.util.Map;
import org.opentripplanner.apis.transmodel.TransmodelRequestContext;
import org.opentripplanner.apis.transmodel.model.plan.TripQuery;
import org.opentripplanner.apis.transmodel.support.DataFetcherDecorator;
import org.opentripplanner.apis.transmodel.support.GqlUtil;
import org.opentripplanner.routing.api.request.RouteRequest;
import org.opentripplanner.standalone.api.OtpServerRequestContext;
import org.opentripplanner.transit.model.framework.FeedScopedId;
import org.opentripplanner.transit.service.TransitService;

public class TripRequestMapper {

Expand All @@ -40,11 +40,16 @@ public static RouteRequest createRequest(DataFetchingEnvironment environment) {
"to",
(Map<String, Object> v) -> request.setTo(GenericLocationMapper.toGenericLocation(v))
);
final TransitService transitService = context.getTransitService();
callWith.argument(
"passThroughPoints",
(List<Map<String, Object>> v) -> {
request.setPassThroughPoints(PassThroughLocationMapper.toLocations(transitService, v));
request.setViaLocations(TripViaLocationMapper.toLegacyPassThroughLocations(v));
}
);
callWith.argument(
TripQuery.FIELD_VIA,
(List<Map<String, Object>> v) -> {
request.setViaLocations(TripViaLocationMapper.mapToViaLocations(v));
}
);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package org.opentripplanner.apis.transmodel.mapping;

import static java.util.stream.Collectors.toList;

import java.time.Duration;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import org.opentripplanner.apis.transmodel.model.plan.TripQuery;
import org.opentripplanner.apis.transmodel.model.plan.ViaLocationInputType;
import org.opentripplanner.apis.transmodel.support.OneOfInputValidator;
import org.opentripplanner.routing.api.request.via.PassThroughViaLocation;
import org.opentripplanner.routing.api.request.via.ViaLocation;
import org.opentripplanner.routing.api.request.via.VisitViaLocation;
import org.opentripplanner.transit.model.framework.FeedScopedId;

class TripViaLocationMapper {

static List<ViaLocation> mapToViaLocations(final List<Map<String, Object>> via) {
return via.stream().map(TripViaLocationMapper::mapViaLocation).collect(toList());
}

/**
* @deprecated Legacy passThrough, use via instead
*/
@Deprecated
static List<ViaLocation> toLegacyPassThroughLocations(
final List<Map<String, Object>> passThroughPoints
) {
return passThroughPoints
.stream()
.map(TripViaLocationMapper::mapLegacyPassThroughViaLocation)
.collect(toList());
}

private static ViaLocation mapViaLocation(Map<String, Object> inputMap) {
var fieldName = OneOfInputValidator.validateOneOf(
inputMap,
TripQuery.FIELD_VIA,
ViaLocationInputType.FIELD_VISIT,
ViaLocationInputType.FIELD_PASS_THROUGH
);

Map<String, Object> value = (Map<String, Object>) inputMap.get(fieldName);

return switch (fieldName) {
case ViaLocationInputType.FIELD_VISIT -> mapVisitViaLocation(value);
case ViaLocationInputType.FIELD_PASS_THROUGH -> mapPassThroughViaLocation(value);
default -> throw new IllegalArgumentException("Unknown field: " + fieldName);
};
}

private static VisitViaLocation mapVisitViaLocation(Map<String, Object> inputMap) {
var label = (String) inputMap.get(ViaLocationInputType.FIELD_LABEL);
var minimumWaitTime = (Duration) inputMap.get(ViaLocationInputType.FIELD_MINIMUM_WAIT_TIME);
var stopLocationIds = mapStopLocationIds(inputMap);
return new VisitViaLocation(label, minimumWaitTime, stopLocationIds, List.of());
}

private static PassThroughViaLocation mapPassThroughViaLocation(Map<String, Object> inputMap) {
var label = (String) inputMap.get(ViaLocationInputType.FIELD_LABEL);
var stopLocationIds = mapStopLocationIds(inputMap);
return new PassThroughViaLocation(label, stopLocationIds);
}

private static List<FeedScopedId> mapStopLocationIds(Map<String, Object> map) {
var c = (Collection<String>) map.get(ViaLocationInputType.FIELD_STOP_LOCATION_IDS);
return c.stream().map(TransitIdMapper::mapIDToDomain).toList();
}

/**
* @deprecated Legacy passThrough, use via instead
*/
@Deprecated
private static ViaLocation mapLegacyPassThroughViaLocation(Map<String, Object> inputMap) {
final String name = (String) inputMap.get("name");
final List<FeedScopedId> stopLocationIds =
((List<String>) inputMap.get("placeIds")).stream()
.map(TransitIdMapper::mapIDToDomain)
.toList();
return new PassThroughViaLocation(name, stopLocationIds);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@
import java.time.Duration;
import java.util.List;
import java.util.Map;
import org.opentripplanner.routing.api.request.ViaLocation;
import org.opentripplanner.routing.api.request.ViaLocationDeprecated;
import org.opentripplanner.routing.api.response.RoutingError;
import org.opentripplanner.routing.api.response.RoutingErrorCode;
import org.opentripplanner.routing.error.RoutingValidationException;

class ViaLocationMapper {
@Deprecated
class ViaLocationDeprecatedMapper {

static ViaLocation mapViaLocation(Map<String, Object> viaLocation) {
static ViaLocationDeprecated mapViaLocation(Map<String, Object> viaLocation) {
try {
return new ViaLocation(
return new ViaLocationDeprecated(
GenericLocationMapper.toGenericLocation(viaLocation),
false,
(Duration) viaLocation.get("minSlack"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import org.opentripplanner.framework.graphql.GraphQLUtils;
import org.opentripplanner.routing.api.request.RouteRequest;
import org.opentripplanner.routing.api.request.RouteViaRequest;
import org.opentripplanner.routing.api.request.ViaLocation;
import org.opentripplanner.routing.api.request.ViaLocationDeprecated;
import org.opentripplanner.routing.api.request.request.JourneyRequest;
import org.opentripplanner.standalone.api.OtpServerRequestContext;

Expand All @@ -27,7 +27,10 @@ public static RouteViaRequest createRouteViaRequest(DataFetchingEnvironment envi
RouteRequest request = serverContext.defaultRouteRequest();

List<Map<String, Object>> viaInput = environment.getArgument("via");
List<ViaLocation> vias = viaInput.stream().map(ViaLocationMapper::mapViaLocation).toList();
List<ViaLocationDeprecated> vias = viaInput
.stream()
.map(ViaLocationDeprecatedMapper::mapViaLocation)
.toList();

List<JourneyRequest> requests;
if (environment.containsArgument("segments")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ public class AuthorityType {

public static GraphQLObjectType create(
GraphQLOutputType lineType,
GraphQLOutputType ptSituationElementType,
GqlUtil gqlUtil
GraphQLOutputType ptSituationElementType
) {
return GraphQLObjectType
.newObject()
Expand Down Expand Up @@ -65,7 +64,7 @@ public static GraphQLObjectType create(
GraphQLFieldDefinition
.newFieldDefinition()
.name("lines")
.withDirective(gqlUtil.timingData)
.withDirective(TransmodelDirectives.TIMING_DATA)
.type(new GraphQLNonNull(new GraphQLList(lineType)))
.dataFetcher(environment ->
getTransitService(environment)
Expand All @@ -80,7 +79,7 @@ public static GraphQLObjectType create(
GraphQLFieldDefinition
.newFieldDefinition()
.name("situations")
.withDirective(gqlUtil.timingData)
.withDirective(TransmodelDirectives.TIMING_DATA)
.description("Get all situations active for the authority.")
.type(new GraphQLNonNull(new GraphQLList(new GraphQLNonNull(ptSituationElementType))))
.dataFetcher(environment ->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.opentripplanner.apis.transmodel.model.util;
package org.opentripplanner.apis.transmodel.model.framework;

import org.opentripplanner.framework.geometry.EncodedPolyline;
import org.opentripplanner.transit.model.site.StopLocation;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ public class OperatorType {

public static GraphQLObjectType create(
GraphQLOutputType lineType,
GraphQLOutputType serviceJourneyType,
GqlUtil gqlUtil
GraphQLOutputType serviceJourneyType
) {
return GraphQLObjectType
.newObject()
Expand Down Expand Up @@ -48,7 +47,7 @@ public static GraphQLObjectType create(
GraphQLFieldDefinition
.newFieldDefinition()
.name("lines")
.withDirective(gqlUtil.timingData)
.withDirective(TransmodelDirectives.TIMING_DATA)
.type(new GraphQLNonNull(new GraphQLList(lineType)))
.dataFetcher(environment ->
GqlUtil
Expand All @@ -64,7 +63,7 @@ public static GraphQLObjectType create(
GraphQLFieldDefinition
.newFieldDefinition()
.name("serviceJourney")
.withDirective(gqlUtil.timingData)
.withDirective(TransmodelDirectives.TIMING_DATA)
.type(new GraphQLNonNull(new GraphQLList(serviceJourneyType)))
.dataFetcher(environment ->
GqlUtil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public class PenaltyForStreetModeType {
private static final String FIELD_TIME_PENALTY = "timePenalty";
private static final String FIELD_COST_FACTOR = "costFactor";

public static GraphQLInputObjectType create(GqlUtil gqlUtil) {
public static GraphQLInputObjectType create() {
return GraphQLInputObjectType
.newInputObject()
.name("PenaltyForStreetMode")
Expand All @@ -70,7 +70,7 @@ public static GraphQLInputObjectType create(GqlUtil gqlUtil) {
GraphQLInputObjectField
.newInputObjectField()
.name(FIELD_TIME_PENALTY)
.type(new GraphQLNonNull(gqlUtil.doubleFunctionScalar))
.type(new GraphQLNonNull(TransmodelScalars.DOUBLE_FUNCTION_SCALAR))
.description(
"""
Penalty applied to the time for the given list of modes.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public static GraphQLInputObjectType create(GqlUtil gqlUtil) {
GraphQLInputObjectField
.newInputObjectField()
.name(FIELD_DURATION)
.type(new GraphQLNonNull(gqlUtil.durationScalar))
.type(new GraphQLNonNull(TransmodelScalars.DURATION_SCALAR))
)
.build();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.opentripplanner.apis.transmodel.model.framework;

import graphql.introspection.Introspection;
import graphql.schema.GraphQLDirective;

public class TransmodelDirectives {

public static final GraphQLDirective TIMING_DATA = GraphQLDirective
.newDirective()
.name("timingData")
.description("Add timing data to prometheus, if Actuator API is enabled")
.validLocation(Introspection.DirectiveLocation.FIELD_DEFINITION)
.build();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.opentripplanner.apis.transmodel.model.framework;

import graphql.schema.GraphQLObjectType;
import graphql.schema.GraphQLScalarType;
import org.opentripplanner.apis.transmodel.model.scalars.DoubleFunctionFactory;
import org.opentripplanner.apis.transmodel.model.scalars.LocalTimeScalarFactory;
import org.opentripplanner.apis.transmodel.model.scalars.TimeScalarFactory;
import org.opentripplanner.framework.graphql.scalar.DateScalarFactory;
import org.opentripplanner.framework.graphql.scalar.DurationScalarFactory;

/**
* This class contains all Transmodel custom scalars, except the
* {@link org.opentripplanner.apis.transmodel.support.GqlUtil#dateTimeScalar}.
*/
public class TransmodelScalars {

public static final GraphQLScalarType DATE_SCALAR;
public static final GraphQLScalarType DOUBLE_FUNCTION_SCALAR;
public static final GraphQLScalarType LOCAL_TIME_SCALAR;
public static final GraphQLObjectType TIME_SCALAR;
public static final GraphQLScalarType DURATION_SCALAR;

static {
DATE_SCALAR = DateScalarFactory.createTransmodelDateScalar();
DOUBLE_FUNCTION_SCALAR = DoubleFunctionFactory.createDoubleFunctionScalar();
LOCAL_TIME_SCALAR = LocalTimeScalarFactory.createLocalTimeScalar();
TIME_SCALAR = TimeScalarFactory.createSecondsSinceMidnightAsTimeObject();
DURATION_SCALAR = DurationScalarFactory.createDurationScalar();
}
}
Loading

0 comments on commit dfe1f12

Please sign in to comment.