use of org.opentripplanner.routing.services.FareService in project OpenTripPlanner by opentripplanner.
the class AddingMultipleFareService method getCost.
@Override
public Fare getCost(GraphPath path) {
Fare fare = null;
for (FareService subService : subServices) {
Fare subFare = subService.getCost(path);
if (subFare == null) {
// No fare, next one please
continue;
}
if (fare == null) {
// Pick first defined fare
fare = new Fare(subFare);
} else {
// Merge subFare with existing fare
// Must use a temporary as we need to keep fare clean during the loop on FareType
Fare newFare = new Fare(fare);
for (FareType fareType : FareType.values()) {
Money cost = fare.getFare(fareType);
Money subCost = subFare.getFare(fareType);
if (cost == null && subCost == null) {
continue;
}
if (cost != null && subCost == null) {
/*
* If for a given fare type we have partial data, we try to pickup the
* default "regular" cost to fill-in the missing information. For example,
* adding a bike fare which define only a "regular" cost, with some transit
* fare defining both "regular" and "student" costs. In that case, we
* probably want the "regular" bike fare to be added to the "student"
* transit fare too. Here we assume "regular" as a sane default value.
*/
subCost = subFare.getFare(FareType.regular);
} else if (cost == null && subCost != null) {
/* Same, but the other way around. */
cost = fare.getFare(FareType.regular);
}
if (cost != null && subCost != null) {
// Add sub cost to cost
newFare.addFare(fareType, cost.getCurrency(), cost.getCents() + subCost.getCents());
} else if (cost == null && subCost != null) {
// Add new cost
// Note: this should not happen often: only if a fare
// did not compute a "regular" fare.
newFare.addFare(fareType, subCost.getCurrency(), subCost.getCents());
}
}
fare = newFare;
}
}
// Can be null here if no sub-service has returned fare
return fare;
}
use of org.opentripplanner.routing.services.FareService in project OpenTripPlanner by opentripplanner.
the class GraphPathToTripPlanConverter method generateItinerary.
/**
* Generate an itinerary from a {@link GraphPath}. This method first slices the list of states
* at the leg boundaries. These smaller state arrays are then used to generate legs. Finally the
* rest of the itinerary is generated based on the complete state array.
*
* @param path The graph path to base the itinerary on
* @param showIntermediateStops Whether to include intermediate stops in the itinerary or not
* @return The generated itinerary
*/
public static Itinerary generateItinerary(GraphPath path, boolean showIntermediateStops, boolean disableAlertFiltering, Locale requestedLocale) {
Itinerary itinerary = new Itinerary();
State[] states = new State[path.states.size()];
State lastState = path.states.getLast();
states = path.states.toArray(states);
Edge[] edges = new Edge[path.edges.size()];
edges = path.edges.toArray(edges);
Graph graph = path.getRoutingContext().graph;
FareService fareService = graph.getService(FareService.class);
State[][] legsStates = sliceStates(states);
if (fareService != null) {
itinerary.fare = fareService.getCost(path);
}
for (State[] legStates : legsStates) {
itinerary.addLeg(generateLeg(graph, legStates, showIntermediateStops, disableAlertFiltering, requestedLocale));
}
addWalkSteps(graph, itinerary.legs, legsStates, requestedLocale);
fixupLegs(itinerary.legs, legsStates);
itinerary.duration = lastState.getElapsedTimeSeconds();
itinerary.startTime = makeCalendar(states[0]);
itinerary.endTime = makeCalendar(lastState);
calculateTimes(itinerary, states);
calculateElevations(itinerary, edges);
itinerary.walkDistance = lastState.getWalkDistance();
itinerary.transfers = lastState.getNumBoardings();
if (itinerary.transfers > 0 && !(states[0].getVertex() instanceof OnboardDepartVertex)) {
itinerary.transfers--;
}
return itinerary;
}
use of org.opentripplanner.routing.services.FareService in project OpenTripPlanner by opentripplanner.
the class TestFares method testKCM.
public void testKCM() throws Exception {
Graph gg = new Graph();
GtfsContext context = GtfsLibrary.readGtfs(new File(ConstantsForTests.KCM_GTFS));
GTFSPatternHopFactory factory = new GTFSPatternHopFactory(context);
factory.setFareServiceFactory(new SeattleFareServiceFactory());
factory.run(gg);
gg.putService(CalendarServiceData.class, GtfsLibrary.createCalendarServiceData(context.getDao()));
RoutingRequest options = new RoutingRequest();
String feedId = gg.getFeedIds().iterator().next();
String vertex0 = feedId + ":2010";
String vertex1 = feedId + ":2140";
ShortestPathTree spt;
GraphPath path = null;
FareService fareService = gg.getService(FareService.class);
long offPeakStartTime = TestUtils.dateInSeconds("America/Los_Angeles", 2016, 5, 24, 5, 0, 0);
options.dateTime = offPeakStartTime;
options.setRoutingContext(gg, vertex0, vertex1);
spt = aStar.getShortestPathTree(options);
path = spt.getPath(gg.getVertex(vertex1), true);
Fare costOffPeak = fareService.getCost(path);
assertEquals(costOffPeak.getFare(FareType.regular), new Money(new WrappedCurrency("USD"), 250));
long onPeakStartTime = TestUtils.dateInSeconds("America/Los_Angeles", 2016, 5, 24, 8, 0, 0);
options.dateTime = onPeakStartTime;
options.setRoutingContext(gg, vertex0, vertex1);
spt = aStar.getShortestPathTree(options);
path = spt.getPath(gg.getVertex(vertex1), true);
Fare costOnPeak = fareService.getCost(path);
assertEquals(costOnPeak.getFare(FareType.regular), new Money(new WrappedCurrency("USD"), 275));
}
use of org.opentripplanner.routing.services.FareService in project OpenTripPlanner by opentripplanner.
the class TestFares method testBasic.
public void testBasic() throws Exception {
Graph gg = new Graph();
GtfsContext context = GtfsLibrary.readGtfs(new File(ConstantsForTests.CALTRAIN_GTFS));
GTFSPatternHopFactory factory = new GTFSPatternHopFactory(context);
factory.run(gg);
gg.putService(CalendarServiceData.class, GtfsLibrary.createCalendarServiceData(context.getDao()));
RoutingRequest options = new RoutingRequest();
String feedId = gg.getFeedIds().iterator().next();
long startTime = TestUtils.dateInSeconds("America/Los_Angeles", 2009, 8, 7, 12, 0, 0);
options.dateTime = startTime;
options.setRoutingContext(gg, feedId + ":Millbrae Caltrain", feedId + ":Mountain View Caltrain");
ShortestPathTree spt;
GraphPath path = null;
spt = aStar.getShortestPathTree(options);
path = spt.getPath(gg.getVertex(feedId + ":Mountain View Caltrain"), true);
FareService fareService = gg.getService(FareService.class);
Fare cost = fareService.getCost(path);
assertEquals(cost.getFare(FareType.regular), new Money(new WrappedCurrency("USD"), 425));
}
use of org.opentripplanner.routing.services.FareService in project OpenTripPlanner by opentripplanner.
the class TestFares method testPortland.
public void testPortland() throws Exception {
Graph gg = ConstantsForTests.getInstance().getPortlandGraph();
String feedId = gg.getFeedIds().iterator().next();
RoutingRequest options = new RoutingRequest();
ShortestPathTree spt;
GraphPath path = null;
long startTime = TestUtils.dateInSeconds("America/Los_Angeles", 2009, 11, 1, 12, 0, 0);
options.dateTime = startTime;
options.setRoutingContext(gg, feedId + ":10579", feedId + ":8371");
// from zone 3 to zone 2
spt = aStar.getShortestPathTree(options);
path = spt.getPath(gg.getVertex(feedId + ":8371"), true);
assertNotNull(path);
FareService fareService = gg.getService(FareService.class);
Fare cost = fareService.getCost(path);
assertEquals(new Money(new WrappedCurrency("USD"), 200), cost.getFare(FareType.regular));
// long trip
startTime = TestUtils.dateInSeconds("America/Los_Angeles", 2009, 11, 1, 14, 0, 0);
options.dateTime = startTime;
options.setRoutingContext(gg, feedId + ":8389", feedId + ":1252");
spt = aStar.getShortestPathTree(options);
path = spt.getPath(gg.getVertex(feedId + ":1252"), true);
assertNotNull(path);
cost = fareService.getCost(path);
// assertEquals(cost.getFare(FareType.regular), new Money(new WrappedCurrency("USD"), 460));
// complex trip
options.maxTransfers = 5;
startTime = TestUtils.dateInSeconds("America/Los_Angeles", 2009, 11, 1, 14, 0, 0);
options.dateTime = startTime;
options.setRoutingContext(gg, feedId + ":10428", feedId + ":4231");
spt = aStar.getShortestPathTree(options);
path = spt.getPath(gg.getVertex(feedId + ":4231"), true);
assertNotNull(path);
cost = fareService.getCost(path);
//
// this is commented out because portland's fares are, I think, broken in the gtfs. see
// thread on gtfs-changes.
// assertEquals(cost.getFare(FareType.regular), new Money(new WrappedCurrency("USD"), 430));
}
Aggregations