View Javadoc

1   package cz.cuni.amis.pogamut.ut2004.tournament.botexecution;
2   
3   import java.io.File;
4   import java.util.HashMap;
5   import java.util.Map;
6   
7   import cz.cuni.amis.pogamut.ut2004.tournament.match.UT2004BotConfig;
8   import cz.cuni.amis.utils.NullCheck;
9   import cz.cuni.amis.utils.token.IToken;
10  import cz.cuni.amis.utils.token.Tokens;
11  
12  public class UT2004BotExecutionConfig {
13  	
14  	/**
15  	 * Unique id of this bot, used for reference inside tournament results.
16  	 * <p><p>
17  	 * DOES NOT MEAN THAT THE EXECUTED BOT WILL HAVE THIS ID IN UT2004!
18  	 */
19  	private IToken botId;
20  		
21  	/**
22  	 * Path to the directory that contains runnable jar file with the bot.
23  	 */
24  	private String pathToBotJar = null;
25  	
26  	/**
27  	 * Whether the StdErr of the bot execution should be redirected to log (== true, default) or sunk (== false).
28  	 */
29  	private boolean redirectStdErr = true;
30  	
31  	/**
32  	 * Whether the StdOut of the bot execution should be redirected to log (== true, default) or sunk (== false).
33  	 */
34  	private boolean redirectStdOut = true;
35  
36  	/**
37  	 * {@link UT2004BotExecutionConfig#pathToBotJar} as a {@link File}.
38  	 */
39  	private File fileToJar;
40  	
41  	/**
42  	 * Parameters to be passed to the bot process via "-D" Java switch.
43  	 */
44  	private Map<String, Object> parameters = new HashMap<String, Object>();
45  
46  	/**
47  	 * Returns ID of this bot configuration. This ID will be used for storing result of the tournament for this bot.
48  	 * <p><p>
49  	 * DOES NOT MEAN THAT THE EXECUTED BOT WILL HAVE THIS ID IN UT2004!
50  	 * 
51  	 * @return
52  	 */
53  	public IToken getBotId() {
54  		return botId;
55  	}
56  	
57  	/**
58  	 * Parameter-less constructor.
59  	 */
60  	public UT2004BotExecutionConfig() {		
61  	}
62  
63  	/**
64  	 * Copy-constructor;
65  	 * @param value
66  	 */
67  	public UT2004BotExecutionConfig(UT2004BotConfig value) {
68  		this.botId = value.getBotId();
69  		this.setPathToBotJar(value.getPathToBotJar());
70  		this.redirectStdErr = value.isRedirectStdErr();
71  		this.redirectStdOut = value.isRedirectStdOut();
72  	}
73  
74  	/**
75  	 * Sets ID of this bot configuration. This ID will be used for storing result of the tournament for this bot.
76  	 * <p><p>
77  	 * DOES NOT MEAN THAT THE EXECUTED BOT WILL HAVE THIS ID IN UT2004!
78  	 * 
79  	 * @param botId
80  	 */
81  	public UT2004BotExecutionConfig setBotId(String botId) {
82  		NullCheck.check(botId, "botId");
83  		this.botId = Tokens.get(botId);
84  		return this;
85  	}
86  	
87  	/**
88  	 * Sets ID of this bot configuration. This ID will be used for storing result of the tournament for this bot.
89  	 * <p><p>
90  	 * DOES NOT MEAN THAT THE EXECUTED BOT WILL HAVE THIS ID IN UT2004!
91  	 * 
92  	 * @param botId
93  	 */
94  	public UT2004BotExecutionConfig setBotId(IToken botId) {
95  		NullCheck.check(botId, "botId");
96  		this.botId = botId;
97  		return this;
98  	}
99  	
100 	/**
101 	 * Path to the runnable jar file contining the bot to be run.
102 	 * @return
103 	 */
104 	public String getPathToBotJar() {
105 		return pathToBotJar;
106 	}
107 
108 	/** 
109 	 * Sets the path to jar-file of the bot.
110 	 * @param botDirPath
111 	 */
112 	public UT2004BotExecutionConfig setPathToBotJar(String pathToBotJar) {
113 		if (pathToBotJar != null) {
114 			this.fileToJar = new File(pathToBotJar);
115 		} else {
116 			this.fileToJar = null;
117 		}
118 		this.pathToBotJar = pathToBotJar;
119 		return this;
120 	}
121 	
122 	/**
123 	 * Whether the jar specified by this config exists.
124 	 * @return
125 	 */
126 	public boolean isBotJarExist() {
127 		if (this.pathToBotJar == null) return false;
128 		File file = getJarFile();
129 		return file.exists() && file.isFile(); 
130 	}
131 
132 	/**
133 	 * Returns path to jar as a file.
134 	 * @return
135 	 */
136 	public File getJarFile() {
137 		return fileToJar;
138 	}
139 	
140 	/**
141 	 * Whether the StdErr of the bot execution should be redirected to log (== true, default) or sunk (== false).
142 	 * @return
143 	 */
144 	public boolean isRedirectStdErr() {
145 		return redirectStdErr;
146 	}
147 
148 	/**
149 	 * Sets whether the StdErr of the bot execution should be redirected to log (== true, default) or sunk (== false).
150 	 * @param redirectStdErr
151 	 */
152 	public UT2004BotExecutionConfig setRedirectStdErr(boolean redirectStdErr) {
153 		this.redirectStdErr = redirectStdErr;
154 		return this;
155 	}
156 
157 	/**
158 	 * Whether the StdOut of the bot execution should be redirected to log (== true, default) or sunk (== false).
159 	 * @return
160 	 */
161 	public boolean isRedirectStdOut() {
162 		return redirectStdOut;
163 	}
164 
165 	/**
166 	 * Sets whether the StdOut of the bot execution should be redirected to log (== true, default) or sunk (== false).
167 	 * @param redirectStdOut
168 	 */
169 	public UT2004BotExecutionConfig setRedirectStdOut(boolean redirectStdOut) {
170 		this.redirectStdOut = redirectStdOut;
171 		return this;
172 	}
173 	
174 	/**
175 	 * Adds parameter to be passed onto the command-line executing the bot via Java "-D" switch.
176 	 * 
177 	 * @param key only alpha-num chars and '_', '-', '.' chars allowed
178 	 * @param value {@link Object#toString()} will be used to serialize the parameter into the command-line, parameter MUST NOT contain '"'
179 	 * @return previously stored value under 'key'.
180 	 */
181 	public Object addParameter(String key, Object value) {
182 		return this.parameters.put(key, value);
183 	}
184 	
185 	/**
186 	 * Removes parameter to be passed onto the command-line executing the bot via Java "-D" switch.
187 	 * 
188 	 * @param key
189 	 * @return
190 	 */
191 	public Object removeParameter(String key) {
192 		return this.parameters.remove(key);
193 	}
194 	
195 	public Map<String, Object> getParameters() {
196 		return this.parameters;
197 	}
198 			
199 	public void setParameters(Map<String, ?> parameters) {
200 		this.parameters = (Map<String, Object>) parameters;
201 	}
202 
203 	@Override
204 	public String toString() {
205 		return "UT2004BotExecutionConfig[botId=" + botId.getToken() + ", jar=" + pathToBotJar + "]";
206 	}
207 
208 }