From a109081407d92de15e4581169a4f65a84da61af0 Mon Sep 17 00:00:00 2001 From: Simon Jacobs Date: Wed, 22 Nov 2017 12:11:48 -0500 Subject: [PATCH] Add 'tripDiscoveryMode': look out two weeks in future or past --- .../api/common/RoutingResource.java | 7 +++++ .../routing/core/RoutingContext.java | 8 ++++++ .../routing/core/RoutingRequest.java | 12 +++++++-- .../routing/impl/DurationComparator.java | 27 +++++++++++++++++++ .../routing/impl/GraphPathFinder.java | 6 ++++- 5 files changed, 57 insertions(+), 3 deletions(-) create mode 100644 src/main/java/org/opentripplanner/routing/impl/DurationComparator.java diff --git a/src/main/java/org/opentripplanner/api/common/RoutingResource.java b/src/main/java/org/opentripplanner/api/common/RoutingResource.java index db3887c0dbf..d6ca5e132fc 100644 --- a/src/main/java/org/opentripplanner/api/common/RoutingResource.java +++ b/src/main/java/org/opentripplanner/api/common/RoutingResource.java @@ -396,6 +396,10 @@ public abstract class RoutingResource { @QueryParam("geoidElevation") private Boolean geoidElevation; + /* If trip discovery mode (look out a week) should be used */ + @QueryParam("tripDiscoveryMode") + private Boolean tripDiscoveryMode; + /* * somewhat ugly bug fix: the graphService is only needed here for fetching per-graph time zones. * this should ideally be done when setting the routing context, but at present departure/ @@ -646,6 +650,9 @@ protected RoutingRequest buildRequest() throws ParameterException { if (geoidElevation != null) request.geoidElevation = geoidElevation; + if (tripDiscoveryMode != null) + request.setTripDiscoveryMode(tripDiscoveryMode); + //getLocale function returns defaultLocale if locale is null request.locale = ResourceBundleSingleton.INSTANCE.getLocale(locale); return request; diff --git a/src/main/java/org/opentripplanner/routing/core/RoutingContext.java b/src/main/java/org/opentripplanner/routing/core/RoutingContext.java index 12e3e13cd6d..afeb838a160 100644 --- a/src/main/java/org/opentripplanner/routing/core/RoutingContext.java +++ b/src/main/java/org/opentripplanner/routing/core/RoutingContext.java @@ -380,7 +380,15 @@ private void setServiceDays() { addIfNotExists(this.serviceDays, new ServiceDay(graph, serviceDate, calendarService, agency.getId())); addIfNotExists(this.serviceDays, new ServiceDay(graph, serviceDate.next(), calendarService, agency.getId())); + if (opt.tripDiscoveryMode) { + ServiceDate sd = serviceDate; + for (int i = 0; i < 15; i++) { + sd = opt.arriveBy ? sd.previous() : sd.next(); + addIfNotExists(this.serviceDays, new ServiceDay(graph, sd, calendarService, agency.getId())); + } + } } + } } diff --git a/src/main/java/org/opentripplanner/routing/core/RoutingRequest.java b/src/main/java/org/opentripplanner/routing/core/RoutingRequest.java index 199d2e6ce99..1571b30585f 100644 --- a/src/main/java/org/opentripplanner/routing/core/RoutingRequest.java +++ b/src/main/java/org/opentripplanner/routing/core/RoutingRequest.java @@ -486,6 +486,9 @@ public class RoutingRequest implements Cloneable, Serializable { public boolean enterStationsWithCar = false; + /** whether to use trip discovery mode - search farther in future or past for shortest trip */ + public boolean tripDiscoveryMode = false; + /** Saves split edge which can be split on origin/destination search * * This is used so that TrivialPathException is thrown if origin and destination search would split the same edge @@ -1007,7 +1010,8 @@ public boolean equals(Object o) { && Objects.equal(startingTransitTripId, other.startingTransitTripId) && useTraffic == other.useTraffic && disableAlertFiltering == other.disableAlertFiltering - && geoidElevation == other.geoidElevation; + && geoidElevation == other.geoidElevation + && tripDiscoveryMode == other.tripDiscoveryMode; } /** @@ -1038,7 +1042,8 @@ public int hashCode() { + new Boolean(reverseOptimizeOnTheFly).hashCode() * 95112799 + new Boolean(ignoreRealtimeUpdates).hashCode() * 154329 + new Boolean(disableRemainingWeightHeuristic).hashCode() * 193939 - + new Boolean(useTraffic).hashCode() * 10169; + + new Boolean(useTraffic).hashCode() * 10169 + + new Boolean(tripDiscoveryMode).hashCode() * 209477; if (batch) { hashCode *= -1; // batch mode, only one of two endpoints matters @@ -1280,4 +1285,7 @@ public void canSplitEdge(StreetEdge edge) { } + public void setTripDiscoveryMode(boolean tripDiscoveryMode) { + this.tripDiscoveryMode = tripDiscoveryMode; + } } diff --git a/src/main/java/org/opentripplanner/routing/impl/DurationComparator.java b/src/main/java/org/opentripplanner/routing/impl/DurationComparator.java new file mode 100644 index 00000000000..fc1b79a77f6 --- /dev/null +++ b/src/main/java/org/opentripplanner/routing/impl/DurationComparator.java @@ -0,0 +1,27 @@ +/* This program is free software: you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public License + as published by the Free Software Foundation, either version 3 of + the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +package org.opentripplanner.routing.impl; + +import org.opentripplanner.routing.spt.GraphPath; + +import java.util.Comparator; + +public class DurationComparator implements Comparator { + + @Override + public int compare(GraphPath o1, GraphPath o2) { + return o1.getDuration() - o2.getDuration(); + } + +} diff --git a/src/main/java/org/opentripplanner/routing/impl/GraphPathFinder.java b/src/main/java/org/opentripplanner/routing/impl/GraphPathFinder.java index 2e95c6629db..e8503b7d76d 100644 --- a/src/main/java/org/opentripplanner/routing/impl/GraphPathFinder.java +++ b/src/main/java/org/opentripplanner/routing/impl/GraphPathFinder.java @@ -208,7 +208,11 @@ public List getPaths(RoutingRequest options) { LOG.debug("we have {} paths", paths.size()); } LOG.debug("END SEARCH ({} msec)", System.currentTimeMillis() - searchBeginTime); - Collections.sort(paths, new PathComparator(options.arriveBy)); + if (options.tripDiscoveryMode) { + paths.sort(new DurationComparator()); + } else { + paths.sort(new PathComparator(options.arriveBy)); + } return paths; }