refactor filtering logic which determines if additional time of an alternative route isacceptable
LocationBasedServices/sumoc-backend/pipeline/head This commit looks good
LocationBasedServices/sumoc-backend/pipeline/head This commit looks good
This commit is contained in:
@@ -3,14 +3,13 @@ package SuMoC.backend.routing.services;
|
||||
import SuMoC.backend.carta.CartaClient;
|
||||
import SuMoC.backend.geocoding.AddressDTO;
|
||||
import SuMoC.backend.geocoding.GeoCodingService;
|
||||
import SuMoC.backend.routing.config.RoutingProperties;
|
||||
import SuMoC.backend.routing.dtos.RouteDTO;
|
||||
import SuMoC.backend.routing.dtos.RoutingAlternativeRequestDTO;
|
||||
import SuMoC.backend.routing.dtos.StepDTO;
|
||||
import SuMoC.backend.routing.exceptions.NoCarRouteFoundException;
|
||||
import SuMoC.backend.routing.exceptions.NoCarbonEmissionFoundException;
|
||||
import SuMoC.backend.trip.Modality;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
import reactor.core.publisher.Mono;
|
||||
import sumoc.backend.carta.client.model.Direction;
|
||||
@@ -23,21 +22,14 @@ import java.util.Map;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
@Service
|
||||
// TODO refactor this service into smaller submodules
|
||||
@RequiredArgsConstructor
|
||||
public class RoutingService {
|
||||
|
||||
private final GeoCodingService geoCodingService;
|
||||
|
||||
private final CartaClient cartaClient;
|
||||
|
||||
private final RoutingProperties routingProperties;
|
||||
|
||||
@Autowired
|
||||
public RoutingService(CartaClient cartaClient, RoutingProperties routingProperties, GeoCodingService geoCodingService) {
|
||||
this.cartaClient = cartaClient;
|
||||
this.routingProperties = routingProperties;
|
||||
this.geoCodingService = geoCodingService;
|
||||
}
|
||||
private final AlternativeFilterService alternativeFilterService;
|
||||
|
||||
/**
|
||||
* Makes an individual call to the CARTA API and returns a direction future.
|
||||
@@ -114,13 +106,24 @@ public class RoutingService {
|
||||
return directions;
|
||||
}
|
||||
|
||||
public List<RouteDTO> getAlternativeRoute(RoutingAlternativeRequestDTO requestDTO) {
|
||||
/**
|
||||
* Gets possible routes from CARTA, converts them to our format and applies filter to only provide acceptable solutions without excessive additional travel time.
|
||||
*
|
||||
* @param requestDTO
|
||||
* @return
|
||||
*/
|
||||
public List<RouteDTO> determineAlternativeRoutes(RoutingAlternativeRequestDTO requestDTO) {
|
||||
List<RouteDTO> routes = fetchAndConvertRoutes(requestDTO);
|
||||
routes = alternativeFilterService.filterForAdditionalTravelTime(requestDTO, routes);
|
||||
|
||||
return routes;
|
||||
}
|
||||
|
||||
private List<RouteDTO> fetchAndConvertRoutes(RoutingAlternativeRequestDTO requestDTO) {
|
||||
assert requestDTO.getRoutingPreferencesDTO() != null;
|
||||
HashMap<String, Direction> directionMap = getPossibleDirectionsFromCarta(requestDTO);
|
||||
ArrayList<RouteDTO> suggestedAlternatives = new ArrayList<>();
|
||||
|
||||
// TODO we have to use actual trip duration as comparison and not cartas car duration
|
||||
|
||||
// Fetch origin and destination addresses concurrently
|
||||
var addressesMono = Mono.zip(
|
||||
geoCodingService.getAddress(requestDTO.getOriginLat(), requestDTO.getOriginLon()),
|
||||
@@ -130,12 +133,6 @@ public class RoutingService {
|
||||
AddressDTO originAddress = addressesTuple.getT1();
|
||||
AddressDTO destinationAddress = addressesTuple.getT2();
|
||||
|
||||
// Define max additional time for sustainable alternatives
|
||||
int maxAdditionalTimeNonPhysical = routingProperties.getToleratedAdditionalTravelSeconds();
|
||||
int maxAdditionalTimePhysical = requestDTO.getRoutingPreferencesDTO().isPreferPhysicalActivity() ?
|
||||
routingProperties.getToleratedAdditionalPhysicalActivitiesSeconds() :
|
||||
routingProperties.getToleratedAdditionalTravelSeconds();
|
||||
|
||||
// Setting up car data for baseline
|
||||
Route carRoute;
|
||||
try {
|
||||
@@ -154,34 +151,31 @@ public class RoutingService {
|
||||
Direction bikeDirection = directionMap.get("bike");
|
||||
if (bikeDirection.getRoutes() != null && !bikeDirection.getRoutes().isEmpty()) {
|
||||
Route bikeRoute = bikeDirection.getRoutes().getFirst();
|
||||
int durationDifferenceCar = bikeRoute.getDuration() - carRoute.getDuration();
|
||||
if (durationDifferenceCar < maxAdditionalTimePhysical) {
|
||||
ArrayList<StepDTO> steps = new ArrayList<>();
|
||||
steps.add(new StepDTO(
|
||||
Modality.BICYCLE,
|
||||
null,
|
||||
bikeRoute.getDistance()
|
||||
)
|
||||
);
|
||||
ArrayList<StepDTO> steps = new ArrayList<>();
|
||||
steps.add(new StepDTO(
|
||||
Modality.BICYCLE,
|
||||
null,
|
||||
bikeRoute.getDistance()
|
||||
)
|
||||
);
|
||||
|
||||
RouteDTO routeDTO = new RouteDTO(
|
||||
originAddress,
|
||||
null,
|
||||
bikeRoute.getSourceCoordinate().getLatitude(),
|
||||
bikeRoute.getSourceCoordinate().getLongitude(),
|
||||
destinationAddress,
|
||||
null,
|
||||
bikeRoute.getTargetCoordinate().getLatitude(),
|
||||
bikeRoute.getTargetCoordinate().getLongitude(),
|
||||
requestDTO.getArrivalTime().minusSeconds(bikeRoute.getDuration()),
|
||||
requestDTO.getArrivalTime(),
|
||||
steps,
|
||||
carCarbondDioxide,
|
||||
bikeRoute.getDuration() - carRoute.getDuration(),
|
||||
0.0
|
||||
);
|
||||
suggestedAlternatives.add(routeDTO);
|
||||
}
|
||||
RouteDTO routeDTO = new RouteDTO(
|
||||
originAddress,
|
||||
null,
|
||||
bikeRoute.getSourceCoordinate().getLatitude(),
|
||||
bikeRoute.getSourceCoordinate().getLongitude(),
|
||||
destinationAddress,
|
||||
null,
|
||||
bikeRoute.getTargetCoordinate().getLatitude(),
|
||||
bikeRoute.getTargetCoordinate().getLongitude(),
|
||||
requestDTO.getArrivalTime().minusSeconds(bikeRoute.getDuration()),
|
||||
requestDTO.getArrivalTime(),
|
||||
steps,
|
||||
carCarbondDioxide,
|
||||
bikeRoute.getDuration() - carRoute.getDuration(),
|
||||
0.0
|
||||
);
|
||||
suggestedAlternatives.add(routeDTO);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -189,33 +183,30 @@ public class RoutingService {
|
||||
Direction walkDirection = directionMap.get("walk");
|
||||
if (walkDirection.getRoutes() != null && !walkDirection.getRoutes().isEmpty()) {
|
||||
Route walkRoute = walkDirection.getRoutes().getFirst();
|
||||
int durationDifferenceCar = walkRoute.getDuration() - carRoute.getDuration();
|
||||
if (durationDifferenceCar < maxAdditionalTimePhysical) {
|
||||
ArrayList<StepDTO> steps = new ArrayList<>();
|
||||
steps.add(new StepDTO(
|
||||
Modality.WALKING,
|
||||
null,
|
||||
walkRoute.getDistance()
|
||||
));
|
||||
ArrayList<StepDTO> steps = new ArrayList<>();
|
||||
steps.add(new StepDTO(
|
||||
Modality.WALKING,
|
||||
null,
|
||||
walkRoute.getDistance()
|
||||
));
|
||||
|
||||
RouteDTO routeDTO = new RouteDTO(
|
||||
originAddress,
|
||||
null,
|
||||
walkRoute.getSourceCoordinate().getLatitude(),
|
||||
walkRoute.getSourceCoordinate().getLongitude(),
|
||||
destinationAddress,
|
||||
null,
|
||||
walkRoute.getTargetCoordinate().getLatitude(),
|
||||
walkRoute.getTargetCoordinate().getLongitude(),
|
||||
requestDTO.getArrivalTime().minusSeconds(walkRoute.getDuration()),
|
||||
requestDTO.getArrivalTime(),
|
||||
steps,
|
||||
carCarbondDioxide,
|
||||
walkRoute.getDuration() - carRoute.getDuration(),
|
||||
0.0
|
||||
);
|
||||
suggestedAlternatives.add(routeDTO);
|
||||
}
|
||||
RouteDTO routeDTO = new RouteDTO(
|
||||
originAddress,
|
||||
null,
|
||||
walkRoute.getSourceCoordinate().getLatitude(),
|
||||
walkRoute.getSourceCoordinate().getLongitude(),
|
||||
destinationAddress,
|
||||
null,
|
||||
walkRoute.getTargetCoordinate().getLatitude(),
|
||||
walkRoute.getTargetCoordinate().getLongitude(),
|
||||
requestDTO.getArrivalTime().minusSeconds(walkRoute.getDuration()),
|
||||
requestDTO.getArrivalTime(),
|
||||
steps,
|
||||
carCarbondDioxide,
|
||||
walkRoute.getDuration() - carRoute.getDuration(),
|
||||
0.0
|
||||
);
|
||||
suggestedAlternatives.add(routeDTO);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -223,8 +214,6 @@ public class RoutingService {
|
||||
Direction transitDirection = directionMap.get("transit");
|
||||
if (transitDirection.getRoutes() != null && !transitDirection.getRoutes().isEmpty()) {
|
||||
for (Route route : transitDirection.getRoutes()) {
|
||||
// TODO check if length toleratable - maybe move this check to the end
|
||||
|
||||
CartaTransitRouteProcessor routeProcessor = new CartaTransitRouteProcessor(route);
|
||||
|
||||
RouteDTO routeDTO = new RouteDTO(
|
||||
@@ -247,9 +236,6 @@ public class RoutingService {
|
||||
}
|
||||
}
|
||||
}
|
||||
// TODO if consider weather
|
||||
// get weather for origin
|
||||
// if bad, remove walking and biking options
|
||||
|
||||
return suggestedAlternatives;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user