1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
40
41
42
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 }