Skip to content

Commit

Permalink
Add filter just for direct flex itineraries
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardehrenfried committed Sep 19, 2024
1 parent 91d1698 commit ff4bf13
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/main/java/org/opentripplanner/model/plan/Itinerary.java
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,13 @@ public boolean isOnStreetAllTheWay() {
return isStreetOnly();
}

/**
* Returns true if this itinerary has only flex and walking legs.
*/
public boolean isFlexAndWalkOnly() {
return legs.stream().allMatch(l -> l.isFlexibleTrip() || l.isWalkingLeg());
}

/** TRUE if at least one leg is a transit leg. */
public boolean hasTransit() {
return legs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.opentripplanner.routing.algorithm.filterchain.filters.street.RemoveNonTransitItinerariesBasedOnGeneralizedCost;
import org.opentripplanner.routing.algorithm.filterchain.filters.street.RemoveParkAndRideWithMostlyWalkingFilter;
import org.opentripplanner.routing.algorithm.filterchain.filters.street.RemoveWalkOnlyFilter;
import org.opentripplanner.routing.algorithm.filterchain.filters.system.FlexSearchWindowFilter;
import org.opentripplanner.routing.algorithm.filterchain.filters.system.NumItinerariesFilter;
import org.opentripplanner.routing.algorithm.filterchain.filters.system.OutsideSearchWindowFilter;
import org.opentripplanner.routing.algorithm.filterchain.filters.system.PagingFilter;
Expand Down Expand Up @@ -468,6 +469,7 @@ public ItineraryListFilterChain build() {
filters,
new OutsideSearchWindowFilter(earliestDepartureTime, searchWindow)
);
addRemoveFilter(filters, new FlexSearchWindowFilter(earliestDepartureTime));
}

// Remove itineraries present in the page retrieved before this page/search.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package org.opentripplanner.routing.algorithm.filterchain.filters.system;

import java.time.Instant;
import java.util.function.Predicate;
import org.opentripplanner.model.plan.Itinerary;
import org.opentripplanner.routing.algorithm.filterchain.framework.spi.RemoveItineraryFlagger;

/**
* The flex router doesn't use the transit router's time window but nevertheless using it
* for filtering is useful when combining flex with transit.
* <p>
* The flex router also searches the previous day (arrive by) or the next one (depart after).
* If you didn't do it you could get yesterday's or tomorrow's results where you would not expect it.
*/
public class FlexSearchWindowFilter implements RemoveItineraryFlagger {

public static final String TAG = "outside-flex-window";

private final Instant earliestDepartureTime;

public FlexSearchWindowFilter(Instant earliestDepartureTime) {
this.earliestDepartureTime = earliestDepartureTime;
}

@Override
public String name() {
return TAG;
}

@Override
public Predicate<Itinerary> shouldBeFlaggedForRemoval() {
return it -> {
if (it.isFlexAndWalkOnly()) {
var time = it.startTime().toInstant();
return time.isBefore(earliestDepartureTime);
} else {
return false;
}
};
}

@Override
public boolean skipAlreadyFlaggedItineraries() {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
import org.opentripplanner.routing.algorithm.filterchain.framework.spi.RemoveItineraryFlagger;

/**
* This filter will remove all itineraries that are outside the search-window. In some
* cases the access is time-shifted after the end of the search-window. These results
* This filter will remove all itineraries that are both search-window aware and outside the
* search-window. Only those that use transit are search-window, street and flex itineraries are not.
* <p>
* In some cases the access is time-shifted after the end of the search-window. These results
* should appear again when paging to the next page. Hence, this filter will remove
* such itineraries. The same is true for when paging to the previous page for arriveBy=true.
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,12 @@ void bothPenalties() {
);
}

@Test
void flexAndWalk() {
assertFalse(itinerary().isFlexAndWalkOnly());
assertTrue(newItinerary(A).flex(T11_10, T11_20, B).build().isFlexAndWalkOnly());
}

private static Itinerary itinerary() {
return newItinerary(A).bus(1, T11_04, T11_14, B).build();
}
Expand Down

0 comments on commit ff4bf13

Please sign in to comment.