Search in sources :

Example 1 with FareRule

use of org.onebusaway.gtfs.model.FareRule in project OpenTripPlanner by opentripplanner.

the class DefaultFareServiceFactory method fillFareRules.

protected void fillFareRules(String agencyId, Collection<FareAttribute> fareAttributes, Collection<FareRule> fareRules, Map<AgencyAndId, FareRuleSet> fareRuleSet) {
    /*
         * Create an empty FareRuleSet for each FareAttribute, as some FareAttribute may have no
         * rules attached to them.
         */
    for (FareAttribute fare : fareAttributes) {
        AgencyAndId id = fare.getId();
        FareRuleSet fareRule = fareRuleSet.get(id);
        if (fareRule == null) {
            fareRule = new FareRuleSet(fare);
            fareRuleSet.put(id, fareRule);
            if (agencyId != null) {
                // TODO With the new GTFS lib, use fareAttribute.agency_id directly
                fareRule.setAgency(agencyId);
            }
        }
    }
    /*
         * For each fare rule, add it to the FareRuleSet of the fare.
         */
    for (FareRule rule : fareRules) {
        FareAttribute fare = rule.getFare();
        AgencyAndId id = fare.getId();
        FareRuleSet fareRule = fareRuleSet.get(id);
        if (fareRule == null) {
            // Should never happen by design
            LOG.error("Inexistant fare ID in fare rule: " + id);
            continue;
        }
        String contains = rule.getContainsId();
        if (contains != null) {
            fareRule.addContains(contains);
        }
        String origin = rule.getOriginId();
        String destination = rule.getDestinationId();
        if (origin != null || destination != null) {
            fareRule.addOriginDestination(origin, destination);
        }
        Route route = rule.getRoute();
        if (route != null) {
            AgencyAndId routeId = route.getId();
            fareRule.addRoute(routeId);
        }
    }
}
Also used : FareAttribute(org.onebusaway.gtfs.model.FareAttribute) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) FareRule(org.onebusaway.gtfs.model.FareRule) FareRuleSet(org.opentripplanner.routing.core.FareRuleSet) Route(org.onebusaway.gtfs.model.Route)

Example 2 with FareRule

use of org.onebusaway.gtfs.model.FareRule in project onebusaway-gtfs-modules by OneBusAway.

the class StopMatrixFareModificationStrategy method run.

