-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDriver.java
More file actions
executable file
·166 lines (138 loc) · 5.38 KB
/
Driver.java
File metadata and controls
executable file
·166 lines (138 loc) · 5.38 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
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
import java.lang.Integer;
import java.util.*;
public class Driver extends SimulationManager {
Environment world;
int numMacrophages;
int numBacteria;
ArrayList<Microbe> agents;
public Driver(int _numMacrophages, int _numBacteria, int _gridSize)
{
// initialize GUI grid size and gui cell width
super(_gridSize, 50);
// set the number of each type of microbe in the environment
numMacrophages = _numMacrophages;
numBacteria = _numBacteria;
// add a list to keep track of all our microbes in our environment
agents = new ArrayList<Microbe>();
// initialize our environement with the specified parameters and a pointer to the driver
world = new Environment(_gridSize, _gridSize, numMacrophages, numBacteria, this);
// add the specified number of bacterium to our world
Bacterium nextBacterium;
for (int i = 0; i < numBacteria; i++)
{
nextBacterium = new Bacterium(world); // give our bacteria a pointer to the world
agents.add(nextBacterium); // add the bacteria to the list in our driver
}
// add the specified number of macrophages to our world
Macrophage nextMacrophage;
for (int i = 0; i < numMacrophages; i++)
{
nextMacrophage = new Macrophage(world); // give our macrophage a pointer to the world
agents.add(nextMacrophage); // add the macrophage to the list in our driver
}
// add the microbes the our enviroment
for (Microbe agent : agents)
{
world.addMicrobe(agent);
}
// initialize timer
time = 0;
// Initialize GUI
this.gui = new AgentGUI(this, _gridSize, 50);
}
/**************************************************************************
* Accessor method that returns the number of macrophages still present.
* @return an integer representing the number of macrophages present
**************************************************************************/
public int getNumMacrophages() {
return numMacrophages;
}
/**************************************************************************
* Accessor method that returns the number of bacteria still present.
* @return an integer representing the number of bacteria present
**************************************************************************/
public int getNumBacteria() {
return numBacteria;
}
/**************************************************************************
* Accessor method that returns the current time of the simulation clocl.
* @return a double representing the current time in simulated time
**************************************************************************/
public double getTime() {
return time;
}
/**************************************************************************
* Method that constructs and returns a single list of all agents present.
* This method is used by the gui drawing routines to update the gui based
* on the number and positions of agents present.
*
* @return an ArrayList<AgentInterface> containing references to all agents
**************************************************************************/
public ArrayList<AgentInterface> getListOfAgents() {
ArrayList<AgentInterface> retval = new ArrayList<AgentInterface>();
for (int i = 0; i < agents.size(); i++) {
retval.add(agents.get(i));
}
//for (Microbe m : agents) {
// retval.add(m);
//}
return retval;
}
public void addBacterium(Microbe agent)
{
agents.add(agent);
Collections.sort(agents);
numBacteria++;
}
public void removeBacterium(Microbe agent)
{
if (agents.remove(agent)) {
numBacteria--;
}
//Collections.sort(agents);
}
public void addMacrophage(Microbe agent)
{
agents.add(agent);
Collections.sort(agents);
numMacrophages++;
}
public void removeMacrophage(Microbe agent)
{
if (agents.remove(agent)) {
numMacrophages--;
}
}
/**************************************************************************
* Method used to run the simulation. This method should contain the
* implementation of your next-event simulation engine (while loop handling
* various event types).
*
* @param guiDelay delay in seconds between redraws of the gui
**************************************************************************/
public void run(double guiDelay) throws InterruptedException {
while(true) {
Collections.sort(agents);
time = (agents.get(0)).getNextEventTime();
System.out.printf("t = %f\n", time);
if (agents.size() == 0) {return;}
(agents.get(0)).executeNextEvent();
gui.update(guiDelay);
}
}
public static void main(String[] args) {
if (args.length < 3)
{
System.out.println("Usage: Java Driver numRows numMacrophages numBacteria");
System.exit(1);
}
int numRowCols = Integer.parseInt(args[0]); // grid size
int numMacrophages = Integer.parseInt(args[1]); // numMacrophages
int numBacteria = Integer.parseInt(args[2]); // numBacteria
Driver d = new Driver(numMacrophages, numBacteria, numRowCols);
try {
d.run(.01);
} catch (InterruptedException ex) {
}
}
}