Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/dev-2.x' into otp2_journey_request
Browse files Browse the repository at this point in the history
  • Loading branch information
hannesj committed Sep 14, 2022
2 parents efda68f + 485e32b commit 56b8200
Show file tree
Hide file tree
Showing 59 changed files with 522 additions and 826 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import static org.opentripplanner.graph_builder.DataImportIssueStore.noopIssueStore;
import static org.opentripplanner.graph_builder.module.FakeGraph.getFileForResource;
import static org.opentripplanner.routing.api.request.StreetMode.FLEXIBLE;
import static org.opentripplanner.routing.core.TraverseMode.BUS;
import static org.opentripplanner.routing.core.TraverseMode.WALK;
import static org.opentripplanner.transit.model.basic.TransitMode.BUS;

import java.io.File;
import java.net.URISyntaxException;
Expand All @@ -31,7 +31,6 @@
import org.opentripplanner.model.plan.Itinerary;
import org.opentripplanner.routing.RoutingService;
import org.opentripplanner.routing.api.request.RouteRequest;
import org.opentripplanner.routing.core.TraverseMode;
import org.opentripplanner.routing.graph.Graph;
import org.opentripplanner.transit.service.TransitModel;
import org.opentripplanner.util.OTPFeature;
Expand Down Expand Up @@ -81,17 +80,17 @@ void shouldReturnARouteTransferringFromBusToFlex() {

assertEquals(4, itin.getLegs().size());

var walkToBus = itin.getLegs().get(0);
assertEquals(TraverseMode.WALK, walkToBus.getMode());
var walkToBus = itin.getStreetLeg(0);
assertEquals(WALK, walkToBus.getMode());

var bus = itin.getLegs().get(1);
var bus = itin.getTransitLeg(1);
assertEquals(BUS, bus.getMode());
assertEquals("30", bus.getRoute().getShortName());

var transfer = itin.getLegs().get(2);
assertEquals(TraverseMode.WALK, transfer.getMode());
var transfer = itin.getStreetLeg(2);
assertEquals(WALK, transfer.getMode());

var flex = itin.getLegs().get(3);
var flex = itin.getTransitLeg(3);
assertEquals(BUS, flex.getMode());
assertEquals("Zone 2", flex.getRoute().getShortName());
assertTrue(flex.isFlexibleTrip());
Expand All @@ -111,21 +110,21 @@ void shouldReturnARouteWithTwoTransfers() {

assertEquals(5, itin.getLegs().size());

var firstBus = itin.getLegs().get(0);
var firstBus = itin.getTransitLeg(0);
assertEquals(BUS, firstBus.getMode());
assertEquals("856", firstBus.getRoute().getShortName());

var transferToSecondBus = itin.getLegs().get(1);
var transferToSecondBus = itin.getStreetLeg(1);
assertEquals(WALK, transferToSecondBus.getMode());

var secondBus = itin.getLegs().get(2);
var secondBus = itin.getTransitLeg(2);
assertEquals(BUS, secondBus.getMode());
assertEquals("30", secondBus.getRoute().getShortName());

var transferToFlex = itin.getLegs().get(3);
var transferToFlex = itin.getStreetLeg(3);
assertEquals(WALK, transferToFlex.getMode());

var finalFlex = itin.getLegs().get(4);
var finalFlex = itin.getTransitLeg(4);
assertEquals(BUS, finalFlex.getMode());
assertEquals("Zone 2", finalFlex.getRoute().getShortName());
assertTrue(finalFlex.isFlexibleTrip());
Expand All @@ -145,10 +144,10 @@ void flexDirect() {
assertEquals("2021-12-02T12:53:12-05:00[America/New_York]", itin.startTime().toString());
assertEquals(3173, itin.getGeneralizedCost());

var walkToFlex = itin.getLegs().get(0);
assertEquals(TraverseMode.WALK, walkToFlex.getMode());
var walkToFlex = itin.getStreetLeg(0);
assertEquals(WALK, walkToFlex.getMode());

var flex = itin.getLegs().get(1);
var flex = itin.getTransitLeg(1);
assertEquals(BUS, flex.getMode());
assertEquals("Zone 2", flex.getRoute().getShortName());
assertTrue(flex.isFlexibleTrip());
Expand Down Expand Up @@ -227,7 +226,7 @@ private Itinerary getItinerary(
request.setNumItineraries(10);
request.setSearchWindow(Duration.ofHours(2));

var modes = request.journey().modes().copy();
var modes = request.journey().modes().copyOf();
modes.withEgressMode(FLEXIBLE);

if (onlyDirect) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.util.stream.Stream;
import org.opentripplanner.model.plan.Itinerary;
import org.opentripplanner.model.plan.Leg;
import org.opentripplanner.model.plan.TransitLeg;
import org.opentripplanner.routing.core.FareType;
import org.opentripplanner.routing.core.ItineraryFares;
import org.opentripplanner.routing.core.Money;
Expand Down Expand Up @@ -339,8 +340,8 @@ private static Ride mapToRide(Itinerary itinerary, Leg leg) {
Ride ride = new Ride();
ride.classifier = NycRideClassifier.WALK;
return ride;
} else if (leg.isTransitLeg()) {
Ride ride = rideForTransitPathLeg(leg);
} else if (leg instanceof TransitLeg transitLeg) {
Ride ride = rideForTransitPathLeg(transitLeg);
Route route = leg.getRoute();
int routeType = route.getGtfsType();

Expand Down Expand Up @@ -387,15 +388,15 @@ private static List<FeedScopedId> makeMtaStopList(String... stops) {
return out;
}

private static Ride rideForTransitPathLeg(Leg leg) {
private static Ride rideForTransitPathLeg(TransitLeg transitLeg) {
Ride ride = new Ride();
ride.firstStop = leg.getFrom().stop;
ride.lastStop = leg.getTo().stop;
ride.firstStop = transitLeg.getFrom().stop;
ride.lastStop = transitLeg.getTo().stop;

ride.startZone = ride.firstStop.getFirstZoneAsString();
ride.endZone = ride.lastStop.getFirstZoneAsString();

var zones = leg
var zones = transitLeg
.getIntermediateStops()
.stream()
.map(stopArrival -> stopArrival.place.stop.getFirstZoneAsString())
Expand All @@ -406,15 +407,15 @@ private static Ride rideForTransitPathLeg(Leg leg) {
);

ride.zones = zones;
ride.agency = leg.getRoute().getAgency().getId();
ride.route = leg.getRoute().getId();
ride.trip = leg.getTrip().getId();
ride.agency = transitLeg.getRoute().getAgency().getId();
ride.route = transitLeg.getRoute().getId();
ride.trip = transitLeg.getTrip().getId();

ride.startTime = leg.getStartTime();
ride.endTime = leg.getEndTime();
ride.startTime = transitLeg.getStartTime();
ride.endTime = transitLeg.getEndTime();

// In the default fare service, we classify rides by mode.
ride.classifier = leg.getMode();
ride.classifier = transitLeg.getMode();
return ride;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,15 @@
import java.util.Set;
import org.opentripplanner.ext.fares.model.FareRuleSet;
import org.opentripplanner.model.plan.Leg;
import org.opentripplanner.model.plan.TransitLeg;
import org.opentripplanner.routing.core.FareType;
import org.opentripplanner.routing.core.ItineraryFares;
import org.opentripplanner.routing.core.TraverseMode;
import org.opentripplanner.transit.model.basic.TransitMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SFBayFareServiceImpl extends DefaultFareServiceImpl {

private static final long serialVersionUID = 20120229L;

@SuppressWarnings("unused")
private static final Logger LOG = LoggerFactory.getLogger(SFBayFareServiceImpl.class);

Expand Down Expand Up @@ -65,7 +64,8 @@ protected float getLowestCost(
bartBlock = null;
}
if (agencyId.equals("SFMTA")) {
if (ride.getMode().equals(TraverseMode.CABLE_CAR)) {
TransitMode mode = (ride instanceof TransitLeg transitLeg) ? transitLeg.getMode() : null;
if (mode == TransitMode.CABLE_CAR) {
// no transfers issued or accepted
cost += CABLE_CAR_FARE;
} else if (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.annotation.Nonnull;
import org.locationtech.jts.geom.LineString;
import org.opentripplanner.ext.flex.edgetype.FlexTripEdge;
import org.opentripplanner.model.BookingInfo;
import org.opentripplanner.model.PickDrop;
import org.opentripplanner.model.plan.Leg;
import org.opentripplanner.model.plan.Place;
import org.opentripplanner.model.plan.StopArrival;
import org.opentripplanner.model.plan.TransitLeg;
import org.opentripplanner.routing.alertpatch.TransitAlert;
import org.opentripplanner.routing.core.TraverseMode;
import org.opentripplanner.transit.model.basic.TransitMode;
import org.opentripplanner.transit.model.basic.WheelchairAccessibility;
import org.opentripplanner.transit.model.framework.AbstractTransitEntity;
import org.opentripplanner.transit.model.network.Route;
Expand All @@ -28,7 +30,7 @@
* One leg of a trip -- that is, a temporally continuous piece of the journey that takes place on a
* particular vehicle, which is running on flexible trip, i.e. not using fixed schedule and stops.
*/
public class FlexibleTransitLeg implements Leg {
public class FlexibleTransitLeg implements TransitLeg {

private final FlexTripEdge edge;

Expand All @@ -54,11 +56,6 @@ public FlexibleTransitLeg(
this.generalizedCost = generalizedCost;
}

@Override
public boolean isTransitLeg() {
return true;
}

@Override
public Agency getAgency() {
return getTrip().getRoute().getAgency();
Expand All @@ -85,8 +82,9 @@ public WheelchairAccessibility getTripWheelchairAccessibility() {
}

@Override
public TraverseMode getMode() {
return TraverseMode.fromTransitMode(getTrip().getMode());
@Nonnull
public TransitMode getMode() {
return getTrip().getMode();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
public class FlexTripEdge extends Edge {

private static final Logger LOG = LoggerFactory.getLogger(FlexTripEdge.class);

private static final long serialVersionUID = 1L;
private final FlexTrip trip;
public StopLocation s1;
public StopLocation s2;
Expand Down Expand Up @@ -64,7 +62,7 @@ public State traverse(State s0) {
return null;
}
StateEditor editor = s0.edit(this);
editor.setBackMode(TraverseMode.BUS);
editor.setBackMode(TraverseMode.FLEX);
// TODO: decide good value
editor.incrementWeight(10 * 60);
int timeInSeconds = getTimeInSeconds();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import org.opentripplanner.model.PickDrop;
import org.opentripplanner.model.plan.Leg;
import org.opentripplanner.model.plan.StopArrival;
import org.opentripplanner.model.plan.StreetLeg;
import org.opentripplanner.model.plan.TransitLeg;
import org.opentripplanner.model.plan.WalkStep;
import org.opentripplanner.routing.RoutingService;
import org.opentripplanner.transit.model.network.Route;
Expand Down Expand Up @@ -123,7 +125,16 @@ public DataFetcher<Geometry> legGeometry() {

@Override
public DataFetcher<String> mode() {
return environment -> getSource(environment).getMode().name();
return environment -> {
Leg leg = getSource(environment);
if (leg instanceof StreetLeg s) {
return s.getMode().name();
}
if (leg instanceof TransitLeg s) {
return s.getMode().name();
}
throw new IllegalStateException("Unhandled leg type: " + leg);
};
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,29 +254,25 @@ public class EnumTypes {
)
.build();

public static GraphQLEnumType MODE = GraphQLEnumType
public static GraphQLEnumType LEG_MODE = GraphQLEnumType
.newEnum()
.name("Mode")
.value("air", TraverseMode.AIRPLANE)
.value("air", TransitMode.AIRPLANE)
.value("bicycle", TraverseMode.BICYCLE)
.value("bus", TraverseMode.BUS)
.value("cableway", TraverseMode.CABLE_CAR)
.value("water", TraverseMode.FERRY)
.value("funicular", TraverseMode.FUNICULAR)
.value("lift", TraverseMode.GONDOLA)
.value("rail", TraverseMode.RAIL)
.value("metro", TraverseMode.SUBWAY)
.value("tram", TraverseMode.TRAM)
.value("coach", TraverseMode.BUS)
.description("NOT IMPLEMENTED")
.value("transit", TraverseMode.TRANSIT, "Any for of public transportation")
.value("bus", TransitMode.BUS)
.value("cableway", TransitMode.CABLE_CAR)
.value("water", TransitMode.FERRY)
.value("funicular", TransitMode.FUNICULAR)
.value("lift", TransitMode.GONDOLA)
.value("rail", TransitMode.RAIL)
.value("metro", TransitMode.SUBWAY)
.value("tram", TransitMode.TRAM)
.value("trolleybus", TransitMode.TROLLEYBUS)
.value("monorail", TransitMode.MONORAIL)
.value("coach", TransitMode.COACH)
.value("foot", TraverseMode.WALK)
.value("car", TraverseMode.CAR)
.value("scooter", TraverseMode.SCOOTER)
// TODO OTP2 - Car park no added
// .value("car_park", TraverseMode.CAR_PARK, "Combine with foot and transit for park and ride.")
// .value("car_dropoff", TraverseMode.CAR_DROPOFF, "Combine with foot and transit for kiss and ride.")
// .value("car_pickup", TraverseMode.CAR_PICKUP, "Combine with foot and transit for ride and kiss.")
.build();

public static GraphQLEnumType TRANSPORT_MODE = GraphQLEnumType
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.opentripplanner.ext.transmodelapi.model.plan;

import static org.opentripplanner.ext.transmodelapi.model.EnumTypes.ALTERNATIVE_LEGS_FILTER;
import static org.opentripplanner.ext.transmodelapi.model.EnumTypes.MODE;
import static org.opentripplanner.ext.transmodelapi.model.EnumTypes.LEG_MODE;

import graphql.Scalars;
import graphql.scalars.ExtendedScalars;
Expand All @@ -16,13 +16,16 @@
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.opentripplanner.ext.transmodelapi.model.EnumTypes;
import org.opentripplanner.ext.transmodelapi.model.TransmodelTransportSubmode;
import org.opentripplanner.ext.transmodelapi.model.TripTimeShortHelper;
import org.opentripplanner.ext.transmodelapi.support.GqlUtil;
import org.opentripplanner.model.plan.Leg;
import org.opentripplanner.model.plan.StopArrival;
import org.opentripplanner.model.plan.StreetLeg;
import org.opentripplanner.model.plan.TransitLeg;
import org.opentripplanner.model.plan.legreference.LegReferenceSerializer;
import org.opentripplanner.routing.alternativelegs.AlternativeLegs;
import org.opentripplanner.transit.model.timetable.TripIdAndServiceDate;
Expand Down Expand Up @@ -119,8 +122,8 @@ public static GraphQLObjectType create(
.description(
"The mode of transport or access (e.g., foot) used when traversing this leg."
)
.type(new GraphQLNonNull(MODE))
.dataFetcher(env -> leg(env).getMode())
.type(new GraphQLNonNull(LEG_MODE))
.dataFetcher(env -> onLeg(env, StreetLeg::getMode, TransitLeg::getMode))
.build()
)
.field(
Expand Down Expand Up @@ -525,4 +528,19 @@ public static GraphQLObjectType create(
private static Leg leg(DataFetchingEnvironment environment) {
return environment.getSource();
}

private static <S, T> Object onLeg(
DataFetchingEnvironment environment,
Function<StreetLeg, S> streetLegAccessor,
Function<TransitLeg, T> transitLegAccessor
) {
Leg leg = leg(environment);
if (leg instanceof StreetLeg sl) {
return streetLegAccessor.apply(sl);
}
if (leg instanceof TransitLeg tl) {
return transitLegAccessor.apply(tl);
}
throw new IllegalStateException("Unhandled leg type: " + leg);
}
}
Loading

0 comments on commit 56b8200

Please sign in to comment.