Skip to content
This repository was archived by the owner on Mar 3, 2025. It is now read-only.

Commit

Permalink
Adds a matcher API for the transit service and makes use of it in the…
Browse files Browse the repository at this point in the history
… DatedServiceJourneyQuery.

This is the first simple implementation of a filter using the unified matcher API.
  • Loading branch information
eibakke committed Feb 29, 2024
1 parent 6a02985 commit 00949b5
Show file tree
Hide file tree
Showing 17 changed files with 826 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
import graphql.schema.GraphQLOutputType;
import java.time.LocalDate;
import java.util.List;
import java.util.stream.Stream;
import org.opentripplanner.apis.transmodel.mapping.TransitIdMapper;
import org.opentripplanner.apis.transmodel.model.EnumTypes;
import org.opentripplanner.apis.transmodel.support.GqlUtil;
import org.opentripplanner.transit.api.request.TripOnServiceDateRequest;
import org.opentripplanner.transit.api.request.TripOnServiceDateRequestBuilder;
import org.opentripplanner.transit.model.framework.FeedScopedId;
import org.opentripplanner.transit.model.timetable.TripAlteration;
import org.opentripplanner.transit.model.timetable.TripOnServiceDate;

/**
* A GraphQL query for retrieving data on DatedServiceJourneys
Expand Down Expand Up @@ -93,72 +93,38 @@ public static GraphQLFieldDefinition createQuery(
.type(new GraphQLList(new GraphQLNonNull(Scalars.GraphQLString)))
)
.dataFetcher(environment -> {
Stream<TripOnServiceDate> stream = GqlUtil
.getTransitService(environment)
.getAllTripOnServiceDates()
.stream();

var authorities = mapIDsToDomainNullSafe(environment.getArgument("authorities"));
var lines = mapIDsToDomainNullSafe(environment.getArgument("lines"));
var serviceJourneys = mapIDsToDomainNullSafe(environment.getArgument("serviceJourneys"));
var replacementFor = mapIDsToDomainNullSafe(environment.getArgument("replacementFor"));
var privateCodes = environment.<List<String>>getArgument("privateCodes");
var operatingDays = environment.<List<LocalDate>>getArgument("operatingDays");
var alterations = environment.<List<TripAlteration>>getArgument("alterations");
var authorities = mapIDsToDomainNullSafe(environment.getArgument("authorities"));
var replacementFor = mapIDsToDomainNullSafe(environment.getArgument("replacementFor"));

if (!lines.isEmpty()) {
stream =
stream.filter(tripOnServiceDate ->
lines.contains(tripOnServiceDate.getTrip().getRoute().getId())
);
if (operatingDays == null || operatingDays.isEmpty()) {
throw new IllegalArgumentException("At least one operatingDay must be provided.");
}

if (!serviceJourneys.isEmpty()) {
stream =
stream.filter(tripOnServiceDate ->
serviceJourneys.contains(tripOnServiceDate.getTrip().getId())
);
}
TripOnServiceDateRequestBuilder tripOnServiceDateRequestBuilder = TripOnServiceDateRequest
.of(operatingDays)
.withAuthorities(authorities)
.withLines(lines)
.withServiceJourneys(serviceJourneys)
.withReplacementFor(replacementFor);

if (privateCodes != null && !privateCodes.isEmpty()) {
stream =
stream.filter(tripOnServiceDate ->
privateCodes.contains(tripOnServiceDate.getTrip().getNetexInternalPlanningCode())
);
tripOnServiceDateRequestBuilder =
tripOnServiceDateRequestBuilder.withPrivateCodes(privateCodes);
}

// At least one operationg day is required
var days = operatingDays.stream().toList();

stream =
stream.filter(tripOnServiceDate -> days.contains(tripOnServiceDate.getServiceDate()));

if (alterations != null && !alterations.isEmpty()) {
stream =
stream.filter(tripOnServiceDate ->
alterations.contains(tripOnServiceDate.getTripAlteration())
);
}

if (!authorities.isEmpty()) {
stream =
stream.filter(tripOnServiceDate ->
authorities.contains(tripOnServiceDate.getTrip().getRoute().getAgency().getId())
);
tripOnServiceDateRequestBuilder =
tripOnServiceDateRequestBuilder.withAlterations(alterations);
}

if (!replacementFor.isEmpty()) {
stream =
stream.filter(tripOnServiceDate ->
!tripOnServiceDate.getReplacementFor().isEmpty() &&
tripOnServiceDate
.getReplacementFor()
.stream()
.anyMatch(replacement -> replacementFor.contains(replacement.getId()))
);
}

return stream.toList();
return GqlUtil
.getTransitService(environment)
.getTripOnServiceDates(tripOnServiceDateRequestBuilder.build());
})
.build();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package org.opentripplanner.transit.api.request;

import java.time.LocalDate;
import java.util.List;
import org.opentripplanner.transit.model.framework.FeedScopedId;
import org.opentripplanner.transit.model.timetable.TripAlteration;

/*
* A request for trips on a specific service date.
*
* This request is used to retrieve TripsOnServiceDates that match the provided criteria.
* At least one operatingDay must be provided.
*/
public class TripOnServiceDateRequest {

private final List<FeedScopedId> authorities;
private final List<FeedScopedId> lines;
private final List<FeedScopedId> serviceJourneys;
private final List<FeedScopedId> replacementFor;
private final List<String> privateCodes;
private final List<TripAlteration> alterations;
private final List<LocalDate> operatingDays;

protected TripOnServiceDateRequest(
List<FeedScopedId> authorities,
List<FeedScopedId> lines,
List<FeedScopedId> serviceJourneys,
List<FeedScopedId> replacementFor,
List<String> privateCodes,
List<LocalDate> operatingDays,
List<TripAlteration> alterations
) {
this.authorities = authorities;
this.lines = lines;
this.serviceJourneys = serviceJourneys;
this.replacementFor = replacementFor;
this.privateCodes = privateCodes;
this.alterations = alterations;
this.operatingDays = operatingDays;
}

public static TripOnServiceDateRequestBuilder of(List<LocalDate> operatingDays) {
return new TripOnServiceDateRequestBuilder(operatingDays);
}

public List<FeedScopedId> getAuthorities() {
return authorities;
}

public List<FeedScopedId> getLines() {
return lines;
}

public List<FeedScopedId> getServiceJourneys() {
return serviceJourneys;
}

public List<FeedScopedId> getReplacementFor() {
return replacementFor;
}

public List<String> getPrivateCodes() {
return privateCodes;
}

public List<TripAlteration> getAlterations() {
return alterations;
}

public List<LocalDate> getOperatingDays() {
return operatingDays;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package org.opentripplanner.transit.api.request;

import java.time.LocalDate;
import java.util.List;
import org.opentripplanner.transit.model.framework.FeedScopedId;
import org.opentripplanner.transit.model.timetable.TripAlteration;

public class TripOnServiceDateRequestBuilder {

private List<FeedScopedId> authorities = List.of();
private List<FeedScopedId> lines = List.of();
private List<FeedScopedId> serviceJourneys = List.of();
private List<FeedScopedId> replacementFor = List.of();
private List<String> privateCodes = List.of();
private List<TripAlteration> alterations = List.of();
private final List<LocalDate> operatingDays;

protected TripOnServiceDateRequestBuilder(List<LocalDate> operatingDays) {
if (operatingDays == null || operatingDays.isEmpty()) {
throw new IllegalArgumentException("operatingDays must have at least one date");
}
this.operatingDays = operatingDays;
}

public TripOnServiceDateRequestBuilder withAuthorities(List<FeedScopedId> authorities) {
this.authorities = authorities;
return this;
}

public TripOnServiceDateRequestBuilder withLines(List<FeedScopedId> lines) {
this.lines = lines;
return this;
}

public TripOnServiceDateRequestBuilder withServiceJourneys(List<FeedScopedId> serviceJourneys) {
this.serviceJourneys = serviceJourneys;
return this;
}

public TripOnServiceDateRequestBuilder withReplacementFor(List<FeedScopedId> replacementFor) {
this.replacementFor = replacementFor;
return this;
}

public TripOnServiceDateRequestBuilder withPrivateCodes(List<String> privateCodes) {
this.privateCodes = privateCodes;
return this;
}

public TripOnServiceDateRequestBuilder withAlterations(List<TripAlteration> alterations) {
this.alterations = alterations;
return this;
}

public TripOnServiceDateRequest build() {
return new TripOnServiceDateRequest(
authorities,
lines,
serviceJourneys,
replacementFor,
privateCodes,
operatingDays,
alterations
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package org.opentripplanner.transit.model.filter.expr;

import static org.opentripplanner.transit.model.filter.expr.BinaryOperator.AND;

import java.util.List;

/**
* Takes a list of matchers and provides a single interface. All matchers in the list must match for
* the composite matcher to return a match.
*/
public final class AndMatcher<T> implements Matcher<T> {

private final Matcher<T>[] matchers;

private AndMatcher(List<Matcher<T>> matchers) {
this.matchers = matchers.toArray(Matcher[]::new);
}

public static <T> Matcher<T> of(List<Matcher<T>> matchers) {
// simplify a list of one element
if (matchers.size() == 1) {
return matchers.get(0);
}
return new AndMatcher<>(matchers);
}

@Override
public boolean match(T entity) {
for (var m : matchers) {
if (!m.match(entity)) {
return false;
}
}
return true;
}

@Override
public String toString() {
return "(" + AND.arrayToString(matchers) + ')';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.opentripplanner.transit.model.filter.expr;

import java.util.Arrays;
import java.util.Collection;
import java.util.Objects;
import java.util.stream.Collectors;

/**
* Used to concatenate matches with either the logical "AND" or "OR" operator.
*/
enum BinaryOperator {
AND("&"),
OR("|");

private final String token;

BinaryOperator(String token) {
this.token = token;
}

@Override
public String toString() {
return token;
}

<T> String arrayToString(T[] values) {
return colToString(Arrays.asList(values));
}

<T> String colToString(Collection<T> values) {
return values.stream().map(Objects::toString).collect(Collectors.joining(" " + token + " "));
}

<T> String toString(T a, T b) {
return a.toString() + " " + token + " " + b.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.opentripplanner.transit.model.filter.expr;

import java.util.Collection;
import java.util.function.Function;

/**
* A matcher that checks if a collection contains a value.
* <p>
* The collection is provided by a function that takes the entity being matched as an argument.
*/
public class ContainsMatcher<T, V> implements Matcher<T> {

private final String typeName;
private final V value;
private final Function<T, Collection<V>> valueProvider;

public ContainsMatcher(String typeName, V value, Function<T, Collection<V>> valueProvider) {
this.typeName = typeName;
this.value = value;
this.valueProvider = valueProvider;
}

@Override
public boolean match(T entity) {
Collection<V> values = valueProvider.apply(entity);
if (values == null) {
return false;
}
return values.contains(value);
}

@Override
public String toString() {
return typeName + " contains " + value;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.opentripplanner.transit.model.filter.expr;

import java.util.function.Function;

/**
* A matcher that checks if a value is equal to another value.
* <p>
* The value is provided by a function that takes the entity being matched as an argument.
*/
public class EqualityMatcher<T, V> implements Matcher<T> {

private final String typeName;
private final V value;
private final Function<T, V> valueProvider;

public EqualityMatcher(String typeName, V value, Function<T, V> valueProvider) {
this.typeName = typeName;
this.value = value;
this.valueProvider = valueProvider;
}

@Override
public boolean match(T entity) {
return value.equals(valueProvider.apply(entity));
}

@Override
public String toString() {
return typeName + "==" + value;
}
}
Loading

0 comments on commit 00949b5

Please sign in to comment.