33import java .util .Hashtable ;
44import java .util .List ;
55
6- import aima .search .map .Map ;
76import aima .util .LabeledGraph ;
87import aima .util .Util ;
98
10-
119/**
1210 * Implements a map with locations, distance labeled links between the
13- * locations, straight line distances, and 2d-placement positions of
14- * locations. Locations are represented by strings and travel distances
15- * by integer values. In spite of its superclass (which was in fact only added
16- * for compatibility reasons), in this implementation locations and links can
17- * be dynamically added and removed after creation. This enables to read maps
18- * from file or to modify it with respect to newly obtained knowledge.
11+ * locations, straight line distances, and 2d-placement positions of locations.
12+ * Locations are represented by strings and travel distances by integer values.
13+ * In spite of its superclass (which was in fact only added for compatibility
14+ * reasons), in this implementation locations and links can be dynamically added
15+ * and removed after creation. This enables to read maps from file or to modify
16+ * it with respect to newly obtained knowledge.
17+ *
1918 * @author R. Lunde
2019 */
2120public class MapWithSLD extends Map {
2221
2322 /**
24- * Stores road data. Locations are represented as vertices and
25- * roads (links) as directed edges labeled with corresponding
26- * travel distances.
23+ * Stores map data. Locations are represented as vertices and connections
24+ * (links) as directed edges labeled with corresponding travel distances.
2725 */
28- private final LabeledGraph <String , Integer > roads ;
29-
26+ private final LabeledGraph <String , Integer > links ;
27+
3028 /** Stores xy-coordinates for each location. */
3129 private final Hashtable <String , double []> locationCoords ;
3230
3331 /** Creates an empty map. */
3432 public MapWithSLD () {
35- super (new String []{}); // we overwrite everything!
36- roads = new LabeledGraph <String , Integer >();
33+ super (new String [] {}); // we overwrite everything!
34+ links = new LabeledGraph <String , Integer >();
3735 locationCoords = new Hashtable <String , double []>();
3836 }
39-
37+
4038 /** Removes everything. */
4139 public void clear () {
42- roads .clear ();
40+ links .clear ();
4341 locationCoords .clear ();
4442 }
45-
46- /** Clears all roads but keeps location position informations. */
43+
44+ /** Clears all connections but keeps location position informations. */
4745 public void clearLinks () {
48- roads .clear ();
46+ links .clear ();
4947 }
50-
48+
5149 /** Returns a list of all locations. */
5250 public List <String > getLocations () {
53- return roads .getVertexLabels ();
51+ return links .getVertexLabels ();
5452 }
55-
53+
5654 /** Checks whether the given string is the name of a location. */
5755 public boolean isLocation (String str ) {
58- return roads .isVertexLabel (str );
56+ return links .isVertexLabel (str );
5957 }
60-
61- /////////////////////////////////////////////////////////////////
58+
59+ // ///////////////////////////////////////////////////////////////
6260 // overridden methods of Map
63-
61+
6462 /**
6563 * Answers to the question: Where can I get, following one of the
66- * roads starting at the specified location?
64+ * connections starting at the specified location?
6765 */
68- public List <String > getLocationsLinkedTo (String fromLocation ) {
69- return roads .getSuccessors (fromLocation );
66+ @ Override
67+ public List <String > getLocationsLinkedTo (String fromLocation ) {
68+ return links .getSuccessors (fromLocation );
7069 }
71-
70+
7271 /**
73- * Returns the travel distance between the two specified locations
74- * if they are linked by a road and null otherwise.
72+ * Returns the travel distance between the two specified locations if they
73+ * are linked by a connection and null otherwise.
7574 */
7675 @ Override
7776 public Integer getDistance (String fromLocation , String toLocation ) {
78- return roads .get (fromLocation , toLocation );
77+ return links .get (fromLocation , toLocation );
7978 }
8079
81- /** Adds a one-way street to the map. */
80+ /** Adds a one-way connection to the map. */
81+ @ Override
8282 public void addUnidirectionalLink (String fromLocation , String toLocation ,
8383 Integer distance ) {
84- roads .set (fromLocation , toLocation , distance );
84+ links .set (fromLocation , toLocation , distance );
8585 }
8686
8787 /**
88- * Adds a road which can be traveled in both direction. Internally,
89- * such a road is represented as two one-way streets .
88+ * Adds a connection which can be traveled in both direction. Internally,
89+ * such a connection is represented as two one-way connections .
9090 */
91+ @ Override
9192 public void addBidirectionalLink (String fromLocation , String toLocation ,
9293 Integer distance ) {
93- roads .set (fromLocation , toLocation , distance );
94- roads .set (toLocation , fromLocation , distance );
94+ links .set (fromLocation , toLocation , distance );
95+ links .set (toLocation , fromLocation , distance );
9596 }
9697
9798 /**
9899 * Returns a location which is selected by random.
99100 */
101+ @ Override
100102 public String randomlyGenerateDestination () {
101103 return Util .selectRandomlyFromList (getLocations ());
102104 }
103-
104- /////////////////////////////////////////////////////////////////
105+
106+ // ///////////////////////////////////////////////////////////////
105107 // additional methods
106-
107- /** Removes a one-way street . */
108+
109+ /** Removes a one-way connection . */
108110 public void removeUnidirectionalLink (String fromLocation , String toLocation ) {
109- roads .remove (fromLocation , toLocation );
111+ links .remove (fromLocation , toLocation );
110112 }
111113
112- /** Removes the two corresponding one-way streets . */
114+ /** Removes the two corresponding one-way connections . */
113115 public void removeBidirectionalLink (String fromLocation , String toLocation ) {
114- roads .remove (fromLocation , toLocation );
115- roads .remove (toLocation , fromLocation );
116+ links .remove (fromLocation , toLocation );
117+ links .remove (toLocation , fromLocation );
116118 }
117119
118120 /**
119121 * Defines the position of a location as with respect to an orthogonal
120- * coordinate system.
122+ * coordinate system.
121123 */
122124 public void setCoords (String loc , double x , double y ) {
123- locationCoords .put (loc , new double []{ x , y });
125+ locationCoords .put (loc , new double [] { x , y });
124126 }
127+
125128 /**
126- * Defines the position of a location within the map. Using this method,
127- * one location should be selected as reference position
128- * (<code>dist=0</code> and <code>dir=0</code>) and all the other
129- * location should be placed relative to it.
130- * @param loc location name
131- * @param dist distance to a reference position
132- * @param dir bearing (compass direction) in which the location is seen
133- * from the reference position
129+ * Defines the position of a location within the map. Using this method, one
130+ * location should be selected as reference position (<code>dist=0</code>
131+ * and <code>dir=0</code>) and all the other location should be placed
132+ * relative to it.
133+ *
134+ * @param loc
135+ * location name
136+ * @param dist
137+ * distance to a reference position
138+ * @param dir
139+ * bearing (compass direction) in which the location is seen from
140+ * the reference position
134141 */
135142 public void setDistAndDirToRefLocation (String loc , double dist , int dir ) {
136143 double [] coords = new double [2 ];
@@ -141,23 +148,24 @@ public void setDistAndDirToRefLocation(String loc, double dist, int dir) {
141148
142149 /**
143150 * Returns the straight line distance between two specified locations.
144- * @return positive distance value or -1 denoting no information available.
151+ *
152+ * @return positive distance value or -1 denoting no information available.
145153 */
146154 public double getStraightLineDistance (String loc1 , String loc2 ) {
147155 double result = -1 ;
148156 double [] cl1 = locationCoords .get (loc1 );
149157 double [] cl2 = locationCoords .get (loc2 );
150158 if (cl1 != null && cl2 != null ) {
151- result = (cl2 [0 ]- cl1 [0 ])* (cl2 [0 ]- cl1 [0 ]);
152- result += (cl2 [1 ]- cl1 [1 ])* (cl2 [1 ]- cl1 [1 ]);
159+ result = (cl2 [0 ] - cl1 [0 ]) * (cl2 [0 ] - cl1 [0 ]);
160+ result += (cl2 [1 ] - cl1 [1 ]) * (cl2 [1 ] - cl1 [1 ]);
153161 result = Math .sqrt (result );
154162 }
155163 return result ;
156164 }
157165
158166 /**
159- * Returns an array with two integers describing the
160- * the position of the specified location.
167+ * Returns an array with two integers describing the the position of the
168+ * specified location.
161169 */
162170 public double [] getXY (String loc ) {
163171 return locationCoords .get (loc );
0 commit comments