-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSimulationController.java
More file actions
executable file
·261 lines (202 loc) · 7.25 KB
/
SimulationController.java
File metadata and controls
executable file
·261 lines (202 loc) · 7.25 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
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
/*****************************************************************************
* Copyright 2007-2015 DCA-FEEC-UNICAMP
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Contributors:
* Klaus Raizer, Andre Paraense, Ricardo Ribeiro Gudwin
*****************************************************************************/
package support;
import com.google.gson.Gson;
import ws3dproxy.model.Creature;
import ws3dproxy.model.World;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;
/**
* @author rgudwin
*/
public class SimulationController {
private Timer t;
private Random r;
private Creature creature;
private Date initDate;
private int defaultTime = 10;
private double time = 0;
private int counterToGenerateThings = 0;
//private Agent agent;
private File fileEnergySpent;
private File fileCreatureScore;
private File fileDrivesActivation;
private List<Result> resultEnergySpent;
private List<Result> resultDrivesActivation;
private List<Result> resultCreatureScore;
public SimulationController(String name) {
String timeLog = new SimpleDateFormat("yyyyMMdd_HHmmss").format(Calendar.getInstance().getTime());
setResultEnergySpent(new ArrayList<>());
setResultDrivesActivation(new ArrayList<>());
setResultCreatureScore(new ArrayList<>());
setR(new Random());
setFileEnergySpent(new File("reportFiles/Lida_EnergySpent" + timeLog + ".txt"));
setFileCreatureScore(new File("reportFiles/Lida_Score" + timeLog + ".txt"));
setInitDate(new Date());
}
public void setCreature(Creature creature) {
this.creature = creature;
}
public Creature getCreature() {
return this.creature;
}
public void StartTimer() {
setT(new Timer());
MVTimerTask tt = new MVTimerTask(this);
getT().scheduleAtFixedRate(tt, 0, 1000);
}
public synchronized void tick() {
if ((getCounterToGenerateThings() /60) == 1) {
createObjectsInWorld();
setCounterToGenerateThings(0);
}
reportEnergySpent(getTime());
reportCreatureScore(getTime());
if((getTime() / 60) == getDefaultTime()){
finalizeReport("Creature's Energy", "Time", "Energy", getResultEnergySpent(), getFileEnergySpent());
finalizeReport("Creature's Score", "Time", "Score", getResultCreatureScore(), getFileCreatureScore());
getT().cancel();
getT().purge();
//agent.stop();
}
setCounterToGenerateThings(getCounterToGenerateThings() + 1);
setTime(getTime() + 1);
}
private void createObjectsInWorld(){
try {
Random random = new Random();
World.createJewel(random.nextInt(6), getR().nextInt(800), getR().nextInt(600));
World.createJewel(random.nextInt(6), getR().nextInt(800), getR().nextInt(600));
World.createJewel(random.nextInt(6), getR().nextInt(800), getR().nextInt(600));
World.createFood(random.nextInt(2), getR().nextInt(800), getR().nextInt(600));
} catch (Exception e) {
e.printStackTrace();
}
}
private boolean checkTimeStop() {
boolean bFinish = false;
if ((double) ((new Date()).getTime() - getInitDate().getTime()) >= this.getDefaultTime() * 60 * Math.pow(10, 3)) {
bFinish = true;
}
return bFinish;
}
private void reportEnergySpent(double time) {
this.getResultEnergySpent().add(new Result("Energy Spent", time, getCreature().getFuel()));
}
private void reportCreatureScore(double time) {
this.getResultCreatureScore().add(new Result("Score Obtained", time, getCreature().s.score));
}
private void finalizeReport(String graphName, String xTitle, String yTitle, List<Result> results, File file) {
Gson gson = new Gson();
String sResults = gson.toJson(new Graph(graphName, xTitle, yTitle, results));
writeInFile(sResults, file);
}
private void writeInFile(String line, File file) {
BufferedWriter writer = null;
try {
writer = new BufferedWriter(new FileWriter(file));
writer.write(line);
System.out.println(file.getAbsolutePath());
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public List<Result> getResultEnergySpent() {
return resultEnergySpent;
}
public void setResultEnergySpent(List<Result> resultEnergySpent) {
this.resultEnergySpent = resultEnergySpent;
}
public List<Result> getResultDrivesActivation() {
return resultDrivesActivation;
}
public void setResultDrivesActivation(List<Result> resultDrivesActivation) {
this.resultDrivesActivation = resultDrivesActivation;
}
public List<Result> getResultCreatureScore() {
return resultCreatureScore;
}
public void setResultCreatureScore(List<Result> resultCreatureScore) {
this.resultCreatureScore = resultCreatureScore;
}
public Timer getT() {
return t;
}
public void setT(Timer t) {
this.t = t;
}
public Random getR() {
return r;
}
public void setR(Random r) {
this.r = r;
}
public Date getInitDate() {
return initDate;
}
public void setInitDate(Date initDate) {
this.initDate = initDate;
}
public int getDefaultTime() {
return defaultTime;
}
public void setDefaultTime(int defaultTime) {
this.defaultTime = defaultTime;
}
public double getTime() {
return time;
}
public void setTime(double time) {
this.time = time;
}
public int getCounterToGenerateThings() {
return counterToGenerateThings;
}
public void setCounterToGenerateThings(int counterToGenerateThings) {
this.counterToGenerateThings = counterToGenerateThings;
}
public File getFileEnergySpent() {
return fileEnergySpent;
}
public void setFileEnergySpent(File fileEnergySpent) {
this.fileEnergySpent = fileEnergySpent;
}
public File getFileCreatureScore() {
return fileCreatureScore;
}
public void setFileCreatureScore(File fileCreatureScore) {
this.fileCreatureScore = fileCreatureScore;
}
public File getFileDrivesActivation() {
return fileDrivesActivation;
}
public void setFileDrivesActivation(File fileDrivesActivation) {
this.fileDrivesActivation = fileDrivesActivation;
}
}