Skip to content

Commit

Permalink
Merge pull request #5455 from entur/add_support_for_dsj_in_leg_reference
Browse files Browse the repository at this point in the history
Add support for DSJ in transit leg reference
  • Loading branch information
vpaturet authored Oct 30, 2023
2 parents 2df233d + ef5f54c commit ea1edcd
Show file tree
Hide file tree
Showing 16 changed files with 602 additions and 245 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.opentripplanner.model.plan.Itinerary;
import org.opentripplanner.model.plan.Leg;
import org.opentripplanner.model.plan.ScheduledTransitLeg;
import org.opentripplanner.model.plan.ScheduledTransitLegBuilder;
import org.opentripplanner.transit.service.TransitService;

public class RealtimeResolver {
Expand Down Expand Up @@ -53,20 +54,12 @@ private static Leg combineReferenceWithOriginal(
ScheduledTransitLeg reference,
ScheduledTransitLeg original
) {
var leg = new ScheduledTransitLeg(
reference.getTripTimes(),
reference.getTripPattern(),
reference.getBoardStopPosInPattern(),
reference.getAlightStopPosInPattern(),
reference.getStartTime(),
reference.getEndTime(),
reference.getServiceDate(),
reference.getZoneId(),
original.getTransferFromPrevLeg(),
original.getTransferToNextLeg(),
original.getGeneralizedCost(),
original.accessibilityScore()
);
var leg = new ScheduledTransitLegBuilder<>(reference)
.withTransferFromPreviousLeg(original.getTransferFromPrevLeg())
.withTransferToNextLeg(original.getTransferToNextLeg())
.withGeneralizedCost(original.getGeneralizedCost())
.withAccessibilityScore(original.accessibilityScore())
.build();
reference.getTransitAlerts().forEach(leg::addAlert);
return leg;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,36 +21,9 @@ public class FrequencyTransitLeg extends ScheduledTransitLeg {

private final int frequencyHeadwayInSeconds;

public FrequencyTransitLeg(
TripTimes tripTimes,
TripPattern tripPattern,
int boardStopIndexInPattern,
int alightStopIndexInPattern,
ZonedDateTime startTime,
ZonedDateTime endTime,
LocalDate serviceDate,
ZoneId zoneId,
ConstrainedTransfer transferFromPreviousLeg,
ConstrainedTransfer transferToNextLeg,
int generalizedCost,
int frequencyHeadwayInSeconds,
@Nullable Float accessibilityScore
) {
super(
tripTimes,
tripPattern,
boardStopIndexInPattern,
alightStopIndexInPattern,
startTime,
endTime,
serviceDate,
zoneId,
transferFromPreviousLeg,
transferToNextLeg,
generalizedCost,
accessibilityScore
);
this.frequencyHeadwayInSeconds = frequencyHeadwayInSeconds;
FrequencyTransitLeg(FrequencyTransitLegBuilder builder) {
super(builder);
this.frequencyHeadwayInSeconds = builder.frequencyHeadwayInSeconds();
}

@Override
Expand Down Expand Up @@ -101,20 +74,6 @@ public List<StopArrival> getIntermediateStops() {

@Override
public ScheduledTransitLeg withAccessibilityScore(Float score) {
return new FrequencyTransitLeg(
tripTimes,
tripPattern,
boardStopPosInPattern,
alightStopPosInPattern,
getStartTime(),
getEndTime(),
serviceDate,
zoneId,
getTransferFromPrevLeg(),
getTransferToNextLeg(),
getGeneralizedCost(),
frequencyHeadwayInSeconds,
score
);
return new FrequencyTransitLegBuilder(this).withAccessibilityScore(score).build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.opentripplanner.model.plan;

public class FrequencyTransitLegBuilder
extends ScheduledTransitLegBuilder<FrequencyTransitLegBuilder> {

private int frequencyHeadwayInSeconds;

public FrequencyTransitLegBuilder() {}

public FrequencyTransitLegBuilder(FrequencyTransitLeg original) {
super(original);
frequencyHeadwayInSeconds = original.getHeadway();
}

public FrequencyTransitLegBuilder withFrequencyHeadwayInSeconds(int frequencyHeadwayInSeconds) {
this.frequencyHeadwayInSeconds = frequencyHeadwayInSeconds;
return instance();
}

public int frequencyHeadwayInSeconds() {
return frequencyHeadwayInSeconds;
}

@Override
public FrequencyTransitLeg build() {
return new FrequencyTransitLeg(this);
}
}
9 changes: 9 additions & 0 deletions src/main/java/org/opentripplanner/model/plan/Leg.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.opentripplanner.transit.model.organization.Operator;
import org.opentripplanner.transit.model.site.FareZone;
import org.opentripplanner.transit.model.timetable.Trip;
import org.opentripplanner.transit.model.timetable.TripOnServiceDate;

/**
* One leg of a trip -- that is, a temporally continuous piece of the journey that takes place on a
Expand Down Expand Up @@ -187,6 +188,14 @@ default Trip getTrip() {
return null;
}

/**
* For transit legs, the trip on service date, if it exists. For non-transit legs, null.
*/
@Nullable
default TripOnServiceDate getTripOnServiceDate() {
return null;
}

default Accessibility getTripWheelchairAccessibility() {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.opentripplanner.transit.model.organization.Operator;
import org.opentripplanner.transit.model.site.StopLocation;
import org.opentripplanner.transit.model.timetable.Trip;
import org.opentripplanner.transit.model.timetable.TripOnServiceDate;
import org.opentripplanner.transit.model.timetable.TripTimes;

/**
Expand All @@ -57,47 +58,37 @@ public class ScheduledTransitLeg implements TransitLeg {
private final int generalizedCost;
protected final LocalDate serviceDate;
protected final ZoneId zoneId;
private final TripOnServiceDate tripOnServiceDate;
private double distanceMeters;
private final double directDistanceMeters;
private final Float accessibilityScore;
private List<FareProductUse> fareProducts = List.of();

public ScheduledTransitLeg(
TripTimes tripTimes,
TripPattern tripPattern,
int boardStopIndexInPattern,
int alightStopIndexInPattern,
ZonedDateTime startTime,
ZonedDateTime endTime,
LocalDate serviceDate,
ZoneId zoneId,
ConstrainedTransfer transferFromPreviousLeg,
ConstrainedTransfer transferToNextLeg,
int generalizedCost,
@Nullable Float accessibilityScore
) {
this.tripTimes = tripTimes;
this.tripPattern = tripPattern;
ScheduledTransitLeg(ScheduledTransitLegBuilder<?> builder) {
this.tripTimes = builder.tripTimes();
this.tripPattern = builder.tripPattern();

this.boardStopPosInPattern = boardStopIndexInPattern;
this.alightStopPosInPattern = alightStopIndexInPattern;
this.boardStopPosInPattern = builder.boardStopIndexInPattern();
this.alightStopPosInPattern = builder.alightStopIndexInPattern();

this.startTime = startTime;
this.endTime = endTime;
this.startTime = builder.startTime();
this.endTime = builder.endTime();

this.serviceDate = serviceDate;
this.zoneId = zoneId;
this.serviceDate = builder.serviceDate();
this.zoneId = builder.zoneId();

this.transferFromPrevLeg = transferFromPreviousLeg;
this.transferToNextLeg = transferToNextLeg;
this.tripOnServiceDate = builder.tripOnServiceDate();

this.generalizedCost = generalizedCost;
this.transferFromPrevLeg = builder.transferFromPreviousLeg();
this.transferToNextLeg = builder.transferToNextLeg();

this.accessibilityScore = accessibilityScore;
this.generalizedCost = builder.generalizedCost();

this.accessibilityScore = builder.accessibilityScore();
List<Coordinate> transitLegCoordinates = extractTransitLegCoordinates(
tripPattern,
boardStopIndexInPattern,
alightStopIndexInPattern
builder.boardStopIndexInPattern(),
builder.alightStopIndexInPattern()
);
this.legGeometry = GeometryUtils.makeLineString(transitLegCoordinates);

Expand Down Expand Up @@ -127,10 +118,12 @@ public Instant getServiceDateMidnight() {
return ServiceDateUtils.asStartOfService(serviceDate, zoneId).toInstant();
}

@Override
public boolean isScheduledTransitLeg() {
return true;
}

@Override
public ScheduledTransitLeg asScheduledTransitLeg() {
return this;
}
Expand Down Expand Up @@ -244,6 +237,12 @@ public LocalDate getServiceDate() {
return serviceDate;
}

@Override
@Nullable
public TripOnServiceDate getTripOnServiceDate() {
return tripOnServiceDate;
}

@Override
public Place getFrom() {
return Place.forStop(tripPattern.getStop(boardStopPosInPattern));
Expand Down Expand Up @@ -344,18 +343,25 @@ public int getGeneralizedCost() {
return generalizedCost;
}

/**
* Construct a leg reference from this leg.
* If the trip is based on a TripOnServiceDate, the leg reference will contain the
* TripOnServiceDate id instead of the Trip id.
*/
@Override
public LegReference getLegReference() {
return new ScheduledTransitLegReference(
tripTimes.getTrip().getId(),
tripOnServiceDate == null ? tripTimes.getTrip().getId() : null,
serviceDate,
boardStopPosInPattern,
alightStopPosInPattern,
tripPattern.getStops().get(boardStopPosInPattern).getId(),
tripPattern.getStops().get(alightStopPosInPattern).getId()
tripPattern.getStops().get(alightStopPosInPattern).getId(),
tripOnServiceDate == null ? null : tripOnServiceDate.getId()
);
}

@Override
public void addAlert(TransitAlert alert) {
transitAlerts.add(alert);
}
Expand All @@ -377,20 +383,7 @@ public Float accessibilityScore() {
}

public ScheduledTransitLeg withAccessibilityScore(Float score) {
return new ScheduledTransitLeg(
tripTimes,
tripPattern,
boardStopPosInPattern,
alightStopPosInPattern,
startTime,
endTime,
serviceDate,
zoneId,
transferFromPrevLeg,
transferToNextLeg,
generalizedCost,
score
);
return new ScheduledTransitLegBuilder<>(this).withAccessibilityScore(score).build();
}

/**
Expand Down
Loading

0 comments on commit ea1edcd

Please sign in to comment.