Просмотр исходного кода

Merged PR "rename names in pathfiner package" with Spring Boot changes.

marlin 9 лет назад
Родитель
Сommit
99de934ce1

+ 9
- 6
src/main/java/com/pathfinder/api/GraphTraversalService.java Просмотреть файл

@@ -1,5 +1,7 @@
1 1
 package com.pathfinder.api;
2 2
 
3
+import java.rmi.Remote;
4
+import java.rmi.RemoteException;
3 5
 import java.util.List;
4 6
 import java.util.Properties;
5 7
 
@@ -8,16 +10,17 @@ import java.util.Properties;
8 10
  * and used by us (booking and tracking team).
9 11
  *
10 12
  */
11
-public interface GraphTraversalService {
13
+public interface GraphTraversalService extends Remote {
12 14
 
13 15
   /**
14
-   * @param originUnLocode origin UN Locode
15
-   * @param destinationUnLocode destination UN Locode
16
+   * @param origin origin point
17
+   * @param destination destination point
16 18
    * @param limitations restrictions on the path selection, as key-value according to some API specification
17
-   * @return A list of transit pathss
19
+   * @return A list of transit paths
20
+   * @throws RemoteException RMI problem
18 21
    */
19
-  List<TransitPath> findShortestPath(String originUnLocode,
20
-                                     String destinationUnLocode,
22
+  List<TransitPath> findShortestPath(String origin,
23
+                                     String destination,
21 24
                                      Properties limitations);
22 25
 
23 26
 }

+ 18
- 18
src/main/java/com/pathfinder/api/TransitEdge.java Просмотреть файл

@@ -10,43 +10,43 @@ import java.util.Date;
10 10
  */
11 11
 public final class TransitEdge implements Serializable {
12 12
 
13
-  private final String voyageNumber;
14
-  private final String fromUnLocode;
15
-  private final String toUnLocode;
13
+  private final String edge;
14
+  private final String fromNode;
15
+  private final String toNode;
16 16
   private final Date fromDate;
17 17
   private final Date toDate;
18 18
 
19 19
   /**
20 20
    * Constructor.
21 21
    *
22
-   * @param voyageNumber
23
-   * @param fromUnLocode
24
-   * @param toUnLocode
22
+   * @param edge
23
+   * @param fromNode
24
+   * @param toNode
25 25
    * @param fromDate
26 26
    * @param toDate
27 27
    */
28
-  public TransitEdge(final String voyageNumber,
29
-                     final String fromUnLocode,
30
-                     final String toUnLocode,
28
+  public TransitEdge(final String edge,
29
+                     final String fromNode,
30
+                     final String toNode,
31 31
                      final Date fromDate,
32 32
                      final Date toDate) {
33
-    this.voyageNumber = voyageNumber;
34
-    this.fromUnLocode = fromUnLocode;
35
-    this.toUnLocode = toUnLocode;
33
+    this.edge = edge;
34
+    this.fromNode = fromNode;
35
+    this.toNode = toNode;
36 36
     this.fromDate = fromDate;
37 37
     this.toDate = toDate;
38 38
   }
39 39
 
40
-  public String getVoyageNumber() {
41
-    return voyageNumber;
40
+  public String getEdge() {
41
+    return edge;
42 42
   }
43 43
 
44
-  public String getFromUnLocode() {
45
-    return fromUnLocode;
44
+  public String getFromNode() {
45
+    return fromNode;
46 46
   }
47 47
 
48
-  public String getToUnLocode() {
49
-    return toUnLocode;
48
+  public String getToNode() {
49
+    return toNode;
50 50
   }
51 51
 
52 52
   public Date getFromDate() {

+ 2
- 1
src/main/java/com/pathfinder/config/PathfinderApplicationContext.java Просмотреть файл

@@ -2,6 +2,7 @@ package com.pathfinder.config;
2 2
 
3 3
 import com.pathfinder.api.GraphTraversalService;
4 4
 import com.pathfinder.internal.GraphDAO;
5
+import com.pathfinder.internal.GraphDAOStub;
5 6
 import com.pathfinder.internal.GraphTraversalServiceImpl;
6 7
 import org.springframework.context.annotation.Bean;
7 8
 import org.springframework.context.annotation.Configuration;
@@ -10,7 +11,7 @@ import org.springframework.context.annotation.Configuration;
10 11
 public class PathfinderApplicationContext {
11 12
 
12 13
     private GraphDAO graphDAO() {
13
-        return new GraphDAO();
14
+        return new GraphDAOStub();
14 15
     }
15 16
 
16 17
     @Bean

+ 3
- 22
src/main/java/com/pathfinder/internal/GraphDAO.java Просмотреть файл

@@ -1,27 +1,8 @@
1 1
 package com.pathfinder.internal;
2 2
 
3
-import java.util.ArrayList;
4
-import java.util.Arrays;
5 3
 import java.util.List;
6
-import java.util.Random;
7 4
 
8
-public class GraphDAO {
9
-
10
-  private static final Random random = new Random();
11
-
12
-  public List<String> listLocations() {
13
-    return new ArrayList<String>(Arrays.asList(
14
-      "CNHKG", "AUMEL", "SESTO", "FIHEL", "USCHI", "JNTKO", "DEHAM", "CNSHA", "NLRTM", "SEGOT", "CNHGH", "USNYC", "USDAL"
15
-    ));
16
-  }
17
-
18
-  public String getVoyageNumber(String from, String to) {
19
-    final int i = random.nextInt(5);
20
-    if (i == 0) return "0100S";
21
-    if (i == 1) return "0200T";
22
-    if (i == 2) return "0300A";
23
-    if (i == 3) return "0301S";
24
-    return "0400S";
25
-  }
26
-  
5
+public interface GraphDAO {
6
+	List<String> listAllNodes();
7
+	String getTransitEdge(String from, String to);
27 8
 }

+ 27
- 0
src/main/java/com/pathfinder/internal/GraphDAOStub.java Просмотреть файл

@@ -0,0 +1,27 @@
1
+package com.pathfinder.internal;
2
+
3
+import java.util.ArrayList;
4
+import java.util.Arrays;
5
+import java.util.List;
6
+import java.util.Random;
7
+
8
+public class GraphDAOStub implements GraphDAO{
9
+
10
+  private static final Random random = new Random();
11
+
12
+  public List<String> listAllNodes() {
13
+    return new ArrayList<String>(Arrays.asList(
14
+      "CNHKG", "AUMEL", "SESTO", "FIHEL", "USCHI", "JNTKO", "DEHAM", "CNSHA", "NLRTM", "SEGOT", "CNHGH", "USNYC", "USDAL"
15
+    ));
16
+  }
17
+
18
+  public String getTransitEdge(String from, String to) {
19
+    final int i = random.nextInt(5);
20
+    if (i == 0) return "0100S";
21
+    if (i == 1) return "0200T";
22
+    if (i == 2) return "0300A";
23
+    if (i == 3) return "0301S";
24
+    return "0400S";
25
+  }
26
+  
27
+}

+ 15
- 15
src/main/java/com/pathfinder/internal/GraphTraversalServiceImpl.java Просмотреть файл

@@ -18,20 +18,20 @@ public class GraphTraversalServiceImpl implements GraphTraversalService {
18 18
     this.random = new Random();
19 19
   }
20 20
 
21
-  public List<TransitPath> findShortestPath(final String originUnLocode,
22
-                                            final String destinationUnLocode,
21
+  public List<TransitPath> findShortestPath(final String originNode,
22
+                                            final String destinationNode,
23 23
                                             final Properties limitations) {
24 24
     Date date = nextDate(new Date());
25 25
 
26
-    List<String> allVertices = dao.listLocations();
27
-    allVertices.remove(originUnLocode);
28
-    allVertices.remove(destinationUnLocode);
26
+    List<String> allVertices = dao.listAllNodes();
27
+    allVertices.remove(originNode);
28
+    allVertices.remove(destinationNode);
29 29
 
30 30
     final int candidateCount = getRandomNumberOfCandidates();
31 31
     final List<TransitPath> candidates = new ArrayList<TransitPath>(candidateCount);
32 32
 
33 33
     for (int i = 0; i < candidateCount; i++) {
34
-      allVertices = getRandomChunkOfLocations(allVertices);
34
+      allVertices = getRandomChunkOfNodes(allVertices);
35 35
       final List<TransitEdge> transitEdges = new ArrayList<TransitEdge>(allVertices.size() - 1);
36 36
       final String firstLegTo = allVertices.get(0);
37 37
 
@@ -40,8 +40,8 @@ public class GraphTraversalServiceImpl implements GraphTraversalService {
40 40
       date = nextDate(toDate);
41 41
 
42 42
       transitEdges.add(new TransitEdge(
43
-        dao.getVoyageNumber(originUnLocode, firstLegTo),
44
-        originUnLocode, firstLegTo, fromDate, toDate));
43
+        dao.getTransitEdge(originNode, firstLegTo),
44
+        originNode, firstLegTo, fromDate, toDate));
45 45
 
46 46
       for (int j = 0; j < allVertices.size() - 1; j++) {
47 47
         final String curr = allVertices.get(j);
@@ -49,15 +49,15 @@ public class GraphTraversalServiceImpl implements GraphTraversalService {
49 49
         fromDate = nextDate(date);
50 50
         toDate = nextDate(fromDate);
51 51
         date = nextDate(toDate);
52
-        transitEdges.add(new TransitEdge(dao.getVoyageNumber(curr, next), curr, next, fromDate, toDate));
52
+        transitEdges.add(new TransitEdge(dao.getTransitEdge(curr, next), curr, next, fromDate, toDate));
53 53
       }
54 54
 
55 55
       final String lastLegFrom = allVertices.get(allVertices.size() - 1);
56 56
       fromDate = nextDate(date);
57 57
       toDate = nextDate(fromDate);
58 58
       transitEdges.add(new TransitEdge(
59
-        dao.getVoyageNumber(lastLegFrom, destinationUnLocode),
60
-        lastLegFrom, destinationUnLocode, fromDate, toDate));
59
+        dao.getTransitEdge(lastLegFrom, destinationNode),
60
+        lastLegFrom, destinationNode, fromDate, toDate));
61 61
 
62 62
       candidates.add(new TransitPath(transitEdges));
63 63
     }
@@ -73,11 +73,11 @@ public class GraphTraversalServiceImpl implements GraphTraversalService {
73 73
     return 3 + random.nextInt(3);
74 74
   }
75 75
 
76
-  private List<String> getRandomChunkOfLocations(List<String> allLocations) {
77
-    Collections.shuffle(allLocations);
78
-    final int total = allLocations.size();
76
+  private List<String> getRandomChunkOfNodes(List<String> allNodes) {
77
+    Collections.shuffle(allNodes);
78
+    final int total = allNodes.size();
79 79
     final int chunk = total > 4 ? 1 + new Random().nextInt(5) : total;
80
-    return allLocations.subList(0, chunk);
80
+    return allNodes.subList(0, chunk);
81 81
   }
82 82
 
83 83
 }

+ 3
- 3
src/main/java/se/citerus/dddsample/infrastructure/routing/ExternalRoutingService.java Просмотреть файл

@@ -77,9 +77,9 @@ public class ExternalRoutingService implements RoutingService {
77 77
 
78 78
   private Leg toLeg(TransitEdge edge) {
79 79
     return new Leg(
80
-      voyageRepository.find(new VoyageNumber(edge.getVoyageNumber())),
81
-      locationRepository.find(new UnLocode(edge.getFromUnLocode())),
82
-      locationRepository.find(new UnLocode(edge.getToUnLocode())),
80
+      voyageRepository.find(new VoyageNumber(edge.getEdge())),
81
+      locationRepository.find(new UnLocode(edge.getFromNode())),
82
+      locationRepository.find(new UnLocode(edge.getToNode())),
83 83
       edge.getFromDate(), edge.getToDate()
84 84
     );
85 85
   }

+ 6
- 5
src/test/java/se/citerus/dddsample/infrastructure/routing/ExternalRoutingServiceTest.java Просмотреть файл

@@ -1,14 +1,12 @@
1 1
 package se.citerus.dddsample.infrastructure.routing;
2 2
 
3 3
 import com.pathfinder.api.GraphTraversalService;
4
-import com.pathfinder.internal.GraphDAO;
4
+import com.pathfinder.internal.GraphDAOStub;
5 5
 import com.pathfinder.internal.GraphTraversalServiceImpl;
6 6
 import junit.framework.TestCase;
7
-import static org.easymock.EasyMock.*;
8 7
 import se.citerus.dddsample.domain.model.cargo.*;
9 8
 import se.citerus.dddsample.domain.model.location.Location;
10 9
 import se.citerus.dddsample.domain.model.location.LocationRepository;
11
-import static se.citerus.dddsample.domain.model.location.SampleLocations.*;
12 10
 import se.citerus.dddsample.domain.model.voyage.SampleVoyages;
13 11
 import se.citerus.dddsample.domain.model.voyage.VoyageNumber;
14 12
 import se.citerus.dddsample.domain.model.voyage.VoyageRepository;
@@ -18,6 +16,9 @@ import java.util.Arrays;
18 16
 import java.util.Date;
19 17
 import java.util.List;
20 18
 
19
+import static org.easymock.EasyMock.*;
20
+import static se.citerus.dddsample.domain.model.location.SampleLocations.*;
21
+
21 22
 public class ExternalRoutingServiceTest extends TestCase {
22 23
 
23 24
   private ExternalRoutingService externalRoutingService;
@@ -31,8 +32,8 @@ public class ExternalRoutingServiceTest extends TestCase {
31 32
     voyageRepository = createMock(VoyageRepository.class);
32 33
     externalRoutingService.setVoyageRepository(voyageRepository);
33 34
 
34
-    GraphTraversalService graphTraversalService = new GraphTraversalServiceImpl(new GraphDAO() {
35
-      public List<String> listLocations() {
35
+    GraphTraversalService graphTraversalService = new GraphTraversalServiceImpl(new GraphDAOStub() {
36
+      public List<String> listAllNodes() {
36 37
         return Arrays.asList(TOKYO.unLocode().idString(), STOCKHOLM.unLocode().idString(), GOTHENBURG.unLocode().idString());
37 38
       }
38 39