|
|
@@ -1,67 +1,14 @@
|
|
1
|
1
|
package se.citerus.routingteam;
|
|
2
|
2
|
|
|
3
|
|
-import java.util.*;
|
|
|
3
|
+import java.util.List;
|
|
4
|
4
|
|
|
5
|
|
-public class GraphTraversalService {
|
|
|
5
|
+/**
|
|
|
6
|
+ * Part of the external graph traversal API exposed by the routing team
|
|
|
7
|
+ * and used by us (booking and tracking team).
|
|
|
8
|
+ *
|
|
|
9
|
+ */
|
|
|
10
|
+public interface GraphTraversalService {
|
|
6
|
11
|
|
|
7
|
|
- private GraphDAO dao;
|
|
8
|
|
- private Random random;
|
|
9
|
|
-
|
|
10
|
|
- public GraphTraversalService(GraphDAO dao) {
|
|
11
|
|
- this.dao = dao;
|
|
12
|
|
- this.random = new Random();
|
|
13
|
|
- }
|
|
14
|
|
-
|
|
15
|
|
- public List<TransitPath> performHeavyCalculations(String originUnLocode, String destinationUnLocode) {
|
|
16
|
|
- List<String> allVertices = dao.listLocations();
|
|
17
|
|
- allVertices.remove(originUnLocode);
|
|
18
|
|
- allVertices.remove(destinationUnLocode);
|
|
19
|
|
-
|
|
20
|
|
- final int candidateCount = getRandomNumberOfCandidates();
|
|
21
|
|
- final List<TransitPath> candidates = new ArrayList<TransitPath>(candidateCount);
|
|
22
|
|
-
|
|
23
|
|
- for (int i = 0; i < candidateCount; i++) {
|
|
24
|
|
- allVertices = getRandomChunkOfLocations(allVertices);
|
|
25
|
|
- final List<TransitEdge> transitEdges = new ArrayList<TransitEdge>(allVertices.size() - 1);
|
|
26
|
|
- final String firstLegTo = allVertices.get(0);
|
|
27
|
|
-
|
|
28
|
|
- transitEdges.add(new TransitEdge(
|
|
29
|
|
- getRandomCarrierMovementId(originUnLocode, firstLegTo),
|
|
30
|
|
- originUnLocode, firstLegTo));
|
|
31
|
|
-
|
|
32
|
|
- for (int j = 0; j < allVertices.size() - 1; j++) {
|
|
33
|
|
- final String curr = allVertices.get(j);
|
|
34
|
|
- final String next = allVertices.get(j + 1);
|
|
35
|
|
- transitEdges.add(new TransitEdge(getRandomCarrierMovementId(curr, next), curr, next));
|
|
36
|
|
- }
|
|
37
|
|
-
|
|
38
|
|
- final String lastLegFrom = allVertices.get(allVertices.size() - 1);
|
|
39
|
|
- transitEdges.add(new TransitEdge(
|
|
40
|
|
- getRandomCarrierMovementId(lastLegFrom, destinationUnLocode),
|
|
41
|
|
- lastLegFrom, destinationUnLocode));
|
|
42
|
|
-
|
|
43
|
|
- candidates.add(new TransitPath(transitEdges));
|
|
44
|
|
- }
|
|
45
|
|
-
|
|
46
|
|
- return candidates;
|
|
47
|
|
- }
|
|
48
|
|
-
|
|
49
|
|
- private String getRandomCarrierMovementId(String from, String to) {
|
|
50
|
|
- final String random = UUID.randomUUID().toString().toUpperCase();
|
|
51
|
|
- final String cmId = random.substring(0, 4);
|
|
52
|
|
- dao.storeCarrierMovementId(cmId, from, to);
|
|
53
|
|
- return cmId;
|
|
54
|
|
- }
|
|
55
|
|
-
|
|
56
|
|
- private int getRandomNumberOfCandidates() {
|
|
57
|
|
- return 1 + random.nextInt(4);
|
|
58
|
|
- }
|
|
59
|
|
-
|
|
60
|
|
- private List<String> getRandomChunkOfLocations(List<String> allLocations) {
|
|
61
|
|
- Collections.shuffle(allLocations);
|
|
62
|
|
- final int total = allLocations.size();
|
|
63
|
|
- final int chunk = total > 4 ? (total - 4) + random.nextInt(5) : total;
|
|
64
|
|
- return allLocations.subList(0, chunk);
|
|
65
|
|
- }
|
|
|
12
|
+ List<TransitPath> performHeavyCalculations(String originUnLocode, String destinationUnLocode);
|
|
66
|
13
|
|
|
67
|
14
|
}
|