Search in sources :

Example 1 with WrappedCurrency

use of org.opentripplanner.routing.core.WrappedCurrency in project OpenTripPlanner by opentripplanner.

the class DefaultFareServiceImpl method getCost.

@Override
public Fare getCost(GraphPath path) {
    List<Ride> rides = createRides(path);
    // If there are no rides, there's no fare.
    if (rides.size() == 0) {
        return null;
    }
    Fare fare = new Fare();
    boolean hasFare = false;
    for (Map.Entry<FareType, Collection<FareRuleSet>> kv : fareRulesPerType.entrySet()) {
        FareType fareType = kv.getKey();
        Collection<FareRuleSet> fareRules = kv.getValue();
        // pick up a random currency from fareAttributes,
        // we assume that all tickets use the same currency
        Currency currency = null;
        WrappedCurrency wrappedCurrency = null;
        if (fareRules.size() > 0) {
            currency = Currency.getInstance(fareRules.iterator().next().getFareAttribute().getCurrencyType());
            wrappedCurrency = new WrappedCurrency(currency);
        }
        hasFare = populateFare(fare, currency, fareType, rides, fareRules);
    }
    return hasFare ? fare : null;
}
Also used : FareType(org.opentripplanner.routing.core.Fare.FareType) Currency(java.util.Currency) WrappedCurrency(org.opentripplanner.routing.core.WrappedCurrency) Collection(java.util.Collection) WrappedCurrency(org.opentripplanner.routing.core.WrappedCurrency) FareRuleSet(org.opentripplanner.routing.core.FareRuleSet) HashMap(java.util.HashMap) Map(java.util.Map) Fare(org.opentripplanner.routing.core.Fare)

Example 2 with WrappedCurrency

use of org.opentripplanner.routing.core.WrappedCurrency in project OpenTripPlanner by opentripplanner.

the class DefaultFareServiceImpl method getMoney.

protected static Money getMoney(Currency currency, float cost) {
    int fractionDigits = 2;
    if (currency != null)
        fractionDigits = currency.getDefaultFractionDigits();
    int cents = (int) Math.round(cost * Math.pow(10, fractionDigits));
    return new Money(new WrappedCurrency(currency), cents);
}
Also used : Money(org.opentripplanner.routing.core.Money) WrappedCurrency(org.opentripplanner.routing.core.WrappedCurrency)

Example 3 with WrappedCurrency

use of org.opentripplanner.routing.core.WrappedCurrency 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));
}
Also used : SeattleFareServiceFactory(org.opentripplanner.routing.impl.SeattleFareServiceFactory) GtfsContext(org.opentripplanner.gtfs.GtfsContext) GraphPath(org.opentripplanner.routing.spt.GraphPath) WrappedCurrency(org.opentripplanner.routing.core.WrappedCurrency) GTFSPatternHopFactory(org.opentripplanner.routing.edgetype.factory.GTFSPatternHopFactory) FareService(org.opentripplanner.routing.services.FareService) Fare(org.opentripplanner.routing.core.Fare) Money(org.opentripplanner.routing.core.Money) Graph(org.opentripplanner.routing.graph.Graph) ShortestPathTree(org.opentripplanner.routing.spt.ShortestPathTree) RoutingRequest(org.opentripplanner.routing.core.RoutingRequest) File(java.io.File)

Example 4 with WrappedCurrency

use of org.opentripplanner.routing.core.WrappedCurrency 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));
}
Also used : GtfsContext(org.opentripplanner.gtfs.GtfsContext) GraphPath(org.opentripplanner.routing.spt.GraphPath) WrappedCurrency(org.opentripplanner.routing.core.WrappedCurrency) GTFSPatternHopFactory(org.opentripplanner.routing.edgetype.factory.GTFSPatternHopFactory) FareService(org.opentripplanner.routing.services.FareService) Fare(org.opentripplanner.routing.core.Fare) Money(org.opentripplanner.routing.core.Money) Graph(org.opentripplanner.routing.graph.Graph) ShortestPathTree(org.opentripplanner.routing.spt.ShortestPathTree) RoutingRequest(org.opentripplanner.routing.core.RoutingRequest) File(java.io.File)

Example 5 with WrappedCurrency

use of org.opentripplanner.routing.core.WrappedCurrency 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));
}
Also used : Money(org.opentripplanner.routing.core.Money) Graph(org.opentripplanner.routing.graph.Graph) ShortestPathTree(org.opentripplanner.routing.spt.ShortestPathTree) GraphPath(org.opentripplanner.routing.spt.GraphPath) RoutingRequest(org.opentripplanner.routing.core.RoutingRequest) WrappedCurrency(org.opentripplanner.routing.core.WrappedCurrency) FareService(org.opentripplanner.routing.services.FareService) Fare(org.opentripplanner.routing.core.Fare)

Aggregations

WrappedCurrency (org.opentripplanner.routing.core.WrappedCurrency)9 Fare (org.opentripplanner.routing.core.Fare)8 Money (org.opentripplanner.routing.core.Money)5 FareService (org.opentripplanner.routing.services.FareService)5 RoutingRequest (org.opentripplanner.routing.core.RoutingRequest)4 Graph (org.opentripplanner.routing.graph.Graph)4 GraphPath (org.opentripplanner.routing.spt.GraphPath)4 ShortestPathTree (org.opentripplanner.routing.spt.ShortestPathTree)4 File (java.io.File)3 GtfsContext (org.opentripplanner.gtfs.GtfsContext)3 GTFSPatternHopFactory (org.opentripplanner.routing.edgetype.factory.GTFSPatternHopFactory)3 Currency (java.util.Currency)2 AgencyAndId (org.onebusaway.gtfs.model.AgencyAndId)2 State (org.opentripplanner.routing.core.State)2 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Route (org.onebusaway.gtfs.model.Route)1 Trip (org.onebusaway.gtfs.model.Trip)1