View Javadoc

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