Explorar el Código

Merge 31c2ee6660f6ea9e683e7d6be9559054136870ab into ecca53f8d466159975602d0d7961c90223e2efd5

Zhang Gang hace 9 años
padre
commit
2ba7fd46b2

+ 4
- 4
src/main/java/com/pathfinder/api/GraphTraversalService.java Ver fichero

@@ -13,14 +13,14 @@ import java.util.Properties;
13 13
 public interface GraphTraversalService extends Remote {
14 14
 
15 15
   /**
16
-   * @param originUnLocode origin UN Locode
17
-   * @param destinationUnLocode destination UN Locode
16
+   * @param origin origin point
17
+   * @param destination destination point
18 18
    * @param limitations restrictions on the path selection, as key-value according to some API specification
19 19
    * @return A list of transit paths
20 20
    * @throws RemoteException RMI problem
21 21
    */
22
-  List<TransitPath> findShortestPath(String originUnLocode,
23
-                                     String destinationUnLocode,
22
+  List<TransitPath> findShortestPath(String origin,
23
+                                     String destination,
24 24
                                      Properties limitations) throws RemoteException;
25 25
 
26 26
 }

+ 18
- 18
src/main/java/com/pathfinder/api/TransitEdge.java Ver fichero

@@ -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() {

+ 3
- 22
src/main/java/com/pathfinder/internal/GraphDAO.java Ver fichero

@@ -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 Ver fichero

@@ -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 Ver fichero

@@ -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 Ver fichero

@@ -84,9 +84,9 @@ public class ExternalRoutingService implements RoutingService {
84 84
 
85 85
   private Leg toLeg(TransitEdge edge) {
86 86
     return new Leg(
87
-      voyageRepository.find(new VoyageNumber(edge.getVoyageNumber())),
88
-      locationRepository.find(new UnLocode(edge.getFromUnLocode())),
89
-      locationRepository.find(new UnLocode(edge.getToUnLocode())),
87
+      voyageRepository.find(new VoyageNumber(edge.getEdge())),
88
+      locationRepository.find(new UnLocode(edge.getFromNode())),
89
+      locationRepository.find(new UnLocode(edge.getToNode())),
90 90
       edge.getFromDate(), edge.getToDate()
91 91
     );
92 92
   }

+ 2
- 2
src/main/resources/com/pathfinder/internal/applicationContext.xml Ver fichero

@@ -8,7 +8,7 @@
8 8
     <constructor-arg ref="graphDAO"/>
9 9
   </bean>
10 10
 
11
-  <bean id="graphDAO" class="com.pathfinder.internal.GraphDAO"/>
11
+  <bean id="graphDAO" class="com.pathfinder.internal.GraphDAOStub"/>
12 12
 
13 13
   <bean id="registry" class="org.springframework.remoting.rmi.RmiRegistryFactoryBean">
14 14
     <property name="port" value="1199"/>
@@ -21,4 +21,4 @@
21 21
     <property name="registry" ref="registry"/>
22 22
   </bean>
23 23
 
24
-</beans>
24
+</beans>

+ 3
- 3
src/test/java/se/citerus/dddsample/infrastructure/routing/ExternalRoutingServiceTest.java Ver fichero

@@ -1,7 +1,7 @@
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 7
 import static org.easymock.EasyMock.*;
@@ -31,8 +31,8 @@ public class ExternalRoutingServiceTest extends TestCase {
31 31
     voyageRepository = createMock(VoyageRepository.class);
32 32
     externalRoutingService.setVoyageRepository(voyageRepository);
33 33
 
34
-    GraphTraversalService graphTraversalService = new GraphTraversalServiceImpl(new GraphDAO() {
35
-      public List<String> listLocations() {
34
+    GraphTraversalService graphTraversalService = new GraphTraversalServiceImpl(new GraphDAOStub() {
35
+      public List<String> listAllNodes() {
36 36
         return Arrays.asList(TOKYO.unLocode().idString(), STOCKHOLM.unLocode().idString(), GOTHENBURG.unLocode().idString());
37 37
       }
38 38