Search in sources :

Example 6 with FareService

use of org.opentripplanner.routing.services.FareService 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"));
}
Also used : AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) 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) FareComponent(org.opentripplanner.routing.core.FareComponent) File(java.io.File)

Example 7 with FareService

use of org.opentripplanner.routing.services.FareService 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());
}
Also used : WrappedCurrency(org.opentripplanner.routing.core.WrappedCurrency) FareService(org.opentripplanner.routing.services.FareService) Fare(org.opentripplanner.routing.core.Fare)

Aggregations

FareService (org.opentripplanner.routing.services.FareService)7 Fare (org.opentripplanner.routing.core.Fare)6 Money (org.opentripplanner.routing.core.Money)5 WrappedCurrency (org.opentripplanner.routing.core.WrappedCurrency)5 Graph (org.opentripplanner.routing.graph.Graph)5 RoutingRequest (org.opentripplanner.routing.core.RoutingRequest)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 AgencyAndId (org.onebusaway.gtfs.model.AgencyAndId)1 FareType (org.opentripplanner.routing.core.Fare.FareType)1 FareComponent (org.opentripplanner.routing.core.FareComponent)1 Edge (org.opentripplanner.routing.graph.Edge)1 SeattleFareServiceFactory (org.opentripplanner.routing.impl.SeattleFareServiceFactory)1