Skip to content

Commit

Permalink
Add implementation for Service Journey filter
Browse files Browse the repository at this point in the history
  • Loading branch information
bartosz committed Jan 24, 2024
1 parent 3f69e5e commit faf6205
Show file tree
Hide file tree
Showing 10 changed files with 206 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ static SelectRequest mapSelectRequest(Map<String, List<?>> input) {
selectRequestBuilder.withGroupOfRoutes(mapIDsToDomainNullSafe(groupOfLines));
}

if (input.containsKey("serviceJourneys")) {
var serviceJourneys = (List<String>) input.get("serviceJourneys");
selectRequestBuilder.withTrips(mapIDsToDomainNullSafe(serviceJourneys));
}

if (input.containsKey("transportModes")) {
var tModes = new ArrayList<MainAndSubMode>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,23 +176,34 @@ private static List<TripPatternForDate> filterActiveTripPatterns(
boolean firstDay,
TransitDataProviderFilter filter
) {
System.out.println("filter active trip patterns");

// On the first search day we want to add both TripPatternsForDate objects that start that day
// and any previous day, while on subsequent search days we only want to add the
// TripPatternForDate objects that start on that particular day. This is to prevent duplicates.
// This was previously a stream, but was unrolled for improved performance.

var hasTripFilters = filter.hasTripFilters();
Predicate<TripTimes> tripTimesWithSubmodesPredicate = tripTimes ->
filter.tripTimesPredicate(tripTimes, filter.hasSubModeFilters());
filter.tripTimesPredicate(tripTimes, filter.hasSubModeFilters() || hasTripFilters);

Predicate<TripTimes> tripTimesWithoutSubmodesPredicate = tripTimes ->
filter.tripTimesPredicate(tripTimes, false);

Collection<TripPatternForDate> tripPatternsForDate = transitLayer.getTripPatternsForDate(date);
List<TripPatternForDate> result = new ArrayList<>(tripPatternsForDate.size());

System.out.println("tripPatternsForDate: {}" + tripPatternsForDate.size());

for (TripPatternForDate p : tripPatternsForDate) {
if (firstDay || p.getStartOfRunningPeriod().equals(date)) {
System.out.println("*");
if (filter.tripPatternPredicate(p)) {
var tripTimesPredicate = p.getTripPattern().getPattern().getContainsMultipleModes()

var tripTimesPredicate = hasTripFilters || p.getTripPattern().getPattern().getContainsMultipleModes()
? tripTimesWithSubmodesPredicate
: tripTimesWithoutSubmodesPredicate;

TripPatternForDate tripPatternForDate = p.newWithFilteredTripTimes(tripTimesPredicate);
if (tripPatternForDate != null) {
result.add(tripPatternForDate);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public class RouteRequestTransitDataProviderFilter implements TransitDataProvide

private final boolean hasSubModeFilters;

private final boolean hasTripFilters;

public RouteRequestTransitDataProviderFilter(RouteRequest request) {
this(
request.journey().transfer().mode() == StreetMode.BIKE,
Expand Down Expand Up @@ -68,13 +70,19 @@ public RouteRequestTransitDataProviderFilter(
this.bannedTrips = bannedTrips;
this.filters = filters.toArray(TransitFilter[]::new);
this.hasSubModeFilters = filters.stream().anyMatch(TransitFilter::isSubModePredicate);
this.hasTripFilters = filters.stream().anyMatch(TransitFilter::isTripPredicate);
}

@Override
public boolean hasSubModeFilters() {
return hasSubModeFilters;
}

@Override
public boolean hasTripFilters() {
return hasTripFilters;
}

public static BikeAccess bikeAccessForTrip(Trip trip) {
if (trip.getBikesAllowed() != BikeAccess.UNKNOWN) {
return trip.getBikesAllowed();
Expand All @@ -85,6 +93,7 @@ public static BikeAccess bikeAccessForTrip(Trip trip) {

@Override
public boolean tripPatternPredicate(TripPatternForDate tripPatternForDate) {
System.out.println("tripPatternPredicate");
for (TransitFilter filter : filters) {
if (filter.matchTripPattern(tripPatternForDate.getTripPattern().getPattern())) {
return true;
Expand All @@ -95,6 +104,7 @@ public boolean tripPatternPredicate(TripPatternForDate tripPatternForDate) {

@Override
public boolean tripTimesPredicate(TripTimes tripTimes, boolean withFilters) {
System.out.println("tripTimesPredicate");
final Trip trip = tripTimes.getTrip();

if (requireBikesAllowed) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public interface TransitDataProviderFilter {

boolean hasSubModeFilters();

boolean hasTripFilters();

boolean tripTimesPredicate(TripTimes tripTimes, boolean withFilters);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public static Builder of() {
private final List<FeedScopedId> agencies;
private final List<FeedScopedId> groupOfRoutes;
private final List<FeedScopedId> routes;
private final List<FeedScopedId> trips;

public SelectRequest(Builder builder) {
if (builder.transportModes.isEmpty()) {
Expand All @@ -35,7 +36,14 @@ public SelectRequest(Builder builder) {

this.agencies = List.copyOf(builder.agencies);
this.groupOfRoutes = List.copyOf(builder.groupOfRoutes);
this.routes = builder.routes;
this.routes = List.copyOf(builder.routes);
this.trips = List.copyOf(builder.trips);

}

public boolean isTripsOnly() {
return this.transportModeFilter == null && this.agencies.isEmpty() &&
this.groupOfRoutes.isEmpty() && this.routes.isEmpty();
}

public boolean matches(TripPattern tripPattern) {
Expand Down Expand Up @@ -77,21 +85,26 @@ public boolean matches(TripPattern tripPattern) {
public boolean matchesSelect(TripTimes tripTimes) {
var trip = tripTimes.getTrip();

return (
this.transportModeFilter == null ||
this.transportModeFilter.match(trip.getMode(), trip.getNetexSubMode())
);
var tripFilter = this.getTrips();

var matchesTrip = (tripFilter == null || tripFilter.isEmpty()) || tripFilter.contains(trip.getId());
var matchesTransportMode = this.transportModeFilter == null || this.transportModeFilter.match(trip.getMode(), trip.getNetexSubMode());

return matchesTrip && matchesTransportMode;
}

/**
* Matches the not clause of a transit filter request.
*/
public boolean matchesNot(TripTimes tripTimes) {
var trip = tripTimes.getTrip();
return (
this.transportModeFilter != null &&
this.transportModeFilter.match(trip.getMode(), trip.getNetexSubMode())
);

var tripFilter = this.getTrips();

var matchesTrip = (tripFilter != null && !tripFilter.isEmpty()) && tripFilter.contains(trip.getId());
var matchesTransportMode = this.transportModeFilter != null && this.transportModeFilter.match(trip.getMode(), trip.getNetexSubMode());

return matchesTrip || matchesTransportMode;
}

@Override
Expand Down Expand Up @@ -120,6 +133,10 @@ public List<FeedScopedId> routes() {
return routes;
}

public List<FeedScopedId> getTrips() {
return trips;
}

private String transportModesToString() {
if (transportModes == null) {
return null;
Expand All @@ -146,6 +163,7 @@ public static class Builder {
private List<FeedScopedId> agencies = new ArrayList<>();
private List<FeedScopedId> groupOfRoutes = new ArrayList<>();
private List<FeedScopedId> routes = new ArrayList<>();
private List<FeedScopedId> trips = new ArrayList<>();

public Builder withTransportModes(List<MainAndSubMode> transportModes) {
this.transportModes = transportModes;
Expand Down Expand Up @@ -186,6 +204,11 @@ public Builder withGroupOfRoutes(List<FeedScopedId> groupOfRoutes) {
return this;
}

public Builder withTrips(List<FeedScopedId> trips) {
this.trips = trips;
return this;
}

public SelectRequest build() {
return new SelectRequest(this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,8 @@ public interface TransitFilter {
default boolean isSubModePredicate() {
return false;
}

default boolean isTripPredicate() {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,33 @@ public boolean isSubModePredicate() {
return false;
}

@Override
public boolean isTripPredicate() {
for (var selectRequest : select) {
if (
selectRequest.getTrips() != null &&
!selectRequest.getTrips().isEmpty()
) {
return true;
}
}

for (var selectRequest : not) {
if (
selectRequest.getTrips() != null &&
!selectRequest.getTrips().isEmpty()
) {
return true;
}
}
return false;
}

@Override
public boolean matchTripPattern(TripPattern tripPattern) {
if (select.length != 0) {
var tripPatternSelect = Arrays.stream(select).filter(s -> !s.isTripsOnly()).toArray();

if (tripPatternSelect.length != 0) {
var anyMatch = false;
for (SelectRequest s : select) {
if (s.matches(tripPattern)) {
Expand All @@ -73,7 +97,8 @@ public boolean matchTripPattern(TripPattern tripPattern) {
}

for (SelectRequest s : not) {
if (s.matches(tripPattern)) {
// We'll filter trips in the next step
if (!s.isTripsOnly() && s.matches(tripPattern)) {
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -769,4 +769,6 @@ public void testGroupOfLinesExcludeFunctionality() {
assertEquals(1, bannedPatterns.size());
assertTrue(bannedPatterns.contains(id(JOURNEY_PATTERN_ID_1)));
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,11 @@ public boolean hasSubModeFilters() {
return false;
}

@Override
public boolean hasTripFilters() {
return false;
}

@Override
public BitSet filterAvailableStops(
RoutingTripPattern tripPattern,
Expand All @@ -152,5 +157,6 @@ public BitSet filterAvailableStops(
) {
return boardingPossible;
}

}
}
Loading

0 comments on commit faf6205

Please sign in to comment.