1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package cz.cuni.amis.pogamut.ut2004.agent.navigation.stuckdetector;
18
19 import cz.cuni.amis.pogamut.base.agent.navigation.IStuckDetector;
20 import cz.cuni.amis.pogamut.base.communication.worldview.IWorldView;
21 import cz.cuni.amis.pogamut.base.communication.worldview.event.IWorldEventListener;
22 import cz.cuni.amis.pogamut.base.communication.worldview.object.IWorldObjectEvent;
23 import cz.cuni.amis.pogamut.base.communication.worldview.object.IWorldObjectListener;
24 import cz.cuni.amis.pogamut.base.communication.worldview.object.event.WorldObjectUpdatedEvent;
25 import cz.cuni.amis.pogamut.base3d.worldview.object.ILocated;
26 import cz.cuni.amis.pogamut.ut2004.bot.impl.UT2004Bot;
27 import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.EndMessage;
28 import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.Self;
29 import java.util.logging.Level;
30 import java.util.logging.Logger;
31
32
33
34
35
36
37
38
39
40
41
42 public class AccUT2004TimeStuckDetector implements IStuckDetector {
43
44
45
46
47 private static final double NO_MOVEMENT_SIZE = 10;
48
49
50
51
52
53 private static int DEFAULT_TIMEOUT = 3000;
54
55
56
57
58 private static int DEFAULT_WAITING_TIMEOUT = 10000;
59
60
61
62
63 private UT2004Bot bot;
64
65
66
67
68
69 private double timeout;
70
71
72
73
74
75 private double waitingTimeout;
76
77 private boolean botWaiting = false;
78
79
80
81
82
83 private boolean bWasIsStuckCalled = false;
84
85
86
87
88 private long currentTime;
89
90
91
92
93
94
95
96 private class SelfListener implements IWorldObjectListener<Self> {
97
98 public SelfListener(IWorldView worldView) {
99 worldView.addObjectListener(Self.class, WorldObjectUpdatedEvent.class, this);
100 }
101
102 @Override
103 public void notify(IWorldObjectEvent<Self> event) {
104 eventSelf(event);
105 }
106
107 };
108
109
110
111
112
113
114
115 private class EndListener implements IWorldEventListener<EndMessage> {
116
117 public EndListener(IWorldView worldView) {
118 worldView.addEventListener(EndMessage.class, this);
119 }
120
121 @Override
122 public void notify(EndMessage event) {
123 eventEndMessage(event);
124 }
125
126 };
127
128
129
130
131
132 private SelfListener selfListener;
133
134
135
136
137
138 private EndListener endListener;
139
140
141
142
143
144 private Double lastMovementTime = null;
145
146
147
148
149 private boolean stuck = false;
150
151 private boolean enabled;
152
153 private Logger log;
154
155 public AccUT2004TimeStuckDetector(UT2004Bot bot) {
156 this(bot, DEFAULT_TIMEOUT, DEFAULT_WAITING_TIMEOUT);
157 }
158
159 public AccUT2004TimeStuckDetector(UT2004Bot bot, double timeoutMillis,
160 double waitingTimeoutMillis) {
161 if (this.log == null) {
162 this.log = bot.getLogger().getCategory(
163 this.getClass().getSimpleName());
164 }
165 this.bot = bot;
166 this.timeout = timeoutMillis;
167 this.waitingTimeout = waitingTimeoutMillis;
168 selfListener = new SelfListener(bot.getWorldView());
169 endListener = new EndListener(bot.getWorldView());
170 }
171
172 public void eventSelf(IWorldObjectEvent<Self> event) {
173 if (!enabled) {
174 return;
175 }
176
177
178 long simTime = event.getObject().getSimTime();
179 if(simTime == 0) {
180 return;
181 }
182 currentTime = simTime;
183
184
185 if (!bWasIsStuckCalled) {
186 return;
187 }
188
189
190 if (event.getObject().getVelocity().size() > NO_MOVEMENT_SIZE || lastMovementTime == null) {
191 lastMovementTime = (double) event.getObject().getSimTime();
192 } else {
193 log.info("Bot not moving. Velocity: " + event.getObject().getVelocity().size());
194 }
195
196 }
197
198 public void eventEndMessage(EndMessage event) {
199 if (!enabled || lastMovementTime == null) {
200 return;
201 }
202
203
204 currentTime = event.getSimTime();
205
206 if (!bWasIsStuckCalled) {
207 return;
208 }
209 if (botWaiting) {
210 if (currentTime - lastMovementTime >= waitingTimeout) {
211 stuck = true;
212 if (log != null && log.isLoggable(Level.WARNING)) {
213 log.warning("Bot is WAITING for more than " + waitingTimeout + " ms, considering that it has stuck.");
214 }
215 }
216 } else {
217 if (currentTime - lastMovementTime >= timeout) {
218 stuck = true;
219 if (log != null && log.isLoggable(Level.WARNING)) {
220 log.warning("Bot should be moving but it is standing still for more than " + timeout + " ms, considering that it has stuck.");
221 }
222 }
223 }
224 }
225
226 @Override
227 public void setEnabled(boolean state) {
228 if (this.enabled == state) {
229 return;
230 }
231 this.enabled = state;
232 }
233
234 @Override
235 public void setBotWaiting(boolean state) {
236 botWaiting = state;
237 lastMovementTime = null;
238 }
239
240 @Override
241 public boolean isStuck() {
242 if (!bWasIsStuckCalled) {
243
244
245 lastMovementTime = (double) currentTime;
246 bWasIsStuckCalled = true;
247 return false;
248 }
249
250 return stuck;
251 }
252
253 @Override
254 public void reset() {
255 if (log != null && log.isLoggable(Level.FINER)) {
256 log.finer("Reset.");
257 }
258 lastMovementTime = Double.NEGATIVE_INFINITY;
259 bWasIsStuckCalled = false;
260 stuck = false;
261 }
262
263 @Override
264 public void setBotTarget(ILocated target) {
265
266 }
267
268 }