-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
REFACTOR: 158 - Draft based on query template and routing facade
- Still has open TODOs and points to be discussed. - Introduce routing query facade the hide complexity of routing from the service.
- Loading branch information
Showing
13 changed files
with
693 additions
and
482 deletions.
There are no files selected for viewing
This file contains 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
491 changes: 16 additions & 475 deletions
491
src/main/java/ch/naviqore/service/gtfs/raptor/GtfsRaptorService.java
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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
54 changes: 54 additions & 0 deletions
54
src/main/java/ch/naviqore/service/gtfs/raptor/routing/ConnectionGeoToGeo.java
This file contains 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 |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package ch.naviqore.service.gtfs.raptor.routing; | ||
|
||
import ch.naviqore.service.Connection; | ||
import ch.naviqore.service.TimeType; | ||
import ch.naviqore.service.Walk; | ||
import ch.naviqore.service.config.ConnectionQueryConfig; | ||
import ch.naviqore.utils.spatial.GeoCoordinate; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.Map; | ||
|
||
class ConnectionGeoToGeo extends ConnectionQueryTemplate<GeoCoordinate, GeoCoordinate> { | ||
|
||
ConnectionGeoToGeo(LocalDateTime time, TimeType timeType, ConnectionQueryConfig queryConfig, | ||
RoutingQueryUtils utils, GeoCoordinate source, GeoCoordinate target) { | ||
super(time, timeType, queryConfig, utils, source, target); | ||
} | ||
|
||
@Override | ||
protected Map<String, LocalDateTime> prepareSourceStops(GeoCoordinate source) { | ||
return utils.getStopsWithWalkTimeFromLocation(source, time, timeType, queryConfig); | ||
} | ||
|
||
@Override | ||
protected Map<String, Integer> prepareTargetStops(GeoCoordinate target) { | ||
return utils.getStopsWithWalkTimeFromLocation(target, queryConfig); | ||
} | ||
|
||
@Override | ||
protected Connection postprocessConnection(GeoCoordinate source, ch.naviqore.raptor.Connection connection, | ||
GeoCoordinate target) { | ||
LocalDateTime departureTime = connection.getDepartureTime(); | ||
Walk firstMile = utils.createFirstWalk(source, connection.getFromStopId(), departureTime); | ||
|
||
LocalDateTime arrivalTime = connection.getArrivalTime(); | ||
Walk lastMile = utils.createLastWalk(target, connection.getToStopId(), arrivalTime); | ||
|
||
// TODO: Add sourceStop as source for firstMile if sourceStop != null | ||
// TODO: Add targetStop as target for lastMile if targetStop != null | ||
// COMMENT: I think this is already handled inside 'utils.createFirstWalk' and 'utils.createLastWalk' | ||
|
||
// TODO: Handle case where firstMile is not null and first leg is a transfer --> use walkCalculator? | ||
// TODO: Handle case where lastMile is not null and last leg is a transfer --> use walkCalculator? | ||
// COMMENT: This means we would currently have a walk form our coordinate to the next stop, but then from | ||
// there another walk to another stop? Have we considered this case in the old version (non-refactored) version? | ||
|
||
return utils.composeConnection(firstMile, connection, lastMile); | ||
} | ||
|
||
@Override | ||
protected ConnectionQueryTemplate<GeoCoordinate, GeoCoordinate> swap(GeoCoordinate source, GeoCoordinate target) { | ||
return new ConnectionGeoToGeo(time, timeType, queryConfig, utils, target, source); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
src/main/java/ch/naviqore/service/gtfs/raptor/routing/ConnectionGeoToStop.java
This file contains 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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package ch.naviqore.service.gtfs.raptor.routing; | ||
|
||
import ch.naviqore.service.Connection; | ||
import ch.naviqore.service.Stop; | ||
import ch.naviqore.service.TimeType; | ||
import ch.naviqore.service.Walk; | ||
import ch.naviqore.service.config.ConnectionQueryConfig; | ||
import ch.naviqore.utils.spatial.GeoCoordinate; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.Map; | ||
|
||
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); | ||
} | ||
|
||
@Override | ||
protected Map<String, LocalDateTime> prepareSourceStops(GeoCoordinate source) { | ||
return utils.getStopsWithWalkTimeFromLocation(source, time, timeType, queryConfig); | ||
} | ||
|
||
@Override | ||
protected Map<String, Integer> prepareTargetStops(Stop target) { | ||
return utils.getAllChildStopsFromStop(target); | ||
} | ||
|
||
@Override | ||
protected Connection postprocessConnection(GeoCoordinate source, ch.naviqore.raptor.Connection connection, | ||
Stop target) { | ||
LocalDateTime departureTime = connection.getDepartureTime(); | ||
Walk firstMile = utils.createLastWalk(source, connection.getFromStopId(), departureTime); | ||
Walk lastMile = utils.createLastWalk(source, connection.getFromStopId(), departureTime); | ||
|
||
// TODO: Handle case where firstMile is not null and first leg is a transfer --> use walkCalculator | ||
|
||
return utils.composeConnection(firstMile, connection, lastMile); | ||
} | ||
|
||
@Override | ||
protected ConnectionQueryTemplate<Stop, GeoCoordinate> swap(GeoCoordinate source, Stop target) { | ||
return new ConnectionStopToGeo(time, timeType, queryConfig, utils, target, source); | ||
} | ||
} |
85 changes: 85 additions & 0 deletions
85
src/main/java/ch/naviqore/service/gtfs/raptor/routing/ConnectionQueryTemplate.java
This file contains 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 |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package ch.naviqore.service.gtfs.raptor.routing; | ||
|
||
import ch.naviqore.raptor.RaptorAlgorithm; | ||
import ch.naviqore.service.Connection; | ||
import ch.naviqore.service.TimeType; | ||
import ch.naviqore.service.config.ConnectionQueryConfig; | ||
import ch.naviqore.service.exception.ConnectionRoutingException; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
@RequiredArgsConstructor | ||
@Slf4j | ||
abstract class ConnectionQueryTemplate<S, T> { | ||
|
||
protected final LocalDateTime time; | ||
protected final TimeType timeType; | ||
protected final ConnectionQueryConfig queryConfig; | ||
protected final RoutingQueryUtils utils; | ||
|
||
private final S source; | ||
private final T target; | ||
|
||
protected abstract Map<String, LocalDateTime> prepareSourceStops(S source); | ||
|
||
protected abstract Map<String, Integer> prepareTargetStops(T target); | ||
|
||
protected abstract Connection postprocessConnection(S source, ch.naviqore.raptor.Connection connection, T target); | ||
|
||
protected abstract ConnectionQueryTemplate<T, S> swap(S source, T target); | ||
|
||
List<Connection> run() throws ConnectionRoutingException { | ||
// TODO: / COMMENT: This is dangerous because if you run the query multiple times (e.g. when routing from | ||
// location instead of stop due to InvalidStopException, the swap might occur twice resulting in incorrect | ||
// results. | ||
|
||
// swap source and target if time type is arrival, which means routing in the reverse time dimension | ||
if (timeType == TimeType.ARRIVAL) { | ||
return swap(source, target).process(); | ||
} | ||
|
||
return process(); | ||
} | ||
|
||
List<Connection> process() throws ConnectionRoutingException { | ||
|
||
Map<String, LocalDateTime> sourceStops = prepareSourceStops(source); | ||
Map<String, Integer> targetStops = prepareTargetStops(target); | ||
|
||
// no source stop or target stop is within walkable distance, and therefore no connections are available | ||
if (sourceStops.isEmpty() || targetStops.isEmpty()) { | ||
return List.of(); | ||
} | ||
|
||
// query connection from raptor | ||
List<ch.naviqore.raptor.Connection> connections; | ||
try { | ||
connections = utils.routeConnections(sourceStops, targetStops, timeType, queryConfig); | ||
} catch (RaptorAlgorithm.InvalidStopException e) { | ||
log.debug("{}: {}", e.getClass().getSimpleName(), e.getMessage()); | ||
// TODO: Implement abstract handleInvalidStopException method? | ||
return List.of(); | ||
} catch (IllegalArgumentException e) { | ||
throw new ConnectionRoutingException(e); | ||
} | ||
|
||
// assemble connection results | ||
List<Connection> result = new ArrayList<>(); | ||
for (ch.naviqore.raptor.Connection raptorConnection : connections) { | ||
|
||
Connection serviceConnection = postprocessConnection(source, raptorConnection, target); | ||
|
||
if (utils.isBelowMaximumTravelTime(serviceConnection, queryConfig)) { | ||
result.add(serviceConnection); | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
|
||
} |
43 changes: 43 additions & 0 deletions
43
src/main/java/ch/naviqore/service/gtfs/raptor/routing/ConnectionStopToGeo.java
This file contains 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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package ch.naviqore.service.gtfs.raptor.routing; | ||
|
||
import ch.naviqore.service.Connection; | ||
import ch.naviqore.service.Stop; | ||
import ch.naviqore.service.TimeType; | ||
import ch.naviqore.service.Walk; | ||
import ch.naviqore.service.config.ConnectionQueryConfig; | ||
import ch.naviqore.utils.spatial.GeoCoordinate; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.Map; | ||
|
||
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); | ||
} | ||
|
||
@Override | ||
protected Map<String, LocalDateTime> prepareSourceStops(Stop source) { | ||
return utils.getAllChildStopsFromStop(source, time); | ||
} | ||
|
||
@Override | ||
protected Map<String, Integer> prepareTargetStops(GeoCoordinate target) { | ||
return utils.getStopsWithWalkTimeFromLocation(target, queryConfig); | ||
} | ||
|
||
@Override | ||
protected Connection postprocessConnection(Stop source, ch.naviqore.raptor.Connection connection, | ||
GeoCoordinate target) { | ||
LocalDateTime arrivalTime = connection.getArrivalTime(); | ||
Walk lastMile = utils.createLastWalk(target, connection.getToStopId(), arrivalTime); | ||
|
||
return utils.composeConnection(connection, lastMile); | ||
} | ||
|
||
@Override | ||
protected ConnectionQueryTemplate<GeoCoordinate, Stop> swap(Stop source, GeoCoordinate target) { | ||
return new ConnectionGeoToStop(time, timeType, queryConfig, utils, target, source); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
src/main/java/ch/naviqore/service/gtfs/raptor/routing/ConnectionStopToStop.java
This file contains 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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package ch.naviqore.service.gtfs.raptor.routing; | ||
|
||
import ch.naviqore.service.Connection; | ||
import ch.naviqore.service.Stop; | ||
import ch.naviqore.service.TimeType; | ||
import ch.naviqore.service.config.ConnectionQueryConfig; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.Map; | ||
|
||
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); | ||
} | ||
|
||
@Override | ||
protected Map<String, LocalDateTime> prepareSourceStops(Stop source) { | ||
return utils.getAllChildStopsFromStop(source, time); | ||
} | ||
|
||
@Override | ||
protected Map<String, Integer> prepareTargetStops(Stop target) { | ||
return utils.getAllChildStopsFromStop(target); | ||
} | ||
|
||
@Override | ||
protected Connection postprocessConnection(Stop source, ch.naviqore.raptor.Connection connection, Stop target) { | ||
return utils.composeConnection(connection); | ||
} | ||
|
||
@Override | ||
protected ConnectionQueryTemplate<Stop, Stop> swap(Stop source, Stop target) { | ||
return new ConnectionStopToStop(time, timeType, queryConfig, utils, source, target); | ||
} | ||
} |
Oops, something went wrong.