use of org.opentripplanner.routing.core.FareRuleSet 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;
}
use of org.opentripplanner.routing.core.FareRuleSet 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());
}
use of org.opentripplanner.routing.core.FareRuleSet in project OpenTripPlanner by opentripplanner.
the class SeattleFareServiceFactory method processGtfs.
@Override
public void processGtfs(GtfsRelationalDao dao) {
// Add custom extension: trips may have a fare ID specified in KCM GTFS.
// Need to ensure that we are scoped to feed when adding trips to FareRuleSet,
// since fare IDs may not be unique across feeds and trip agency IDsqq
// may not match fare attribute agency IDs (which are feed IDs).
Map<AgencyAndId, FareRuleSet> feedFareRules = new HashMap<>();
fillFareRules(null, dao.getAllFareAttributes(), dao.getAllFareRules(), feedFareRules);
regularFareRules.putAll(feedFareRules);
Map<String, FareRuleSet> feedFareRulesById = new HashMap<>();
for (FareRuleSet rule : regularFareRules.values()) {
String id = rule.getFareAttribute().getId().getId();
feedFareRulesById.put(id, rule);
}
for (Trip trip : dao.getAllTrips()) {
String fareId = trip.getFareId();
FareRuleSet rule = feedFareRulesById.get(fareId);
if (rule != null)
rule.addTrip(trip.getId());
}
}
use of org.opentripplanner.routing.core.FareRuleSet 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);
}
}
}
Aggregations