This repository was archived by the owner on May 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 42
WIP - Fix #154 start time of freq based trip should be immutable. #336
Open
scrudden
wants to merge
7
commits into
CUTR-at-USF:master
Choose a base branch
from
scrudden:E053
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7f21b52
Implementation of E053 check.
scrudden 95f03b1
Now uses previousFeedMessage parameter rather than a member hashmap.
scrudden 27623ef
Fix NPE. Previous feed is set to null for other tests so need to chec…
scrudden 1e05604
Revisions based on pull request comments from Sean Barbeau.
scrudden 4761991
Added hashmaps for tripupdates.
scrudden 0eb5739
Fix build. Remove import.
scrudden 9b93487
Remove hashmap for current message and add new logic.
scrudden File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,6 +17,8 @@ | |
| package edu.usf.cutr.gtfsrtvalidator.lib.validation.rules; | ||
|
|
||
| import com.google.transit.realtime.GtfsRealtime; | ||
| import com.google.transit.realtime.GtfsRealtime.TripUpdate; | ||
|
|
||
| import edu.usf.cutr.gtfsrtvalidator.lib.model.MessageLogModel; | ||
| import edu.usf.cutr.gtfsrtvalidator.lib.model.OccurrenceModel; | ||
| import edu.usf.cutr.gtfsrtvalidator.lib.model.helper.ErrorListHelperModel; | ||
|
|
@@ -26,8 +28,9 @@ | |
| import org.onebusaway.gtfs.services.GtfsMutableDao; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.text.ParseException; | ||
| import java.util.*; | ||
| import java.text.SimpleDateFormat; | ||
|
|
||
| import static edu.usf.cutr.gtfsrtvalidator.lib.validation.ValidationRules.*; | ||
|
|
||
|
|
@@ -37,6 +40,7 @@ | |
| * E006 - Missing required vehicle_position trip field for frequency-based exact_times = 0 | ||
| * E013 - Frequency type 0 trip schedule_relationship should be UNSCHEDULED or empty | ||
| * W005 - Missing vehicle_id in trip_update for frequency-based exact_times = 0 | ||
| * E053 - An exact_times = 0 frequencies.txt trip should have the same start_time through it's trip | ||
| */ | ||
| public class FrequencyTypeZeroValidator implements FeedEntityValidator { | ||
|
|
||
|
|
@@ -47,9 +51,22 @@ public List<ErrorListHelperModel> validate(long currentTimeMillis, GtfsMutableDa | |
| List<OccurrenceModel> errorListE006 = new ArrayList<>(); | ||
| List<OccurrenceModel> errorListE013 = new ArrayList<>(); | ||
| List<OccurrenceModel> errorListW005 = new ArrayList<>(); | ||
| List<OccurrenceModel> errorListE053 = new ArrayList<>(); | ||
|
|
||
| HashMap<TripUpdateKey, List<TripUpdate>> previousTripUpdates = new HashMap<TripUpdateKey, List<TripUpdate>>(); | ||
|
|
||
| if (previousFeedMessage != null) { | ||
| for (GtfsRealtime.FeedEntity entity : previousFeedMessage.getEntityList()) { | ||
| if (entity.hasTripUpdate()) { | ||
| GtfsRealtime.TripUpdate tripUpdate = entity.getTripUpdate(); | ||
| addTripUpdate(previousTripUpdates, tripUpdate); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| for (GtfsRealtime.FeedEntity entity : feedMessage.getEntityList()) { | ||
| if (entity.hasTripUpdate()) { | ||
|
|
||
| GtfsRealtime.TripUpdate tripUpdate = entity.getTripUpdate(); | ||
|
|
||
| if (gtfsMetadata.getExactTimesZeroTripIds().contains(tripUpdate.getTrip().getTripId())) { | ||
|
|
@@ -75,9 +92,11 @@ public List<ErrorListHelperModel> validate(long currentTimeMillis, GtfsMutableDa | |
| // W005 - Missing vehicle_id in trip_update for frequency-based exact_times = 0 | ||
| RuleUtils.addOccurrence(W005, "trip_id " + tripUpdate.getTrip().getTripId(), errorListW005, _log); | ||
| } | ||
|
|
||
| if(previousFeedMessage!=null) | ||
| checkE053(tripUpdate, previousTripUpdates, errorListE053); | ||
| } | ||
| } | ||
|
|
||
| if (entity.hasVehicle()) { | ||
| GtfsRealtime.VehiclePosition vehiclePosition = entity.getVehicle(); | ||
| if (vehiclePosition.hasTrip() && | ||
|
|
@@ -109,6 +128,7 @@ public List<ErrorListHelperModel> validate(long currentTimeMillis, GtfsMutableDa | |
| } | ||
| } | ||
| } | ||
|
|
||
| List<ErrorListHelperModel> errors = new ArrayList<>(); | ||
| if (!errorListE006.isEmpty()) { | ||
| errors.add(new ErrorListHelperModel(new MessageLogModel(E006), errorListE006)); | ||
|
|
@@ -119,6 +139,126 @@ public List<ErrorListHelperModel> validate(long currentTimeMillis, GtfsMutableDa | |
| if (!errorListW005.isEmpty()) { | ||
| errors.add(new ErrorListHelperModel(new MessageLogModel(W005), errorListW005)); | ||
| } | ||
| if (!errorListE053.isEmpty()) { | ||
| errors.add(new ErrorListHelperModel(new MessageLogModel(E053), errorListE053)); | ||
| } | ||
| return errors; | ||
|
|
||
| } | ||
|
|
||
| private void checkE053(TripUpdate currentTripUpdate, HashMap<TripUpdateKey, List<TripUpdate>> previousTripUpdates, List<OccurrenceModel> errorListE053) { | ||
|
|
||
| if (!inTripUpdates(currentTripUpdate, previousTripUpdates)) { | ||
| if (!isNewTrip(previousTripUpdates, currentTripUpdate)) { | ||
| String errorMessage = "vehicle_id " + currentTripUpdate.getVehicle().getId() + " startTime has changed and is now " + currentTripUpdate.getTrip().getStartTime(); | ||
| RuleUtils.addOccurrence(E053, errorMessage, errorListE053, _log); | ||
| } | ||
| } | ||
|
|
||
| } | ||
|
|
||
| private boolean isNewTrip(HashMap<TripUpdateKey, List<TripUpdate>> previousTripUpdates, TripUpdate tripUpdate) { | ||
| TripUpdate latest = null; | ||
| List<TripUpdate> tripUpdates = previousTripUpdates.get(new TripUpdateKey(tripUpdate.getVehicle().getId(), tripUpdate.getTrip().getTripId())); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you add some comments here to make it a bit easier to follow the logic? |
||
| if (tripUpdates != null) { | ||
| for (TripUpdate previousTripUpdate : tripUpdates) { | ||
| if (latest == null) { | ||
| latest = previousTripUpdate; | ||
| } | ||
| if (new TripUpdateStartTimeComparator().compare(latest, previousTripUpdate) > 0) { | ||
| latest = previousTripUpdate; | ||
| } | ||
| } | ||
| if (new TripUpdateStartTimeComparator().compare(tripUpdate, latest) > 0) { | ||
| return true; | ||
| } else { | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| private boolean inTripUpdates(TripUpdate tripUpdate, HashMap<TripUpdateKey, List<TripUpdate>> previousTripUpdates ) { | ||
|
|
||
| List<TripUpdate> tripUpdates = previousTripUpdates.get(new TripUpdateKey(tripUpdate.getVehicle().getId(), tripUpdate.getTrip().getTripId())); | ||
| if(tripUpdates !=null ) | ||
| { | ||
| for (TripUpdate previousTripUpdate : tripUpdates) { | ||
| if (previousTripUpdate.getTrip().getStartTime().equals(tripUpdate.getTrip().getStartTime())) { | ||
| return true; | ||
| } | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
|
|
||
| private void addTripUpdate(HashMap<TripUpdateKey, List<TripUpdate>> tripUpdatesMap, GtfsRealtime.TripUpdate tripUpdate) { | ||
| if (tripUpdate.hasVehicle() && tripUpdate.getVehicle().hasId()) { | ||
|
|
||
| List<TripUpdate> tripUpdates = null; | ||
| if (!tripUpdatesMap.containsKey(new TripUpdateKey(tripUpdate.getVehicle().getId(), tripUpdate.getTrip().getTripId()))) { | ||
| tripUpdates = new ArrayList<TripUpdate>(); | ||
| } else { | ||
| tripUpdates = tripUpdatesMap.get(new TripUpdateKey(tripUpdate.getVehicle().getId(), tripUpdate.getTrip().getTripId())); | ||
| } | ||
| tripUpdates.add(tripUpdate); | ||
|
|
||
| //Collections.sort(tripUpdates, new TripUpdateStartTimeComparator()); | ||
|
|
||
| tripUpdatesMap.put(new TripUpdateKey(tripUpdate.getVehicle().getId(), tripUpdate.getTrip().getTripId()), tripUpdates); | ||
| } | ||
| } | ||
| private class TripUpdateKey | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you could just concatenate the vehicle_id and trip_id strings and that by itself would be fine for a composite key? So "1234_400" or something similar. Having an entire class for this seems overblown to me. |
||
| { | ||
| String vehicle_id; | ||
| String trip_id; | ||
|
|
||
| public TripUpdateKey(String vehicle_id, String trip_id) | ||
| { | ||
| this.vehicle_id=vehicle_id; | ||
| this.trip_id=trip_id; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (this == o) return true; | ||
| if (o == null || getClass() != o.getClass()) return false; | ||
|
|
||
| TripUpdateKey that = (TripUpdateKey) o; | ||
|
|
||
| if (vehicle_id != null ? !vehicle_id.equals(that.vehicle_id) : that.vehicle_id != null) return false; | ||
| return trip_id != null ? trip_id.equals(that.trip_id) : that.trip_id == null; | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| int result = vehicle_id != null ? vehicle_id.hashCode() : 0; | ||
| result = 31 * result + (trip_id != null ? trip_id.hashCode() : 0); | ||
| return result; | ||
| } | ||
| } | ||
|
|
||
| private class TripUpdateStartTimeComparator implements Comparator<TripUpdate> { | ||
| SimpleDateFormat formatter = new SimpleDateFormat("hh:mm:ssa"); | ||
|
|
||
| @Override | ||
| public int compare(TripUpdate t1, TripUpdate t2) { | ||
|
|
||
| try { | ||
| if(t1.hasTrip()&&t1.getTrip().hasStartTime()&&t2.hasTrip()&&t2.getTrip().hasStartTime()) { | ||
|
|
||
| Date t1_date = formatter.parse(t1.getTrip().getStartTime()); | ||
|
|
||
| Date t2_date = formatter.parse(t2.getTrip().getStartTime()); | ||
|
|
||
| return t1_date.compareTo(t2_date); | ||
| } | ||
| } catch (ParseException e) { | ||
| _log.error(e.getMessage(), e); | ||
| } | ||
| return -1; | ||
| } | ||
|
|
||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd prefer to have
addTripUpdate()return the HashMap, rather than passing it as a parameter.