Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored Graph Traversal Implementation #258

Merged
merged 7 commits into from
Apr 20, 2023
Merged
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 18 additions & 37 deletions src/main/java/org/eclipse/pathfinder/api/GraphTraversalService.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,53 +50,34 @@ 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);
allVertices.remove(destinationUnLocode);

int candidateCount = getRandomNumberOfCandidates();
List<TransitPath> candidates = new ArrayList<>(candidateCount);
List<TransitPath> candidates = new ArrayList<>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please revert this. It is inefficient to create an empty list when capacity is known.


for (int i = 0; i < candidateCount; i++) {
allVertices = getRandomChunkOfLocations(allVertices);
List<TransitEdge> transitEdges = new ArrayList<>(allVertices.size() - 1);
String firstLegTo = allVertices.get(0);

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);
List<TransitEdge> transitEdges = new ArrayList<>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please revert this. It is inefficient to create an empty list when capacity is known.

String fromUnLocode = originUnLocode;
LocalDateTime date = LocalDateTime.now();

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));
}

Expand All @@ -114,7 +95,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);
}
}