-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimulation.java
More file actions
132 lines (115 loc) · 4.03 KB
/
Simulation.java
File metadata and controls
132 lines (115 loc) · 4.03 KB
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
import java.awt.*;
import java.awt.event.*;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;
/**
* Simulation manages a window, file I/O, and simulation stepping
* for a test scenario describing agents acting in an environment.
*
* @author Matthew Stone
* @version 1.0
*/
public class Simulation extends Frame {
/**
* Java AWT components are required to be serializable,
* and therefore require a long int id indicating what
* version of the code the serialization comes from.
*/
private static final long serialVersionUID = 1L;
/**
* Holds the world that this object will be simulating.
* @see World
*/
private World w = null;
/**
* Constructor: creates the frame and then adds
* cleanup code hiding the window, terminating
* the simulation, releasing graphics resources,
* and quitting.
*/
public Simulation() {
super("Agents acting in an environment");
// exit cleanly
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
setVisible(false);
if (w != null && w.isRunnable()) {
w.finishLogging();
}
w = null;
dispose();
System.exit(0);
}
});
}
/**
* We create a Cleanup object as a handler for
* shutdown events (triggered for example by
* hitting Control-C at the command line).
* This is called a shutdown hook.
* The code here makes sure that any log file
* being created remains valid XML.
*/
class Cleanup extends Thread {
public void run() {
if (w != null && w.isRunnable()) {
w.finishLogging();
}
}
}
/**
* Command-line interface to simulation class.
*
* @param args array of strings specified on the
* command line; should specify a
* single XML specification of a world
*/
public static void main(String[] args) {
Simulation s = new Simulation();
// Clean up if control-C is pressed
Runtime.getRuntime().addShutdownHook(s.new Cleanup());
if (args.length != 1) {
System.err.println("Usage error: run as <program> <specfile> for a single XML world spec.");
}
try {
// Set up SAX reader, which processes XML objects as file is read.
XMLReader xr = XMLReaderFactory.createXMLReader();
FlockingReader handler = new FlockingReader(s);
xr.setContentHandler(handler);
xr.setErrorHandler(handler);
// Parse the XML
FileReader r = new FileReader(args[0]);
xr.parse(new InputSource(r));
// Show the simulation on screen, if you haven't already
s.w = handler.getWorld();
if (s.w == null)
s.pack();
s.setVisible(true);
// Get ready to record activities
if (s.w != null)
s.w.startLogging();
// Run any simulation indefinitely
while (s.w != null && s.w.isRunnable()) {
s.w.stepWorld();
try {
Thread.sleep(s.w.getDelay());
}
catch (InterruptedException e) {
}
}
// Clean up if the world spec did not want a simulation
s.processWindowEvent(new WindowEvent(s, WindowEvent.WINDOW_CLOSING));
} catch (SAXException e) {
System.err.println(e.getMessage());
} catch (FileNotFoundException e) {
System.err.println(e.getMessage());
} catch (IOException e) {
System.err.println(e.getMessage());
}
}
}