@Override
public void run(TransformContext context, GtfsMutableRelationalDao dao) {
    // remove rules for route
    for (FareRule rule : new HashSet<FareRule>(dao.getAllFareRules())) {
        if (rule.getRoute() != null && rule.getRoute().getId().getId().equals(routeId)) {
            if (!isExemplarSet()) {
                setAttributesFromExemplar(rule.getFare());
            }
            route = rule.getRoute();
            dao.removeEntity(rule);
        }
    }
    for (FareAttribute attr : new HashSet<FareAttribute>(dao.getAllFareAttributes())) {
        if (dao.getFareRulesForFareAttribute(attr).isEmpty()) {
            dao.removeEntity(attr);
        }
    }
    // add new rules
    FareCreationListener listener = new FareCreationListener();
    listener.setDao(dao);
    try {
        URL url = new URL(csvUrl);
        try (InputStream is = url.openStream()) {
            new CSVLibrary().parse(is, listener);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    listener.flushNewFares();
}
Also used : FareAttribute(org.onebusaway.gtfs.model.FareAttribute) InputStream(java.io.InputStream) FareRule(org.onebusaway.gtfs.model.FareRule) CSVLibrary(org.onebusaway.csv_entities.CSVLibrary) URL(java.net.URL) HashSet(java.util.HashSet)

Example 3 with FareRule

use of org.onebusaway.gtfs.model.FareRule in project onebusaway-gtfs-modules by OneBusAway.

the class GtfsDaoImplTest method testBart.

@Test
public void testBart() throws IOException {
    GtfsDaoImpl dao = new GtfsDaoImpl();
    GtfsTestData.readGtfs(dao, GtfsTestData.getBartGtfs(), "BART");
    Collection<Agency> agencies = dao.getAllAgencies();
    assertEquals(2, agencies.size());
    Agency agency = dao.getAgencyForId("BART");
    assertEquals("BART", agency.getId());
    Collection<ServiceCalendarDate> calendarDates = dao.getAllCalendarDates();
    assertEquals(32, calendarDates.size());
    ServiceCalendarDate calendarDate = dao.getCalendarDateForId(1);
    assertEquals(new AgencyAndId("BART", "SUN"), calendarDate.getServiceId());
    assertEquals(new ServiceDate(2009, 1, 1), calendarDate.getDate());
    assertEquals(1, calendarDate.getExceptionType());
    Collection<ServiceCalendar> calendars = dao.getAllCalendars();
    assertEquals(5, calendars.size());
    ServiceCalendar calendar = dao.getCalendarForId(1);
    assertEquals(new AgencyAndId("BART", "WKDY"), calendar.getServiceId());
    assertEquals(new ServiceDate(2007, 1, 1), calendar.getStartDate());
    assertEquals(new ServiceDate(2010, 12, 31), calendar.getEndDate());
    assertEquals(1, calendar.getMonday());
    assertEquals(1, calendar.getTuesday());
    assertEquals(1, calendar.getWednesday());
    assertEquals(1, calendar.getThursday());
    assertEquals(1, calendar.getFriday());
    assertEquals(0, calendar.getSaturday());
    assertEquals(0, calendar.getSunday());
    Collection<FareAttribute> fareAttributes = dao.getAllFareAttributes();
    assertEquals(106, fareAttributes.size());
    FareAttribute fareAttribute = dao.getFareAttributeForId(new AgencyAndId("BART", "30"));
    assertEquals(new AgencyAndId("BART", "30"), fareAttribute.getId());
    Collection<FareRule> fareRules = dao.getAllFareRules();
    assertEquals(1849, fareRules.size());
    FareRule fareRule = dao.getFareRuleForId(1);
    assertEquals(new AgencyAndId("BART", "98"), fareRule.getFare().getId());
    Collection<Frequency> frequencies = dao.getAllFrequencies();
    assertEquals(6, frequencies.size());
    Frequency frequency = dao.getFrequencyForId(1);
    assertEquals(new AgencyAndId("AirBART", "M-FSAT1DN"), frequency.getTrip().getId());
    Collection<Route> routes = dao.getAllRoutes();
    assertEquals(11, routes.size());
    Route route = dao.getRouteForId(new AgencyAndId("BART", "01"));
    assertEquals(new AgencyAndId("BART", "01"), route.getId());
    Collection<ShapePoint> shapePoints = dao.getAllShapePoints();
    assertEquals(105, shapePoints.size());
    ShapePoint shapePoint = dao.getShapePointForId(1);
    assertEquals(new AgencyAndId("BART", "airbart-dn.csv"), shapePoint.getShapeId());
    Collection<Stop> stops = dao.getAllStops();
    assertEquals(46, stops.size());
    Stop stop = dao.getStopForId(new AgencyAndId("BART", "DBRK"));
    assertEquals("Downtown Berkeley BART", stop.getName());
    Collection<StopTime> stopTimes = dao.getAllStopTimes();
    assertEquals(33270, stopTimes.size());
    StopTime stopTime = stopTimes.iterator().next();
    assertEquals(18000, stopTime.getArrivalTime());
    Collection<Transfer> transfers = dao.getAllTransfers();
    assertEquals(4, transfers.size());
    Transfer transfer = dao.getTransferForId(1);
    assertEquals(1, transfer.getTransferType());
    Collection<Trip> trips = dao.getAllTrips();
    assertEquals(1620, trips.size());
    Trip trip = dao.getTripForId(new AgencyAndId("BART", "15PB1"));
    assertEquals(new AgencyAndId("BART", "WKDY"), trip.getServiceId());
}
Also used : FareAttribute(org.onebusaway.gtfs.model.FareAttribute) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) Stop(org.onebusaway.gtfs.model.Stop) ServiceDate(org.onebusaway.gtfs.model.calendar.ServiceDate) ShapePoint(org.onebusaway.gtfs.model.ShapePoint) Route(org.onebusaway.gtfs.model.Route) StopTime(org.onebusaway.gtfs.model.StopTime) ServiceCalendarDate(org.onebusaway.gtfs.model.ServiceCalendarDate) Trip(org.onebusaway.gtfs.model.Trip) Agency(org.onebusaway.gtfs.model.Agency) FareRule(org.onebusaway.gtfs.model.FareRule) Transfer(org.onebusaway.gtfs.model.Transfer) Frequency(org.onebusaway.gtfs.model.Frequency) ServiceCalendar(org.onebusaway.gtfs.model.ServiceCalendar) Test(org.junit.Test)

Aggregations

FareAttribute (org.onebusaway.gtfs.model.FareAttribute)3 FareRule (org.onebusaway.gtfs.model.FareRule)3 AgencyAndId (org.onebusaway.gtfs.model.AgencyAndId)2 Route (org.onebusaway.gtfs.model.Route)2 InputStream (java.io.InputStream)1 URL (java.net.URL)1 HashSet (java.util.HashSet)1 Test (org.junit.Test)1 CSVLibrary (org.onebusaway.csv_entities.CSVLibrary)1 Agency (org.onebusaway.gtfs.model.Agency)1 Frequency (org.onebusaway.gtfs.model.Frequency)1 ServiceCalendar (org.onebusaway.gtfs.model.ServiceCalendar)1 ServiceCalendarDate (org.onebusaway.gtfs.model.ServiceCalendarDate)1 ShapePoint (org.onebusaway.gtfs.model.ShapePoint)1 Stop (org.onebusaway.gtfs.model.Stop)1 StopTime (org.onebusaway.gtfs.model.StopTime)1 Transfer (org.onebusaway.gtfs.model.Transfer)1 Trip (org.onebusaway.gtfs.model.Trip)1 ServiceDate (org.onebusaway.gtfs.model.calendar.ServiceDate)1 FareRuleSet (org.opentripplanner.routing.core.FareRuleSet)1