From c48856a114032fdfd44804339b36b695e41b18fb Mon Sep 17 00:00:00 2001 From: Leonard Ehrenfried Date: Tue, 30 Jan 2024 14:00:22 +0100 Subject: [PATCH 1/4] Adapt to new, de-duplicated location groups --- .../impl/HibernateGtfsRelationalDaoImpl.java | 2 +- .../onebusaway/gtfs/model/LocationGroup.java | 36 +++++++++++-------- .../gtfs/model/LocationGroupElement.java | 13 ++++--- .../GtfsEntitySchemaFactory.java | 1 + .../gtfs/serialization/GtfsReader.java | 12 +++---- .../gtfs/serialization/FlexReaderTest.java | 6 ++++ .../location_group_stops.txt | 31 ++++++++++++++++ .../auburn-transit-flex/location_groups.txt | 33 ++--------------- 8 files changed, 73 insertions(+), 61 deletions(-) create mode 100644 onebusaway-gtfs/src/test/resources/org/onebusaway/gtfs/auburn-transit-flex/location_group_stops.txt diff --git a/onebusaway-gtfs-hibernate/src/main/java/org/onebusaway/gtfs/impl/HibernateGtfsRelationalDaoImpl.java b/onebusaway-gtfs-hibernate/src/main/java/org/onebusaway/gtfs/impl/HibernateGtfsRelationalDaoImpl.java index 89f7a5881..6ddc0b329 100644 --- a/onebusaway-gtfs-hibernate/src/main/java/org/onebusaway/gtfs/impl/HibernateGtfsRelationalDaoImpl.java +++ b/onebusaway-gtfs-hibernate/src/main/java/org/onebusaway/gtfs/impl/HibernateGtfsRelationalDaoImpl.java @@ -295,7 +295,7 @@ public Collection getAllLocationGroupElements() { LocationGroupElement locationGroupElement = new LocationGroupElement(); locationGroupElement.setLocationGroupId(group.getId()); locationGroupElement.setName(group.getName()); - locationGroupElement.setLocation(stopLocation); + locationGroupElement.setStop(stopLocation); return locationGroupElement; })).collect(Collectors.toList()); } diff --git a/onebusaway-gtfs/src/main/java/org/onebusaway/gtfs/model/LocationGroup.java b/onebusaway-gtfs/src/main/java/org/onebusaway/gtfs/model/LocationGroup.java index 46ef67e98..82e897587 100644 --- a/onebusaway-gtfs/src/main/java/org/onebusaway/gtfs/model/LocationGroup.java +++ b/onebusaway-gtfs/src/main/java/org/onebusaway/gtfs/model/LocationGroup.java @@ -15,18 +15,28 @@ */ package org.onebusaway.gtfs.model; -import java.util.HashSet; +import java.util.ArrayList; +import java.util.List; import java.util.Set; +import org.onebusaway.csv_entities.schema.annotations.CsvField; +import org.onebusaway.csv_entities.schema.annotations.CsvFields; +import org.onebusaway.gtfs.serialization.mappings.DefaultAgencyIdFieldMappingFactory; +@CsvFields(filename = "location_groups.txt", required = false) public class LocationGroup extends IdentityBean implements StopLocation { private static final long serialVersionUID = 1L; + @CsvField(name = "location_group_id", mapping = DefaultAgencyIdFieldMappingFactory.class) private AgencyAndId id; - private Set locations = new HashSet<>(); - + @CsvField(name = "location_group_name") private String name; + // we use a List, not Set to keep the insertion order. by definition these stops don't have an + // order but it's nice for clients to not randomly change it. + @CsvField(ignore = true) + private List stops = new ArrayList<>(); + @Override public AgencyAndId getId() { return id; @@ -36,18 +46,6 @@ public void setId(AgencyAndId id) { this.id = id; } - public Set getLocations() { - return locations; - } - - private void setLocations(Set locations) { - this.locations = locations; - } - - public void addLocation(StopLocation location) { - this.locations.add(location); - } - public String getName() { return name; } @@ -55,4 +53,12 @@ public String getName() { public void setName(String name) { this.name = name; } + + public void addStop(StopLocation stop) { + stops.add(stop); + } + + public Set getStops() { + return Set.copyOf(stops); + } } diff --git a/onebusaway-gtfs/src/main/java/org/onebusaway/gtfs/model/LocationGroupElement.java b/onebusaway-gtfs/src/main/java/org/onebusaway/gtfs/model/LocationGroupElement.java index f12f17c08..4cbfd5293 100644 --- a/onebusaway-gtfs/src/main/java/org/onebusaway/gtfs/model/LocationGroupElement.java +++ b/onebusaway-gtfs/src/main/java/org/onebusaway/gtfs/model/LocationGroupElement.java @@ -20,19 +20,18 @@ import org.onebusaway.gtfs.serialization.mappings.DefaultAgencyIdFieldMappingFactory; import org.onebusaway.gtfs.serialization.mappings.StopLocationFieldMappingFactory; -@CsvFields(filename = "location_groups.txt", required = false, prefix = "location_group_") +@CsvFields(filename = "location_groups_stops.txt", required = false, prefix = "location_group_") public class LocationGroupElement extends IdentityBean { private static final long serialVersionUID = 1L; - @CsvField(ignore = true) private int id; @CsvField(name = "location_group_id", mapping = DefaultAgencyIdFieldMappingFactory.class) private AgencyAndId locationGroupId; @CsvField(name = "stop_id", mapping = StopLocationFieldMappingFactory.class) - private StopLocation location; + private StopLocation stop; @CsvField(optional = true) private String name; @@ -54,12 +53,12 @@ public void setLocationGroupId(AgencyAndId locationGroupId) { this.locationGroupId = locationGroupId; } - public StopLocation getLocation() { - return location; + public StopLocation getStop() { + return stop; } - public void setLocation(StopLocation location) { - this.location = location; + public void setStop(StopLocation stop) { + this.stop = stop; } public String getName() { diff --git a/onebusaway-gtfs/src/main/java/org/onebusaway/gtfs/serialization/GtfsEntitySchemaFactory.java b/onebusaway-gtfs/src/main/java/org/onebusaway/gtfs/serialization/GtfsEntitySchemaFactory.java index 8250536fe..f246718a9 100644 --- a/onebusaway-gtfs/src/main/java/org/onebusaway/gtfs/serialization/GtfsEntitySchemaFactory.java +++ b/onebusaway-gtfs/src/main/java/org/onebusaway/gtfs/serialization/GtfsEntitySchemaFactory.java @@ -47,6 +47,7 @@ public static List> getEntityClasses() { entityClasses.add(Level.class); entityClasses.add(Stop.class); entityClasses.add(StopAreaElement.class); + entityClasses.add(LocationGroup.class); entityClasses.add(LocationGroupElement.class); entityClasses.add(Trip.class); entityClasses.add(Note.class); diff --git a/onebusaway-gtfs/src/main/java/org/onebusaway/gtfs/serialization/GtfsReader.java b/onebusaway-gtfs/src/main/java/org/onebusaway/gtfs/serialization/GtfsReader.java index 1168c09f7..963c388c8 100644 --- a/onebusaway-gtfs/src/main/java/org/onebusaway/gtfs/serialization/GtfsReader.java +++ b/onebusaway-gtfs/src/main/java/org/onebusaway/gtfs/serialization/GtfsReader.java @@ -74,6 +74,7 @@ public GtfsReader() { _entityClasses.add(Level.class); _entityClasses.add(Stop.class); _entityClasses.add(Location.class); + _entityClasses.add(LocationGroup.class); _entityClasses.add(LocationGroupElement.class); _entityClasses.add(Trip.class); _entityClasses.add(StopAreaElement.class); @@ -360,16 +361,13 @@ public void handleEntity(Object entity) { } else if (entity instanceof Location) { Location location = (Location) entity; registerAgencyId(Location.class, location.getId()); + } else if (entity instanceof LocationGroup) { + var group = (LocationGroup) entity; + registerAgencyId(LocationGroup.class, group.getId()); } else if (entity instanceof LocationGroupElement) { LocationGroupElement locationGroupElement = (LocationGroupElement) entity; LocationGroup locationGroup = _entityStore.getEntityForId(LocationGroup.class, locationGroupElement.getLocationGroupId()); - if (locationGroup == null) { - locationGroup = new LocationGroup(); - locationGroup.setId(locationGroupElement.getLocationGroupId()); - locationGroup.setName(locationGroupElement.getName()); - _entityStore.saveEntity(locationGroup); - } - locationGroup.addLocation(locationGroupElement.getLocation()); + locationGroup.addStop(locationGroupElement.getStop()); } else if (entity instanceof StopAreaElement) { var stopAreaElement = (StopAreaElement) entity; var stopArea = _entityStore.getEntityForId(StopArea.class, stopAreaElement.getArea().getId()); diff --git a/onebusaway-gtfs/src/test/java/org/onebusaway/gtfs/serialization/FlexReaderTest.java b/onebusaway-gtfs/src/test/java/org/onebusaway/gtfs/serialization/FlexReaderTest.java index 79557807a..dd4566072 100644 --- a/onebusaway-gtfs/src/test/java/org/onebusaway/gtfs/serialization/FlexReaderTest.java +++ b/onebusaway-gtfs/src/test/java/org/onebusaway/gtfs/serialization/FlexReaderTest.java @@ -105,6 +105,12 @@ public void locationIdAsASeparateColumn() throws CsvEntityIOException, IOExcepti @Test public void locationGroupIdAsSeparateColumn() throws CsvEntityIOException, IOException { var dao = processFeed(GtfsTestData.getAuburnTransitFlex(), AGENCY_ID, false); + var locationGroup = List.copyOf(dao.getAllLocationGroups()).get(0); + assertEquals("Aurburn Loop Stops", locationGroup.getName()); + assertEquals("1_4230479", locationGroup.getId().toString()); + assertEquals(Set.of("1_4230479"), locationGroup.getStops().stream().map(s -> s.getId().toString()).collect( + Collectors.toSet())); + var trip = dao.getAllTrips().stream().filter(t -> t.getId().getId().equals("t_5756013_b_33000_tn_0")).findAny().get(); var stopTimes = dao.getStopTimesForTrip(trip); stopTimes.forEach(st -> assertNotNull(st.getStopLocation())); diff --git a/onebusaway-gtfs/src/test/resources/org/onebusaway/gtfs/auburn-transit-flex/location_group_stops.txt b/onebusaway-gtfs/src/test/resources/org/onebusaway/gtfs/auburn-transit-flex/location_group_stops.txt new file mode 100644 index 000000000..a2c6ecce9 --- /dev/null +++ b/onebusaway-gtfs/src/test/resources/org/onebusaway/gtfs/auburn-transit-flex/location_group_stops.txt @@ -0,0 +1,31 @@ +location_group_id,stop_id +4230479,2583236 +4230479,2583237 +4230479,2583238 +4230479,2583242 +4230479,2583244 +4230479,2583246 +4230479,2583249 +4230479,2583250 +4230479,2583251 +4230479,2583252 +4230479,2583253 +4230479,2583254 +4230479,2583255 +4230479,2583256 +4230479,2583259 +4230479,2583260 +4230479,2583262 +4230479,2583263 +4230479,2583266 +4230479,2583268 +4230479,2583271 +4230479,2583276 +4230479,2583280 +4230479,2583281 +4230479,2583282 +4230479,2583284 +4230479,2583285 +4230479,2751414 +4230479,3446932 +4230479,3446933 diff --git a/onebusaway-gtfs/src/test/resources/org/onebusaway/gtfs/auburn-transit-flex/location_groups.txt b/onebusaway-gtfs/src/test/resources/org/onebusaway/gtfs/auburn-transit-flex/location_groups.txt index 7fcf9b6d2..b9eba1b72 100644 --- a/onebusaway-gtfs/src/test/resources/org/onebusaway/gtfs/auburn-transit-flex/location_groups.txt +++ b/onebusaway-gtfs/src/test/resources/org/onebusaway/gtfs/auburn-transit-flex/location_groups.txt @@ -1,31 +1,2 @@ -location_group_id,stop_id,location_group_name -4230479,2583236,Aurburn Loop Stops -4230479,2583237,Aurburn Loop Stops -4230479,2583238,Aurburn Loop Stops -4230479,2583242,Aurburn Loop Stops -4230479,2583244,Aurburn Loop Stops -4230479,2583246,Aurburn Loop Stops -4230479,2583249,Aurburn Loop Stops -4230479,2583250,Aurburn Loop Stops -4230479,2583251,Aurburn Loop Stops -4230479,2583252,Aurburn Loop Stops -4230479,2583253,Aurburn Loop Stops -4230479,2583254,Aurburn Loop Stops -4230479,2583255,Aurburn Loop Stops -4230479,2583256,Aurburn Loop Stops -4230479,2583259,Aurburn Loop Stops -4230479,2583260,Aurburn Loop Stops -4230479,2583262,Aurburn Loop Stops -4230479,2583263,Aurburn Loop Stops -4230479,2583266,Aurburn Loop Stops -4230479,2583268,Aurburn Loop Stops -4230479,2583271,Aurburn Loop Stops -4230479,2583276,Aurburn Loop Stops -4230479,2583280,Aurburn Loop Stops -4230479,2583281,Aurburn Loop Stops -4230479,2583282,Aurburn Loop Stops -4230479,2583284,Aurburn Loop Stops -4230479,2583285,Aurburn Loop Stops -4230479,2751414,Aurburn Loop Stops -4230479,3446932,Aurburn Loop Stops -4230479,3446933,Aurburn Loop Stops +location_group_id,location_group_name +4230479,Aurburn Loop Stops \ No newline at end of file From 2ed2380b82191f93a1769eb6d19e2609390b0bf9 Mon Sep 17 00:00:00 2001 From: Leonard Ehrenfried Date: Tue, 30 Jan 2024 14:22:22 +0100 Subject: [PATCH 2/4] Use correct file name --- .../java/org/onebusaway/gtfs/model/LocationGroupElement.java | 2 +- .../org/onebusaway/gtfs/serialization/FlexReaderTest.java | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/onebusaway-gtfs/src/main/java/org/onebusaway/gtfs/model/LocationGroupElement.java b/onebusaway-gtfs/src/main/java/org/onebusaway/gtfs/model/LocationGroupElement.java index 4cbfd5293..2e508c8cd 100644 --- a/onebusaway-gtfs/src/main/java/org/onebusaway/gtfs/model/LocationGroupElement.java +++ b/onebusaway-gtfs/src/main/java/org/onebusaway/gtfs/model/LocationGroupElement.java @@ -20,7 +20,7 @@ import org.onebusaway.gtfs.serialization.mappings.DefaultAgencyIdFieldMappingFactory; import org.onebusaway.gtfs.serialization.mappings.StopLocationFieldMappingFactory; -@CsvFields(filename = "location_groups_stops.txt", required = false, prefix = "location_group_") +@CsvFields(filename = "location_group_stops.txt", required = false, prefix = "location_group_") public class LocationGroupElement extends IdentityBean { private static final long serialVersionUID = 1L; diff --git a/onebusaway-gtfs/src/test/java/org/onebusaway/gtfs/serialization/FlexReaderTest.java b/onebusaway-gtfs/src/test/java/org/onebusaway/gtfs/serialization/FlexReaderTest.java index dd4566072..316094cb5 100644 --- a/onebusaway-gtfs/src/test/java/org/onebusaway/gtfs/serialization/FlexReaderTest.java +++ b/onebusaway-gtfs/src/test/java/org/onebusaway/gtfs/serialization/FlexReaderTest.java @@ -108,8 +108,8 @@ public void locationGroupIdAsSeparateColumn() throws CsvEntityIOException, IOExc var locationGroup = List.copyOf(dao.getAllLocationGroups()).get(0); assertEquals("Aurburn Loop Stops", locationGroup.getName()); assertEquals("1_4230479", locationGroup.getId().toString()); - assertEquals(Set.of("1_4230479"), locationGroup.getStops().stream().map(s -> s.getId().toString()).collect( - Collectors.toSet())); + var actualStops = locationGroup.getStops().stream().map(s -> s.getId().toString()).collect(Collectors.toList()); + assertEquals(30, actualStops.size()); var trip = dao.getAllTrips().stream().filter(t -> t.getId().getId().equals("t_5756013_b_33000_tn_0")).findAny().get(); var stopTimes = dao.getStopTimesForTrip(trip); From 5039d899d597eb2b25be4b8f08101d7addf40e59 Mon Sep 17 00:00:00 2001 From: Leonard Ehrenfried Date: Tue, 30 Jan 2024 14:27:46 +0100 Subject: [PATCH 3/4] Fix rename --- .../onebusaway/gtfs/impl/HibernateGtfsRelationalDaoImpl.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/onebusaway-gtfs-hibernate/src/main/java/org/onebusaway/gtfs/impl/HibernateGtfsRelationalDaoImpl.java b/onebusaway-gtfs-hibernate/src/main/java/org/onebusaway/gtfs/impl/HibernateGtfsRelationalDaoImpl.java index 6ddc0b329..0b9fac66e 100644 --- a/onebusaway-gtfs-hibernate/src/main/java/org/onebusaway/gtfs/impl/HibernateGtfsRelationalDaoImpl.java +++ b/onebusaway-gtfs-hibernate/src/main/java/org/onebusaway/gtfs/impl/HibernateGtfsRelationalDaoImpl.java @@ -291,7 +291,7 @@ public Collection getAllAreas() { @Override public Collection getAllLocationGroupElements() { Collection groups = _ops.find("FROM LocationGroup"); - return groups.stream().flatMap(group -> group.getLocations().stream().map(stopLocation -> { + return groups.stream().flatMap(group -> group.getStops().stream().map(stopLocation -> { LocationGroupElement locationGroupElement = new LocationGroupElement(); locationGroupElement.setLocationGroupId(group.getId()); locationGroupElement.setName(group.getName()); From 266852aca5b05b68c2a994ee4d3f9e35f1415ce2 Mon Sep 17 00:00:00 2001 From: Leonard Ehrenfried Date: Tue, 30 Jan 2024 14:36:34 +0100 Subject: [PATCH 4/4] Make names conform to hibernate expectations --- .../gtfs/impl/HibernateGtfsRelationalDaoImpl.java | 2 +- .../java/org/onebusaway/gtfs/model/LocationGroup.java | 8 ++++++-- .../org/onebusaway/gtfs/serialization/GtfsReader.java | 2 +- .../org/onebusaway/gtfs/serialization/FlexReaderTest.java | 2 +- 4 files changed, 9 insertions(+), 5 deletions(-) diff --git a/onebusaway-gtfs-hibernate/src/main/java/org/onebusaway/gtfs/impl/HibernateGtfsRelationalDaoImpl.java b/onebusaway-gtfs-hibernate/src/main/java/org/onebusaway/gtfs/impl/HibernateGtfsRelationalDaoImpl.java index 0b9fac66e..6ddc0b329 100644 --- a/onebusaway-gtfs-hibernate/src/main/java/org/onebusaway/gtfs/impl/HibernateGtfsRelationalDaoImpl.java +++ b/onebusaway-gtfs-hibernate/src/main/java/org/onebusaway/gtfs/impl/HibernateGtfsRelationalDaoImpl.java @@ -291,7 +291,7 @@ public Collection getAllAreas() { @Override public Collection getAllLocationGroupElements() { Collection groups = _ops.find("FROM LocationGroup"); - return groups.stream().flatMap(group -> group.getStops().stream().map(stopLocation -> { + return groups.stream().flatMap(group -> group.getLocations().stream().map(stopLocation -> { LocationGroupElement locationGroupElement = new LocationGroupElement(); locationGroupElement.setLocationGroupId(group.getId()); locationGroupElement.setName(group.getName()); diff --git a/onebusaway-gtfs/src/main/java/org/onebusaway/gtfs/model/LocationGroup.java b/onebusaway-gtfs/src/main/java/org/onebusaway/gtfs/model/LocationGroup.java index 82e897587..b7c884dcb 100644 --- a/onebusaway-gtfs/src/main/java/org/onebusaway/gtfs/model/LocationGroup.java +++ b/onebusaway-gtfs/src/main/java/org/onebusaway/gtfs/model/LocationGroup.java @@ -16,6 +16,7 @@ package org.onebusaway.gtfs.model; import java.util.ArrayList; +import java.util.Collection; import java.util.List; import java.util.Set; import org.onebusaway.csv_entities.schema.annotations.CsvField; @@ -54,11 +55,14 @@ public void setName(String name) { this.name = name; } - public void addStop(StopLocation stop) { + public void addLocation(StopLocation stop) { stops.add(stop); } + public void setLocations(Collection stop) { + stops.addAll(stop); + } - public Set getStops() { + public Set getLocations() { return Set.copyOf(stops); } } diff --git a/onebusaway-gtfs/src/main/java/org/onebusaway/gtfs/serialization/GtfsReader.java b/onebusaway-gtfs/src/main/java/org/onebusaway/gtfs/serialization/GtfsReader.java index 963c388c8..2cb17d277 100644 --- a/onebusaway-gtfs/src/main/java/org/onebusaway/gtfs/serialization/GtfsReader.java +++ b/onebusaway-gtfs/src/main/java/org/onebusaway/gtfs/serialization/GtfsReader.java @@ -367,7 +367,7 @@ public void handleEntity(Object entity) { } else if (entity instanceof LocationGroupElement) { LocationGroupElement locationGroupElement = (LocationGroupElement) entity; LocationGroup locationGroup = _entityStore.getEntityForId(LocationGroup.class, locationGroupElement.getLocationGroupId()); - locationGroup.addStop(locationGroupElement.getStop()); + locationGroup.addLocation(locationGroupElement.getStop()); } else if (entity instanceof StopAreaElement) { var stopAreaElement = (StopAreaElement) entity; var stopArea = _entityStore.getEntityForId(StopArea.class, stopAreaElement.getArea().getId()); diff --git a/onebusaway-gtfs/src/test/java/org/onebusaway/gtfs/serialization/FlexReaderTest.java b/onebusaway-gtfs/src/test/java/org/onebusaway/gtfs/serialization/FlexReaderTest.java index 316094cb5..880377d60 100644 --- a/onebusaway-gtfs/src/test/java/org/onebusaway/gtfs/serialization/FlexReaderTest.java +++ b/onebusaway-gtfs/src/test/java/org/onebusaway/gtfs/serialization/FlexReaderTest.java @@ -108,7 +108,7 @@ public void locationGroupIdAsSeparateColumn() throws CsvEntityIOException, IOExc var locationGroup = List.copyOf(dao.getAllLocationGroups()).get(0); assertEquals("Aurburn Loop Stops", locationGroup.getName()); assertEquals("1_4230479", locationGroup.getId().toString()); - var actualStops = locationGroup.getStops().stream().map(s -> s.getId().toString()).collect(Collectors.toList()); + var actualStops = locationGroup.getLocations().stream().map(s -> s.getId().toString()).collect(Collectors.toList()); assertEquals(30, actualStops.size()); var trip = dao.getAllTrips().stream().filter(t -> t.getId().getId().equals("t_5756013_b_33000_tn_0")).findAny().get();