View Javadoc

1   /**
2    * BaseUnrealEnvironment, an implementation of the environment interface standard that 
3    * facilitates the connection between GOAL and the UT2004 engine. 
4    * 
5    * Copyright (C) 2012 BaseUnrealEnvironment authors.
6    * 
7    * This program is free software: you can redistribute it and/or modify it under
8    * the terms of the GNU General Public License as published by the Free Software
9    * Foundation, either version 3 of the License, or (at your option) any later
10   * version.
11   * 
12   * This program is distributed in the hope that it will be useful, but WITHOUT
13   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14   * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
15   * details.
16   * 
17   * You should have received a copy of the GNU General Public License along with
18   * this program. If not, see <http://www.gnu.org/licenses/>.
19   */
20  package nl.tudelft.goal.unreal.messages;
21  
22  import java.util.Map;
23  import java.util.Map.Entry;
24  import java.util.logging.Logger;
25  
26  import nl.tudelft.goal.EIS2Java.exception.TranslationException;
27  import nl.tudelft.goal.unreal.environment.UnrealEnvironmentException;
28  import cz.cuni.amis.pogamut.base.agent.IAgentId;
29  import cz.cuni.amis.pogamut.base.agent.impl.AgentId;
30  import cz.cuni.amis.pogamut.base.agent.params.IAgentParameters;
31  import cz.cuni.amis.pogamut.base.communication.connection.IWorldConnectionAddress;
32  import cz.cuni.amis.pogamut.base.communication.connection.impl.socket.SocketConnectionAddress;
33  import cz.cuni.amis.pogamut.base.component.IComponent;
34  import cz.cuni.amis.pogamut.base.utils.logging.IAgentLogger;
35  import cz.cuni.amis.pogamut.ut2004.bot.params.UT2004BotParameters;
36  import eis.iilang.Parameter;
37  
38  /**
39   * Base class for the {@link EnvironmentParameters} and {@link BotParameters}.
40   * These parameters are instantiated using the init values provided by EIS.
41   * 
42   * @author mpkorstanje
43   * 
44   */
45  public abstract class Parameters extends UT2004BotParameters implements IComponent {
46  
47  	public static final int BOT_SERVER_PORT = 3000;
48  	public static final int CONTROL_SERVER_PORT = 3001;
49  	public static final String LOCAL_HOST = "127.0.0.1";
50  	public static final String DEFAULT_NAME = "UnrealGoalBot";
51  
52  	protected final Logger log;
53  
54  	public Parameters(IAgentLogger log) {
55  		super();
56  		this.log = log.getCategory(this);
57  	}
58  
59  	@Override
60  	public void assignDefaults(IAgentParameters defaults) {
61  		super.assignDefaults(defaults);
62  	}
63  
64  	public Parameters(Map<String, Parameter> parameters, IAgentLogger log) throws UnrealEnvironmentException {
65  		this(log);
66  
67  		for (Entry<String, Parameter> entry : parameters.entrySet()) {
68  			String key = entry.getKey();
69  			Parameter value = entry.getValue();
70  			try {
71  				setKey(Key.parseKey(key), value);
72  			} catch (TranslationException e) {
73  				String message = String.format("Could not set %s to %s. Cause: %s", key, value, e.getMessage());
74  				throw new UnrealEnvironmentException(message, e);
75  			} catch (IllegalArgumentException e) {
76  				String message = String.format("Could not set %s to %s. Cause: %s", key, value, e.getMessage());
77  				throw new UnrealEnvironmentException(message, e);
78  			}
79  		}
80  	}
81  
82  	protected abstract void setKey(Key key, Parameter value) throws TranslationException;
83  
84  	@Override
85  	public Parameters setAgentId(IAgentId agentId) {
86  		super.setAgentId(agentId);
87  		return this;
88  	}
89  
90  	public Parameters setAgentId(String name) {
91  		assert name != null;
92  		this.setAgentId(new AgentId(name));
93  		return this;
94  	}
95  
96  	@Override
97  	public Parameters setWorldAddress(IWorldConnectionAddress address) {
98  		super.setWorldAddress(address);
99  		log.info(String.format("Set %s to %s.", Key.BOTSERVER, address));
100 		return this;
101 	}
102 
103 	public Parameters setWorldAddress(String host, Integer port) {
104 		this.setWorldAddress(new SocketConnectionAddress(host, port));
105 		return this;
106 	}
107 
108 }