1 package cz.cuni.amis.pogamut.ut2004multi.communication.worldview;
2
3 import java.io.IOException;
4 import java.util.HashMap;
5 import java.util.HashSet;
6 import java.util.LinkedList;
7 import java.util.List;
8 import java.util.Map;
9 import java.util.Random;
10 import java.util.Set;
11 import java.util.WeakHashMap;
12 import java.util.concurrent.CountDownLatch;
13 import java.util.concurrent.TimeUnit;
14 import java.util.logging.ConsoleHandler;
15 import java.util.logging.FileHandler;
16 import java.util.logging.Formatter;
17 import java.util.logging.Level;
18 import java.util.logging.Logger;
19 import java.util.logging.SimpleFormatter;
20
21 import junit.framework.Assert;
22
23 import org.junit.Ignore;
24 import org.junit.Test;
25
26 import cz.cuni.amis.pogamut.base.agent.IAgentId;
27 import cz.cuni.amis.pogamut.base.communication.mediator.IMediator;
28 import cz.cuni.amis.pogamut.base.communication.worldview.object.WorldObjectId;
29 import cz.cuni.amis.pogamut.base.component.controller.ComponentDependencies;
30 import cz.cuni.amis.pogamut.base.component.controller.ComponentDependencyType;
31 import cz.cuni.amis.pogamut.base.component.lifecyclebus.LifecycleBus;
32 import cz.cuni.amis.pogamut.base.utils.logging.AgentLogger;
33 import cz.cuni.amis.pogamut.base.utils.logging.IAgentLogger;
34 import cz.cuni.amis.pogamut.multi.agent.ITeamedAgentId;
35 import cz.cuni.amis.pogamut.multi.agent.impl.TeamedAgentId;
36 import cz.cuni.amis.pogamut.multi.communication.worldview.ILocalWorldView;
37 import cz.cuni.amis.pogamut.multi.communication.worldview.ISharedWorldView;
38 import cz.cuni.amis.pogamut.multi.communication.worldview.impl.BatchAwareLocalWorldView;
39 import cz.cuni.amis.pogamut.multi.utils.timekey.TimeKey;
40 import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.BeginMessage;
41 import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.EndMessage;
42 import cz.cuni.amis.pogamut.ut2004.component.ComponentStub;
43 import cz.cuni.amis.pogamut.ut2004multi.communication.worldview.objects.TestCompositeViewableObject;
44 import cz.cuni.amis.pogamut.ut2004multi.communication.worldview.objects.TestCompositeViewableObjectMessage;
45 import cz.cuni.amis.pogamut.ut2004multi.communication.worldview.stubs.MediatorStub;
46 import cz.cuni.amis.pogamut.ut2004multi.communication.worldview.stubs.UT2004TestLocalWorldView;
47 import cz.cuni.amis.pogamut.ut2004multi.communication.worldview.stubs.UT2004TestSharedWorldView;
48 import cz.cuni.amis.tests.BaseTest;
49
50 @Ignore
51 public class Test02_UT2004VisionLocalWorldView_visibilityTest extends BaseTest {
52
53 private static final long GLOBAL_TIMEOUT_IN_MINUTES = 10;
54
55 public static FileHandler fh;
56
57 public static Logger global;
58 public static List<IAgentLogger> agentLogs;
59
60
61 public static ISharedWorldView sharedWV;
62
63
64 public static void setLogLevel( Level level)
65 {
66 global.setLevel(level);
67 for ( IAgentLogger log : agentLogs)
68 {
69 log.setLevel(level);
70 }
71 }
72
73 public static void initSWV()
74 {
75 try {
76 fh = new FileHandler("./log01");
77 } catch (SecurityException e) {
78
79 e.printStackTrace();
80 } catch (IOException e) {
81
82 e.printStackTrace();
83 }
84 Formatter f = new SimpleFormatter();
85 fh.setFormatter(f);
86
87 agentLogs = new LinkedList<IAgentLogger>();
88
89 global = Logger.getLogger("Global");
90 global.setLevel(Level.FINER);
91 ConsoleHandler consoleHandler = new ConsoleHandler();
92 consoleHandler.setLevel(Level.FINER);
93 global.addHandler( consoleHandler );
94 global.addHandler(fh);
95 sharedWV = new UT2004TestSharedWorldView(global);
96 }
97
98 public static class Handler
99 {
100 Map<IAgentId, Integer> cycles;
101
102 public Handler()
103 {
104 cycles = new HashMap<IAgentId, Integer>();
105 }
106
107 public void addNew( IAgentId id, int cycles)
108 {
109 this.cycles.put(id, cycles);
110 }
111
112 public boolean allFinished()
113 {
114 for ( Integer i : cycles.values() )
115 {
116 if ( i >= 0)
117 {
118 return false;
119 }
120 }
121 return true;
122 }
123
124 public synchronized void setEnd( IAgentId id)
125 {
126 this.cycles.put(id, -1);
127 }
128
129 public synchronized void decrease( IAgentId id)
130 {
131 int n = cycles.get(id);
132 --n;
133 cycles.put(id, n);
134 }
135
136 public int cyclesToRun( IAgentId id)
137 {
138 return ( cycles.get(id ));
139 }
140 }
141
142 public static class VisibilityChecker
143 {
144 public VisibilityChecker(TeamedAgentId agentId)
145 {
146 this.agentId = agentId;
147 }
148
149 TeamedAgentId agentId;
150 Map<TimeKey, Set<WorldObjectId>> visibleObjects = new WeakHashMap<TimeKey, Set<WorldObjectId>>();
151
152 public Set<WorldObjectId> getAllVisible( long time )
153 {
154 return visibleObjects.get( TimeKey.get(time) );
155 }
156
157 public void addVisible( WorldObjectId id, long time )
158 {
159 Set<WorldObjectId> set = visibleObjects.get( TimeKey.get(time));
160 if ( set == null )
161 {
162 set = new HashSet<WorldObjectId>();
163 visibleObjects.put(TimeKey.get(time), set);
164 }
165 set.add(id);
166 }
167
168 }
169
170 public static Random rand = new Random( System.currentTimeMillis() );
171
172
173
174
175
176
177 public static class EventGenerator extends Thread
178 {
179 int events;
180 long time;
181 ILocalWorldView localWV;
182 TeamedAgentId agentId;
183
184 VisibilityChecker checker;
185
186 public EventGenerator( int events, long time, ILocalWorldView localWV, VisibilityChecker visChecker)
187 {
188 this.events = events;
189 this.time = time;
190 this.localWV = localWV;
191 agentId = (TeamedAgentId) localWV.getAgentId();
192 this.checker = visChecker;
193 }
194
195 protected void generateEvents()
196 {
197 System.out.println(agentId + " : generating Events [Time:" + time + "]" );
198 localWV.notify( new BeginMessage(this.time));
199 for ( int i = 0; i < events; ++i)
200 {
201
202 if ( rand.nextInt() % 100 > 30)
203 {
204 WorldObjectId id = WorldObjectId.get("TestObject["+i+"]");
205 TestCompositeViewableObject obj = new TestCompositeViewableObjectMessage(id, this.time, "LS:"+agentId.toString()+"["+i+"]"+"("+time+")",
206 i+time, "ShS:"+agentId.getTeamId().toString()+"["+i+"]"+"("+time+")",
207 i+1000+time, "StaticString["+i+"]" , (long)i, true);
208 checker.addVisible( id, this.time );
209 localWV.notify(obj.createUpdateEvent(time, agentId.getTeamId()));
210 }
211 }
212
213 localWV.notify( new EndMessage(this.time ) );
214 System.out.println(agentId + ": generating end");
215 }
216
217 @Override
218 public void run()
219 {
220 try {
221 generateEvents();
222 } catch (Exception e) {
223 e.printStackTrace();
224 failure = true;
225 totalCountDown2();
226 return;
227 }
228 latch2.countDown();
229 }
230 }
231
232 public static class LogicRunner extends Thread
233 {
234 protected BatchAwareLocalWorldView wv;
235 protected ITeamedAgentId id;
236
237 int runs;
238 int objects;
239 long sleepTime;
240 Handler handler;
241 ComponentStub starter;
242 VisibilityChecker checker;
243
244
245 public LogicRunner(ITeamedAgentId id, int runs, int objects, long sleepTime, Handler handler, VisibilityChecker visChecker)
246 {
247 IAgentLogger log = new AgentLogger(id);
248 agentLogs.add(log);
249 log.setLevel(Level.ALL);
250 log.addDefaultConsoleHandler();
251 log.addDefaultHandler(fh);
252 LifecycleBus bus = new LifecycleBus(log);
253 starter = new ComponentStub(log,bus );
254 IMediator m = new MediatorStub(log);
255 try
256 {
257 wv = new UT2004TestLocalWorldView( new ComponentDependencies(ComponentDependencyType.STARTS_WITH).add(starter),
258 m, bus, log, sharedWV, id);
259 }
260 catch (Exception e)
261
262 {
263 failure = true;
264 e.printStackTrace();
265 }
266
267
268 this.id = id;
269 this.runs = runs;
270 this.objects = objects;
271 this.sleepTime = sleepTime;
272 this.handler = handler;
273 this.checker = visChecker;
274 handler.addNew(id, runs);
275 }
276
277 public void startWV()
278 {
279 starter.getController().manualStart("Test");
280 }
281
282 public void setSleepTime( long newSleepTime )
283 {
284 this.sleepTime = newSleepTime;
285 }
286
287 @Override
288 public void run()
289 {
290 try {
291 System.out.println( id + " : Logic Runner run()");
292 for ( int r = 0; r <= runs; ++r)
293 {
294 if (failure) throw new RuntimeException("FAILURE DETECTED!");
295 wv.lock();
296
297
298 System.out.println( id + "Runner run ["+r+"] remaining : time " + wv.getCurrentTimeKey().getTime() + ", remaining runs " + handler.cyclesToRun(id) );
299
300 Set<WorldObjectId> shouldBeVisible = checker.getAllVisible(wv.getCurrentTimeKey().getTime());
301 Set<WorldObjectId> areVisible = wv.getAllVisible( TestCompositeViewableObject.class ).keySet();
302
303 if (areVisible.containsAll(shouldBeVisible) && (areVisible.size() == shouldBeVisible.size()))
304 {
305 System.out.println("VisibilityCheck OK ; visible : " + areVisible.size());
306 }
307 else
308 {
309 System.err.println(" ShouldBeVisible : " + shouldBeVisible.size() + " ; Visible : " + areVisible.size() );
310 System.out.println("Should BE VISIBLE :");
311
312 for ( WorldObjectId id : shouldBeVisible )
313 {
314 System.out.println(id);
315 }
316 System.out.println("VISIBLE :");
317 for ( WorldObjectId id : areVisible )
318 {
319 System.out.println(id);
320 }
321 failure = true;
322 Assert.fail("VisibilityFail");
323 }
324
325
326 this.sleep(sleepTime);
327 wv.unlock();
328 handler.decrease(id);
329 }
330 } catch (Exception e) {
331 e.printStackTrace();
332 failure = true;
333 totalCountDown();
334 return;
335 }
336 latch.countDown();
337 }
338 }
339
340
341
342
343
344
345
346 public static class EventGeneratorHandler extends Thread
347 {
348 Map<TeamedAgentId, ILocalWorldView> localWorldViews;
349 Map<TeamedAgentId, VisibilityChecker> checkers;
350 int eventsPerCycle;
351 long currentTime;
352 Handler handler;
353 long sleepTime;
354
355 public EventGeneratorHandler( int eventsPerCycle, long initTime, long sleepTime, Handler handlerInstance)
356 {
357 this.eventsPerCycle = eventsPerCycle;
358 this.currentTime = initTime;
359 this.handler = handlerInstance;
360 this.sleepTime = sleepTime;
361 localWorldViews = new HashMap<TeamedAgentId,ILocalWorldView>();
362 checkers = new HashMap<TeamedAgentId,VisibilityChecker>();
363 }
364
365 public void addWorldView( ILocalWorldView wv, VisibilityChecker checker)
366 {
367 this.localWorldViews.put( (TeamedAgentId)wv.getAgentId() , wv);
368 this.checkers.put( (TeamedAgentId)wv.getAgentId(), checker);
369 }
370
371 @Override
372 public void run()
373 {
374 try {
375 while ( !handler.allFinished() )
376 {
377 if (failure) throw new RuntimeException("FAILURE DETECTED!");
378 List<Thread> thrds = new LinkedList<Thread>();
379 for ( TeamedAgentId id : localWorldViews.keySet() )
380 {
381 if ( handler.cyclesToRun(id) >= 0)
382 {
383 thrds.add( new EventGenerator(eventsPerCycle, currentTime, localWorldViews.get(id), checkers.get(id) ) );
384 }
385 }
386 latch2 = new CountDownLatch(thrds.size());
387 for (Thread t : thrds)
388 {
389 t.start();
390 }
391 latch2.await(GLOBAL_TIMEOUT_IN_MINUTES, TimeUnit.MINUTES);
392 if (latch2.getCount() > 0 || failure) {
393 throw new RuntimeException("FAILURE DETECTED!");
394 }
395 ++currentTime;
396 sleep(sleepTime);
397 }
398 } catch (Exception e) {
399 e.printStackTrace();
400 failure = true;
401 totalCountDown();
402 return;
403 }
404 latch.countDown();
405 }
406
407 }
408
409 static boolean failure = false;
410
411 static CountDownLatch latch;
412 static CountDownLatch latch2;
413
414 @Test
415 public void simpleTest()
416 {
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481 }
482
483
484 public static void totalCountDown2() {
485 while (latch2.getCount() > 0) latch2.countDown();
486 }
487
488 public static void totalCountDown() {
489 while (latch.getCount() > 0) latch.countDown();
490 }
491 }