Skip to content

Commit

Permalink
Updated for a working initial implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
skinkie committed May 4, 2016
1 parent 72cceae commit b34e4c2
Showing 1 changed file with 29 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -143,29 +143,31 @@ private float getCostByUnits(String fareZone, int units, int prevSumUnits, Colle
}

LOG.warn("Can't find price for " + fareZone + " with " + prevSumUnits + " units");

return Float.POSITIVE_INFINITY;
}

return cost;
}

private float getEasyTripFareByLineFromTo(AgencyAndId route, String firstStop, String lastStop,
private float getEasyTripFareByLineFromTo(String route, String firstStop, String lastStop,
boolean entranceFee, Collection<FareRuleSet> fareRules) {

float cost = 0f;

for (FareRuleSet ruleSet : fareRules) {
Set<String> agencies = new HashSet<>();
agencies.add(route.getAgencyId());
float cost = Float.POSITIVE_INFINITY;

Set<AgencyAndId> routes = new HashSet<>();
routes.add(route);
String fareId = route + ":" + firstStop + "-" + lastStop;

if (ruleSet.matches(agencies, firstStop, lastStop, new HashSet<String>(), routes)) {
for (FareRuleSet ruleSet : fareRules) {
if (ruleSet.getFareAttribute().getId().getId().equals(fareId)) {
cost = ruleSet.getFareAttribute().getPrice();
break;
}
}

if (cost == Float.POSITIVE_INFINITY) {
LOG.warn("Can't find price for " + firstStop + " to " + lastStop + " operated on " + route);

LOG.warn("Can't find price for " + firstStop + " to " + lastStop + " operated on " + route.getId());
return cost;
}

if (entranceFee) cost += 89f; /* TODO: Configurable? */
Expand Down Expand Up @@ -195,15 +197,15 @@ protected float getLowestCost(FareType fareType, List<Ride> rides,
LOG.warn(String.format("%s %s %s %s %s %s", ride.startZone, ride.endZone, ride.firstStop, ride.lastStop, ride.route, ride.agency));

if (ride.agency.startsWith("IFF:")) {
LOG.warn("1. NS");
LOG.warn("1. Trains");
/* In Reizen op Saldo we will try to fares as long as possible. */

/* If our previous agency isn't this agency, then we must have checked out */
mustHaveCheckedOut |= !ride.agency.equals(lastAgencyId);

/* When a user has checked out, we first calculate the units made until then. */
if (mustHaveCheckedOut && lastAgencyId != null) {
LOG.warn("2. Must Have Checked Out");
LOG.warn("2. Must have checked out from a station");
UnitsFareZone unitsFareZone = getUnitsByZones(lastAgencyId, startTariefEenheden, endTariefEenheden, fareRules);
if (unitsFareZone == null) return Float.POSITIVE_INFINITY;
lastFareZone = unitsFareZone.fareZone;
Expand All @@ -216,6 +218,8 @@ protected float getLowestCost(FareType fareType, List<Ride> rides,
if ((alightedTariefEenheden + TRANSFER_DURATION) < ride.startTime) {
LOG.warn("3. Exceeded Transfer Time");
cost += getCostByUnits(lastFareZone, units, prevSumUnits, fareRules);
if (cost == Float.POSITIVE_INFINITY) return cost;

startTariefEenheden = ride.startZone;
units = 0;
prevSumUnits = 0;
Expand All @@ -225,6 +229,8 @@ protected float getLowestCost(FareType fareType, List<Ride> rides,
LOG.warn("4. Swiched Rail Agency");

cost += getCostByUnits(lastFareZone, units, prevSumUnits, fareRules);
if (cost == Float.POSITIVE_INFINITY) return cost;

prevSumUnits += units;
units = 0;
startTariefEenheden = ride.startZone;
Expand All @@ -244,19 +250,24 @@ protected float getLowestCost(FareType fareType, List<Ride> rides,
boolean entranceFee = ((alightedEasyTrip + TRANSFER_DURATION) < ride.startTime);

/* EasyTrip will always calculate its price per leg */
cost += getEasyTripFareByLineFromTo(ride.route, ride.firstStop.toString(), ride.lastStop.toString(), entranceFee, fareRules);
cost += getEasyTripFareByLineFromTo(ride.route.getId(), ride.startZone, ride.endZone, entranceFee, fareRules);
if (cost == Float.POSITIVE_INFINITY) return cost;

alightedEasyTrip = ride.endTime;
}
}

LOG.warn("6. Final");
UnitsFareZone unitsFareZone = getUnitsByZones(lastAgencyId, startTariefEenheden, endTariefEenheden, fareRules);
if (unitsFareZone == null) return Float.POSITIVE_INFINITY;
if (lastAgencyId != null) {
UnitsFareZone unitsFareZone = getUnitsByZones(lastAgencyId, startTariefEenheden, endTariefEenheden, fareRules);
if (unitsFareZone == null) return Float.POSITIVE_INFINITY;

lastFareZone = unitsFareZone.fareZone;
units += unitsFareZone.units;
cost += getCostByUnits(lastFareZone, units, prevSumUnits, fareRules);
}

lastFareZone = unitsFareZone.fareZone;
units += unitsFareZone.units;
cost += getCostByUnits(lastFareZone, units, prevSumUnits, fareRules);
if (cost == Float.POSITIVE_INFINITY) return cost;

return cost / 100f;
}
Expand Down

0 comments on commit b34e4c2

Please sign in to comment.