Skip to content

Commit

Permalink
Clean out Siri code
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardehrenfried committed Jun 6, 2024
1 parent 519a1c2 commit 21956b5
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 168 deletions.
128 changes: 0 additions & 128 deletions src/ext/java/org/opentripplanner/ext/siri/SiriTripPatternCache.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
package org.opentripplanner.ext.siri;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ListMultimap;
import com.google.common.collect.Multimaps;
import java.time.LocalDate;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import javax.annotation.Nonnull;
import org.opentripplanner.transit.model.network.StopPattern;
import org.opentripplanner.transit.model.network.TripPattern;
import org.opentripplanner.transit.model.site.RegularStop;
import org.opentripplanner.transit.model.site.StopLocation;
import org.opentripplanner.transit.model.timetable.Trip;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Threadsafe mechanism for tracking any TripPatterns added to the graph via SIRI realtime messages.
Expand All @@ -39,23 +30,11 @@
*/
public class SiriTripPatternCache {

private static final Logger LOG = LoggerFactory.getLogger(SiriTripPatternCache.class);

// TODO RT_AB: Improve documentation. This seems to be the primary collection of added
// TripPatterns, with other collections serving as indexes. Similar to TripPatternCache.cache
// in the GTFS version of this class, but with service date as part of the key.
private final Map<StopPatternServiceDateKey, TripPattern> cache = new HashMap<>();

// TODO RT_AB: Improve documentation. This field appears to be an index that exists only in the
// SIRI version of this class (i.e. this version and not the older TripPatternCache that
// handles GTFS-RT). This index appears to be tailored for use by the Transmodel GraphQL APIs.
private final ListMultimap<StopLocation, TripPattern> patternsForStop = Multimaps.synchronizedListMultimap(
ArrayListMultimap.create()
);

// TODO RT_AB: clarify name and add documentation to this field.
private final Map<TripServiceDateKey, TripPattern> updatedTripPatternsForTripCache = new HashMap<>();

// TODO RT_AB: generalize this so we can generate IDs for SIRI or GTFS-RT sources.
private final SiriTripPatternIdGenerator tripPatternIdGenerator;

Expand Down Expand Up @@ -128,85 +107,8 @@ public synchronized TripPattern getOrCreateTripPattern(
cache.put(key, tripPattern);
}

/*
When the StopPattern is first modified (e.g. change of platform), then updated (or vice
versa), the stopPattern is altered, and the StopPattern-object for the different states will
not be equal.
This causes both tripPatterns to be added to all unchanged stops along the route, which again
causes duplicate results in departureRow-searches (one departure for "updated", one for
"modified").
Full example:
Planned stops: Stop 1 - Platform 1, Stop 2 - Platform 1
StopPattern #rt1: "updated" stopPattern cached in 'patternsForStop':
- Stop 1, Platform 1
- StopPattern #rt1
- Stop 2, Platform 1
- StopPattern #rt1
"modified" stopPattern: Stop 1 - Platform 1, Stop 2 - Platform 2
StopPattern #rt2: "modified" stopPattern cached in 'patternsForStop' will then be:
- Stop 1, Platform 1
- StopPattern #rt1, StopPattern #rt2
- Stop 2, Platform 1
- StopPattern #rt1
- Stop 2, Platform 2
- StopPattern #rt2
Therefore, we must clean up the duplicates by deleting the previously added (and thus
outdated) tripPattern for all affected stops. In example above, "StopPattern #rt1" should be
removed from all stops.
TODO RT_AB: review why this particular case is handled in an ad-hoc manner. It seems like all
such indexes should be constantly rebuilt and versioned along with the TimetableSnapshot.
*/
TripServiceDateKey tripServiceDateKey = new TripServiceDateKey(trip, serviceDate);
if (updatedTripPatternsForTripCache.containsKey(tripServiceDateKey)) {
// Remove previously added TripPatterns for the trip currently being updated - if the stopPattern does not match
TripPattern cachedTripPattern = updatedTripPatternsForTripCache.get(tripServiceDateKey);
if (cachedTripPattern != null && !tripPattern.stopPatternIsEqual(cachedTripPattern)) {
int sizeBefore = patternsForStop.values().size();
long t1 = System.currentTimeMillis();
patternsForStop.values().removeAll(Arrays.asList(cachedTripPattern));
int sizeAfter = patternsForStop.values().size();

LOG.debug(
"Removed outdated TripPattern for {} stops in {} ms - tripId: {}",
(sizeBefore - sizeAfter),
(System.currentTimeMillis() - t1),
trip.getId()
);
// TODO: Also remove previously updated - now outdated - TripPattern from cache ?
// cache.remove(new StopPatternServiceDateKey(cachedTripPattern.stopPattern, serviceDate));
}
}

// To make these trip patterns visible for departureRow searches.
for (var stop : tripPattern.getStops()) {
if (!patternsForStop.containsEntry(stop, tripPattern)) {
patternsForStop.put(stop, tripPattern);
}
}

// Cache the last added tripPattern that has been used to update a specific trip
updatedTripPatternsForTripCache.put(tripServiceDateKey, tripPattern);

return tripPattern;
}

