View Javadoc

1   package cz.cuni.amis.pogamut.udk.factory.guice.remoteagent;
2   
3   import com.google.inject.AbstractModule;
4   import com.google.inject.name.Names;
5   
6   import cz.cuni.amis.pogamut.base.agent.IAgent;
7   import cz.cuni.amis.pogamut.base.communication.messages.InfoMessage;
8   import cz.cuni.amis.pogamut.base.communication.translator.IWorldMessageTranslator;
9   import cz.cuni.amis.pogamut.base.communication.worldview.ILockableWorldView;
10  import cz.cuni.amis.pogamut.base.communication.worldview.IWorldView;
11  import cz.cuni.amis.pogamut.base.component.controller.ComponentDependencies;
12  import cz.cuni.amis.pogamut.base.component.controller.ComponentDependencyType;
13  import cz.cuni.amis.pogamut.base.factory.guice.GuiceAgentModule;
14  import cz.cuni.amis.pogamut.base.factory.guice.GuiceRemoteAgentModule;
15  import cz.cuni.amis.pogamut.base.utils.guice.AdaptableProvider;
16  import cz.cuni.amis.pogamut.base3d.ILockableVisionWorldView;
17  import cz.cuni.amis.pogamut.base3d.agent.IAgent3D;
18  import cz.cuni.amis.pogamut.base3d.worldview.IVisionWorldView;
19  import cz.cuni.amis.pogamut.udk.agent.params.UDKAgentParameters;
20  import cz.cuni.amis.pogamut.udk.bot.IUDKBot;
21  import cz.cuni.amis.pogamut.udk.bot.IUDKBotController;
22  import cz.cuni.amis.pogamut.udk.bot.impl.UDKBot;
23  import cz.cuni.amis.pogamut.udk.communication.translator.bot.BotFSM;
24  import cz.cuni.amis.pogamut.udk.communication.worldview.UDKSyncLockableWorldView;
25  
26  /**
27   * Module extending {@link UDKCommunicationModule} for the purpose of {@link UDKBot} instantiation.
28   * <p><p>
29   * Introduces {@link UDKBotModule#worldViewDependenciesProvider}.
30   * <p><p>
31   * Newly binded classes:
32   * <table>
33   * <tr><th>Mapped class</th>                    <th>  </th> <th>Target</th>                          <th>Description</th></tr>
34   * 
35   * <tr><td>{@link IWorldMessageTranslator}</td>	<td>-></td>	<td>{@link BotFSM}</td>					 <td>Protocol-validating translator of {@link InfoMessage}s of GameBots2004.</td></tr>
36   * <tr><td>{@link IWorldView}</td>				<td>-></td> <td>{@link IVisionWorldView}</td>        <td>Binds world view as vision world view.</td></tr>
37   * <tr><td>{@link IVisionWorldView}</td>		<td>-></td> <td>{@link ILockableVisionWorldView}</td><td>Binds vision world view as lockable one.</td></tr>
38   * <tr><td>{@link ILockableWorldView}</td>		<td>-></td> <td>{@link ILockableVisionWorldView}</td><td>Binds lockable world view as vision world view.</td></tr>
39   * <tr><td>{@link ILockableVisionWorldView}</td><td>-></td> <td>{@link UDKSyncLockableWorldView}</td> <td>Binds world view with concrete implementation.</td></tr>
40   * <tr><td>{@link UDKSyncLockableWorldView} dependencies</td>
41   *                                              <td>-></td> <td>{@link UDKBotModule#worldViewDependenciesProvider}</td></tr>
42   * <tr><td>{@link IAgent}</td>                  <td>-></td> <td>{@link IAgent3D}</td>                <td></td></tr>
43   * <tr><td>{@link IAgent3D}</td>                <td>-></td> <td>{@link IUDKBot}</td>            <td></td></tr>
44   * <tr><td>{@link IUDKBot}</td>            <td>-></td> <td>{@link UDKBot}</td>               <td>Binds concrete implementation of the agent.</td></tr>
45   * 
46   * </table>
47   * To have <b>successful module</b> the descendant <b>must specify</b> these <b>missing bindings</b>:
48   * <table>
49   * <tr><th>Mapped class</th>                    <th>Description</th></tr>
50   * 
51   * <tr><td>{@link IUDKBotController}</td>    <td>Controller of the bot.</td></tr>
52   * </table>
53   * ... plus all newly introduced dependencies (by various implementors of mentioned interfaces).<p>
54   * ... <b>don't forget to call super.configureModules()</b> in the subclasses ;-)
55   * 
56   * @see UDKCommunicationModule
57   * @see GuiceRemoteAgentModule
58   * @see GuiceAgentModule
59   * @author Jimmy
60   */
61  public class UDKBotModule<PARAMS extends UDKAgentParameters> extends UDKCommunicationModule<PARAMS> {
62  
63  	/**
64  	 * Dependency provider for the world view, so the world view know when to start.
65  	 */
66  	protected AdaptableProvider<ComponentDependencies> worldViewDependenciesProvider = new AdaptableProvider<ComponentDependencies>(null);
67  	
68  	private Class<? extends IUDKBotController> botControllerClass;
69  	
70  	protected UDKBotModule() {
71  	}
72  	
73  	public UDKBotModule(Class<? extends IUDKBotController> botControllerClass) {
74  		this.botControllerClass = botControllerClass;
75  	}
76  
77  	@Override
78  	public void prepareNewAgent(PARAMS agentParameters) {
79  		super.prepareNewAgent(agentParameters);
80  		worldViewDependenciesProvider.set(new ComponentDependencies(ComponentDependencyType.STARTS_WITH).add(agentParameters.getAgentId()));
81  	}
82  	
83  	@Override
84  	protected void configureModules() {
85  		super.configureModules();
86  		addModule(new AbstractModule() {
87  
88  			@Override
89  			public void configure() {
90  				bind(IWorldMessageTranslator.class).to(BotFSM.class);
91  				bind(IWorldView.class).to(IVisionWorldView.class);
92  				bind(IVisionWorldView.class).to(ILockableVisionWorldView.class);
93  				bind(ILockableWorldView.class).to(ILockableVisionWorldView.class);
94  				bind(ILockableVisionWorldView.class).to(UDKSyncLockableWorldView.class);
95  				bind(ComponentDependencies.class).annotatedWith(Names.named(UDKSyncLockableWorldView.WORLDVIEW_DEPENDENCY)).toProvider(worldViewDependenciesProvider);
96  				bind(IAgent.class).to(IAgent3D.class);
97  				bind(IAgent3D.class).to(IUDKBot.class);
98  				bind(IUDKBot.class).to(UDKBot.class);
99  				if (botControllerClass != null) {
100 					bind(IUDKBotController.class).to(botControllerClass);
101 				}
102 			}
103 			
104 		});
105 	}
106 
107 }