Skip to content

Commit

Permalink
Merge pull request opentripplanner#5426 from ibi-group/fix-orca-cross…
Browse files Browse the repository at this point in the history
…-agency-fares

Fix ORCA cross-agency fares
  • Loading branch information
leonardehrenfried authored Oct 13, 2023
2 parents 99f9fd0 + 1e09af2 commit dcc4037
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ public static void setUpClass() {
* types.
*/
private static void calculateFare(List<Leg> legs, FareType fareType, Money expectedPrice) {
ItineraryFares fare = new ItineraryFares();
orcaFareService.populateFare(fare, USD, fareType, legs, null);
assertEquals(expectedPrice, fare.getFare(fareType));
var itinerary = new Itinerary(legs);
var itineraryFares = orcaFareService.calculateFares(itinerary);
assertEquals(expectedPrice, itineraryFares.getFare(fareType));
}

private static void assertLegFareEquals(
Expand Down Expand Up @@ -642,25 +642,27 @@ private static Itinerary createItinerary(
String firstStopName,
String lastStopName
) {
// Use the agency ID as feed ID to make sure that we have a new feed ID for each different agency
// This tests to make sure we are calculating transfers across feeds correctly.
Agency agency = Agency
.of(new FeedScopedId(FEED_ID, agencyId))
.of(new FeedScopedId(agencyId, agencyId))
.withName(agencyId)
.withTimezone(ZoneIds.NEW_YORK.getId())
.build();

// Set up stops
RegularStop firstStop = RegularStop
.of(new FeedScopedId(FEED_ID, "1"))
.of(new FeedScopedId(agencyId, "1"))
.withCoordinate(new WgsCoordinate(1, 1))
.withName(new NonLocalizedString(firstStopName))
.build();
RegularStop lastStop = RegularStop
.of(new FeedScopedId(FEED_ID, "2"))
.of(new FeedScopedId(agencyId, "2"))
.withCoordinate(new WgsCoordinate(1, 2))
.withName(new NonLocalizedString(lastStopName))
.build();

FeedScopedId routeFeedScopeId = new FeedScopedId(FEED_ID, routeId);
FeedScopedId routeFeedScopeId = new FeedScopedId(agencyId, routeId);
NonLocalizedString longName = null;
if (routeLongName != null) {
longName = new NonLocalizedString(routeLongName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,15 @@ public Map<FareType, Collection<FareRuleSet>> getFareRulesPerType() {
return fareRulesPerType;
}

/**
* Takes a legs and returns a map of their agency's feed id and all corresponding legs.
*/
protected Map<String, List<Leg>> fareLegsByFeed(List<Leg> fareLegs) {
return fareLegs
.stream()
.collect(Collectors.groupingBy(leg -> leg.getAgency().getId().getFeedId()));
}

@Override
public ItineraryFares calculateFares(Itinerary itinerary) {
var fareLegs = itinerary
Expand All @@ -105,9 +114,7 @@ public ItineraryFares calculateFares(Itinerary itinerary) {
if (fareLegs.isEmpty()) {
return null;
}
var fareLegsByFeed = fareLegs
.stream()
.collect(Collectors.groupingBy(leg -> leg.getAgency().getId().getFeedId()));
var fareLegsByFeed = fareLegsByFeed(fareLegs);

ItineraryFares fare = ItineraryFares.empty();
boolean hasFare = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,15 @@ protected Collection<FareRuleSet> fareRulesForFeed(FareType fareType, String fee
return fareRulesPerType.get(fareType);
}

/**
* Disables functionality grouping legs by their feed.
* This ensures we can calculate transfers between agencies/feeds.
*/
@Override
protected Map<String, List<Leg>> fareLegsByFeed(List<Leg> fareLegs) {
return Map.of(FEED_ID, fareLegs);
}

/**
* Check if trip falls within the transfer time window.
*/
Expand Down

0 comments on commit dcc4037

Please sign in to comment.