Search in sources :

Example 1 with FareAttribute

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

the class DefaultFareServiceImpl method getBestFareAndId.

private FareAndId getBestFareAndId(FareType fareType, List<Ride> rides, Collection<FareRuleSet> fareRules) {
    Set<String> zones = new HashSet<String>();
    Set<AgencyAndId> routes = new HashSet<AgencyAndId>();
    Set<String> agencies = new HashSet<String>();
    Set<AgencyAndId> trips = new HashSet<AgencyAndId>();
    int transfersUsed = -1;
    Ride firstRide = rides.get(0);
    long startTime = firstRide.startTime;
    String startZone = firstRide.startZone;
    String endZone = firstRide.endZone;
    // stops don't really have an agency id, they have the per-feed default id
    String feedId = firstRide.firstStop.getId().getAgencyId();
    long lastRideStartTime = firstRide.startTime;
    long lastRideEndTime = firstRide.endTime;
    for (Ride ride : rides) {
        if (!ride.firstStop.getId().getAgencyId().equals(feedId)) {
            LOG.debug("skipped multi-feed ride sequence {}", rides);
            return new FareAndId(Float.POSITIVE_INFINITY, null);
        }
        lastRideStartTime = ride.startTime;
        lastRideEndTime = ride.endTime;
        endZone = ride.endZone;
        agencies.add(ride.agency);
        routes.add(ride.route);
        zones.addAll(ride.zones);
        trips.add(ride.trip);
        transfersUsed += 1;
    }
    FareAttribute bestAttribute = null;
    float bestFare = Float.POSITIVE_INFINITY;
    long tripTime = lastRideStartTime - startTime;
    long journeyTime = lastRideEndTime - startTime;
    // find the best fare that matches this set of rides
    for (FareRuleSet ruleSet : fareRules) {
        FareAttribute attribute = ruleSet.getFareAttribute();
        // check only if the fare is not mapped to an agency
        if (!ruleSet.hasAgencyDefined() && !attribute.getId().getAgencyId().equals(feedId))
            continue;
        if (ruleSet.matches(agencies, startZone, endZone, zones, routes, trips)) {
            // TODO Maybe move the code below in FareRuleSet::matches() ?
            if (attribute.isTransfersSet() && attribute.getTransfers() < transfersUsed) {
                continue;
            }
            // as trimet does
            if (attribute.isTransferDurationSet() && tripTime > attribute.getTransferDuration()) {
                continue;
            }
            if (attribute.isJourneyDurationSet() && journeyTime > attribute.getJourneyDuration()) {
                continue;
            }
            float newFare = getFarePrice(attribute, fareType);
            if (newFare < bestFare) {
                bestAttribute = attribute;
                bestFare = newFare;
            }
        }
    }
    LOG.debug("{} best for {}", bestAttribute, rides);
    if (bestFare == Float.POSITIVE_INFINITY) {
        LOG.debug("No fare for a ride sequence: {}", rides);
    }
    return new FareAndId(bestFare, bestAttribute == null ? null : bestAttribute.getId());
}
Also used : FareAttribute(org.onebusaway.gtfs.model.FareAttribute) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) FareRuleSet(org.opentripplanner.routing.core.FareRuleSet) HashSet(java.util.HashSet)

Example 2 with FareAttribute

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

the class FareAtrributeAgencyTest method testAgenciesOnBartGtfs.

@Test
public void testAgenciesOnBartGtfs() throws Exception {
    GtfsRelationalDaoImpl dao = new GtfsRelationalDaoImpl();
    GtfsReader gtfsReader = new GtfsReader();
    gtfsReader.setEntityStore(dao);
    gtfsReader.setInputLocation(GtfsTestData.getBartGtfs());
    gtfsReader.run();
    final Collection<FareAttribute> fareAttributes = dao.getAllFareAttributes();
    final Set<String> agencyIdsInFareAttr = fareAttributes.stream().map(FareAttribute::getAgencyId).collect(Collectors.toSet());
    boolean isContainsBothExpectedAgencies = agencyIdsInFareAttr.contains("AirBART") && agencyIdsInFareAttr.contains("BART");
    if (!isContainsBothExpectedAgencies) {
        throw new Exception("Does not contain one of the expected agencies in the fare attributes");
    }
}
Also used : FareAttribute(org.onebusaway.gtfs.model.FareAttribute) GtfsReader(org.onebusaway.gtfs.serialization.GtfsReader) GtfsRelationalDaoImpl(org.onebusaway.gtfs.impl.GtfsRelationalDaoImpl) Test(org.junit.Test)

