Skip to content

Commit

Permalink
Merge pull request #227 from DartExplore/issue#226
Browse files Browse the repository at this point in the history
Issue#226
  • Loading branch information
ZaneBartlett1 committed Aug 15, 2023
2 parents d292517 + d0cf20d commit 4cf571f
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ public ResponseEntity<List<String>> getAllTypes() {
public ResponseEntity<List<StationDTO>> getStationsByConnection(
@RequestParam(value = "currentStation", required = false) Long currentStation,
@RequestParam(value = "maxStationConnections", required = false) Integer maxStationConnections,
@RequestParam(value = "maxTransfers", required = false) Integer maxTransfers,
@RequestParam(value = "amenityIds", required = false) String amenityIdsString,
@RequestParam(value = "types", required = false) String typesString,
@RequestParam(value = "maxWalkTime", required = false) Integer maxWalkTime,
Expand All @@ -136,7 +137,7 @@ public ResponseEntity<List<StationDTO>> getStationsByConnection(
typesList = Arrays.asList(typesString.split(","));
}

List<StationDTO> stations = stationService.getStationsByConnection(currentStation, maxStationConnections, amenityIdList, typesList, maxWalkTime, returnEmpty);
List<StationDTO> stations = stationService.getStationsByConnection(currentStation, maxStationConnections, maxTransfers, amenityIdList, typesList, maxWalkTime, returnEmpty);

return ResponseEntity.ok(stations);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ public interface StationService {

List<PointOfInterestDTO> getPOIsById(List<Long> poiIds) throws DartExploreException, ElementNotFoundException;

List<StationDTO> getStationsByConnection(Long currentStation, Integer stationConnections, List<Long> amenityIdList, List<String> typesList, Integer maxWalkTime, Boolean returnEmpty) throws DartExploreException, ElementNotFoundException;
List<StationDTO> getStationsByConnection(Long currentStation, Integer stationConnections, Integer maxTransfers, List<Long> amenityIdList, List<String> typesList, Integer maxWalkTime, Boolean returnEmpty) throws DartExploreException, ElementNotFoundException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import com.dallasbymetro.backend.repository.AmenityRepository;
import com.dallasbymetro.backend.repository.PointOfInterestRepository;
import com.dallasbymetro.backend.repository.StationRepository;
import com.dallasbymetro.backend.utility.Pair;
import com.dallasbymetro.backend.utility.StationNode;
import org.springframework.stereotype.Service;

import java.util.*;
Expand Down Expand Up @@ -114,7 +114,7 @@ public List<PointOfInterestDTO> getPOIsById(List<Long> poiIds) throws ElementNot
}

@Override
public List<StationDTO> getStationsByConnection(Long currentStation, Integer stationConnections, List<Long> amenityIdList, List<String> typesList, Integer maxWalkTime, Boolean returnEmpty) throws ElementNotFoundException, DartExploreException {
public List<StationDTO> getStationsByConnection(Long currentStation, Integer stationConnections, Integer maxTransfers, List<Long> amenityIdList, List<String> typesList, Integer maxWalkTime, Boolean returnEmpty) throws ElementNotFoundException, DartExploreException {
if ((currentStation == null && stationConnections != null) || (currentStation != null && stationConnections == null)) {
throw new DartExploreException("Both currentStation and stationConnections must be provided together.");
}
Expand All @@ -140,8 +140,8 @@ public List<StationDTO> getStationsByConnection(Long currentStation, Integer sta

Station station = stationOptional.get();

// Perform BFS traversal in the service layer
List<Station> stationsWithinConnection = findStationsWithinConnection(station, stationConnections);
// Perform BFS traversal
List<Station> stationsWithinConnection = findStationsWithinConnection(station, stationConnections, maxTransfers);

// Transform stations to StationDTOs, check that POI have required amenities, and are within walk time
Stream<StationDTO> stream = stationsWithinConnection.stream()
Expand Down Expand Up @@ -172,47 +172,51 @@ private StationDTO prepareStationDTOWithFilteredPOIs(Station station, List<Long>
return StationDTO.prepareStationDTO(station);
}

private List<Station> findStationsWithinConnection(Station currentStation, Integer stationConnections) {
Queue<Pair<Station, Integer>> queue = new LinkedList<>();
Map<Station, Integer> visitedMap = new HashMap<>();

// Create a comparator to sort the pairs by their level and station name
Comparator<Pair<Station, Integer>> pairComparator = Comparator
.<Pair<Station, Integer>>comparingInt(Pair::getSecond)
.thenComparing(pair -> pair.getFirst().getName());

private List<Station> findStationsWithinConnection(Station currentStation, Integer stationConnections, Integer maxTransfers) {
Queue<StationNode> queue = new LinkedList<>();
Set<Station> visitedSet = new HashSet<>();
Set<Station> result = new HashSet<>();
if (maxTransfers == null) {
maxTransfers = 0;
}

// Use a TreeSet to keep the pairs sorted
Set<Pair<Station, Integer>> sortedVisitedPairs = new TreeSet<>(pairComparator);
// Enqueue current station with each of its colors
for (StationColor color : currentStation.getColor()) {
StationNode firstNode = new StationNode(currentStation, 0, 0, Set.of(color));
queue.add(firstNode);
}

Pair<Station, Integer> firstPair = new Pair<>(currentStation, 0);
queue.add(firstPair);
visitedMap.put(currentStation, 0);
sortedVisitedPairs.add(firstPair);
visitedSet.add(currentStation);

while (!queue.isEmpty()) {
Pair<Station, Integer> stationPair = queue.poll();
Station station = stationPair.first;
Integer level = stationPair.second;
StationNode currentNode = queue.poll();
Station station = currentNode.station;
int currentLevel = currentNode.level;
int transferCount = currentNode.transferCount;
StationColor currentColor = currentNode.colors.iterator().next();

if (level < stationConnections) {
if (currentLevel < stationConnections) {
List<Station> connectedStations = stationRepository.findConnectedStations(station.getStationId());

for (Station connectedStation : connectedStations) {
if (!visitedMap.containsKey(connectedStation)) {
Pair<Station, Integer> nextPair = new Pair<>(connectedStation, level + 1);
visitedMap.put(connectedStation, level + 1);
queue.add(nextPair);
sortedVisitedPairs.add(nextPair);
if (!visitedSet.contains(connectedStation)) {
boolean requiresTransfer = !connectedStation.getColor().contains(currentColor);
int newTransferCount = requiresTransfer ? transferCount + 1 : transferCount;

if (newTransferCount <= maxTransfers) {
StationColor nextColor = requiresTransfer ? connectedStation.getColor().iterator().next() : currentColor; // Pick the next line color
StationNode nextNode = new StationNode(connectedStation, currentLevel + 1, newTransferCount, Set.of(nextColor));


visitedSet.add(connectedStation);
queue.add(nextNode);
result.add(connectedStation);
}
}
}
}
}

// Transform the sorted set of pairs to a list of stations

return sortedVisitedPairs.stream()
.map(pair -> pair.first)
.collect(Collectors.toList());
return new ArrayList<>(result);
}
}
19 changes: 0 additions & 19 deletions src/main/java/com/dallasbymetro/backend/utility/Pair.java

This file was deleted.

20 changes: 20 additions & 0 deletions src/main/java/com/dallasbymetro/backend/utility/StationNode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.dallasbymetro.backend.utility;

import com.dallasbymetro.backend.entity.Station;
import com.dallasbymetro.backend.entity.StationColor;

import java.util.Set;

public class StationNode {
public Station station;
public int level;
public int transferCount;
public Set<StationColor> colors;

public StationNode(Station station, int level, int transferCount, Set<StationColor> colors) {
this.station = station;
this.level = level;
this.transferCount = transferCount;
this.colors = colors;
}
}
39 changes: 38 additions & 1 deletion src/main/resources/station_connections.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ WESTMORELAND STATION,HAMPTON STATION
HAMPTON STATION,TYLER VERNON STATION
TYLER VERNON STATION,ZOO STATION
ZOO STATION,8TH & CORINTH STATION
8TH & CORINTH STATION,CEDARS STATION
CEDARS STATION,CONVENTION CENTER STATION
CONVENTION CENTER STATION,EBJ UNION STATION
EBJ UNION STATION,WEST END STATION
WEST END STATION,AKARD STATION
AKARD STATION,ST PAUL STATION
ST PAUL STATION,PEARL/ARTS DISTRICT STATION
PEARL/ARTS DISTRICT STATION,CITYPLACE/UPTOWN STATION
CITYPLACE/UPTOWN STATION,SMU/MOCKINGBIRD STATION
SMU/MOCKINGBIRD STATION,LOVERS LANE STATION
LOVERS LANE STATION,PARK LANE STATION
PARK LANE STATION,WALNUT HILL STATION
WALNUT HILL STATION,FOREST LN STATION
Expand All @@ -32,6 +42,10 @@ BURBANK STATION,INWOOD/LOVE FIELD STATION
INWOOD/LOVE FIELD STATION,SOUTHWEST MEDICAL DISTRICT/PARKLAND
SOUTHWEST MEDICAL DISTRICT/PARKLAND,MARKET CENTER STATION
MARKET CENTER STATION,VICTORY STATION
VICTORY STATION,WEST END STATION
WEST END STATION,AKARD STATION
AKARD STATION,ST PAUL STATION
ST PAUL STATION,PEARL/ARTS DISTRICT STATION
PEARL/ARTS DISTRICT STATION,DEEP ELLUM STATION
DEEP ELLUM STATION,BAYLOR STATION
BAYLOR STATION,FAIR PARK STATION
Expand All @@ -45,4 +59,27 @@ BELT LINE STATION,DALLAS COLLEGE NORTHLAKE CAMPUS STATION
DALLAS COLLEGE NORTHLAKE CAMPUS STATION,HIDDEN RIDGE STATION
HIDDEN RIDGE STATION,IRVING CONVENTION CENTER STATION
IRVING CONVENTION CENTER STATION,LAS COLINAS URBAN CENTER STATION
LAS COLINAS URBAN CENTER STATION,UNIVERSITY OF DALLAS STATION
LAS COLINAS URBAN CENTER STATION,UNIVERSITY OF DALLAS STATION
UNIVERSITY OF DALLAS STATION,BACHMAN STATION
BACHMAN STATION,BURBANK STATION
BURBANK STATION,INWOOD/LOVE FIELD STATION
INWOOD/LOVE FIELD STATION,SOUTHWEST MEDICAL DISTRICT/PARKLAND
SOUTHWEST MEDICAL DISTRICT/PARKLAND,MARKET CENTER STATION
MARKET CENTER STATION,VICTORY STATION
VICTORY STATION,WEST END STATION
WEST END STATION,AKARD STATION
AKARD STATION,ST PAUL STATION
ST PAUL STATION,PEARL/ARTS DISTRICT STATION
PEARL/ARTS DISTRICT STATION,CITYPLACE/UPTOWN STATION
CITYPLACE/UPTOWN STATION,SMU/MOCKINGBIRD STATION
SMU/MOCKINGBIRD STATION,LOVERS LANE STATION
LOVERS LANE STATION,PARK LANE STATION
PARK LANE STATION,WALNUT HILL STATION
WALNUT HILL STATION,FOREST LN STATION
FOREST LN STATION,LBJ / CENTRAL STATION
LBJ / CENTRAL STATION,SPRING VALLEY STATION
SPRING VALLEY STATION,ARAPAHO CENTER STATION
ARAPAHO CENTER STATION,GALATYN PARK STATION
GALATYN PARK STATION,CITYLINE/BUSH STATION
CITYLINE/BUSH STATION, DOWNTOWN PLANO STATION
DOWNTOWN PLANO STATION, PARKER ROAD STATION

0 comments on commit 4cf571f

Please sign in to comment.