/**
* Returns any new TripPatterns added by real time information for a given stop.
* TODO RT_AB: this appears to be currently unused. Perhaps remove it if the API has changed.
*
* @param stop the stop
* @return list of TripPatterns created by real time sources for the stop.
*/
public List<TripPattern> getAddedTripPatternsForStop(RegularStop stop) {
return patternsForStop.get(stop);
}
}

// TODO RT_AB: move the below classes inside the above class as private static inner classes.
Expand Down Expand Up @@ -243,33 +145,3 @@ public boolean equals(Object thatObject) {
return (this.stopPattern.equals(that.stopPattern) && this.serviceDate.equals(that.serviceDate));
}
}

/**
* An alternative key for looking up realtime-added TripPatterns by trip and service date instead
* of stop pattern and service date. Must define hashcode and equals to confer semantic identity.
* TODO RT_AB: verify whether one map is considered the definitive collection and the other an index.
*/
class TripServiceDateKey {

Trip trip;
LocalDate serviceDate;

public TripServiceDateKey(Trip trip, LocalDate serviceDate) {
this.trip = trip;
this.serviceDate = serviceDate;
}

@Override
public int hashCode() {
return trip.hashCode() + serviceDate.hashCode();
}

@Override
public boolean equals(Object thatObject) {
if (!(thatObject instanceof TripServiceDateKey)) {
return false;
}
TripServiceDateKey that = (TripServiceDateKey) thatObject;
return (this.trip.equals(that.trip) && this.serviceDate.equals(that.serviceDate));
}
}
40 changes: 0 additions & 40 deletions src/ext/java/org/opentripplanner/ext/siri/updater/SiriHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,15 @@
import java.util.UUID;
import javax.xml.stream.XMLStreamException;
import org.rutebanken.siri20.util.SiriXml;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import uk.org.siri.siri20.EstimatedTimetableRequestStructure;
import uk.org.siri.siri20.MessageQualifierStructure;
import uk.org.siri.siri20.RequestorRef;
import uk.org.siri.siri20.ServiceRequest;
import uk.org.siri.siri20.Siri;
import uk.org.siri.siri20.SituationExchangeRequestStructure;
import uk.org.siri.siri20.VehicleMonitoringRequestStructure;

public class SiriHelper {

private static final Logger LOG = LoggerFactory.getLogger(SiriHelper.class);

public static Siri unmarshal(InputStream is) throws JAXBException, XMLStreamException {
return SiriXml.parseXml(is);
}
Expand All @@ -30,16 +25,6 @@ public static String createSXServiceRequestAsXml(String requestorRef) throws JAX
return SiriXml.toXml(request);
}

public static String createVMServiceRequestAsXml(String requestorRef) throws JAXBException {
Siri request = createVMServiceRequest(requestorRef);
return SiriXml.toXml(request);
}

public static String createETServiceRequestAsXml(String requestorRef) throws JAXBException {
Siri request = createETServiceRequest(requestorRef, null);
return SiriXml.toXml(request);
}

public static String createETServiceRequestAsXml(String requestorRef, Duration previewInterval)
throws JAXBException {
Siri request = createETServiceRequest(requestorRef, previewInterval);
Expand Down Expand Up @@ -106,29 +91,4 @@ private static Siri createETServiceRequest(String requestorRefValue, Duration pr

return request;
}

private static Siri createVMServiceRequest(String requestorRefValue) {
Siri request = createSiriObject();

ServiceRequest serviceRequest = new ServiceRequest();
serviceRequest.setRequestTimestamp(ZonedDateTime.now());

RequestorRef requestorRef = new RequestorRef();
requestorRef.setValue(requestorRefValue);
serviceRequest.setRequestorRef(requestorRef);

VehicleMonitoringRequestStructure vmRequest = new VehicleMonitoringRequestStructure();
vmRequest.setRequestTimestamp(ZonedDateTime.now());
vmRequest.setVersion("2.0");

MessageQualifierStructure messageIdentifier = new MessageQualifierStructure();
messageIdentifier.setValue(UUID.randomUUID().toString());

vmRequest.setMessageIdentifier(messageIdentifier);
serviceRequest.getVehicleMonitoringRequests().add(vmRequest);

request.setServiceRequest(serviceRequest);

return request;
}
}

0 comments on commit 21956b5

Please sign in to comment.