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  public class UT2004AnalyzerObserver extends UT2004Observer implements IUT2004AnalyzerObserver {
31  	
32  	private UnrealId observedBotId;	
33  	
34  	private IWorldEventListener<GameRestarted> gameRestartedListener = new IWorldEventListener<GameRestarted>() {
35  
36  		@Override
37  		public void notify(GameRestarted event) {
38  			if (event.isStarted()) {
39  				gameRestartStarted();
40  			} else 
41  			if (event.isFinished()) {
42  				gameRestartEnd();
43  			} else {
44  				throw new PogamutException("GameRestarted has started==false && finished==false as well, invalid!", this);
45  			}
46  		}
47  		
48  	};
49  	
50  	@Inject
51  	public UT2004AnalyzerObserver(UT2004AnalyzerObserverParameters params,
52  			IComponentBus bus, IAgentLogger agentLogger,
53  			UT2004WorldView worldView, IAct act) {
54  		super(params, bus, agentLogger, worldView, act);
55  		observedBotId = UnrealId.get(params.getObservedAgentId());
56  		getWorldView().addEventListener(GameRestarted.class, gameRestartedListener);
57  	}
58  	
59  	@Override
60  	public UT2004AnalyzerObserverParameters getParams() {
61  		return (UT2004AnalyzerObserverParameters) super.getParams();
62  	}
63  
64  	@Override
65  	public UnrealId getObservedBotId() {
66  		return observedBotId;
67  	}
68  	
69  	/**
70  	 * Returns path to file that should be used for outputting the data
71  	 * @return
72  	 */
73  	public String getOutputFilePath() {
74  		String path = getParams().getOutputPath();
75  		if (path == null) path = ".";
76  		path += File.separator;
77  		if (getParams().getFileName() != null) {
78  			path += getParams().getFileName();
79  		} else {
80  			path += getObservedBotId().toString();
81  			path += ".csv";
82  		}
83  		return path;
84  	}
85  	
86  	/**
87  	 * Called whenever {@link GameRestart} message with {@link GameRestarted#isStarted()} is received.
88  	 * <p><p>
89  	 * You probably won't need to override this method, better override {@link UT2004AnalyzerObserver#gameRestartEnd()}, that
90  	 * is the place where you should reset data collection statistics / start them in case of {@link UT2004AnalyzerObserverParameters#isWaitForMatchRestart()}.
91  	 * <p><p>
92  	 * Current implementation is empty.
93  	 */
94  	protected void gameRestartStarted() {
95  	}
96  	
97  	/**
98  	 * Called whenever {@link GameRestart} message with {@link GameRestarted#isFinished()} is received.
99  	 * <p><p>
100 	 * Place where you should reset data collection statistics / start them 
101 	 * in case of {@link UT2004AnalyzerObserverParameters#isWaitForMatchRestart()}.
102 	 * <p><p>
103 	 * Current implementation is empty.
104 	 */
105 	protected void gameRestartEnd() {
106 	}
107 	
108 	/**
109 	 * Initialize the observer to listen on the {@link UT2004AnalyzerObserverParameters#getObservedAgentId()} that is obtained from
110 	 * the {@link UT2004AnalyzerObserver#getParams()}.
111 	 */
112 	@Override
113 	protected void startAgent() {
114 		super.startAgent();
115 		getAct().act(new InitializeObserver().setId(getParams().getObservedAgentId()));
116 		configureObserver();
117 	}
118 	
119 	@Override
120 	protected void startPausedAgent() {
121 		super.startPausedAgent();
122 		getAct().act(new InitializeObserver().setId(getParams().getObservedAgentId()));
123 		configureObserver();
124 	}
125 	
126 	/**
127 	 * Called from the {@link UT2004AnalyzerObserver#startAgent()} after {@link InitializeObserver} command
128 	 * is sent to configure the observer instance.
129 	 * <p><p>
130 	 * Actually enables {@link Self}, {@link MyInventory} and async messages (i.e., {@link BotKilled}).
131 	 */
132 	protected void configureObserver() {
133 		getAct().act(new ConfigurationObserver().setUpdate(0.2).setAll(true).setSelf(true).setAsync(true).setGame(false).setSee(false).setSpecial(false));		
134 	}
135 
136 }