Skip to content
Open
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 @@ -6,10 +6,13 @@
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import java.util.Set;
import org.jspecify.annotations.Nullable;
import org.opentripplanner.core.model.id.FeedScopedId;
import org.opentripplanner.graph_builder.issue.api.DataImportIssueStore;
import org.opentripplanner.gtfs.mapping.DirectionMapper;
import org.opentripplanner.routing.alertpatch.EntitySelector;
import org.opentripplanner.routing.alertpatch.StopCondition;
import org.opentripplanner.transit.model.timetable.Direction;
Expand All @@ -18,7 +21,10 @@
import org.opentripplanner.transit.repository.TimetableRepository;
import org.opentripplanner.transit.service.TransitRepository;
import org.opentripplanner.updater.trip.siri.EntityResolver;
import org.opentripplanner.utils.logging.Throttle;
import org.opentripplanner.utils.time.ServiceDateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import uk.org.ifopt.siri21.StopPlaceRef;
import uk.org.siri.siri21.AffectedLineStructure;
import uk.org.siri.siri21.AffectedOperatorStructure;
Expand Down Expand Up @@ -47,6 +53,12 @@
*/
public class AffectsMapper {

private static final Logger LOG = LoggerFactory.getLogger(AffectsMapper.class);
private static final DirectionMapper GTFS_DIRECTION_MAPPER = new DirectionMapper(
DataImportIssueStore.NOOP
);
private static final Throttle UNKNOWN_DIRECTION_LOG_THROTTLE = Throttle.ofOneSecond();

private final String feedId;
private final TransitRepository transitRepository;

Expand Down Expand Up @@ -292,13 +304,25 @@ private List<Direction> mapDirections(List<DirectionStructure> directionStructur
return res.isEmpty() ? null : res;
}

/// SIRI-SX producers using GTFS may send the trip's numeric direction ID.
/// Use the same mapping as the static feed so the alert selector matches those trips.
private Optional<Direction> mapDirection(String directionRef) {
return switch (directionRef.toUpperCase()) {
return switch (directionRef.toUpperCase(Locale.ROOT)) {
case "0", "1" -> Optional.of(GTFS_DIRECTION_MAPPER.map(Integer.parseInt(directionRef)));
case "INBOUND" -> Optional.of(Direction.INBOUND);
case "OUTBOUND" -> Optional.of(Direction.OUTBOUND);
case "CLOCKWISE" -> Optional.of(Direction.CLOCKWISE);
case "ANTICLOCKWISE" -> Optional.of(Direction.ANTICLOCKWISE);
default -> Optional.empty();
default -> {
UNKNOWN_DIRECTION_LOG_THROTTLE.throttle(() ->
LOG.warn(
"Ignoring unrecognized SIRI-SX DirectionRef '{}' for feed {}",
directionRef,
feedId
)
);
yield Optional.empty();
}
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import org.jspecify.annotations.Nullable;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.opentripplanner.GtfsTest;
import org.opentripplanner.core.model.id.FeedScopedId;
import org.opentripplanner.core.model.time.TimePeriod;
Expand Down Expand Up @@ -647,6 +649,34 @@ public void testSiriSxUpdateForLine() {
assertEquals(endTime, effectiveEndDate);
}

@ParameterizedTest
@CsvSource({ "0, OUTBOUND, INBOUND", "1, INBOUND, OUTBOUND" })
void testSiriSxUpdateForLineAndGtfsDirection(
String directionRef,
Direction affectedDirection,
Direction oppositeDirection
) {
var lineRef = new FeedScopedId(FEED_ID, "route0");
var situation = createPtSituationElement(
"TST:SituationNumber:1",
ZonedDateTime.parse("2014-01-01T00:00:00+01:00"),
ZonedDateTime.parse("2014-01-01T23:59:59+01:00"),
createAffectsLineAndDirection(lineRef.getId(), List.of(directionRef))
);

alertsUpdateHandler.update(createServiceDelivery(situation), realTimeUpdateContext);

var alerts = transitAlertService.getDirectionAndRouteAlerts(affectedDirection, lineRef);
assertEquals(1, alerts.size());
assertThat(alerts.iterator().next().entities()).containsExactly(
new EntitySelector.DirectionAndRoute(lineRef, affectedDirection)
);
assertThat(
transitAlertService.getDirectionAndRouteAlerts(oppositeDirection, lineRef)
).isEmpty();
assertThat(transitAlertService.getRouteAlerts(lineRef)).isEmpty();
}

@Test
public void testSiriSxUpdateForLineThenExpiry() {
final FeedScopedId lineRef = new FeedScopedId(FEED_ID, "route0");
Expand Down Expand Up @@ -802,8 +832,9 @@ public void testSiriSxUpdateForLineAndExternallyDefinedStopPoint() {
assertSeparateLineAndStopAlerts(situationNumber, routeId, stopId0, stopId1);
}

@Test
public void testSiriSxUpdateForLineAndStopAndDirection() {
@ParameterizedTest
@CsvSource({ "OUTBOUND, INBOUND", "0, 1" })
void testSiriSxUpdateForLineAndStopAndDirection(String outboundRef, String inboundRef) {
var routeId = "route0";
var stopId0 = "stop0";
var stopId1 = "stop1";
Expand All @@ -815,15 +846,15 @@ public void testSiriSxUpdateForLineAndStopAndDirection() {
situationNumber1,
ZonedDateTime.parse("2014-01-01T00:00:00+01:00"),
ZonedDateTime.parse("2014-01-01T23:59:59+01:00"),
createAffectsLineAndDirection(routeId, List.of("OUTBOUND"), stopId0, stopId1)
createAffectsLineAndDirection(routeId, List.of(outboundRef), stopId0, stopId1)
)
);
var inbound = createServiceDelivery(
createPtSituationElement(
situationNumber2,
ZonedDateTime.parse("2014-01-01T00:00:00+01:00"),
ZonedDateTime.parse("2014-01-01T23:59:59+01:00"),
createAffectsLineAndDirection(routeId, List.of("INBOUND"), stopId0, stopId1)
createAffectsLineAndDirection(routeId, List.of(inboundRef), stopId0, stopId1)
)
);

Expand Down
6 changes: 6 additions & 0 deletions doc/user/SIRI-Config.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,12 @@ HTTP headers to add to the request. Any header key, value can be inserted.

## SIRI-SX Request/Response via HTTPS

For GTFS-based feeds, an affected line can use the GTFS `direction_id` in its
`DirectionRef`: `0` matches outbound trips and `1` matches inbound trips. Named
directions (`outbound`, `inbound`, `clockwise`, `anticlockwise`) are also supported.
Unrecognized values are ignored and logged as warnings. If no recognized
direction remains, the alert is not restricted by direction. This also applies to SIRI-SX Lite.

<!-- siri-sx-updater BEGIN -->
<!-- NOTE! This section is auto-generated. Do not change, change doc in code instead. -->

Expand Down