Skip to content

Commit

Permalink
ENH: #181 - Add basic rules for transfer behaviors to template query …
Browse files Browse the repository at this point in the history
…classes in service.
  • Loading branch information
clukas1 committed Feb 4, 2025
1 parent 460d842 commit d8eb944
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class ConnectionGeoToGeo extends ConnectionQueryTemplate<GeoCoordinate, GeoCoord
*/
ConnectionGeoToGeo(LocalDateTime time, TimeType timeType, ConnectionQueryConfig queryConfig,
RoutingQueryUtils utils, GeoCoordinate source, GeoCoordinate target) {
super(time, timeType, queryConfig, utils, source, target);
super(time, timeType, queryConfig, utils, source, target, false, false);
sourceStop = null;
targetStop = null;
}
Expand All @@ -39,7 +39,7 @@ class ConnectionGeoToGeo extends ConnectionQueryTemplate<GeoCoordinate, GeoCoord
*/
ConnectionGeoToGeo(LocalDateTime time, TimeType timeType, ConnectionQueryConfig queryConfig,
RoutingQueryUtils utils, Stop source, GeoCoordinate target) {
super(time, timeType, queryConfig, utils, source.getCoordinate(), target);
super(time, timeType, queryConfig, utils, source.getCoordinate(), target, false, false);
sourceStop = source;
targetStop = null;
}
Expand All @@ -49,7 +49,7 @@ class ConnectionGeoToGeo extends ConnectionQueryTemplate<GeoCoordinate, GeoCoord
*/
ConnectionGeoToGeo(LocalDateTime time, TimeType timeType, ConnectionQueryConfig queryConfig,
RoutingQueryUtils utils, GeoCoordinate source, Stop target) {
super(time, timeType, queryConfig, utils, source, target.getCoordinate());
super(time, timeType, queryConfig, utils, source, target.getCoordinate(), false, false);
sourceStop = null;
targetStop = target;
}
Expand All @@ -59,7 +59,7 @@ class ConnectionGeoToGeo extends ConnectionQueryTemplate<GeoCoordinate, GeoCoord
*/
ConnectionGeoToGeo(LocalDateTime time, TimeType timeType, ConnectionQueryConfig queryConfig,
RoutingQueryUtils utils, Stop source, Stop target) {
super(time, timeType, queryConfig, utils, source.getCoordinate(), target.getCoordinate());
super(time, timeType, queryConfig, utils, source.getCoordinate(), target.getCoordinate(), false, false);
sourceStop = source;
targetStop = target;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class ConnectionGeoToStop extends ConnectionQueryTemplate<GeoCoordinate, Stop> {

ConnectionGeoToStop(LocalDateTime time, TimeType timeType, ConnectionQueryConfig queryConfig,
RoutingQueryUtils utils, GeoCoordinate source, Stop target) {
super(time, timeType, queryConfig, utils, source, target);
super(time, timeType, queryConfig, utils, source, target, false, true);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ abstract class ConnectionQueryTemplate<S, T> {
private final S source;
private final T target;

private final boolean allowSourceTransfers;
private final boolean allowTargetTransfers;

protected abstract Map<String, LocalDateTime> prepareSourceStops(S source);

protected abstract Map<String, Integer> prepareTargetStops(T target);
Expand Down Expand Up @@ -78,7 +81,8 @@ List<Connection> process() throws ConnectionRoutingException {
// query connection from raptor
List<ch.naviqore.raptor.Connection> connections;
try {
connections = utils.routeConnections(sourceStops, targetStops, timeType, queryConfig);
connections = utils.routeConnections(sourceStops, targetStops, timeType, queryConfig, allowSourceTransfers,
allowTargetTransfers);
} catch (RaptorAlgorithm.InvalidStopException e) {
log.debug("{}: {}", e.getClass().getSimpleName(), e.getMessage());
return handleInvalidStopException(e, source, target);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class ConnectionStopToGeo extends ConnectionQueryTemplate<Stop, GeoCoordinate> {

ConnectionStopToGeo(LocalDateTime time, TimeType timeType, ConnectionQueryConfig queryConfig,
RoutingQueryUtils utils, Stop source, GeoCoordinate target) {
super(time, timeType, queryConfig, utils, source, target);
super(time, timeType, queryConfig, utils, source, target, true, false);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class ConnectionStopToStop extends ConnectionQueryTemplate<Stop, Stop> {

ConnectionStopToStop(LocalDateTime time, TimeType timeType, ConnectionQueryConfig queryConfig,
RoutingQueryUtils utils, Stop source, Stop target) {
super(time, timeType, queryConfig, utils, source, target);
super(time, timeType, queryConfig, utils, source, target, true, true);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class IsolineGeoSource extends IsolineQueryTemplate<GeoCoordinate> {

IsolineGeoSource(LocalDateTime time, TimeType timeType, ConnectionQueryConfig queryConfig, RoutingQueryUtils utils,
GeoCoordinate source) {
super(time, timeType, queryConfig, utils, source);
super(time, timeType, queryConfig, utils, source, false);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ abstract class IsolineQueryTemplate<T> {

private final T source;

private final boolean allowSourceTransfers;

protected abstract Map<String, LocalDateTime> prepareSourceStops(T source);

protected abstract Map<Stop, Connection> handleInvalidStopException(RaptorAlgorithm.InvalidStopException exception,
Expand All @@ -50,7 +52,7 @@ Map<Stop, Connection> run() throws ConnectionRoutingException {
// query isolines from raptor
Map<String, ch.naviqore.raptor.Connection> isolines;
try {
isolines = utils.createIsolines(sourceStops, timeType, queryConfig);
isolines = utils.createIsolines(sourceStops, timeType, queryConfig, allowSourceTransfers);
} catch (RaptorAlgorithm.InvalidStopException e) {
log.debug("{}: {}", e.getClass().getSimpleName(), e.getMessage());
return handleInvalidStopException(e, source);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class IsolineStopSource extends IsolineQueryTemplate<Stop> {

IsolineStopSource(LocalDateTime time, TimeType timeType, ConnectionQueryConfig queryConfig, RoutingQueryUtils utils,
Stop source) {
super(time, timeType, queryConfig, utils, source);
super(time, timeType, queryConfig, utils, source, true);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ch.naviqore.service.gtfs.raptor.routing;

import ch.naviqore.gtfs.schedule.model.GtfsSchedule;
import ch.naviqore.raptor.QueryConfig;
import ch.naviqore.raptor.RaptorAlgorithm;
import ch.naviqore.service.*;
import ch.naviqore.service.config.ConnectionQueryConfig;
Expand Down Expand Up @@ -39,17 +40,39 @@ class RoutingQueryUtils {

List<ch.naviqore.raptor.Connection> routeConnections(Map<String, LocalDateTime> sourceStops,
Map<String, Integer> targetStops, TimeType timeType,
ConnectionQueryConfig queryConfig) {
ConnectionQueryConfig queryConfig, boolean allowSourceTransfer,
boolean allowTargetTransfer) {
QueryConfig config = prepareQueryConfig(queryConfig, allowSourceTransfer, allowTargetTransfer);

if (timeType == TimeType.DEPARTURE) {
return raptor.routeEarliestArrival(sourceStops, targetStops, TypeMapper.map(queryConfig));
return raptor.routeEarliestArrival(sourceStops, targetStops, config);
} else {
return raptor.routeLatestDeparture(targetStops, sourceStops, TypeMapper.map(queryConfig));
return raptor.routeLatestDeparture(targetStops, sourceStops, config);
}
}

Map<String, ch.naviqore.raptor.Connection> createIsolines(Map<String, LocalDateTime> sourceStops, TimeType timeType,
ConnectionQueryConfig queryConfig) {
return raptor.routeIsolines(sourceStops, TypeMapper.map(timeType), TypeMapper.map(queryConfig));
ConnectionQueryConfig queryConfig,
boolean allowSourceTransfer) {
// TODO: discuss whether allowTargetTransfers should be modifiable
return raptor.routeIsolines(sourceStops, TypeMapper.map(timeType),
prepareQueryConfig(queryConfig, allowSourceTransfer, true));
}

private static QueryConfig prepareQueryConfig(ConnectionQueryConfig queryConfig, boolean allowSourceTransfer,
boolean allowTargetTransfer) {
QueryConfig config = TypeMapper.map(queryConfig);
// TODO: discuss whether this should be modifiable
// we don't want consecutive transfers in connections
config.setAllowConsecutiveTransfers(false);
// TODO: discuss whether this should be modifiable
// we don't want two one leg results if connection can be achieved with initial transfer (Round 0) and a one leg
// connection (that is faster) --> Round 1
config.setDoInitialTransferRelaxation(false);
config.setAllowSourceTransfer(allowSourceTransfer);
config.setAllowTargetTransfer(allowTargetTransfer);

return config;
}

Map<String, LocalDateTime> getStopsWithWalkTimeFromLocation(GeoCoordinate location, LocalDateTime startTime,
Expand Down

0 comments on commit d8eb944

Please sign in to comment.