Example 3 with FareAttribute

use of org.onebusaway.gtfs.model.FareAttribute 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 4 with FareAttribute

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

the class AgencyMergeStrategyTest method testRenameAllAgencyIdReferences.

@Test
public void testRenameAllAgencyIdReferences() {
    GtfsRelationalDaoImpl sourceA = new GtfsRelationalDaoImpl();
    Agency agencyA = new Agency();
    agencyA.setId("1");
    agencyA.setName("Metro");
    agencyA.setUrl("http://metro.gov/");
    sourceA.saveEntity(agencyA);
    GtfsRelationalDaoImpl sourceB = new GtfsRelationalDaoImpl();
    Agency agencyB = new Agency();
    agencyB.setId("1");
    agencyA.setName("Metra");
    agencyA.setUrl("http://metra.gov/");
    sourceB.saveEntity(agencyB);
    Route route = new Route();
    route.setAgency(agencyB);
    route.setId(new AgencyAndId("1", "routeId"));
    sourceB.saveEntity(route);
    Trip trip = new Trip();
    trip.setRoute(route);
    trip.setId(new AgencyAndId("1", "tripId"));
    trip.setServiceId(new AgencyAndId("1", "serviceId"));
    trip.setShapeId(new AgencyAndId("1", "shapeId"));
    sourceB.saveEntity(trip);
    FareAttribute fare = new FareAttribute();
    fare.setId(new AgencyAndId("1", "fareId"));
    sourceB.saveEntity(fare);
    Stop stop = new Stop();
    stop.setId(new AgencyAndId("1", "stopId"));
    sourceB.saveEntity(stop);
    ServiceCalendar calendar = new ServiceCalendar();
    calendar.setServiceId(new AgencyAndId("1", "serviceId"));
    sourceB.saveEntity(calendar);
    ServiceCalendarDate calendarDate = new ServiceCalendarDate();
    calendarDate.setServiceId(new AgencyAndId("1", "serviceId"));
    sourceB.saveEntity(calendarDate);
    ShapePoint point = new ShapePoint();
    point.setShapeId(new AgencyAndId("1", "shapeId"));
    sourceB.saveEntity(point);
    _strategy.merge(context(sourceA, _target, "a-"));
    _strategy.merge(context(sourceB, _target, "b-"));
    Collection<Agency> agencies = _target.getAllAgencies();
    assertEquals(2, agencies.size());
    assertSame(agencyA, _target.getAgencyForId("1"));
    assertSame(agencyB, _target.getAgencyForId("b-1"));
    assertEquals("b-1", route.getId().getAgencyId());
    assertEquals("b-1", trip.getId().getAgencyId());
    assertEquals("b-1", trip.getServiceId().getAgencyId());
    assertEquals("b-1", trip.getShapeId().getAgencyId());
    assertEquals("b-1", fare.getId().getAgencyId());
    assertEquals("b-1", stop.getId().getAgencyId());
    assertEquals("b-1", calendar.getServiceId().getAgencyId());
    assertEquals("b-1", calendarDate.getServiceId().getAgencyId());
    assertEquals("b-1", point.getShapeId().getAgencyId());
}
Also used : FareAttribute(org.onebusaway.gtfs.model.FareAttribute) ServiceCalendarDate(org.onebusaway.gtfs.model.ServiceCalendarDate) Trip(org.onebusaway.gtfs.model.Trip) ShapePoint(org.onebusaway.gtfs.model.ShapePoint) Agency(org.onebusaway.gtfs.model.Agency) AgencyAndId(org.onebusaway.gtfs.model.AgencyAndId) Stop(org.onebusaway.gtfs.model.Stop) GtfsRelationalDaoImpl(org.onebusaway.gtfs.impl.GtfsRelationalDaoImpl) Route(org.onebusaway.gtfs.model.Route) ServiceCalendar(org.onebusaway.gtfs.model.ServiceCalendar) Test(org.junit.Test)

Example 5 with FareAttribute

use of org.onebusaway.gtfs.model.FareAttribute 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)

Aggregations

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