Skip to content

Commit d6d54a2

Browse files
Ruediger.LundeRuediger.Lunde
authored andcommitted
Some refactoring performed to improve code structure.
- Map renamed to ExtendableMap. - New interface Map added. - Class Point2D added to encapsulate location position information. - AbstractMapAgentModel merged into MapAgentModel.
1 parent 27a3f91 commit d6d54a2

24 files changed

+221
-385
lines changed

src/aima/gui/applications/search/map/AbstractMapAgentController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public abstract class AbstractMapAgentController extends AgentAppController {
3333
/** Clears the model's tour history. */
3434
@Override
3535
public void clearAgent() {
36-
((AbstractMapAgentModel) model).clearTourHistory();
36+
((MapAgentModel) model).clearTourHistory();
3737
frame.modelChanged();
3838
}
3939

src/aima/gui/applications/search/map/AbstractMapAgentModel.java

Lines changed: 0 additions & 84 deletions
This file was deleted.

src/aima/gui/applications/search/map/MapAgentFrame.java

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import aima.gui.framework.AgentView;
99
import aima.search.framework.SearchFactory;
1010
import aima.search.map.Map;
11+
import aima.search.map.Point2D;
1112

1213

1314
/**
@@ -60,7 +61,7 @@ class MapAgentView extends AgentView {
6061

6162
/** Clears the panel and draws the map and the tour history. */
6263
public void paint(java.awt.Graphics g) {
63-
AbstractMapAgentModel maModel = (AbstractMapAgentModel) model;
64+
MapAgentModel maModel = (MapAgentModel) model;
6465
java.awt.Graphics2D g2 = (java.awt.Graphics2D) g;
6566
g2.setColor(Color.white);
6667
g2.fillRect(0, 0, getWidth(), getHeight());
@@ -78,19 +79,19 @@ public void paint(java.awt.Graphics g) {
7879
* view without scrolling.
7980
*/
8081
private void adjustTransformation() {
81-
AbstractMapAgentModel maModel = (AbstractMapAgentModel) model;
82+
MapAgentModel maModel = (MapAgentModel) model;
8283
List<String> locs = maModel.getLocations();
8384
// adjust coordinates relative to the left upper corner of the graph area
8485
double minX = Double.POSITIVE_INFINITY;
8586
double minY = Double.POSITIVE_INFINITY;
8687
double maxX = Double.NEGATIVE_INFINITY;
8788
double maxY = Double.NEGATIVE_INFINITY;
8889
for (String loc : locs) {
89-
double[] xy = maModel.getLocCoords(loc);
90-
if (xy[0] < minX) minX = xy[0];
91-
if (xy[1] < minY) minY = xy[1];
92-
if (xy[0] > maxX) maxX = xy[0];
93-
if (xy[1] > maxY) maxY = xy[1];
90+
Point2D xy = maModel.getLocCoords(loc);
91+
if (xy.getX() < minX) minX = xy.getX();
92+
if (xy.getY() < minY) minY = xy.getY();
93+
if (xy.getX() > maxX) maxX = xy.getX();
94+
if (xy.getY() > maxY) maxY = xy.getY();
9495
}
9596
this.setBorder(20, 20, 20, 100);
9697
adjustTransformation(minX, minY, maxX, maxY);
@@ -100,31 +101,31 @@ private void adjustTransformation() {
100101
* Represents roads by lines and locations by name-labeled points.
101102
*/
102103
private void paintMap(java.awt.Graphics2D g2) {
103-
AbstractMapAgentModel maModel = (AbstractMapAgentModel) model;
104+
MapAgentModel maModel = (MapAgentModel) model;
104105
Map envMap = maModel.getEnvMap();
105106
Map agentMap = maModel.getAgentMap();
106107
List<Roadblock> roadblocks = new ArrayList<Roadblock>();
107108
for (String l1 : maModel.getLocations()) {
108-
double[] xy1 = maModel.getLocCoords(l1);
109+
Point2D pt1 = maModel.getLocCoords(l1);
109110
List<String> linkedLocs = envMap.getLocationsLinkedTo(l1);
110111
for (String l2 : agentMap.getLocationsLinkedTo(l1))
111112
if (!linkedLocs.contains(l2))
112113
linkedLocs.add(l2);
113114
for (String l2 : linkedLocs) {
114-
double[] xy2 = maModel.getLocCoords(l2);
115+
Point2D pt2 = maModel.getLocCoords(l2);
115116
g2.setColor(Color.lightGray);
116-
g2.drawLine(x(xy1), y(xy1), x(xy2), y(xy2));
117+
g2.drawLine(x(pt1), y(pt1), x(pt2), y(pt2));
117118
boolean blockedInEnv =
118119
!envMap.getLocationsLinkedTo(l2).contains(l1);
119120
boolean blockedInAgent =
120121
!agentMap.getLocationsLinkedTo(l2).contains(l1);
121-
roadblocks.add(new Roadblock(xy1, xy2, blockedInEnv, blockedInAgent));
122+
roadblocks.add(new Roadblock(pt1, pt2, blockedInEnv, blockedInAgent));
122123
if (blockedInEnv && blockedInAgent) {
123124
boolean blockedInEnvOtherDir =
124125
!envMap.getLocationsLinkedTo(l1).contains(l2);
125126
boolean blockedInAgentOtherDir =
126127
!agentMap.getLocationsLinkedTo(l1).contains(l2);
127-
roadblocks.add(new Roadblock(xy2, xy1, blockedInEnvOtherDir, blockedInAgentOtherDir));
128+
roadblocks.add(new Roadblock(pt2, pt1, blockedInEnvOtherDir, blockedInAgentOtherDir));
128129
}
129130
}
130131
}
@@ -134,15 +135,15 @@ private void paintMap(java.awt.Graphics2D g2) {
134135

135136
/** The track of the agent is visualized with red lines. */
136137
private void paintTour(java.awt.Graphics2D g2) {
137-
AbstractMapAgentModel maModel = (AbstractMapAgentModel) model;
138-
double[] lastXY = null;
138+
MapAgentModel maModel = (MapAgentModel) model;
139+
Point2D lastPt = null;
139140
g2.setColor(Color.red);
140141
for (String loc : maModel.getTourHistory()) {
141-
double[] xy = maModel.getLocCoords(loc);
142-
if (xy != null && lastXY != null) {
143-
g2.drawLine(x(xy), y(xy), x(lastXY), y(lastXY));
142+
Point2D pt = maModel.getLocCoords(loc);
143+
if (pt != null && lastPt != null) {
144+
g2.drawLine(x(pt), y(pt), x(lastPt), y(lastPt));
144145
}
145-
lastXY = xy;
146+
lastPt = pt;
146147
}
147148
}
148149

@@ -166,11 +167,11 @@ else if (!block.inEnvMap)
166167
}
167168

168169
private void paintLoc(java.awt.Graphics2D g2, String loc) {
169-
AbstractMapAgentModel maModel = (AbstractMapAgentModel) model;
170-
double[] xy = maModel.getLocCoords(loc);
171-
if (xy != null) {
172-
int x = x(xy);
173-
int y = y(xy);
170+
MapAgentModel maModel = (MapAgentModel) model;
171+
Point2D pt = maModel.getLocCoords(loc);
172+
if (pt != null) {
173+
int x = x(pt);
174+
int y = y(pt);
174175
String info = "";
175176
List<String> history = maModel.getTourHistory();
176177
ArrayList<Integer> list = new ArrayList<Integer>();
@@ -214,11 +215,11 @@ else if (history.contains(loc))
214215
* the road itself so that they always appear in front.
215216
*/
216217
private static class Roadblock {
217-
double[] pos1;
218-
double[] pos2;
218+
Point2D pos1;
219+
Point2D pos2;
219220
boolean inEnvMap;
220221
boolean inAgentMap;
221-
private Roadblock(double[] pos1, double[] pos2, boolean inEnvMap, boolean inAgentMap) {
222+
private Roadblock(Point2D pos1, Point2D pos2, boolean inEnvMap, boolean inAgentMap) {
222223
this.pos1 = pos1;
223224
this.pos2 = pos2;
224225
this.inEnvMap = inEnvMap;

src/aima/gui/applications/search/map/MapAgentModel.java

Lines changed: 57 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
import java.util.List;
55

66
import aima.basic.Agent;
7+
import aima.gui.framework.AgentAppModel;
8+
import aima.search.map.DynAttributeNames;
79
import aima.search.map.Map;
10+
import aima.search.map.Point2D;
811
import aima.search.map.Scenario;
912

1013
/**
@@ -16,12 +19,46 @@
1619
*
1720
* @author R. Lunde
1821
*/
19-
public class MapAgentModel extends AbstractMapAgentModel {
22+
public class MapAgentModel extends AgentAppModel {
2023
/** A scenario. */
2124
protected Scenario scenario;
2225
/** A list of location names, possibly null. */
2326
protected List<String> destinations;
2427

28+
/** Stores the locations, the agent has already visited. */
29+
private final ArrayList<String> tourHistory = new ArrayList<String>();
30+
31+
/** Returns a list of all already visited agent locations. */
32+
public List<String> getTourHistory() {
33+
return tourHistory;
34+
}
35+
36+
/** Clears the list of already visited locations. */
37+
public void clearTourHistory() {
38+
tourHistory.clear();
39+
}
40+
41+
/**
42+
* Reacts on environment changes and updates the tour history. The command
43+
* string is always send to all registered model change listeners. If the
44+
* command is a location name (with attached position info) or
45+
* {@link aima.basic.Agent#NO_OP} or {@link aima.basic.Agent#DIE}, the
46+
* agent's current location is added to the tour history and all listeners
47+
* are informed about the change.
48+
*/
49+
@Override
50+
public void envChanged(String command) {
51+
for (AgentAppModel.ModelChangedListener listener : listeners)
52+
listener.logMessage(command);
53+
if (getLocCoords(command) != null || command.equals(Agent.NO_OP)
54+
|| command.equals(Agent.DIE)) {
55+
String loc = (String) getAgent().getAttribute(
56+
DynAttributeNames.AGENT_LOCATION);
57+
tourHistory.add(loc);
58+
fireModelChanged();
59+
}
60+
}
61+
2562
/**
2663
* Assigns values to the attributes {@link #scenario} and
2764
* {@link #destinations}, clears the history and informs all interested
@@ -40,7 +77,6 @@ public void prepare(Scenario s, List<String> d) {
4077
}
4178

4279
/** Checks whether there is a scenario available. */
43-
@Override
4480
public boolean isEmpty() {
4581
return scenario == null;
4682
}
@@ -49,7 +85,6 @@ public boolean isEmpty() {
4985
* Returns all location names mentioned in the environment map or in the
5086
* agent map.
5187
*/
52-
@Override
5388
public List<String> getLocations() {
5489
List<String> result = scenario.getEnvMap().getLocations();
5590
if (!result.containsAll(scenario.getAgentMap().getLocations())) {
@@ -61,44 +96,51 @@ public List<String> getLocations() {
6196
return result;
6297
}
6398

64-
@Override
99+
/** Returns the map used by the agent. */
65100
public Map getAgentMap() {
66101
return scenario.getAgentMap();
67102
}
68103

69-
@Override
104+
/** Returns the map used by the environment. */
70105
public Map getEnvMap() {
71106
return scenario.getEnvMap();
72107
}
73108

74-
@Override
109+
/** Returns the agent. */
75110
public Agent getAgent() {
76111
return (Agent) scenario.getEnv().getAgents().get(0);
77112
}
78113

79-
@Override
114+
/** Checks whether a given location is the initial location of the agent. */
80115
public boolean isStart(String loc) {
81116
return scenario.getInitAgentLocation() == loc;
82117
}
83118

84-
@Override
119+
/** Checks whether a given location is one of the specified destinations. */
85120
public boolean isDestination(String loc) {
86121
return destinations != null && destinations.contains(loc);
87122
}
88123

89-
@Override
90-
public double[] getLocCoords(String loc) {
91-
return scenario.getEnvMap().getXY(loc);
124+
/**
125+
* Returns the coordinates of the specified location.
126+
*/
127+
public Point2D getLocCoords(String loc) {
128+
return scenario.getEnvMap().getPosition(loc);
92129
}
93130

94-
/** Always returns false. */
95-
@Override
131+
/**
132+
* Checks whether special informations can be perceived at the location.
133+
* This implementation always returns false.
134+
*/
135+
96136
public boolean hasInfos(String loc) {
97137
return false; // not implemented yet
98138
}
99139

100-
/** Always returns false. */
101-
@Override
140+
/**
141+
* Checks whether interesting objects can be perceived at the location.
142+
* This implementation always returns false.
143+
*/
102144
public boolean hasObjects(String loc) {
103145
return false; // not implemented yet
104146
}

0 commit comments

Comments
 (0)