-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFlockingReader.java
384 lines (350 loc) · 13.9 KB
/
FlockingReader.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
import java.awt.Frame;
import org.xml.sax.Attributes;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
/**
* Class to read world specifications as XML documents
* using SAX interface.
*
* Fleshes out a few of the SAX callback methods
* to create and update objects and keep the visual
* display current.
*
* @author Matthew Stone
* @version 1.0
*/
public class FlockingReader extends DefaultHandler {
/* Allows us to allocate unique identifiers to new objects */
static int nextId = 1;
/* Links back to the window where events we read should be displayed */
private Frame frame;
/* Used to construct error messages based on file position */
private Locator locator;
/* The overall world being specified in the current file */
private World world;
/* Used to tell if we are handling defaults */
private boolean inDefaults = false;
/* XML element beginning defaults */
static final String DEFAULT_ELEMENT = "defaults";
/**
* Returns the world being specified by the file as it is parsed
* @return a world
* @see World
*/
public World getWorld() {
return world;
}
/**
* called when XML parser starts reading and gives us tabs
* on the dynamic Locator object parameter
*
* @param locator updated by XML parser to hold current file location
*/
public void setDocumentLocator(Locator locator) {
this.locator = locator;
}
/**
* Pretty print the current position in the source file for
* error messages.
*
* @return String specification of current position in locator
*/
static public String locationMsg(Locator locator) {
String location = "";
if (locator != null) {
String id = locator.getSystemId();
if (id != null)
location = id; // XML-document name;
location += " line " + locator.getLineNumber();
location += ", column " + locator.getColumnNumber();
location += ": ";
}
return location;
}
/**
* Helper function for extracting information from the XML attributes
* of an XML element.
*
* @param atts the XML attributes found on this element by the parser
* @param param the specific attribute whose value we want
* @param defaultValue used when element did not specify this value
* @return value found if present, otherwise default
*
*/
static public String getStringParam(Attributes atts, String param, String defaultValue, Locator locator)
{
String result = defaultValue;
String value = atts.getValue(World.XMLNS, param);
if (value == null) {
value = atts.getValue("", param);
}
if (value != null) {
result = value;
}
return result;
}
/**
* Helper function for extracting information from the XML attributes
* of an XML element
*
* @param atts the XML attributes found on this element by the parser
* @param param the specific attribute whose value we want
* @param defaultValue used when element did not specify this value
* @return value found if present, otherwise default
* @throws SAXException when value should be but is not an integer
*/
static public int getIntParam(Attributes atts, String param, int defaultValue, Locator locator)
throws SAXException {
int result = defaultValue;
String value = atts.getValue(World.XMLNS, param);
if (value == null) {
value = atts.getValue("", param);
}
if (value != null) {
try {
result = Integer.parseInt(value);
} catch (NumberFormatException e) {
// compose a text with location of error-case:
throw new SAXException(locationMsg(locator) + "Bad integer format of " + value + " for " + param);
}
}
return result;
}
/**
* Helper function for extracting information from the XML attributes
* of an XML element
*
* @param atts the XML attributes found on this element by the parser
* @param param the specific attribute whose value we want
* @param defaultValue used when element did not specify this value
* @return value found if present, otherwise default
* @throws SAXException when value should be but is not a double
*/
static public double getDoubleParam(Attributes atts, String param, double defaultValue, Locator locator)
throws SAXException {
double result = defaultValue;
String value = atts.getValue(World.XMLNS, param);
if (value == null) {
value = atts.getValue("", param);
}
if (value != null) {
try {
result = Double.parseDouble(value);
} catch (NumberFormatException e) {
// compose a text with location of error-case:
throw new SAXException(locationMsg(locator) + "Bad double format of " + value + " for " + param);
}
}
return result;
}
/**
* Helper function for extracting information from the XML attributes
* of an XML element
*
* @param atts the XML attributes found on this element by the parser
* @param param the specific attribute whose value we want
* @param defaultValue used when element did not specify this value
* @return value found if present, otherwise default
*/
static public boolean getBoolParam(Attributes atts, String param, boolean defaultValue, Locator locator)
throws SAXException {
boolean result = defaultValue;
String value = atts.getValue(World.XMLNS, param);
if (value == null) {
value = atts.getValue("", param);
}
if (value != null) {
result = Boolean.parseBoolean(value);
}
return result;
}
/**
* Constructor, keeps the passed frame to build UI for world
*
* @param f window where specified world appears
*/
public FlockingReader(Frame f) {
super();
frame = f;
}
////////////////////////////////////////////////////////////////////
// Event handlers.
////////////////////////////////////////////////////////////////////
public void startDocument ()
{
}
public void endDocument ()
{
}
/**
* SAX callback reached at the start of each element in the document
* This is the point where we create the objects described in the
* world specification, and process commands to update the status of
* existing objects.
*
* This is the place to add code if you have new kinds of agents
* that you want to create with suitable commands in the XML file.
* Check for an element corresponding to your new agent,
* read in values for the parameters for the agent that are
* stored in the attributes, then create a new agent and add
* it to the world (or update the existing one with the specified id).
*
* @param uri local namespace of elements
* @param name name of this element
* @param qName qualified name of this element
* @param atts list of attribute--value data in element declaration
*/
public void startElement (String uri, String name,
String qName, Attributes atts)
throws SAXException
{
if (!World.XMLNS.equals(uri) && !"".equals (uri)) {
System.err.println(locationMsg(locator) + "Namespace: {" + uri + "}" + " not recognized");
return;
}
if (DEFAULT_ELEMENT.equals(name)) {
inDefaults = true;
return;
}
if (World.STATE_NAME.equals(name)) {
int step = getIntParam(atts, World.STEP_NAME, world.getStepCount(), locator);
world.setStepCount(step);
return;
}
if (World.WAIT_NAME.equals(name)) {
int duration = getIntParam(atts, World.WAIT_INTERVAL, World.DEFAULT_WAIT, locator);
try {
Thread.sleep(duration);
} catch (InterruptedException e) {
;
}
return;
}
if (World.XML_NAME.equals(name)) {
int width = getIntParam(atts, World.WIDTH_PARAM, World.DEFAULT_WIDTH, locator);
int height = getIntParam(atts, World.HEIGHT_PARAM, World.DEFAULT_HEIGHT, locator);
int delay = getIntParam(atts, World.WAIT_INTERVAL, World.DEFAULT_WAIT, locator);
String logfile = getStringParam(atts, World.LOGFILE_PARAM, null, locator);
boolean runnable = getBoolParam(atts, World.RUNNABLE_PARAM, true, locator);
boolean debug = getBoolParam(atts, World.DEBUG_PARAM, false, locator);
world = new World(width, height, logfile, runnable, delay, debug);
frame.setSize(width,height);
frame.add(world);
frame.pack();
return;
}
if (world == null) {
System.err.println(locationMsg(locator) + name + " element without a world");
return;
}
int id = getIntParam(atts, Agent.ID_PARAM, nextId++, locator);
Agent a = world.getAgent(id);
if (a != null) {
if (Agent.UPDATE.equals(name))
a.update(atts, locator);
else if (World.DIE_NAME.equals(name))
world.removeAgent(a);
return;
}
if (LightSource.XML_NAME.equals(name)) {
if (inDefaults) {
LightSource.defaultFixedAgentAttributes.update(atts, locator);
LightSource.defaultDynamicAgentAttributes.update(atts, locator);
} else {
LightSource l = new LightSource(world, id, atts, locator);
world.addAgent(l);
}
} else if (Obstacle.XML_NAME.equals(name)) {
if (inDefaults) {
Obstacle.defaultFixedAgentAttributes.update(atts, locator);
Obstacle.defaultDynamicAgentAttributes.update(atts, locator);
} else {
Obstacle o = new Obstacle(world, id, atts, locator);
world.addAgent(o);
}
} else if (Runner.XML_NAME.equals(name)) {
if (inDefaults) {
Runner.defaultDynamicAgentAttributes.update(atts, locator);
Runner.defaultFixedAgentAttributes.update(atts, locator);
} else {
Runner r = new Runner(world, id, atts, locator);
world.addAgent(r);
}
} else if (Follower.XML_NAME.equals(name)) {
if (inDefaults) {
Follower.defaultDynamicAgentAttributes.update(atts, locator);
Follower.defaultFixedAgentAttributes.update(atts, locator);
} else {
Follower f = new Follower(world, id, atts, locator);
world.addAgent(f);
}
} else if (SmartFollower.XML_NAME.equals(name)) {
if (inDefaults) {
SmartFollower.defaultDynamicAgentAttributes.update(atts, locator);
SmartFollower.defaultFixedAgentAttributes.update(atts, locator);
} else {
SmartFollower f = new SmartFollower(world, id, atts, locator);
world.addAgent(f);
}
} else if (Flocker.XML_NAME.equals(name)) {
if (inDefaults) {
Flocker.defaultDynamicAgentAttributes.update(atts, locator);
Flocker.defaultFixedAgentAttributes.update(atts,locator);
Flocker.defaultFlockerAttributes.update(atts, locator);
} else {
Flocker f = new Flocker(world, id, atts, locator);
world.addAgent(f);
}
} else if (ReactivePredator.XML_NAME.equals(name)) {
if (inDefaults) {
ReactivePredator.defaultDynamicAgentAttributes.update(atts, locator);
ReactivePredator.defaultFixedAgentAttributes.update(atts, locator);
} else {
ReactivePredator p = new ReactivePredator(world, id, atts, locator);
world.addAgent(p);
}
} else if (ModelPredator.XML_NAME.equals(name)) {
if (inDefaults) {
ModelPredator.defaultDynamicAgentAttributes.update(atts, locator);
ModelPredator.defaultFixedAgentAttributes.update(atts, locator);
} else {
ModelPredator p = new ModelPredator(world, id, atts, locator);
world.addAgent(p);
}
} else {
System.err.println(locationMsg(locator) + "Unknown agent type " + name);
}
}
/**
* This method is called by the XML document parser when
* it reaches the end of an element specification.
* The only time we need this is in the case of state
* descriptions. The end of a state description marks
* the complete specification of the update of all the
* agents in the world after another time step of action.
* At this point we should make sure that the simulation
* is visible and redraw the world to reflect its updated state.
*
* @param uri local namespace of elements
* @param name name of this element
* @param qName qualified name of this element
*/
public void endElement (String uri, String name, String qName)
{
if ((World.XMLNS.equals(uri) || "".equals (uri))) {
if (World.STATE_NAME.equals(name)) {
frame.setVisible(true);
if (world != null) {
world.repaint();
}
}
else if (DEFAULT_ELEMENT.equals(name)) {
inDefaults = false;
}
}
}
public void characters (char ch[], int start, int length)
{
}
}