Search in sources :

Example 1 with FareAttribute

use of org.opentripplanner.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<>();
    Set<FeedScopedId> routes = new HashSet<>();
    Set<FeedScopedId> trips = new HashSet<>();
    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().getFeedId();
    long lastRideStartTime = firstRide.startTime;
    long lastRideEndTime = firstRide.endTime;
    for (Ride ride : rides) {
        if (!ride.firstStop.getId().getFeedId().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;
        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 (!attribute.getId().getFeedId().equals(feedId))
            continue;
        if (ruleSet.matches(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.opentripplanner.model.FareAttribute) FeedScopedId(org.opentripplanner.model.FeedScopedId) FareRuleSet(org.opentripplanner.routing.core.FareRuleSet) HashSet(java.util.HashSet)

Example 2 with FareAttribute

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

the class CalendarServiceDataFactoryImplTest method createFareAttribute.

private static FareAttribute createFareAttribute(Agency agency) {
    FareAttribute fa = new FareAttribute();
    fa.setId(new FeedScopedId(FEED_ID, "FA"));
    return fa;
}
Also used : FareAttribute(org.opentripplanner.model.FareAttribute) FeedScopedId(org.opentripplanner.model.FeedScopedId)

Example 3 with FareAttribute

use of org.opentripplanner.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<>();
    Set<FeedScopedId> routes = new HashSet<>();
    Set<FeedScopedId> trips = new HashSet<>();
    int transfersUsed = -1;
    Ride firstRide = rides.get(0);
    ZonedDateTime 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().getFeedId();
    ZonedDateTime lastRideStartTime = firstRide.startTime;
    ZonedDateTime lastRideEndTime = firstRide.endTime;
    for (Ride ride : rides) {
        if (!ride.firstStop.getId().getFeedId().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;
        routes.add(ride.route);
        zones.addAll(ride.zones);
        trips.add(ride.trip);
        transfersUsed += 1;
    }
    FareAttribute bestAttribute = null;
    float bestFare = Float.POSITIVE_INFINITY;
    Duration tripTime = Duration.between(startTime, lastRideStartTime);
    Duration journeyTime = Duration.between(startTime, lastRideEndTime);
    // 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 (!attribute.getId().getFeedId().equals(feedId))
            continue;
        if (ruleSet.matches(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.getSeconds() > attribute.getTransferDuration()) {
                continue;
            }
            if (attribute.isJourneyDurationSet() && journeyTime.getSeconds() > 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.opentripplanner.model.FareAttribute) Duration(java.time.Duration) FareRuleSet(org.opentripplanner.routing.core.FareRuleSet) ZonedDateTime(java.time.ZonedDateTime) FeedScopedId(org.opentripplanner.model.FeedScopedId) HashSet(java.util.HashSet)

Example 4 with FareAttribute

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

the class OtpTransitServiceImplTest method createFareRule.

private static FareRule createFareRule() {
    FareAttribute fa = new FareAttribute(new FeedScopedId(FEED_ID, "FA"));
    FareRule rule = new FareRule();
    rule.setOriginId("Zone A");
    rule.setContainsId("Zone B");
    rule.setDestinationId("Zone C");
    rule.setFare(fa);
    return rule;
}
Also used : FareAttribute(org.opentripplanner.model.FareAttribute) FareRule(org.opentripplanner.model.FareRule) FeedScopedId(org.opentripplanner.model.FeedScopedId)

Example 5 with FareAttribute

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

the class OtpTransitServiceBuilderTest method createFareAttribute.

private static FareAttribute createFareAttribute(Agency agency) {
    FareAttribute fa = new FareAttribute();
    fa.setId(new FeedScopedId(FEED_ID, "FA"));
    return fa;
}
Also used : FareAttribute(org.opentripplanner.model.FareAttribute) FeedScopedId(org.opentripplanner.model.FeedScopedId)

Aggregations

FareAttribute (org.opentripplanner.model.FareAttribute)8 FeedScopedId (org.opentripplanner.model.FeedScopedId)7 FareRuleSet (org.opentripplanner.routing.core.FareRuleSet)4 FareRule (org.opentripplanner.model.FareRule)3 HashSet (java.util.HashSet)2 Route (org.opentripplanner.model.Route)2 Duration (java.time.Duration)1 ZonedDateTime (java.time.ZonedDateTime)1