Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add implementation for Service Journey filter #5641

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -181,18 +181,24 @@ private static List<TripPatternForDate> filterActiveTripPatterns(
// 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());

for (TripPatternForDate p : tripPatternsForDate) {
if (firstDay || p.getStartOfRunningPeriod().equals(date)) {
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 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,17 @@ 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 +88,32 @@ public boolean matches(TripPattern tripPattern) {
public boolean matchesSelect(TripTimes tripTimes) {
var trip = tripTimes.getTrip();

return (
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())
);
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 (

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())
);
this.transportModeFilter.match(trip.getMode(), trip.getNetexSubMode());

return matchesTrip || matchesTransportMode;
}

@Override
Expand Down Expand Up @@ -120,6 +142,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 +172,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 +213,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,27 @@ 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 +91,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 @@ -144,6 +144,11 @@ public boolean hasSubModeFilters() {
return false;
}

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

@Override
public BitSet filterAvailableStops(
RoutingTripPattern tripPattern,
Expand Down
Loading
Loading