Skip to content

Proof of concept of adding a Feed type #6516

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

Draft
wants to merge 1 commit into
base: dev-2.x
Choose a base branch
from
Draft
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
@@ -0,0 +1,57 @@
package org.opentripplanner.transit.model.framework;

import java.util.Objects;

/**
* This class is used to identify a feed and works as a factory for creating FeedScopedIds.
*/
public class Feed {

private final String feedId;

public Feed(String feedId) {
this.feedId = feedId;
}

public String getId() {
return feedId;
}

/**
* Create a feedScopedId that is scoped by this feed.
* @param id The id to scope.
*/
public FeedScopedId scopedId(String id) {
return new FeedScopedId(feedId, id);
}

public static Feed parse(String value) throws IllegalArgumentException {
if (value == null || value.isEmpty()) {
throw new IllegalArgumentException("Feed id cannot be empty");
}
int index = value.indexOf(":");
if (index == -1) {
return new Feed(value);
} else {
throw new IllegalArgumentException("Invalid character \":\" in feed id: " + value);
}
}

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

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Feed feed = (Feed) o;
return Objects.equals(this.feedId, feed.feedId);
}

@Override
public int hashCode() {
return Objects.hashCode(feedId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.opentripplanner.routing.alertpatch.TransitAlert;
import org.opentripplanner.routing.alertpatch.TransitAlertBuilder;
import org.opentripplanner.routing.services.TransitAlertService;
import org.opentripplanner.transit.model.framework.Feed;
import org.opentripplanner.transit.model.framework.FeedScopedId;
import org.opentripplanner.updater.RealTimeUpdateContext;
import org.opentripplanner.updater.alert.siri.mapping.AffectsMapper;
Expand Down Expand Up @@ -49,7 +50,7 @@
public class SiriAlertsUpdateHandler {

private static final Logger LOG = LoggerFactory.getLogger(SiriAlertsUpdateHandler.class);
private final String feedId;
private final Feed feed;
private final Set<TransitAlert> alerts = new HashSet<>();
private final TransitAlertService transitAlertService;
private final Duration earlyStart;
Expand All @@ -58,11 +59,11 @@ public class SiriAlertsUpdateHandler {
* @param earlyStart display the alerts to users this long before their activePeriod begins
*/
public SiriAlertsUpdateHandler(
String feedId,
Feed feed,
TransitAlertService transitAlertService,
Duration earlyStart
) {
this.feedId = feedId;
this.feed = feed;
this.transitAlertService = transitAlertService;
this.earlyStart = earlyStart;
}
Expand All @@ -83,7 +84,7 @@ public void update(ServiceDelivery delivery, RealTimeUpdateContext context) {
continue;
}
String situationNumber = sxElement.getSituationNumber().getValue();
FeedScopedId id = new FeedScopedId(feedId, situationNumber);
FeedScopedId id = feed.scopedId(situationNumber);

if (expireSituation) {
alerts.removeIf(transitAlert -> transitAlert.getId().equals(id));
Expand Down Expand Up @@ -184,7 +185,7 @@ private TransitAlert mapSituationToAlert(

alert.addEntites(
new AffectsMapper(
feedId,
feed,
context.siriFuzzyTripMatcher(),
context.transitService()
).mapAffects(situation.getAffects())
Expand Down Expand Up @@ -220,7 +221,7 @@ private long getEpochSecond(ZonedDateTime startTime) {
* provided in the SIRI PtSituation.
*/
private TransitAlertBuilder createAlertWithTexts(PtSituationElement situation) {
return TransitAlert.of(new FeedScopedId(feedId, situation.getSituationNumber().getValue()))
return TransitAlert.of(feed.scopedId(situation.getSituationNumber().getValue()))
.withDescriptionText(mapTranslatedString(situation.getDescriptions()))
.withDetailText(mapTranslatedString(situation.getDetails()))
.withAdviceText(mapTranslatedString(situation.getAdvices()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.Set;
import org.opentripplanner.routing.alertpatch.EntitySelector;
import org.opentripplanner.routing.alertpatch.StopCondition;
import org.opentripplanner.transit.model.framework.Feed;
import org.opentripplanner.transit.model.framework.FeedScopedId;
import org.opentripplanner.transit.model.timetable.Trip;
import org.opentripplanner.transit.model.timetable.TripOnServiceDate;
Expand Down Expand Up @@ -40,21 +41,21 @@
*/
public class AffectsMapper {

private final String feedId;
private final Feed feed;
private final SiriFuzzyTripMatcher siriFuzzyTripMatcher;
private final TransitService transitService;

private final EntityResolver entityResolver;

public AffectsMapper(
String feedId,
Feed feed,
SiriFuzzyTripMatcher siriFuzzyTripMatcher,
TransitService transitService
) {
this.feedId = feedId;
this.feed = feed;
this.siriFuzzyTripMatcher = siriFuzzyTripMatcher;
this.transitService = transitService;
this.entityResolver = new EntityResolver(transitService, feedId);
this.entityResolver = new EntityResolver(transitService, feed);
}

public List<EntitySelector> mapAffects(AffectsScopeStructure affectsStructure) {
Expand Down Expand Up @@ -199,11 +200,11 @@ private List<EntitySelector> mapTripSelectors(
for (AffectedStopPointStructure affectedStop : affectedStops) {
FeedScopedId stop = getStop(
affectedStop.getStopPointRef().getValue(),
feedId,
feed,
transitService
);
if (stop == null) {
stop = new FeedScopedId(feedId, affectedStop.getStopPointRef().getValue());
stop = feed.scopedId(affectedStop.getStopPointRef().getValue());
}
EntitySelector.StopAndTrip entitySelector = new EntitySelector.StopAndTrip(
stop,
Expand Down Expand Up @@ -256,17 +257,17 @@ private List<EntitySelector> mapNetworks(AffectsScopeStructure.Networks networks
}
}
}
FeedScopedId affectedRoute = new FeedScopedId(feedId, lineRef.getValue());
FeedScopedId affectedRoute = feed.scopedId(lineRef.getValue());

if (!affectedStops.isEmpty()) {
for (AffectedStopPointStructure affectedStop : affectedStops) {
FeedScopedId stop = getStop(
affectedStop.getStopPointRef().getValue(),
feedId,
feed,
transitService
);
if (stop == null) {
stop = new FeedScopedId(feedId, affectedStop.getStopPointRef().getValue());
stop = feed.scopedId(affectedStop.getStopPointRef().getValue());
}
EntitySelector.StopAndRoute entitySelector = new EntitySelector.StopAndRoute(
stop,
Expand Down Expand Up @@ -305,10 +306,10 @@ private List<EntitySelector> mapStopPoints(AffectsScopeStructure.StopPoints stop
continue;
}

FeedScopedId stopId = getStop(stopPointRef.getValue(), feedId, transitService);
FeedScopedId stopId = getStop(stopPointRef.getValue(), feed, transitService);

if (stopId == null) {
stopId = new FeedScopedId(feedId, stopPointRef.getValue());
stopId = feed.scopedId(stopPointRef.getValue());
}

EntitySelector.Stop entitySelector = new EntitySelector.Stop(
Expand All @@ -334,10 +335,10 @@ private List<EntitySelector> mapStopPlaces(AffectsScopeStructure.StopPlaces stop
continue;
}

FeedScopedId stopId = getStop(stopPlaceRef.getValue(), feedId, transitService);
FeedScopedId stopId = getStop(stopPlaceRef.getValue(), feed, transitService);

if (stopId == null) {
stopId = new FeedScopedId(feedId, stopPlaceRef.getValue());
stopId = feed.scopedId(stopPlaceRef.getValue());
}

selectors.add(new EntitySelector.Stop(stopId));
Expand All @@ -363,18 +364,18 @@ private List<EntitySelector> mapOperators(AffectsScopeStructure.Operators operat
// I leave this for now.
String agencyId = operatorRef.getValue();

selectors.add(new EntitySelector.Agency(new FeedScopedId(feedId, agencyId)));
selectors.add(new EntitySelector.Agency(feed.scopedId(agencyId)));
}

return selectors;
}

private static FeedScopedId getStop(
String siriStopId,
String feedId,
Feed feed,
TransitService transitService
) {
FeedScopedId id = new FeedScopedId(feedId, siriStopId);
FeedScopedId id = feed.scopedId(siriStopId);
if (transitService.getRegularStop(id) != null) {
return id;
} else if (transitService.getStation(id) != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.opentripplanner.model.RealTimeTripUpdate;
import org.opentripplanner.model.Timetable;
import org.opentripplanner.transit.model.framework.DataValidationException;
import org.opentripplanner.transit.model.framework.Feed;
import org.opentripplanner.transit.model.framework.Result;
import org.opentripplanner.transit.model.network.TripPattern;
import org.opentripplanner.transit.model.timetable.RealTimeTripTimes;
Expand Down Expand Up @@ -90,7 +91,7 @@ public SiriRealTimeTripUpdateAdapter(
public UpdateResult applyEstimatedTimetable(
@Nullable SiriFuzzyTripMatcher fuzzyTripMatcher,
EntityResolver entityResolver,
String feedId,
Feed feed,
UpdateIncrementality incrementality,
List<EstimatedTimetableDeliveryStructure> updates
) {
Expand All @@ -103,7 +104,7 @@ public UpdateResult applyEstimatedTimetable(

if (incrementality == FULL_DATASET) {
// Remove all updates from the buffer
snapshotManager.clearBuffer(feedId);
snapshotManager.clearBuffer(feed);
}

for (var etDelivery : updates) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.opentripplanner.framework.geometry.SphericalDistanceLibrary;
import org.opentripplanner.street.model.vertex.TransitStopVertex;
import org.opentripplanner.street.model.vertex.Vertex;
import org.opentripplanner.transit.model.framework.Feed;
import org.opentripplanner.transit.model.framework.FeedScopedId;
import org.opentripplanner.transit.model.network.Route;
import org.opentripplanner.transit.model.network.TripPattern;
Expand Down Expand Up @@ -56,12 +57,12 @@ public void testIdLookup() {
}

/* Agencies */
String feedId = transitService.listFeedIds().iterator().next();
Feed feed = transitService.listFeeds().iterator().next();
Agency agency;
agency = transitService.getAgency(new FeedScopedId(feedId, "azerty"));
agency = transitService.getAgency(feed.scopedId("azerty"));
assertNull(agency);
agency = transitService.getAgency(new FeedScopedId(feedId, "agency"));
assertEquals(feedId + ":" + "agency", agency.getId().toString());
agency = transitService.getAgency(feed.scopedId("agency"));
assertEquals(feed + ":" + "agency", agency.getId().toString());
assertEquals("Fake Agency", agency.getName());

/* Stops */
Expand Down Expand Up @@ -99,12 +100,12 @@ public void testPatternsCoherent() {

@Test
public void testSpatialIndex() {
String feedId = transitService.listFeedIds().iterator().next();
FeedScopedId idJ = new FeedScopedId(feedId, "J");
Feed feed = transitService.listFeeds().iterator().next();
FeedScopedId idJ = feed.scopedId("J");
var stopJ = transitService.getRegularStop(idJ);
FeedScopedId idL = new FeedScopedId(feedId, "L");
FeedScopedId idL = feed.scopedId("L");
var stopL = transitService.getRegularStop(idL);
FeedScopedId idM = new FeedScopedId(feedId, "M");
FeedScopedId idM = feed.scopedId("M");
var stopM = transitService.getRegularStop(idM);
TransitStopVertex stopvJ = graph.getStopVertexForStopId(idJ);
TransitStopVertex stopvL = graph.getStopVertexForStopId(idL);
Expand Down
Loading