View Javadoc

1   package cz.cuni.amis.pogamut.ut2004.analyzer;
2   
3   import java.io.File;
4   
5   import com.google.inject.Inject;
6   
7   import cz.cuni.amis.pogamut.base.communication.command.IAct;
8   import cz.cuni.amis.pogamut.base.communication.worldview.event.IWorldEventListener;
9   import cz.cuni.amis.pogamut.base.component.bus.IComponentBus;
10  import cz.cuni.amis.pogamut.base.utils.logging.IAgentLogger;
11  import cz.cuni.amis.pogamut.unreal.communication.messages.UnrealId;
12  import cz.cuni.amis.pogamut.ut2004.communication.messages.gbcommands.ConfigurationObserver;
13  import cz.cuni.amis.pogamut.ut2004.communication.messages.gbcommands.InitializeObserver;
14  import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.BotKilled;
15  import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.GameRestarted;
16  import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.MyInventory;
17  import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.Self;
18  import cz.cuni.amis.pogamut.ut2004.communication.worldview.UT2004WorldView;
19  import cz.cuni.amis.pogamut.ut2004.observer.impl.UT2004Observer;
20  import cz.cuni.amis.utils.exception.PogamutException;
21  
22  /**
23   * Base class implementing {@link IUT2004AnalyzerObserver}, does not add that much functionality, except
24   * starting the observation for desired agent and abide watching out for {@link GameRestarted} so you
25   * have easy work to restart the observation data collection (and to abide {@link UT2004AnalyzerObserverParameters#isWaitForMatchRestart()}).
26   * See {@link UT2004AnalyzerObserver#gameRestartStarted()} and {@link UT2004AnalyzerObserver#gameRestartEnd()}.
27   * 
28   * @author Jimmy
29   */
30  @Deprecated
31  public class UT2004AnalyzerObserver extends UT2004Observer implements IUT2004AnalyzerObserver {
32  	
33  	private UnrealId observedBotId;	
34  	
35  	private IWorldEventListener<GameRestarted> gameRestartedListener = new IWorldEventListener<GameRestarted>() {
36  
37  		@Override
38  		public void notify(GameRestarted event) {
39  			if (event.isStarted()) {
40  				gameRestartStarted();
41  			} else 
42  			if (event.isFinished()) {
43  				gameRestartEnd();
44  			} else {
45  				throw new PogamutException("GameRestarted has started==false && finished==false as well, invalid!", this);
46  			}
47  		}
48  		
49  	};
50  	
51  	@Inject
52  	public UT2004AnalyzerObserver(UT2004AnalyzerObserverParameters params,
53  			IComponentBus bus, IAgentLogger agentLogger,
54  			UT2004WorldView worldView, IAct act) {
55  		super(params, bus, agentLogger, worldView, act);
56  		observedBotId = UnrealId.get(params.getObservedAgentId());
57  		getWorldView().addEventListener(GameRestarted.class, gameRestartedListener);
58  	}
59  	
60  	@Override
61  	public UT2004AnalyzerObserverParameters getParams() {
62  		return (UT2004AnalyzerObserverParameters) super.getParams();
63  	}
64  
65  	@Override
66  	public UnrealId getObservedBotId() {
67  		return observedBotId;
68  	}
69  	
70  	/**
71  	 * Returns path to file that should be used for outputting the data
72  	 * @return
73  	 */
74  	public String getOutputFilePath() {
75  		String path = getParams().getOutputPath();
76  		if (path == null) path = ".";
77  		path += File.separator;
78  		if (getParams().getFileName() != null) {
79  			path += getParams().getFileName();
80  		} else {
81  			path += getObservedBotId().toString();
82  			path += ".csv";
83  		}
84  		return path;
85  	}
86  	
87  	/**
88  	 * Called whenever {@link GameRestart} message with {@link GameRestarted#isStarted()} is received.
89  	 * <p><p>
90  	 * You probably won't need to override this method, better override {@link UT2004AnalyzerObserver#gameRestartEnd()}, that
91  	 * is the place where you should reset data collection statistics / start them in case of {@link UT2004AnalyzerObserverParameters#isWaitForMatchRestart()}.
92  	 * <p><p>
93  	 * Current implementation is empty.
94  	 */
95  	protected void gameRestartStarted() {
96  	}
97  	
98  	/**
99  	 * Called whenever {@link GameRestart} message with {@link GameRestarted#isFinished()} is received.
100 	 * <p><p>
101 	 * Place where you should reset data collection statistics / start them 
102 	 * in case of {@link UT2004AnalyzerObserverParameters#isWaitForMatchRestart()}.
103 	 * <p><p>
104 	 * Current implementation is empty.
105 	 */
106 	protected void gameRestartEnd() {
107 	}
108 	
109 	/**
110 	 * Initialize the observer to listen on the {@link UT2004AnalyzerObserverParameters#getObservedAgentId()} that is obtained from
111 	 * the {@link UT2004AnalyzerObserver#getParams()}.
112 	 */
113 	@Override
114 	protected void startAgent() {
115 		super.startAgent();
116 		getAct().act(new InitializeObserver().setId(getParams().getObservedAgentId()));
117 		configureObserver();
118 	}
119 	
120 	@Override
121 	protected void startPausedAgent() {
122 		super.startPausedAgent();
123 		getAct().act(new InitializeObserver().setId(getParams().getObservedAgentId()));
124 		configureObserver();
125 	}
126 	
127 	/**
128 	 * Called from the {@link UT2004AnalyzerObserver#startAgent()} after {@link InitializeObserver} command
129 	 * is sent to configure the observer instance.
130 	 * <p><p>
131 	 * Actually enables {@link Self}, {@link MyInventory} and async messages (i.e., {@link BotKilled}).
132 	 */
133 	protected void configureObserver() {
134 		getAct().act(new ConfigurationObserver().setUpdate(0.2).setAll(true).setSelf(true).setAsync(true).setGame(false).setSee(false).setSpecial(false));		
135 	}
136 
137 }