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