1 | package genius.core.boaframework;
|
---|
2 |
|
---|
3 | import java.io.Serializable;
|
---|
4 | import java.util.ArrayList;
|
---|
5 | import java.util.Collections;
|
---|
6 | import java.util.HashMap;
|
---|
7 | import java.util.Iterator;
|
---|
8 | import java.util.Map;
|
---|
9 | import java.util.Map.Entry;
|
---|
10 | import java.util.Set;
|
---|
11 |
|
---|
12 | import genius.core.boaframework.repository.BOAagentRepository;
|
---|
13 | import genius.core.boaframework.repository.BOArepItem;
|
---|
14 | import genius.core.exceptions.InstantiateException;
|
---|
15 |
|
---|
16 | /**
|
---|
17 | * Creates a BOA component consisting of the classname of the component, the
|
---|
18 | * type of the component, and all parameters. FIXME this creates nothing. It
|
---|
19 | * seems just to contain info that can be used to createFrom a BOA component.
|
---|
20 | */
|
---|
21 | public class BOAcomponent implements Serializable {
|
---|
22 |
|
---|
23 | private static final long serialVersionUID = 9055936213274664445L;
|
---|
24 | /** Classname of the component */
|
---|
25 | private String classname;
|
---|
26 | /** Type of the component, for example "as" for acceptance condition */
|
---|
27 | private BoaType type;
|
---|
28 | /**
|
---|
29 | * Parameters which should be used to initialize the component upon creation
|
---|
30 | */
|
---|
31 | private HashMap<String, Double> parametervalues;
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * Creates a BOA component consisting of the classname of the components,
|
---|
35 | * the type, and the parameters with which the component should be loaded.
|
---|
36 | *
|
---|
37 | * @param classname
|
---|
38 | * of the component. Note, this is not checked at all. We now
|
---|
39 | * also accept absolute file path to a .class file.
|
---|
40 | * @param type
|
---|
41 | * of the component (for example bidding strategy).
|
---|
42 | * @param values
|
---|
43 | * parameters of the component.
|
---|
44 | */
|
---|
45 | public BOAcomponent(String classname, BoaType type, HashMap<String, Double> values) {
|
---|
46 | if (values == null) {
|
---|
47 | throw new NullPointerException("values==null");
|
---|
48 | }
|
---|
49 | this.classname = classname;
|
---|
50 | this.type = type;
|
---|
51 | this.parametervalues = values;
|
---|
52 | }
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * Variant of the main constructor in which it is assumed that the component
|
---|
56 | * has no parameters.
|
---|
57 | *
|
---|
58 | * @param classname
|
---|
59 | * of the component. Note, this is not checked at all. We now
|
---|
60 | * also accept absolute file path to a .class file.
|
---|
61 | * @param type
|
---|
62 | * of the component (for example bidding strategy).
|
---|
63 | */
|
---|
64 | public BOAcomponent(String classname, BoaType type) {
|
---|
65 | this.classname = classname;
|
---|
66 | this.type = type;
|
---|
67 | this.parametervalues = new HashMap<String, Double>();
|
---|
68 | }
|
---|
69 |
|
---|
70 | /**
|
---|
71 | * Add a parameter to the set of parameters of this component.
|
---|
72 | *
|
---|
73 | * @param name
|
---|
74 | * of the parameter.
|
---|
75 | * @param value
|
---|
76 | * of the parameter.
|
---|
77 | */
|
---|
78 | public void addParameter(String name, Double value) {
|
---|
79 | parametervalues.put(name, value);
|
---|
80 | }
|
---|
81 |
|
---|
82 | /**
|
---|
83 | * @return name of the class of the component.
|
---|
84 | */
|
---|
85 | public String getClassname() {
|
---|
86 | return classname;
|
---|
87 | }
|
---|
88 |
|
---|
89 | /**
|
---|
90 | * @return type of the component.
|
---|
91 | */
|
---|
92 | public BoaType getType() {
|
---|
93 | return type;
|
---|
94 | }
|
---|
95 |
|
---|
96 | /**
|
---|
97 | * @return parameters of the component.
|
---|
98 | */
|
---|
99 | public HashMap<String, Double> getParameters() {
|
---|
100 | return decreaseAccuracy(parametervalues);
|
---|
101 | }
|
---|
102 |
|
---|
103 | /**
|
---|
104 | * @return original parameters as specified in the GUI.
|
---|
105 | */
|
---|
106 | public HashMap<String, Double> getFullParameters() {
|
---|
107 | return parametervalues;
|
---|
108 | }
|
---|
109 |
|
---|
110 | private HashMap<String, Double> decreaseAccuracy(HashMap<String, Double> parameters) {
|
---|
111 | Iterator<Entry<String, Double>> it = parameters.entrySet().iterator();
|
---|
112 | HashMap<String, Double> map = new HashMap<String, Double>();
|
---|
113 | while (it.hasNext()) {
|
---|
114 | Map.Entry<String, Double> pairs = (Entry<String, Double>) it.next();
|
---|
115 | map.put(pairs.getKey(), pairs.getValue().doubleValue());
|
---|
116 | }
|
---|
117 | return map;
|
---|
118 | }
|
---|
119 |
|
---|
120 | public String toString() {
|
---|
121 | String params = "";
|
---|
122 | if (parametervalues.size() > 0) {
|
---|
123 | ArrayList<String> keys = new ArrayList<String>(parametervalues.keySet());
|
---|
124 | Collections.sort(keys);
|
---|
125 | params = "{";
|
---|
126 | for (int i = 0; i < keys.size(); i++) {
|
---|
127 | // use doubleValue to keep #digits in string lower
|
---|
128 | params += keys.get(i) + "=" + parametervalues.get(keys.get(i)).doubleValue();
|
---|
129 | if (i < keys.size() - 1) {
|
---|
130 | params += ", ";
|
---|
131 | }
|
---|
132 | }
|
---|
133 | params += "}";
|
---|
134 | }
|
---|
135 | String shortType = "unknown";
|
---|
136 | if (type != null) {
|
---|
137 | switch (type) {
|
---|
138 | case BIDDINGSTRATEGY:
|
---|
139 | shortType = "bs";
|
---|
140 | break;
|
---|
141 | case ACCEPTANCESTRATEGY:
|
---|
142 | shortType = "as";
|
---|
143 | break;
|
---|
144 | case OPPONENTMODEL:
|
---|
145 | shortType = "om";
|
---|
146 | break;
|
---|
147 | case OMSTRATEGY:
|
---|
148 | shortType = "oms";
|
---|
149 | break;
|
---|
150 | }
|
---|
151 | }
|
---|
152 | return shortType + ": " + classname + " " + params;
|
---|
153 | }
|
---|
154 |
|
---|
155 | /**
|
---|
156 | * @return the original parameters from (a temporary instance of) the actual
|
---|
157 | * component.
|
---|
158 | * @throws InstantiateException
|
---|
159 | * if repitem can't be loaded
|
---|
160 | */
|
---|
161 | public Set<BOAparameter> getOriginalParameters() throws InstantiateException {
|
---|
162 | return getRepItem().getInstance().getParameterSpec();
|
---|
163 | }
|
---|
164 |
|
---|
165 | /**
|
---|
166 | * @return Find back this in the repository.
|
---|
167 | */
|
---|
168 | private BOArepItem getRepItem() {
|
---|
169 | // CHECK why don't we use the repository item all along?
|
---|
170 | BOAagentRepository repo = BOAagentRepository.getInstance();
|
---|
171 | switch (type) {
|
---|
172 | case ACCEPTANCESTRATEGY:
|
---|
173 | return repo.getAcceptanceStrategyRepItem(classname);
|
---|
174 | case BIDDINGSTRATEGY:
|
---|
175 | return repo.getBiddingStrategyRepItem(classname);
|
---|
176 | case OMSTRATEGY:
|
---|
177 | return repo.getOpponentModelStrategyRepItem(classname);
|
---|
178 | case OPPONENTMODEL:
|
---|
179 | return repo.getOpponentModelRepItem(classname);
|
---|
180 | default:
|
---|
181 | throw new IllegalStateException("BOAcomponent with unknown type encountered:" + type);
|
---|
182 | }
|
---|
183 | }
|
---|
184 | } |
---|