Skip to content

Commit

Permalink
Take UseRealtimeData into account
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardehrenfried committed Mar 5, 2025
1 parent d2ecd0f commit 530d808
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,18 @@
import de.vdv.ojp20.siri.LineRefStructure;
import de.vdv.ojp20.siri.VehicleModesOfTransportEnumeration;
import java.time.ZonedDateTime;
import java.util.Optional;
import java.util.Set;
import org.junit.jupiter.api.Test;
import org.opentripplanner._support.time.ZoneIds;
import org.opentripplanner.ext.vdv.id.UseFeedIdResolver;
import org.opentripplanner.ext.vdv.ojp.mapping.StopEventResponseMapper;
import org.opentripplanner.transit.model.framework.FeedScopedId;
import org.rutebanken.time.XmlDateTime;

class OjpServiceTest {

private static final ZonedDateTime ZDT = ZonedDateTime.parse("2025-02-17T14:24:02+01:00");
private static final UseFeedIdResolver ID_RESOLVER = new UseFeedIdResolver();
private static final OjpService SERVICE = new OjpService(
null,
ID_RESOLVER,
new StopEventResponseMapper(ZoneIds.BERLIN, ID_RESOLVER, l -> Optional.of("de")),
ZoneIds.BERLIN
);
private static final OjpService SERVICE = new OjpService(null, ID_RESOLVER, ZoneIds.BERLIN);

private static final FeedScopedId LINE_ID = id("line1");
private static final FeedScopedId AGENCY_ID = id("line1");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,15 @@ class OjpMapperTest {
@Test
void test() throws JAXBException {
var mapper = new StopEventResponseMapper(
Set.of(),
ZoneIds.BERLIN,
new UseFeedIdResolver(),
RESOLVE_FEED_LANG
);

var ojp = mapper.mapStopTimesInPattern(
List.of(new CallAtStop(TRIP_TIMES_ON_DATE, WALK_TIME)),
timestamp,
Set.of()
timestamp
);

var context = JAXBContext.newInstance(OJP.class);
Expand All @@ -106,14 +106,14 @@ void test() throws JAXBException {
@Test
void ojpToTrias() {
var mapper = new StopEventResponseMapper(
Set.of(),
ZoneIds.BERLIN,
new UseFeedIdResolver(),
RESOLVE_FEED_LANG
);
var ojp = mapper.mapStopTimesInPattern(
List.of(new CallAtStop(TRIP_TIMES_ON_DATE, WALK_TIME)),
timestamp,
Set.of()
timestamp
);
OjpToTriasTransformer.transform(ojp, new PrintWriter(System.out));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<ojp:StopEventType>departure</ojp:StopEventType>
<ojp:IncludePreviousCalls>true</ojp:IncludePreviousCalls>
<ojp:IncludeOnwardCalls>true</ojp:IncludeOnwardCalls>
<ojp:UseRealtimeData>none</ojp:UseRealtimeData>
</ojp:Params>
</ojp:OJPStopEventRequest>
</siri:ServiceRequest>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
<ojp:StopEventType>departure</ojp:StopEventType>
<ojp:IncludePreviousCalls>true</ojp:IncludePreviousCalls>
<ojp:IncludeOnwardCalls>true</ojp:IncludeOnwardCalls>
<ojp:UseRealtimeData>full</ojp:UseRealtimeData>
</ojp:Params>
</ojp:OJPStopEventRequest>
</siri:ServiceRequest>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<StopEventType>departure</StopEventType>
<IncludePreviousCalls>true</IncludePreviousCalls>
<IncludeOnwardCalls>true</IncludeOnwardCalls>
<IncludeRealtimeData>true</IncludeRealtimeData>
<IncludeRealtimeData>false</IncludeRealtimeData>
</Params>
</StopEventRequest>
</RequestPayload>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import java.time.Instant;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import org.opentripplanner.framework.geometry.WgsCoordinate;
import org.opentripplanner.model.FeedInfo;
import org.opentripplanner.routing.graphfinder.GraphFinder;
import org.opentripplanner.routing.stoptimes.ArrivalDeparture;
import org.opentripplanner.transit.api.model.FilterValues;
Expand All @@ -30,6 +32,10 @@ public VdvService(TransitService transitService, GraphFinder finder) {
this.finder = finder;
}

public Optional<String> resolveLanguage(String feedId) {
return Optional.ofNullable(transitService.getFeedInfo(feedId)).map(FeedInfo::getLang);
}

public List<CallAtStop> findTripTimesOnDate(FeedScopedId stopId, StopEventRequestParams params)
throws EntityNotFoundException {
var stop = transitService.getRegularStop(stopId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static java.lang.Boolean.TRUE;
import static org.opentripplanner.ext.vdv.ojp.mapping.StopEventResponseMapper.OptionalFeature.ONWARD_CALLS;
import static org.opentripplanner.ext.vdv.ojp.mapping.StopEventResponseMapper.OptionalFeature.PREVIOUS_CALLS;
import static org.opentripplanner.ext.vdv.ojp.mapping.StopEventResponseMapper.OptionalFeature.REALTIME_DATA;

import de.vdv.ojp20.LineDirectionFilterStructure;
import de.vdv.ojp20.ModeFilterStructure;
Expand All @@ -13,6 +14,7 @@
import de.vdv.ojp20.PlaceRefStructure;
import de.vdv.ojp20.StopEventParamStructure;
import de.vdv.ojp20.StopEventTypeEnumeration;
import de.vdv.ojp20.UseRealtimeDataEnumeration;
import de.vdv.ojp20.siri.StopPointRefStructure;
import java.time.Duration;
import java.time.ZoneId;
Expand All @@ -38,18 +40,11 @@ public class OjpService {
private static final Duration DEFAULT_TIME_WINDOW = Duration.ofHours(2);
private final VdvService vdvService;
private final IdResolver idResolver;
private final StopEventResponseMapper mapper;
private final ZoneId zoneId;

public OjpService(
VdvService vdvService,
IdResolver idResolver,
StopEventResponseMapper mapper,
ZoneId zoneId
) {
public OjpService(VdvService vdvService, IdResolver idResolver, ZoneId zoneId) {
this.vdvService = vdvService;
this.idResolver = idResolver;
this.mapper = mapper;
this.zoneId = zoneId;
}

Expand All @@ -65,12 +60,14 @@ public OJP handleStopEventRequest(OJPStopEventRequestStructure ser) {
} else if (coordinate.isPresent()) {
callsAtStop = vdvService.findTripTimesOnDate(coordinate.get(), params);
}

return mapper.mapStopTimesInPattern(
callsAtStop,
ZonedDateTime.now(),
mapOptionalFeatures(ser.getParams())
var optional = mapOptionalFeatures(ser.getParams());
var mapper = new StopEventResponseMapper(
optional,
zoneId,
idResolver,
vdvService::resolveLanguage
);
return mapper.mapStopTimesInPattern(callsAtStop, ZonedDateTime.now());
}

protected VdvService.StopEventRequestParams extractStopEventParams(
Expand Down Expand Up @@ -177,6 +174,9 @@ private static Set<StopEventResponseMapper.OptionalFeature> mapOptionalFeatures(
if (TRUE.equals(params.isIncludeOnwardCalls())) {
res.add(ONWARD_CALLS);
}
if (UseRealtimeDataEnumeration.NONE != params.getUseRealtimeData()) {
res.add(REALTIME_DATA);
}
return res;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.opentripplanner.ext.vdv.ojp.mapping;

import static org.opentripplanner.ext.vdv.ojp.mapping.StopEventResponseMapper.OptionalFeature.REALTIME_DATA;

import de.vdv.ojp20.CallAtNearStopStructure;
import de.vdv.ojp20.CallAtStopStructure;
import de.vdv.ojp20.DatedJourneyStructure;
Expand Down Expand Up @@ -48,29 +50,29 @@ public class StopEventResponseMapper {
public enum OptionalFeature {
PREVIOUS_CALLS,
ONWARD_CALLS,
REALTIME_DATA,
}

private static final String OJP_NAMESPACE = "http://www.vdv.de/ojp";
private final Set<OptionalFeature> optionalFeatures;
private final ZoneId zoneId;

public StopEventResponseMapper(
Set<OptionalFeature> optionalFeatures,
ZoneId zoneId,
IdResolver idResolver,
Function<String, Optional<String>> resolveFeedLanguage
) {
this.optionalFeatures = optionalFeatures;
this.zoneId = zoneId;
this.idResolver = idResolver;
this.resolveFeedLanguage = resolveFeedLanguage;
}

public OJP mapStopTimesInPattern(
List<CallAtStop> tripTimesOnDate,
ZonedDateTime timestamp,
Set<OptionalFeature> optionalFeatures
) {
public OJP mapStopTimesInPattern(List<CallAtStop> tripTimesOnDate, ZonedDateTime timestamp) {
List<JAXBElement<StopEventResultStructure>> stopEvents = tripTimesOnDate
.stream()
.map(call -> this.stopEventResult(call, optionalFeatures))
.map(call -> this.stopEventResult(call))
.map(StopEventResponseMapper::jaxbElement)
.toList();

Expand All @@ -85,21 +87,21 @@ public OJP mapStopTimesInPattern(
return new OJP().withOJPResponse(response);
}

private StopEventResultStructure stopEventResult(CallAtStop call, Set<OptionalFeature> features) {
private StopEventResultStructure stopEventResult(CallAtStop call) {
var callAtNearStop = new CallAtNearStopStructure()
.withCallAtStop(callAtStop(call.tripTimeOnDate()))
.withWalkDuration(call.walkTime());

var stopEvent = new StopEventStructure()
.withThisCall(callAtNearStop)
.withService(datedJourney(call.tripTimeOnDate()));
if (features.contains(OptionalFeature.PREVIOUS_CALLS)) {
if (optionalFeatures.contains(OptionalFeature.PREVIOUS_CALLS)) {
call
.tripTimeOnDate()
.previousTimes()
.forEach(previous -> stopEvent.withPreviousCall(callAtNearStop(previous)));
}
if (features.contains(OptionalFeature.ONWARD_CALLS)) {
if (optionalFeatures.contains(OptionalFeature.ONWARD_CALLS)) {
call
.tripTimeOnDate()
.nextTimes()
Expand Down Expand Up @@ -179,6 +181,7 @@ private ServiceDepartureStructure serviceDeparture(TripTimeOnDate tripTimeOnDate
.withTimetabledTime(new XmlDateTime(tripTimeOnDate.scheduledDepartureAt(zoneId)));
tripTimeOnDate
.realtimeDepartureAt(zoneId)
.filter(d -> optionalFeatures.contains(REALTIME_DATA))
.ifPresent(time -> departure.withEstimatedTime(new XmlDateTime(time)));
return departure;
}
Expand All @@ -189,6 +192,7 @@ private ServiceArrivalStructure serviceArrival(TripTimeOnDate tripTimeOnDate) {

tripTimeOnDate
.realtimeArrivalAt(zoneId)
.filter(d -> optionalFeatures.contains(REALTIME_DATA))
.ifPresent(time -> arrival.withEstimatedTime(new XmlDateTime(time)));
return arrival;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import org.opentripplanner.ext.vdv.ojp.OjpService;
import org.opentripplanner.ext.vdv.ojp.mapping.ErrorMapper;
import org.opentripplanner.ext.vdv.ojp.mapping.StopEventResponseMapper;
import org.opentripplanner.model.FeedInfo;
import org.opentripplanner.routing.error.RoutingValidationException;
import org.opentripplanner.standalone.api.OtpServerRequestContext;
import org.opentripplanner.standalone.config.sandbox.TriasApiConfig;
Expand All @@ -54,10 +53,7 @@ public TriasResource(@Context OtpServerRequestContext context) {
var vdvService = new VdvService(context.transitService(), context.graphFinder());

IdResolver idResolver = idResolver(context.triasApiConfig());
var mapper = new StopEventResponseMapper(zoneId, idResolver, feedId ->
Optional.ofNullable(transitService.getFeedInfo(feedId)).map(FeedInfo::getLang)
);
this.ojpService = new OjpService(vdvService, idResolver, mapper, zoneId);
this.ojpService = new OjpService(vdvService, idResolver, zoneId);
}

private IdResolver idResolver(TriasApiConfig triasApiConfig) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,18 @@
</xsl:element>
</xsl:if>
<xsl:if test="//trias:IncludeRealtimeData">
<xsl:element name="IncludeRealtimeData">
<xsl:value-of select="//trias:IncludeRealtimeData"/>
</xsl:element>
<xsl:choose>
<xsl:when test="//trias:IncludeRealtimeData='true'">
<xsl:element name="UseRealtimeData">
<xsl:text>full</xsl:text>
</xsl:element>
</xsl:when>
<xsl:otherwise>
<xsl:element name="UseRealtimeData">
<xsl:text>none</xsl:text>
</xsl:element>
</xsl:otherwise>
</xsl:choose>
</xsl:if>
<xsl:if test="//trias:TimeWindow">
<xsl:element name="TimeWindow">
Expand Down

0 comments on commit 530d808

Please sign in to comment.