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