Skip to content

Commit

Permalink
Merge pull request #258 from yashTEF/master
Browse files Browse the repository at this point in the history
Refactored Graph Traversal Implementation
  • Loading branch information
Reza Rahman authored Apr 20, 2023
2 parents ca6f3a1 + 7914a10 commit dbc3aab
Showing 1 changed file with 15 additions and 35 deletions.
50 changes: 15 additions & 35 deletions src/main/java/org/eclipse/pathfinder/api/GraphTraversalService.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ public List<TransitPath> findShortestPath(
@Size(min = 8, max = 8, message = "Deadline value must be eight characters long.")
@QueryParam("deadline")
String deadline) {
LocalDateTime date = nextDate(LocalDateTime.now());

List<String> allVertices = dao.listLocations();
allVertices.remove(originUnLocode);
Expand All @@ -62,44 +61,25 @@ public List<TransitPath> findShortestPath(
for (int i = 0; i < candidateCount; i++) {
allVertices = getRandomChunkOfLocations(allVertices);
List<TransitEdge> transitEdges = new ArrayList<>(allVertices.size() - 1);
String firstLegTo = allVertices.get(0);
String fromUnLocode = originUnLocode;
LocalDateTime date = LocalDateTime.now();

LocalDateTime fromDate = nextDate(date);
LocalDateTime toDate = nextDate(fromDate);
date = nextDate(toDate);

transitEdges.add(
new TransitEdge(
dao.getVoyageNumber(originUnLocode, firstLegTo),
originUnLocode,
firstLegTo,
fromDate,
toDate));

for (int j = 0; j < allVertices.size() - 1; j++) {
String current = allVertices.get(j);
String next = allVertices.get(j + 1);
fromDate = nextDate(date);
toDate = nextDate(fromDate);
date = nextDate(toDate);
for (int j = 0; j <= allVertices.size(); ++j) {
LocalDateTime fromDate = nextDate(date);
LocalDateTime toDate = nextDate(fromDate);
String toUnLocode = (j >= allVertices.size() ? destinationUnLocode : allVertices.get(j));
transitEdges.add(
new TransitEdge(dao.getVoyageNumber(current, next), current, next, fromDate, toDate));
new TransitEdge(
dao.getVoyageNumber(fromUnLocode, toUnLocode),
fromUnLocode,
toUnLocode,
fromDate,
toDate));
fromUnLocode = toUnLocode;
date = nextDate(toDate);
}

String lastLegFrom = allVertices.get(allVertices.size() - 1);
fromDate = nextDate(date);
toDate = nextDate(fromDate);
transitEdges.add(
new TransitEdge(
dao.getVoyageNumber(lastLegFrom, destinationUnLocode),
lastLegFrom,
destinationUnLocode,
fromDate,
toDate));

candidates.add(new TransitPath(transitEdges));
}

return candidates;
}

Expand All @@ -114,7 +94,7 @@ private int getRandomNumberOfCandidates() {
private List<String> getRandomChunkOfLocations(List<String> allLocations) {
Collections.shuffle(allLocations);
int total = allLocations.size();
int chunk = total > 4 ? 1 + new Random().nextInt(5) : total;
int chunk = total > 4 ? 1 + random.nextInt(5) : total;
return allLocations.subList(0, chunk);
}
}

0 comments on commit dbc3aab

Please sign in to comment.