Skip to content

Commit

Permalink
Add advanced new york fares engine. (#20)
Browse files Browse the repository at this point in the history
* first attempt to implement NYC fare rules (fixed fare, zone-to-zone, transfers, peak hours)

* update code comment

* bug fix and add more testing data (NYCT & MTABC)

* return fares for all fare types and include fares in each leg
  • Loading branch information
DerekEdwards authored Jul 17, 2019
1 parent 631b8ca commit f371eec
Show file tree
Hide file tree
Showing 10 changed files with 732 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/opentripplanner/api/model/Itinerary.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public class Itinerary {
*/
public boolean tooSloped = false;

/**
/**
* adds leg to array list
* @param leg
*/
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/org/opentripplanner/api/model/Leg.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ the License, or (at your option) any later version.
import org.opentripplanner.routing.alertpatch.Alert;
import org.opentripplanner.routing.core.TraverseMode;
import org.opentripplanner.util.model.EncodedPolylineBean;
import org.opentripplanner.routing.core.Fare;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
Expand Down Expand Up @@ -314,6 +315,12 @@ public class Leg {
public VehicleInfo vehicleInfo;

/**
* Leg fare
*/
@JsonSerialize
public Fare fare = new Fare();

/**
* For transit legs, whether or not fare card is accepted.
* For non-transit legs, false.
*/
Expand All @@ -328,8 +335,7 @@ public Boolean isTransitLeg() {
if (mode == null) return null;
else if (mode.equals(TraverseMode.WALK.toString())) return false;
else if (mode.equals(TraverseMode.CAR.toString())) return false;
else if (mode.equals(TraverseMode.BICYCLE.toString())) return false;
else return true;
else return !mode.equals(TraverseMode.BICYCLE.toString());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ the License, or (props, at your option) any later version.
import org.opentripplanner.routing.alertpatch.Alert;
import org.opentripplanner.routing.alertpatch.AlertPatch;
import org.opentripplanner.routing.core.*;
import org.opentripplanner.routing.core.Fare.FareType;
import org.opentripplanner.routing.edgetype.*;
import org.opentripplanner.routing.error.TrivialPathException;
import org.opentripplanner.routing.graph.Edge;
Expand Down Expand Up @@ -170,14 +171,34 @@ public static Itinerary generateItinerary(GraphPath path, boolean showIntermedia

State[][] legsStates = sliceStates(states);

if (fareService != null) {
itinerary.fare = fareService.getCost(path);
}

for (State[] legStates : legsStates) {
itinerary.addLeg(generateLeg(graph, legStates, showIntermediateStops, disableAlertFiltering, requestedLocale));
}

if (fareService != null) {
// for all fare types
FareBundle fareBundle = fareService.getLegCostBreakDown(path);
if(fareBundle != null) {
itinerary.fare = fareBundle.fare;

for(Leg leg : itinerary.legs) {
if(leg.routeId == null) {
continue;
}

if (!fareBundle.legFares.isEmpty()) {
Fare legFare = fareBundle.legFares.get(leg.routeId.toString());
if (legFare != null) {
leg.fare = legFare;
}
}
}
} else {
// only calculate regular fare
itinerary.fare = fareService.getCost(path);
}
}

addWalkSteps(graph, itinerary.legs, legsStates, requestedLocale);

fixupLegs(itinerary.legs, legsStates);
Expand Down Expand Up @@ -1039,7 +1060,7 @@ public static List<WalkStep> generateWalkSteps(Graph graph, State[] states, Walk
// exit != null and uses to <exit>
// the floor name is the AlightEdge name
// reset to avoid confusion with 'Elevator on floor 1 to floor 1'
step.streetName = ((ElevatorAlightEdge) edge).getName(requestedLocale);
step.streetName = edge.getName(requestedLocale);

step.relativeDirection = RelativeDirection.ELEVATOR;

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/opentripplanner/routing/core/Fare.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ the License, or (at your option) any later version.
*/
public class Fare {

public static enum FareType implements Serializable {
public enum FareType implements Serializable {
regular, student, senior, tram, special, youth
}

Expand Down
31 changes: 31 additions & 0 deletions src/main/java/org/opentripplanner/routing/core/FareBundle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/* This program is free software: you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public License
as published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */

package org.opentripplanner.routing.core;

import org.opentripplanner.routing.core.Fare;
import org.opentripplanner.routing.core.Fare.FareType;
import java.util.Map;

/**
* A fare bundle is a combination of total fare and each leg's fare
*/
public class FareBundle {
public Fare fare;
public Map<String, Fare> legFares; // a map of leg identifier and its fare

public FareBundle( Fare fare, Map<String, Fare> legFares) {
this.fare = fare;
this.legFares = legFares;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,9 @@ public static FareServiceFactory fromConfig(JsonNode config) {
case "new-york":
retval = new NycFareServiceFactory();
break;
case "new-york-advanced":
retval = new NycAdvancedFareServiceFactory();
break;
case "seattle":
retval = new SeattleFareServiceFactory();
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ class Ride {

AgencyAndId route;

int routeType;

AgencyAndId trip;

Set<String> zones;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/* This program is free software: you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public License
as published by the Free Software Foundation, either version 3 of
the License, or (props, at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */

package org.opentripplanner.routing.impl;

import org.onebusaway.gtfs.services.GtfsRelationalDao;
import org.opentripplanner.routing.services.FareService;
import org.opentripplanner.routing.services.FareServiceFactory;

import com.fasterxml.jackson.databind.JsonNode;

public class NycAdvancedFareServiceFactory implements FareServiceFactory {

public FareService makeFareService() {
return new NycAdvancedFareServiceImpl();
}

@Override
public void processGtfs(GtfsRelationalDao dao) {
}

@Override
public void configure(JsonNode config) {
}
}
Loading

0 comments on commit f371eec

Please sign in to comment.