use of org.opentripplanner.routing.core.WrappedCurrency in project OpenTripPlanner by opentripplanner.
the class TimeBasedBikeRentalFareService method getCost.
@Override
public Fare getCost(GraphPath path) {
int cost = 0;
long start = -1;
for (State state : path.states) {
if (state.getVertex() instanceof BikeRentalStationVertex && state.getBackState().getVertex() instanceof BikeRentalStationVertex) {
if (start == -1) {
start = state.getTimeSeconds();
} else {
int time_on_bike = (int) (state.getTimeSeconds() - start);
int ride_cost = -1;
for (P2<Integer> bracket : pricing_by_second) {
int time = bracket.first;
if (time_on_bike < time) {
ride_cost = bracket.second;
break;
}
}
if (ride_cost == -1) {
log.warn("Bike rental has no associated pricing (too long?) : " + time_on_bike + " seconds");
} else {
cost += ride_cost;
}
start = -1;
}
}
}
Fare fare = new Fare();
fare.addFare(FareType.regular, new WrappedCurrency(currency), cost);
return fare;
}
use of org.opentripplanner.routing.core.WrappedCurrency 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"));
}
use of org.opentripplanner.routing.core.WrappedCurrency in project OpenTripPlanner by opentripplanner.
the class MultipleFareServiceTest method testAddingMultipleFareService.
public void testAddingMultipleFareService() {
Fare fare1 = new Fare();
fare1.addFare(FareType.regular, new WrappedCurrency("EUR"), 100);
FareService fs1 = new SimpleFareService(fare1);
Fare fare2 = new Fare();
fare2.addFare(FareType.regular, new WrappedCurrency("EUR"), 140);
fare2.addFare(FareType.student, new WrappedCurrency("EUR"), 120);
FareService fs2 = new SimpleFareService(fare2);
/*
* Note: this fare is not very representative, as you should probably always compute a
* "regular" fare in case you want to add bike and transit fares.
*/
Fare fare3 = new Fare();
fare3.addFare(FareType.student, new WrappedCurrency("EUR"), 80);
FareService fs3 = new SimpleFareService(fare3);
AddingMultipleFareService mfs = new AddingMultipleFareService(new ArrayList<FareService>());
Fare fare = mfs.getCost(null);
assertNull(fare);
mfs = new AddingMultipleFareService(Arrays.asList(fs1));
fare = mfs.getCost(null);
assertEquals(100, fare.getFare(FareType.regular).getCents());
assertEquals(null, fare.getFare(FareType.student));
mfs = new AddingMultipleFareService(Arrays.asList(fs2));
fare = mfs.getCost(null);
assertEquals(140, fare.getFare(FareType.regular).getCents());
assertEquals(120, fare.getFare(FareType.student).getCents());
mfs = new AddingMultipleFareService(Arrays.asList(fs1, fs2));
fare = mfs.getCost(null);
assertEquals(240, fare.getFare(FareType.regular).getCents());
assertEquals(220, fare.getFare(FareType.student).getCents());
mfs = new AddingMultipleFareService(Arrays.asList(fs2, fs1));
fare = mfs.getCost(null);
assertEquals(240, fare.getFare(FareType.regular).getCents());
assertEquals(220, fare.getFare(FareType.student).getCents());
mfs = new AddingMultipleFareService(Arrays.asList(fs1, fs3));
fare = mfs.getCost(null);
assertEquals(100, fare.getFare(FareType.regular).getCents());
assertEquals(180, fare.getFare(FareType.student).getCents());
mfs = new AddingMultipleFareService(Arrays.asList(fs3, fs1));
fare = mfs.getCost(null);
assertEquals(100, fare.getFare(FareType.regular).getCents());
assertEquals(180, fare.getFare(FareType.student).getCents());
mfs = new AddingMultipleFareService(Arrays.asList(fs1, fs2, fs3));
fare = mfs.getCost(null);
assertEquals(240, fare.getFare(FareType.regular).getCents());
assertEquals(300, fare.getFare(FareType.student).getCents());
}
use of org.opentripplanner.routing.core.WrappedCurrency in project OpenTripPlanner by opentripplanner.
the class NycFareServiceImpl method getCost.
@Override
public Fare getCost(GraphPath path) {
final List<AgencyAndId> SIR_PAID_STOPS = makeMtaStopList("S31", "S30");
final List<AgencyAndId> SUBWAY_FREE_TRANSFER_STOPS = makeMtaStopList("R11", "B08", "629");
final List<AgencyAndId> SIR_BONUS_STOPS = makeMtaStopList("140", "420", "419", "418", "M22", "M23", "R27", "R26");
final List<AgencyAndId> SIR_BONUS_ROUTES = makeMtaStopList("M5", "M20", "M15-SBS");
final List<AgencyAndId> CANARSIE = makeMtaStopList("L29", "303345");
// List of NYC agencies to set fares for
final List<String> AGENCIES = new ArrayList<>();
AGENCIES.add("MTABC");
AGENCIES.add("MTA NYCT");
LinkedList<State> states = path.states;
// create rides
List<Ride> rides = new ArrayList<Ride>();
Ride newRide = null;
final int SUBWAY = 1;
final int SIR = 2;
final int LOCAL_BUS = 3;
final int EXPRESS_BUS = 30;
final int EXPENSIVE_EXPRESS_BUS = 34;
final int WALK = -1;
for (State state : states) {
Edge backEdge = state.getBackEdge();
if (backEdge instanceof StreetEdge) {
if (newRide == null || !newRide.classifier.equals(WALK)) {
if (rides.size() == 0 || !rides.get(rides.size() - 1).classifier.equals(WALK)) {
newRide = new Ride();
newRide.classifier = WALK;
rides.add(newRide);
}
}
continue;
}
// dwells do not affect fare.
if (backEdge instanceof DwellEdge)
continue;
if (!(backEdge instanceof HopEdge)) {
newRide = null;
continue;
}
AgencyAndId routeId = state.getRoute();
String agencyId = state.getBackTrip().getRoute().getAgency().getId();
if (!AGENCIES.contains(agencyId)) {
continue;
}
if (routeId == null) {
newRide = null;
} else {
if (newRide == null || !routeId.equals(newRide.route)) {
newRide = new Ride();
rides.add(newRide);
newRide.firstStop = ((HopEdge) backEdge).getBeginStop();
newRide.route = routeId;
Trip trip = state.getBackTrip();
Route route = trip.getRoute();
int type = route.getType();
newRide.classifier = type;
String shortName = route.getShortName();
if (shortName == null) {
newRide.classifier = SUBWAY;
} else if (shortName.equals("BxM4C")) {
newRide.classifier = EXPENSIVE_EXPRESS_BUS;
} else if (shortName.startsWith("X") || shortName.startsWith("BxM") || shortName.startsWith("QM") || shortName.startsWith("BM")) {
// Express bus
newRide.classifier = EXPRESS_BUS;
}
newRide.startTime = state.getTimeSeconds();
}
newRide.lastStop = ((HopEdge) backEdge).getBeginStop();
}
}
// There are no rides, so there's no fare.
if (rides.size() == 0) {
return null;
}
NycFareState state = NycFareState.INIT;
boolean lexFreeTransfer = false;
boolean canarsieFreeTransfer = false;
boolean siLocalBus = false;
boolean sirBonusTransfer = false;
float totalFare = 0;
for (Ride ride : rides) {
AgencyAndId firstStopId = null;
AgencyAndId lastStopId = null;
if (ride.firstStop != null) {
firstStopId = ride.firstStop.getId();
lastStopId = ride.lastStop.getId();
}
switch(state) {
case INIT:
lexFreeTransfer = siLocalBus = canarsieFreeTransfer = false;
if (ride.classifier.equals(WALK)) {
// walking keeps you in init
} else if (ride.classifier.equals(SUBWAY)) {
state = NycFareState.SUBWAY_PRE_TRANSFER;
totalFare += ORDINARY_FARE;
if (SUBWAY_FREE_TRANSFER_STOPS.contains(ride.lastStop.getId())) {
lexFreeTransfer = true;
}
if (CANARSIE.contains(ride.lastStop.getId())) {
canarsieFreeTransfer = true;
}
} else if (ride.classifier.equals(SIR)) {
state = NycFareState.SIR_PRE_TRANSFER;
if (SIR_PAID_STOPS.contains(firstStopId) || SIR_PAID_STOPS.contains(lastStopId)) {
totalFare += ORDINARY_FARE;
}
} else if (ride.classifier.equals(LOCAL_BUS)) {
state = NycFareState.BUS_PRE_TRANSFER;
totalFare += ORDINARY_FARE;
if (CANARSIE.contains(ride.lastStop.getId())) {
canarsieFreeTransfer = true;
}
siLocalBus = ride.route.getId().startsWith("S");
} else if (ride.classifier.equals(EXPRESS_BUS)) {
state = NycFareState.BUS_PRE_TRANSFER;
totalFare += EXPRESS_FARE;
} else if (ride.classifier.equals(EXPENSIVE_EXPRESS_BUS)) {
state = NycFareState.EXPENSIVE_EXPRESS_BUS;
totalFare += EXPENSIVE_EXPRESS_FARE;
}
break;
case SUBWAY_PRE_TRANSFER_WALKED:
if (ride.classifier.equals(SUBWAY)) {
// lex and 59/63
if (!(lexFreeTransfer && SUBWAY_FREE_TRANSFER_STOPS.contains(ride.firstStop.getId()))) {
totalFare += ORDINARY_FARE;
}
lexFreeTransfer = canarsieFreeTransfer = false;
if (SUBWAY_FREE_TRANSFER_STOPS.contains(ride.lastStop.getId())) {
lexFreeTransfer = true;
}
if (CANARSIE.contains(ride.lastStop.getId())) {
canarsieFreeTransfer = true;
}
}
/* FALL THROUGH */
case SUBWAY_PRE_TRANSFER:
// hours (if only just)
if (ride.classifier.equals(WALK)) {
state = NycFareState.SUBWAY_PRE_TRANSFER_WALKED;
} else if (ride.classifier.equals(SIR)) {
state = NycFareState.SIR_POST_TRANSFER_FROM_SUBWAY;
} else if (ride.classifier.equals(LOCAL_BUS)) {
if (CANARSIE.contains(ride.firstStop.getId()) && canarsieFreeTransfer) {
state = NycFareState.BUS_PRE_TRANSFER;
} else {
state = NycFareState.INIT;
}
} else if (ride.classifier.equals(EXPRESS_BUS)) {
// need to pay the upgrade cost
totalFare += EXPRESS_FARE - ORDINARY_FARE;
} else if (ride.classifier.equals(EXPENSIVE_EXPRESS_BUS)) {
// no transfers to the
totalFare += EXPENSIVE_EXPRESS_FARE;
// BxMM4C
}
break;
case BUS_PRE_TRANSFER:
if (ride.classifier.equals(SUBWAY)) {
if (CANARSIE.contains(ride.firstStop.getId()) && canarsieFreeTransfer) {
state = NycFareState.SUBWAY_PRE_TRANSFER;
} else {
state = NycFareState.INIT;
}
} else if (ride.classifier.equals(SIR)) {
if (siLocalBus) {
// SI local bus to SIR, so it is as if we started on the
// SIR (except that when we enter the bus or subway system we need to do
// so at certain places)
sirBonusTransfer = true;
state = NycFareState.SIR_PRE_TRANSFER;
} else {
// transfers exhausted
state = NycFareState.INIT;
}
} else if (ride.classifier.equals(LOCAL_BUS)) {
state = NycFareState.INIT;
} else if (ride.classifier.equals(EXPRESS_BUS)) {
// need to pay the upgrade cost
totalFare += EXPRESS_FARE - ORDINARY_FARE;
state = NycFareState.INIT;
} else if (ride.classifier.equals(EXPENSIVE_EXPRESS_BUS)) {
totalFare += EXPENSIVE_EXPRESS_FARE;
// no transfers to the BxMM4C
}
break;
case SIR_PRE_TRANSFER:
if (ride.classifier.equals(SUBWAY)) {
if (sirBonusTransfer && !SIR_BONUS_STOPS.contains(ride.firstStop.getId())) {
// we were relying on the bonus transfer to be in the "pre-transfer state",
// but the bonus transfer does not apply here
totalFare += ORDINARY_FARE;
}
if (CANARSIE.contains(ride.lastStop.getId())) {
canarsieFreeTransfer = true;
}
state = NycFareState.SUBWAY_POST_TRANSFER;
} else if (ride.classifier.equals(SIR)) {
/* should not happen, and unhandled */
LOG.warn("Should not transfer from SIR to SIR");
} else if (ride.classifier.equals(LOCAL_BUS)) {
if (!SIR_BONUS_ROUTES.contains(ride.route)) {
totalFare += ORDINARY_FARE;
}
state = NycFareState.BUS_PRE_TRANSFER;
} else if (ride.classifier.equals(EXPRESS_BUS)) {
totalFare += EXPRESS_BUS;
state = NycFareState.BUS_PRE_TRANSFER;
} else if (ride.classifier.equals(EXPENSIVE_EXPRESS_BUS)) {
totalFare += EXPENSIVE_EXPRESS_BUS;
state = NycFareState.BUS_PRE_TRANSFER;
}
break;
case SIR_POST_TRANSFER_FROM_SUBWAY:
if (ride.classifier.equals(SUBWAY)) {
/* should not happen */
totalFare += ORDINARY_FARE;
state = NycFareState.SUBWAY_PRE_TRANSFER;
} else if (ride.classifier.equals(SIR)) {
/* should not happen, and unhandled */
LOG.warn("Should not transfer from SIR to SIR");
} else if (ride.classifier.equals(LOCAL_BUS)) {
if (!ride.route.getId().startsWith("S")) {
totalFare += ORDINARY_FARE;
state = NycFareState.BUS_PRE_TRANSFER;
} else {
state = NycFareState.INIT;
}
} else if (ride.classifier.equals(EXPRESS_BUS)) {
// need to pay the full cost
totalFare += EXPRESS_FARE;
state = NycFareState.INIT;
} else if (ride.classifier.equals(EXPENSIVE_EXPRESS_BUS)) {
/* should not happen */
// no transfers to the BxMM4C
totalFare += EXPENSIVE_EXPRESS_FARE;
state = NycFareState.BUS_PRE_TRANSFER;
}
break;
case SUBWAY_POST_TRANSFER:
if (ride.classifier.equals(WALK)) {
if (!canarsieFreeTransfer) {
/* note: if we end up walking to another subway after alighting
* at Canarsie, we will mistakenly not be charged, but nobody
* would ever do this */
state = NycFareState.INIT;
}
} else if (ride.classifier.equals(SIR)) {
totalFare += ORDINARY_FARE;
state = NycFareState.SIR_PRE_TRANSFER;
} else if (ride.classifier.equals(LOCAL_BUS)) {
if (!(CANARSIE.contains(ride.firstStop.getId()) && canarsieFreeTransfer)) {
totalFare += ORDINARY_FARE;
}
state = NycFareState.INIT;
} else if (ride.classifier.equals(SUBWAY)) {
// walking transfer
totalFare += ORDINARY_FARE;
state = NycFareState.SUBWAY_PRE_TRANSFER;
} else if (ride.classifier.equals(EXPRESS_BUS)) {
totalFare += EXPRESS_FARE;
state = NycFareState.BUS_PRE_TRANSFER;
} else if (ride.classifier.equals(EXPENSIVE_EXPRESS_BUS)) {
totalFare += EXPENSIVE_EXPRESS_FARE;
state = NycFareState.BUS_PRE_TRANSFER;
}
}
}
Currency currency = Currency.getInstance("USD");
Fare fare = new Fare();
fare.addFare(FareType.regular, new WrappedCurrency(currency), (int) Math.round(totalFare * Math.pow(10, currency.getDefaultFractionDigits())));
return fare;
}
Aggregations