From 0972ef8cae39e1925abd3af1a1a58059230bcd25 Mon Sep 17 00:00:00 2001 From: Stefan de Konink Date: Sat, 13 Jul 2013 01:17:53 +0200 Subject: [PATCH 1/4] Implements the framework for distance based fare calculation to be used in OpenTripPlanner. --- .../strategies/FareRuleMergeStrategy.java | 3 ++ .../onebusaway/gtfs/model/FareAttribute.java | 48 +++++++++++++++++++ .../org/onebusaway/gtfs/model/FareRule.java | 12 +++++ 3 files changed, 63 insertions(+) diff --git a/onebusaway-gtfs-merge/src/main/java/org/onebusaway/gtfs_merge/strategies/FareRuleMergeStrategy.java b/onebusaway-gtfs-merge/src/main/java/org/onebusaway/gtfs_merge/strategies/FareRuleMergeStrategy.java index 152cbc1dc..ef6b7038b 100644 --- a/onebusaway-gtfs-merge/src/main/java/org/onebusaway/gtfs_merge/strategies/FareRuleMergeStrategy.java +++ b/onebusaway-gtfs-merge/src/main/java/org/onebusaway/gtfs_merge/strategies/FareRuleMergeStrategy.java @@ -46,6 +46,9 @@ protected boolean entitiesAreIdentical(FareRule fareRuleA, FareRule fareRuleB) { if (!equals(fareRuleA.getContainsId(), fareRuleB.getContainsId())) { return false; } + if (!equals(fareRuleA.getUnitsTraveled(), fareRuleB.getUnitsTraveled())) { + return false; + } return true; } diff --git a/onebusaway-gtfs/src/main/java/org/onebusaway/gtfs/model/FareAttribute.java b/onebusaway-gtfs/src/main/java/org/onebusaway/gtfs/model/FareAttribute.java index 358612b5f..44d76c283 100644 --- a/onebusaway-gtfs/src/main/java/org/onebusaway/gtfs/model/FareAttribute.java +++ b/onebusaway-gtfs/src/main/java/org/onebusaway/gtfs/model/FareAttribute.java @@ -47,6 +47,15 @@ public final class FareAttribute extends IdentityBean { @CsvField(optional = true) private int journeyDuration = MISSING_VALUE; + @CsvField(name = "distance_mode", optional = true, defaultValue = "0") + private int distanceMode = MISSING_VALUE; + + @CsvField(name = "distance_unit_price", optional = true) + private float distanceUnitPrice = MISSING_VALUE; + + @CsvField(name = "distance_unit_start_offset", optional = true) + private float distanceUnitStartOffset = MISSING_VALUE; + public FareAttribute() { } @@ -59,6 +68,9 @@ public FareAttribute(FareAttribute fa) { this.transfers = fa.transfers; this.transferDuration = fa.transferDuration; this.journeyDuration = fa.journeyDuration; + this.distanceMode = fa.distanceMode; + this.distanceUnitPrice = fa.distanceUnitPrice; + this.distanceUnitStartOffset = fa.distanceUnitStartOffset; } @Override @@ -143,6 +155,42 @@ public void clearJourneyDuration() { this.journeyDuration = MISSING_VALUE; } + public int getDistanceMode() { + return distanceMode; + } + + public void setDistanceMode(int distanceMode) { + this.distanceMode = distanceMode; + } + + public void clearDistanceMode() { + this.distanceMode = MISSING_VALUE; + } + + public float getDistanceUnitPrice() { + return distanceUnitPrice; + } + + public void setDistanceUnitPrice(float distanceUnitPrice) { + this.distanceUnitPrice = distanceUnitPrice; + } + + public void clearDistanceUnitPrice() { + this.distanceUnitPrice = MISSING_VALUE; + } + + public float getDistanceUnitStartOffset() { + return distanceUnitStartOffset; + } + + public void setDistanceUnitStartOffset(float distanceUnitStartOffset) { + this.distanceUnitStartOffset = distanceUnitStartOffset; + } + + public void clearDistanceUnitStartOffset() { + this.distanceUnitStartOffset = MISSING_VALUE; + } + public String toString() { return ""; } diff --git a/onebusaway-gtfs/src/main/java/org/onebusaway/gtfs/model/FareRule.java b/onebusaway-gtfs/src/main/java/org/onebusaway/gtfs/model/FareRule.java index 797e54701..7070c7e52 100644 --- a/onebusaway-gtfs/src/main/java/org/onebusaway/gtfs/model/FareRule.java +++ b/onebusaway-gtfs/src/main/java/org/onebusaway/gtfs/model/FareRule.java @@ -42,6 +42,9 @@ public final class FareRule extends IdentityBean { @CsvField(optional = true) private String containsId; + @CsvField(name = "units_traveled", optional = true) + private float unitsTraveled; + public FareRule() { } @@ -53,6 +56,7 @@ public FareRule(FareRule fr) { this.originId = fr.originId; this.destinationId = fr.destinationId; this.containsId = fr.containsId; + this.unitsTraveled = fr.unitsTraveled; } @Override @@ -105,6 +109,14 @@ public void setContainsId(String containsId) { this.containsId = containsId; } + public float getUnitsTraveled() { + return unitsTraveled; + } + + public void setUnitsTraveled(float unitsTraveled) { + this.unitsTraveled = unitsTraveled; + } + public String toString() { return ""; } From 4620fc0dd280a34c5f535ec5f4a246ee9d2cb8fd Mon Sep 17 00:00:00 2001 From: Stefan de Konink Date: Sat, 13 Jul 2013 12:48:30 +0200 Subject: [PATCH 2/4] Extending the GTFS Hibernate Mapping --- .../org/onebusaway/gtfs/model/GtfsMapping.hibernate.xml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/onebusaway-gtfs-hibernate/src/main/resources/org/onebusaway/gtfs/model/GtfsMapping.hibernate.xml b/onebusaway-gtfs-hibernate/src/main/resources/org/onebusaway/gtfs/model/GtfsMapping.hibernate.xml index 7dc9c6fa7..530cd9773 100644 --- a/onebusaway-gtfs-hibernate/src/main/resources/org/onebusaway/gtfs/model/GtfsMapping.hibernate.xml +++ b/onebusaway-gtfs-hibernate/src/main/resources/org/onebusaway/gtfs/model/GtfsMapping.hibernate.xml @@ -44,6 +44,9 @@ + + + @@ -61,6 +64,7 @@ + From 3712effb4b077e5cf0a831256eac4c365a83854d Mon Sep 17 00:00:00 2001 From: Stefan de Konink Date: Sat, 13 Jul 2013 12:54:08 +0200 Subject: [PATCH 3/4] Adding a Unit Test --- .../gtfs/serialization/GtfsReaderTest.java | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/onebusaway-gtfs/src/test/java/org/onebusaway/gtfs/serialization/GtfsReaderTest.java b/onebusaway-gtfs/src/test/java/org/onebusaway/gtfs/serialization/GtfsReaderTest.java index e0b837e0e..9a6e9ed85 100644 --- a/onebusaway-gtfs/src/test/java/org/onebusaway/gtfs/serialization/GtfsReaderTest.java +++ b/onebusaway-gtfs/src/test/java/org/onebusaway/gtfs/serialization/GtfsReaderTest.java @@ -99,11 +99,11 @@ public void testAllFields() throws IOException { "WEEK,20120304,2"); gtfs.putLines( "fare_attributes.txt", - "fare_id,price,currency_type,payment_method,transfers,transfer_duration,journey_duration", - "FA1,2.0,USD,1,2,60,61"); + "fare_id,price,currency_type,payment_method,transfers,transfer_duration,journey_duration,distance_mode,distance_unit_price,distance_unit_start_offset", + "FA1,2.0,USD,1,2,60,61,1,2.00,3.0"); gtfs.putLines("fare_rules.txt", - "fare_id,route_id,origin_id,destination_id,contains_id", - "FA1,R1,Z1,Z2,Z3"); + "fare_id,route_id,origin_id,destination_id,contains_id,units_traveled", + "FA1,R1,Z1,Z2,Z3,1.0"); gtfs.putLines( "shapes.txt", "shape_id,shape_pt_sequence,shape_pt_lat,shape_pt_lon,shape_dist_traveled", @@ -219,6 +219,9 @@ public void testAllFields() throws IOException { assertEquals(2, fareAttribute.getTransfers()); assertEquals(60, fareAttribute.getTransferDuration()); assertEquals(61, fareAttribute.getJourneyDuration()); + assertEquals(1, fareAttribute.getDistanceMode()); + assertEquals(2.00, fareAttribute.getDistanceUnitPrice()); + assertEquals(3.0, fareAttribute.getDistanceUnitStartOffset()); List rules = dao.getFareRulesForFareAttribute(fareAttribute); FareRule fareRule = rules.get(0); @@ -227,6 +230,7 @@ public void testAllFields() throws IOException { assertEquals("Z1", fareRule.getOriginId()); assertEquals("Z2", fareRule.getDestinationId()); assertEquals("Z3", fareRule.getContainsId()); + assertEquals(1.0, fareRule.getUnitsTraveled()); List shapePoints = dao.getShapePointsForShapeId(new AgencyAndId( "1", "SHP1")); From a5911719af2153daa95fd08c7702848c25b3278a Mon Sep 17 00:00:00 2001 From: Stefan de Konink Date: Sat, 13 Jul 2013 13:08:58 +0200 Subject: [PATCH 4/4] Fixing the Unit Test for floats --- .../org/onebusaway/gtfs/serialization/GtfsReaderTest.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/onebusaway-gtfs/src/test/java/org/onebusaway/gtfs/serialization/GtfsReaderTest.java b/onebusaway-gtfs/src/test/java/org/onebusaway/gtfs/serialization/GtfsReaderTest.java index 9a6e9ed85..430d71aa5 100644 --- a/onebusaway-gtfs/src/test/java/org/onebusaway/gtfs/serialization/GtfsReaderTest.java +++ b/onebusaway-gtfs/src/test/java/org/onebusaway/gtfs/serialization/GtfsReaderTest.java @@ -220,8 +220,8 @@ public void testAllFields() throws IOException { assertEquals(60, fareAttribute.getTransferDuration()); assertEquals(61, fareAttribute.getJourneyDuration()); assertEquals(1, fareAttribute.getDistanceMode()); - assertEquals(2.00, fareAttribute.getDistanceUnitPrice()); - assertEquals(3.0, fareAttribute.getDistanceUnitStartOffset()); + assertEquals(2.00, fareAttribute.getDistanceUnitPrice(), 1.0); + assertEquals(3.0, fareAttribute.getDistanceUnitStartOffset(), 1.0); List rules = dao.getFareRulesForFareAttribute(fareAttribute); FareRule fareRule = rules.get(0); @@ -230,7 +230,7 @@ public void testAllFields() throws IOException { assertEquals("Z1", fareRule.getOriginId()); assertEquals("Z2", fareRule.getDestinationId()); assertEquals("Z3", fareRule.getContainsId()); - assertEquals(1.0, fareRule.getUnitsTraveled()); + assertEquals(1.0, fareRule.getUnitsTraveled(), 1.0); List shapePoints = dao.getShapePointsForShapeId(new AgencyAndId( "1", "SHP1"));