View Javadoc

1   package cz.cuni.amis.pogamut.usar2004.agent.module.logic;
2   
3   import java.util.logging.Level;
4   import java.util.logging.Logger;
5   
6   import com.google.inject.Inject;
7   
8   import cz.cuni.amis.pogamut.base.agent.module.IAgentLogic;
9   import cz.cuni.amis.pogamut.base.component.controller.ComponentDependencies;
10  import cz.cuni.amis.pogamut.base.component.controller.ComponentDependencyType;
11  import cz.cuni.amis.pogamut.base3d.ILockableVisionWorldView;
12  import cz.cuni.amis.pogamut.usar2004.agent.USAR2004Bot;
13  
14  
15  public class SyncUSAR2004BotLogic<BOT extends USAR2004Bot<? extends ILockableVisionWorldView, ?, ?>> extends USAR2004BotLogic<BOT> {
16  
17  
18  
19  	//private ObjectEventReact<ConfigChange, ?> configChangeReaction;
20  	
21  	//private EventReact<EndMessage>        endReactionAfterRespawn;
22  	//private int						      shouldExecuteLogicLatch = 0;
23  	
24  	/*
25           //not needed
26           private ICommandListener<Respawn> respawnListener = new ICommandListener<Respawn>() {
27  
28  		@Override
29  		public void notify(Respawn event) {			
30  			synchronized(respawnListener) {
31  				endReactionAfterRespawn.enable();
32  				shouldExecuteLogicLatch = 2;
33  			}
34  		}
35  		
36  	};*/
37  	
38  	@Inject
39  	public SyncUSAR2004BotLogic(BOT agent, IAgentLogic logic) {
40  		this(agent, logic, null, new ComponentDependencies(ComponentDependencyType.STARTS_AFTER).add(agent.getWorldView()));
41  	}
42  	
43  	public SyncUSAR2004BotLogic(BOT agent, IAgentLogic logic, Logger log) {
44  		this(agent, logic, log, new ComponentDependencies(ComponentDependencyType.STARTS_AFTER).add(agent.getWorldView()));
45  	}
46  	
47  	public SyncUSAR2004BotLogic(BOT agent, IAgentLogic logic, Logger log, ComponentDependencies dependencies) {
48  		super(agent, logic, log, dependencies);
49  		/*
50                   //USAR bots should not die
51                   endReactionAfterRespawn = new EventReact<EndMessage>(EndMessage.class, agent.getWorldView()) {
52  			@Override
53  			protected void react(EndMessage event) {
54  				synchronized(respawnListener) {
55  					if (shouldExecuteLogicLatch > 0) {
56  						--shouldExecuteLogicLatch;
57  					}
58  				}
59  			}
60  		};*/
61  		//agent.getAct().addCommandListener(Respawn.class, respawnListener);
62  		
63  		/*
64                   //it is not possible to change logic frequency at runtime in USARSim...
65                   configChangeReaction = new ObjectEventReact<ConfigChange, IWorldObjectEvent<ConfigChange>>(ConfigChange.class, agent.getWorldView()) {
66  			@Override
67  			protected void react(IWorldObjectEvent<ConfigChange> event) {
68  				setLogicFrequency(1 / (Math.max(0.1, event.getObject().getVisionTime() - 0.02)));
69  			}
70  		};*/
71                  
72  	}
73  	
74  	@Override
75  	protected void beforeLogic(String threadName) {
76  		super.beforeLogic(threadName);
77                  //System.out.println("SyncUSAR2004BotLogic.beforeLogic()");
78  
79  		if (log.isLoggable(Level.FINEST)) log.finest(threadName + ": Locking world view.");
80  		agent.getWorldView().lock();
81  		if (log.isLoggable(Level.FINER)) log.finer(threadName + ": World view locked.");
82  	}
83  	
84  	@Override
85  	protected void afterLogic(String threadName) {
86  		super.afterLogic(threadName);
87                  //System.out.println("SyncUSAR2004BotLogic.afterLogic()");
88  
89  		if (log.isLoggable(Level.FINEST)) log.finest(threadName + ": Unlocking world view.");
90  		agent.getWorldView().unlock();
91  		if (log.isLoggable(Level.FINER)) log.finer(threadName + ": World view unlocked.");
92  	}
93  	
94  	@Override
95  	protected void afterLogicException(String threadName, Throwable e) {
96  		super.afterLogicException(threadName, e);
97  
98                  //System.out.println("SyncUSAR2004BotLogic.afterLogicException()");
99  		if (agent.getWorldView().isLocked()) {
100 			if (log.isLoggable(Level.FINEST)) log.finest("Unlocking world view.");
101 			agent.getWorldView().unlock();
102 			if (log.isLoggable(Level.FINER)) log.finer("World view unlocked.");
103 		}
104 	}
105 	
106 	@Override
107 	protected boolean shouldExecuteLogic() {
108             //System.out.println("SyncUSAR2004BotLogic.shouldExecuteLogic()");
109             // in USARSim the robots should not die - although they can, but it is not supported by USAR messages.
110             return true;
111 		/*synchronized(respawnListener) {
112 			if (shouldExecuteLogicLatch != 0) {
113 				if (log.isLoggable(Level.INFO)) log.info("Respawn command sensed - waiting for the bot respawn to execute logic with correct world view state.");
114 				return false;
115 			} else {
116 				return true;
117 			}
118 		}*/
119 	}
120 	
121 }