use of org.opentripplanner.routing.core.FareComponent in project OpenTripPlanner by opentripplanner.
the class DefaultFareServiceImpl method populateFare.
/**
* Builds the Fare object for the given currency, fareType and fareRules.
* <p>
* Besides calculating the lowest fare, we also break down the fare and which routes
* correspond to which components. Note that even if we cannot get a lowest fare
* (if some rides don't have fare rules), there will still be a breakdown for those
* parts which have fares.
* <p>
* As an example, given the rides A-B and B-C. Where A-B and B-C have fares of 10
* each, 2 fare detail objects are added, one with fare 10 for A-B and one with fare 10
* for B-C.
* <p>
* If we add the rule for A-C with a fare of 15, we will get 1 fare detail object
* with fare 15, which lists both A-B and B-C as routes involved.
* <p>
* If our only rule were A-B with a fare of 10, we would have no lowest fare, but
* we will still have one fare detail with fare 10 for the route A-B. B-C will not
* just not be listed at all.
*/
protected boolean populateFare(Fare fare, Currency currency, FareType fareType, List<Ride> rides, Collection<FareRuleSet> fareRules) {
FareSearch r = performSearch(fareType, rides, fareRules);
List<FareComponent> details = new ArrayList<FareComponent>();
int count = 0;
int start = 0;
int end = rides.size() - 1;
while (start <= end) {
// even if not all legs have fares
while (start <= end && r.endOfComponent[start] < 0) {
++start;
}
if (start > end) {
break;
}
int via = r.next[start][r.endOfComponent[start]];
float cost = r.resultTable[start][via];
AgencyAndId fareId = r.fareIds[start][via];
FareComponent detail = new FareComponent(fareId, getMoney(currency, cost));
for (int i = start; i <= via; ++i) {
detail.addRoute(rides.get(i).route);
}
details.add(detail);
++count;
start = via + 1;
}
fare.addFare(fareType, getMoney(currency, r.resultTable[0][rides.size() - 1]));
fare.addFareDetails(fareType, details);
return count > 0;
}
use of org.opentripplanner.routing.core.FareComponent in project OpenTripPlanner by opentripplanner.
the class TestFares method testFareComponent.
public void testFareComponent() throws Exception {
Graph gg = new Graph();
GtfsContext context = GtfsLibrary.readGtfs(new File(ConstantsForTests.FARE_COMPONENT_GTFS));
GTFSPatternHopFactory factory = new GTFSPatternHopFactory(context);
factory.run(gg);
gg.putService(CalendarServiceData.class, GtfsLibrary.createCalendarServiceData(context.getDao()));
String feedId = gg.getFeedIds().iterator().next();
RoutingRequest options = new RoutingRequest();
long startTime = TestUtils.dateInSeconds("America/Los_Angeles", 2009, 8, 7, 12, 0, 0);
options.dateTime = startTime;
ShortestPathTree spt;
GraphPath path = null;
Fare fare = null;
List<FareComponent> fareComponents = null;
FareService fareService = gg.getService(FareService.class);
Money tenUSD = new Money(new WrappedCurrency("USD"), 1000);
// A -> B, base case
options.setRoutingContext(gg, feedId + ":A", feedId + ":B");
spt = aStar.getShortestPathTree(options);
path = spt.getPath(gg.getVertex(feedId + ":B"), true);
fare = fareService.getCost(path);
fareComponents = fare.getDetails(FareType.regular);
assertEquals(fareComponents.size(), 1);
assertEquals(fareComponents.get(0).price, tenUSD);
assertEquals(fareComponents.get(0).fareId, new AgencyAndId(feedId, "AB"));
assertEquals(fareComponents.get(0).routes.get(0), new AgencyAndId("agency", "1"));
// D -> E, null case
options.setRoutingContext(gg, feedId + ":D", feedId + ":E");
spt = aStar.getShortestPathTree(options);
path = spt.getPath(gg.getVertex(feedId + ":E"), true);
fare = fareService.getCost(path);
assertNull(fare);
// A -> C, 2 components in a path
options.setRoutingContext(gg, feedId + ":A", feedId + ":C");
spt = aStar.getShortestPathTree(options);
path = spt.getPath(gg.getVertex(feedId + ":C"), true);
fare = fareService.getCost(path);
fareComponents = fare.getDetails(FareType.regular);
assertEquals(fareComponents.size(), 2);
assertEquals(fareComponents.get(0).price, tenUSD);
assertEquals(fareComponents.get(0).fareId, new AgencyAndId(feedId, "AB"));
assertEquals(fareComponents.get(0).routes.get(0), new AgencyAndId("agency", "1"));
assertEquals(fareComponents.get(1).price, tenUSD);
assertEquals(fareComponents.get(1).fareId, new AgencyAndId(feedId, "BC"));
assertEquals(fareComponents.get(1).routes.get(0), new AgencyAndId("agency", "2"));
// B -> D, 2 fully connected components
options.setRoutingContext(gg, feedId + ":B", feedId + ":D");
spt = aStar.getShortestPathTree(options);
path = spt.getPath(gg.getVertex(feedId + ":D"), true);
fare = fareService.getCost(path);
fareComponents = fare.getDetails(FareType.regular);
assertEquals(fareComponents.size(), 1);
assertEquals(fareComponents.get(0).price, tenUSD);
assertEquals(fareComponents.get(0).fareId, new AgencyAndId(feedId, "BD"));
assertEquals(fareComponents.get(0).routes.get(0), new AgencyAndId("agency", "2"));
assertEquals(fareComponents.get(0).routes.get(1), new AgencyAndId("agency", "3"));
// E -> G, missing in between fare
options.setRoutingContext(gg, feedId + ":E", feedId + ":G");
spt = aStar.getShortestPathTree(options);
path = spt.getPath(gg.getVertex(feedId + ":G"), true);
fare = fareService.getCost(path);
fareComponents = fare.getDetails(FareType.regular);
assertEquals(fareComponents.size(), 1);
assertEquals(fareComponents.get(0).price, tenUSD);
assertEquals(fareComponents.get(0).fareId, new AgencyAndId(feedId, "EG"));
assertEquals(fareComponents.get(0).routes.get(0), new AgencyAndId("agency", "5"));
assertEquals(fareComponents.get(0).routes.get(1), new AgencyAndId("agency", "6"));
// C -> E, missing fare after
options.setRoutingContext(gg, feedId + ":C", feedId + ":E");
spt = aStar.getShortestPathTree(options);
path = spt.getPath(gg.getVertex(feedId + ":E"), true);
fare = fareService.getCost(path);
fareComponents = fare.getDetails(FareType.regular);
assertEquals(fareComponents.size(), 1);
assertEquals(fareComponents.get(0).price, tenUSD);
assertEquals(fareComponents.get(0).fareId, new AgencyAndId(feedId, "CD"));
assertEquals(fareComponents.get(0).routes.get(0), new AgencyAndId("agency", "3"));
// D -> G, missing fare before
options.setRoutingContext(gg, feedId + ":D", feedId + ":G");
spt = aStar.getShortestPathTree(options);
path = spt.getPath(gg.getVertex(feedId + ":G"), true);
fare = fareService.getCost(path);
fareComponents = fare.getDetails(FareType.regular);
assertEquals(fareComponents.size(), 1);
assertEquals(fareComponents.get(0).price, tenUSD);
assertEquals(fareComponents.get(0).fareId, new AgencyAndId(feedId, "EG"));
assertEquals(fareComponents.get(0).routes.get(0), new AgencyAndId("agency", "5"));
assertEquals(fareComponents.get(0).routes.get(1), new AgencyAndId("agency", "6"));
// A -> D, use individual component parts
options.setRoutingContext(gg, feedId + ":A", feedId + ":D");
spt = aStar.getShortestPathTree(options);
path = spt.getPath(gg.getVertex(feedId + ":D"), true);
fare = fareService.getCost(path);
fareComponents = fare.getDetails(FareType.regular);
assertEquals(fareComponents.size(), 2);
assertEquals(fareComponents.get(0).price, tenUSD);
assertEquals(fareComponents.get(0).fareId, new AgencyAndId(feedId, "AB"));
assertEquals(fareComponents.get(0).routes.get(0), new AgencyAndId("agency", "1"));
assertEquals(fareComponents.get(1).price, tenUSD);
assertEquals(fareComponents.get(1).fareId, new AgencyAndId(feedId, "BD"));
assertEquals(fareComponents.get(1).routes.get(0), new AgencyAndId("agency", "2"));
assertEquals(fareComponents.get(1).routes.get(1), new AgencyAndId("agency", "3"));
}
Aggregations