source: src/main/java/genius/gui/boaframework/BOAAgentUI.java@ 13

Last change on this file since 13 was 1, checked in by Wouter Pasman, 7 years ago

Initial import : Genius 9.0.0

File size: 17.3 KB
Line 
1package genius.gui.boaframework;
2
3import java.awt.Dialog;
4import java.awt.Dimension;
5import java.awt.Font;
6import java.awt.event.ActionEvent;
7import java.awt.event.ActionListener;
8import java.util.ArrayList;
9import java.util.Collections;
10import java.util.HashSet;
11import java.util.Iterator;
12import java.util.Set;
13
14import javax.swing.JButton;
15import javax.swing.JComboBox;
16import javax.swing.JDialog;
17import javax.swing.JLabel;
18import javax.swing.JOptionPane;
19import javax.swing.WindowConstants;
20
21import org.netbeans.lib.awtextra.AbsoluteConstraints;
22import org.netbeans.lib.awtextra.AbsoluteLayout;
23
24import genius.core.boaframework.BOAagentInfo;
25import genius.core.boaframework.BOAcomponent;
26import genius.core.boaframework.BOAparameter;
27import genius.core.boaframework.BoaType;
28import genius.core.boaframework.repository.BOAagentRepository;
29import genius.core.boaframework.repository.BOArepItem;
30import genius.core.misc.Pair;
31import genius.core.misc.SetTools;
32import genius.gui.NegoGUIApp;
33import genius.gui.panels.ExtendedComboBoxModel;
34
35/**
36 *
37 * Show a UI for modifying the BOA parameters of a {@link BOAagentInfo} object
38 *
39 * @author Mark Hendrikx
40 * @author W.Pasman mar'14 original code refactored from {@link BOAagentsFrame}.
41 * Code still could use improvement, there's a lot of code duplication
42 * here.
43 *
44 */
45public class BOAAgentUI extends JDialog {
46
47 // BIDDING STRATEGY
48 private JLabel biddingStrategyLabel;
49 private JComboBox biddingStrategyCB;
50 private BOATextField biddingStrategyTF;
51 private ExtendedComboBoxModel biddingStrategyModel;
52
53 // OPPONENT MODEL
54 private JLabel opponentModelLabel;
55 private JComboBox opponentModelCB;
56 private BOATextField opponentModelTF;
57 private ExtendedComboBoxModel opponentModelModel;
58
59 // ACCEPTANCE STRATEGY
60 private JLabel acceptanceStrategyLabel;
61 private JComboBox acceptanceStrategyCB;
62 private BOATextField acceptanceStrategyTF;
63 private ExtendedComboBoxModel acceptanceStrategyModel;
64
65 // OPPONENT MODEL STRATEGY
66 private JLabel omStrategyLabel;
67 private JComboBox omStrategyCB;
68 private BOATextField omStrategyTF;
69 private ExtendedComboBoxModel<String> omStrategyModel;
70
71 // the null parameter is a parameter added to strategies without
72 // parameters. This is done, such to ensure that the Cartesian product
73 // code can still be applied. If this was not the case, a lot of side-cases
74 // are needed, which considerably reduces the readability of the code.
75 private BOAparameter nullParam = new BOAparameter("null", 1., 1., 1.);
76
77 /**
78 * This field will contain the result when we finish the dialog. Stays null
79 * if user cancels the dialog
80 */
81 private Set<Set<BOAcomponent>> result = null;
82
83 /**
84 * Creates the dialog. Does not yet set it to visible, call getResult for
85 * that.
86 *
87 * @param info
88 * the {@link BOAagentInfo} info to be used for initial setting.
89 */
90 public BOAAgentUI(BOAagentInfo info) {
91 initFrame();
92 initBiddingStrategyUI();
93 initOpponentModelUI();
94 initAcceptanceStrategyUI();
95 initOpponentModelStrategyUI();
96 addButtons();
97 loadLists();
98 if (info != null) {
99 loadInfo(info);
100 }
101 }
102
103 private void addButtons() {
104 JButton okbutton = new JButton("Ok");
105 getContentPane().add(okbutton, new AbsoluteConstraints(20, 120, 75, -1));
106 okbutton.addActionListener(new ActionListener() {
107
108 @Override
109 public void actionPerformed(ActionEvent arg0) {
110 result = generateAgents();
111 dispose();
112 }
113 });
114
115 JButton cancel = new JButton("Cancel");
116 getContentPane().add(cancel, new AbsoluteConstraints(120, 120, 75, -1));
117 cancel.addActionListener(new ActionListener() {
118
119 @Override
120 public void actionPerformed(ActionEvent arg0) {
121 dispose();
122 }
123 });
124
125 }
126
127 /**
128 * show the GUI and wait till user made selection.
129 *
130 * @return a Set of Set of {@link BOAcomponent}s. Each of the elements in
131 * the outer set will be quadruple: a set of four BOAcomponents,
132 * having the four {@link BoaType} types required for a BOA agent.
133 * May return null if the user selected cancel button.
134 */
135 public Set<Set<BOAcomponent>> getResult() {
136 pack();
137 setVisible(true);
138 return result;
139 }
140
141 private void initFrame() {
142 getContentPane().setLayout(new AbsoluteLayout());
143 setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
144 setModalityType(Dialog.ModalityType.DOCUMENT_MODAL);
145
146 setMaximumSize(new Dimension(1010, 200));
147 setMinimumSize(new Dimension(1010, 200));
148 setPreferredSize(new Dimension(1010, 200));
149 setResizable(false);
150 setTitle("Select BOA agent components and parameters");
151 }
152
153 /**
154 * Generate all {@link BOAcomponent}s as set by the parameter ranges. The
155 * user can set for each parameter minimum, max and step size. For each
156 * possible parameter value we createFrom another BOA component.
157 *
158 * @return a Set of Set of {@link BOAcomponent}s. Each of the elements in
159 * the outer set will be quadruple: a set of four BOAcomponents,
160 * having the four {@link BoaType} types required for a BOA agent.
161 */
162 private Set<Set<BOAcomponent>> generateAgents() {
163 Set<BOAcomponent> osStrat = generateStrategies((String) biddingStrategyCB.getSelectedItem(),
164 biddingStrategyTF.getBOAparameters(), BoaType.BIDDINGSTRATEGY);
165 Set<BOAcomponent> asStrat = generateStrategies((String) acceptanceStrategyCB.getSelectedItem(),
166 acceptanceStrategyTF.getBOAparameters(), BoaType.ACCEPTANCESTRATEGY);
167 Set<BOAcomponent> omStrat = generateStrategies((String) opponentModelCB.getSelectedItem(),
168 opponentModelTF.getBOAparameters(), BoaType.OPPONENTMODEL);
169 Set<BOAcomponent> omsStrat = generateStrategies((String) omStrategyCB.getSelectedItem(),
170 omStrategyTF.getBOAparameters(), BoaType.OMSTRATEGY);
171
172 Set<Set<BOAcomponent>> result = SetTools.cartesianProduct(osStrat, asStrat, omStrat, omsStrat);
173 return result;
174 }
175
176 /**
177 * Generates a "strategy" which is a set of {@link BOAcomponent}s, using the
178 * ginve {@link BOAparameter}s. Each parameter can have a range of values,
179 * and what we generate is one {@link BOAcomponent} for each possible
180 * parameter combinatin.
181 *
182 * @param classname
183 * @param parameters
184 * the {@link BOAparameter} containing the parameter values to be
185 * used
186 * @param type
187 * toe {@link BoaType} type
188 * @return a set of {@link BOAcomponent}s, one for each possible combination
189 * of parameters
190 */
191 private Set<BOAcomponent> generateStrategies(String classname, ArrayList<BOAparameter> parameters, BoaType type) {
192
193 if (parameters == null || parameters.size() == 0) {
194 parameters = new ArrayList<BOAparameter>();
195 parameters.add(nullParam);
196 }
197 Set<Pair<String, Double>>[] params = new Set[parameters.size()];
198 for (int i = 0; i < parameters.size(); i++) {
199 params[i] = parameters.get(i).getValuePairs();
200 }
201 Set<Set<Pair<String, Double>>> result = SetTools.cartesianProduct(params);
202
203 Set<BOAcomponent> strategies = new HashSet<BOAcomponent>();
204 Iterator<Set<Pair<String, Double>>> combinationsIterator = result.iterator();
205
206 while (combinationsIterator.hasNext()) {
207 // all combinations
208 Set<Pair<String, Double>> set = combinationsIterator.next();
209 parameters.remove(nullParam);
210 BOAcomponent strat = new BOAcomponent(classname, type);
211 Iterator<Pair<String, Double>> paramIterator = set.iterator();
212 // a set of
213 ArrayList<BOAparameter> param = new ArrayList<BOAparameter>();
214 while (paramIterator.hasNext()) {
215 Pair<String, Double> pair = (Pair<String, Double>) paramIterator.next();
216 strat.addParameter(pair.getFirst(), pair.getSecond());
217 param.add(new BOAparameter(pair.getFirst(), pair.getSecond(), pair.getSecond(), 1.));
218 }
219 strat.getFullParameters().remove("null");
220 strategies.add(strat);
221 }
222 return strategies;
223 }
224
225 /**
226 * load previous info settings to the panels. Prints stacktraces if problems
227 * occur creating the panels.
228 *
229 * @param s
230 */
231 private void loadInfo(BOAagentInfo s) {
232 Set<BOAparameter> params = null;
233
234 biddingStrategyCB.setSelectedItem(s.getOfferingStrategy().getClassname());
235 biddingStrategyCB.updateUI();
236 try {
237 params = s.getOfferingStrategy().getOriginalParameters();
238 if (params != null && params.size() > 0) {
239 biddingStrategyTF.setParams(params);
240 biddingStrategyTF.updateUI();
241 }
242 } catch (Exception e) {
243 e.printStackTrace();
244 }
245
246 acceptanceStrategyCB.setSelectedItem(s.getAcceptanceStrategy().getClassname());
247 acceptanceStrategyCB.updateUI();
248 try {
249 params = s.getAcceptanceStrategy().getOriginalParameters();
250 if (params != null && params.size() > 0) {
251 acceptanceStrategyTF.setParams(params);
252 acceptanceStrategyTF.updateUI();
253 }
254 } catch (Exception e) {
255 e.printStackTrace();
256 }
257
258 opponentModelCB.setSelectedItem(s.getOpponentModel().getClassname());
259 opponentModelCB.updateUI();
260 try {
261 params = s.getOpponentModel().getOriginalParameters();
262 if (params != null && params.size() > 0) {
263 opponentModelTF.setParams(params);
264 opponentModelTF.updateUI();
265 }
266 } catch (Exception e) {
267 e.printStackTrace();
268 }
269
270 omStrategyCB.setSelectedItem(s.getOMStrategy().getClassname());
271 omStrategyCB.updateUI();
272 try {
273 params = s.getOMStrategy().getOriginalParameters();
274 if (params != null && params.size() > 0) {
275 omStrategyTF.setParams(params);
276 omStrategyTF.updateUI();
277 }
278 } catch (Exception e) {
279 e.printStackTrace();
280 }
281
282 }
283
284 private void loadLists() {
285 BOAagentRepository dar = BOAagentRepository.getInstance();
286 ArrayList<String> offeringStrategies = dar.getOfferingStrategies();
287 ArrayList<String> acceptanceConditions = dar.getAcceptanceStrategies();
288 ArrayList<String> opponentModels = dar.getOpponentModels();
289 ArrayList<String> omStrategies = dar.getOMStrategies();
290
291 biddingStrategyModel = new ExtendedComboBoxModel<String>();
292 Collections.sort(offeringStrategies);
293 biddingStrategyModel.setInitialContent(offeringStrategies);
294 biddingStrategyCB.setModel(biddingStrategyModel);
295 if (offeringStrategies.size() > 0) {
296 biddingStrategyCB.setSelectedIndex(0);
297 }
298
299 acceptanceStrategyModel = new ExtendedComboBoxModel<String>();
300 Collections.sort(acceptanceConditions);
301 acceptanceStrategyModel.setInitialContent(acceptanceConditions);
302 acceptanceStrategyCB.setModel(acceptanceStrategyModel);
303 if (acceptanceConditions.size() > 0) {
304 acceptanceStrategyCB.setSelectedIndex(0);
305 }
306
307 opponentModelModel = new ExtendedComboBoxModel<String>();
308 Collections.sort(opponentModels);
309 opponentModelModel.setInitialContent(opponentModels);
310 opponentModelCB.setModel(opponentModelModel);
311 if (opponentModels.size() > 0) {
312 opponentModelCB.setSelectedIndex(0);
313 }
314
315 omStrategyModel = new ExtendedComboBoxModel<String>();
316 Collections.sort(omStrategies);
317 omStrategyModel.setInitialContent(omStrategies);
318 omStrategyCB.setModel(omStrategyModel);
319 if (omStrategies.size() > 0) {
320 omStrategyCB.setSelectedIndex(0);
321 }
322 }
323
324 private void initBiddingStrategyUI() {
325 final JButton change = new JButton("Change");
326
327 biddingStrategyLabel = new JLabel();
328 biddingStrategyLabel.setFont(new Font("Tahoma", 1, 13));
329 biddingStrategyLabel.setText("Bidding Strategy");
330 getContentPane().add(biddingStrategyLabel, new AbsoluteConstraints(10, 15, 230, -1));
331
332 biddingStrategyTF = new BOATextField(NegoGUIApp.negoGUIView.getFrame());
333 getContentPane().add(biddingStrategyTF, new AbsoluteConstraints(10, 70, 150, -1));
334
335 biddingStrategyCB = new JComboBox();
336 getContentPane().add(biddingStrategyCB, new AbsoluteConstraints(10, 40, 230, -1));
337 biddingStrategyCB.addActionListener(new java.awt.event.ActionListener() {
338 public void actionPerformed(java.awt.event.ActionEvent evt) {
339 BOArepItem item = BOAagentRepository.getInstance()
340 .getBiddingStrategyRepItem(biddingStrategyCB.getSelectedItem().toString());
341 try {
342 Set<BOAparameter> params = item.getInstance().getParameterSpec();
343 biddingStrategyTF.setParams(params);
344 change.setEnabled(!(params.isEmpty()));
345 } catch (Exception e) {
346 e.printStackTrace();
347 }
348 }
349 });
350
351 getContentPane().add(change, new AbsoluteConstraints(165, 70, 75, -1));
352 change.addActionListener(new java.awt.event.ActionListener() {
353 public void actionPerformed(java.awt.event.ActionEvent evt) {
354 if (biddingStrategyTF.getBOAparameters().size() == 0) {
355 JOptionPane.showMessageDialog(null, "This item has no parameters.", "Item notification", 1);
356 } else {
357 Set<BOAparameter> result = new ParameterFrame(NegoGUIApp.negoGUIView.getFrame())
358 .getResult(biddingStrategyTF.getBOAparameters());
359 biddingStrategyTF.setParams(result);
360 }
361 }
362 });
363 }
364
365 private void initOpponentModelUI() {
366 final JButton change = new JButton("Change");
367
368 opponentModelLabel = new JLabel();
369 opponentModelLabel.setFont(new Font("Tahoma", 1, 13));
370 opponentModelLabel.setText("Opponent Model");
371 getContentPane().add(opponentModelLabel, new AbsoluteConstraints(510, 15, 230, -1));
372
373 opponentModelTF = new BOATextField(NegoGUIApp.negoGUIView.getFrame());
374 getContentPane().add(opponentModelTF, new AbsoluteConstraints(510, 70, 150, -1));
375
376 opponentModelCB = new JComboBox();
377 getContentPane().add(opponentModelCB, new AbsoluteConstraints(510, 40, 230, -1));
378 opponentModelCB.addActionListener(new java.awt.event.ActionListener() {
379 public void actionPerformed(java.awt.event.ActionEvent evt) {
380 BOArepItem item = BOAagentRepository.getInstance()
381 .getOpponentModelRepItem(opponentModelCB.getSelectedItem().toString());
382 try {
383 Set<BOAparameter> params = item.getInstance().getParameterSpec();
384 opponentModelTF.setParams(params);
385 change.setEnabled(!(params.isEmpty()));
386 } catch (Exception e) {
387 e.printStackTrace();
388 }
389 }
390 });
391
392 getContentPane().add(change, new AbsoluteConstraints(665, 70, 75, -1));
393 change.addActionListener(new java.awt.event.ActionListener() {
394 public void actionPerformed(java.awt.event.ActionEvent evt) {
395 if (opponentModelTF.getBOAparameters().size() == 0) {
396 JOptionPane.showMessageDialog(null, "This item has no parameters.", "Item notification", 1);
397 } else {
398 Set<BOAparameter> result = new ParameterFrame(NegoGUIApp.negoGUIView.getFrame())
399 .getResult(opponentModelTF.getBOAparameters());
400 opponentModelTF.setParams(result);
401 }
402 }
403 });
404 }
405
406 private void initAcceptanceStrategyUI() {
407 final JButton change = new JButton("Change");
408
409 acceptanceStrategyLabel = new JLabel();
410 acceptanceStrategyLabel.setFont(new Font("Tahoma", 1, 13));
411 acceptanceStrategyLabel.setText("Acceptance Strategy");
412 getContentPane().add(acceptanceStrategyLabel, new AbsoluteConstraints(260, 15, 230, 21));
413
414 acceptanceStrategyTF = new BOATextField(NegoGUIApp.negoGUIView.getFrame());
415 getContentPane().add(acceptanceStrategyTF, new AbsoluteConstraints(260, 70, 150, -1));
416
417 acceptanceStrategyCB = new JComboBox();
418 getContentPane().add(acceptanceStrategyCB, new AbsoluteConstraints(260, 40, 230, -1));
419 acceptanceStrategyCB.addActionListener(new java.awt.event.ActionListener() {
420 public void actionPerformed(java.awt.event.ActionEvent evt) {
421 BOArepItem item = BOAagentRepository.getInstance()
422 .getAcceptanceStrategyRepItem(acceptanceStrategyCB.getSelectedItem().toString());
423 try {
424 Set<BOAparameter> params = item.getInstance().getParameterSpec();
425 acceptanceStrategyTF.setParams(params);
426 change.setEnabled(!(params.isEmpty()));
427
428 } catch (Exception e) {
429 e.printStackTrace();
430 }
431 }
432 });
433
434 getContentPane().add(change, new AbsoluteConstraints(415, 70, 75, -1));
435 change.addActionListener(new java.awt.event.ActionListener() {
436 public void actionPerformed(java.awt.event.ActionEvent evt) {
437 if (acceptanceStrategyTF.getBOAparameters().size() == 0) {
438 JOptionPane.showMessageDialog(null, "This item has no parameters.", "Item notification", 1);
439 } else {
440 Set<BOAparameter> result = new ParameterFrame(NegoGUIApp.negoGUIView.getFrame())
441 .getResult(acceptanceStrategyTF.getBOAparameters());
442 acceptanceStrategyTF.setParams(result);
443 }
444 }
445 });
446 }
447
448 private void initOpponentModelStrategyUI() {
449 final JButton change = new JButton("Change");
450
451 omStrategyLabel = new JLabel();
452 omStrategyLabel.setFont(new Font("Tahoma", 1, 13));
453 omStrategyLabel.setText("Opponent Model Strategy");
454 getContentPane().add(omStrategyLabel, new AbsoluteConstraints(760, 15, 230, -1));
455
456 omStrategyTF = new BOATextField(NegoGUIApp.negoGUIView.getFrame());
457 getContentPane().add(omStrategyTF, new AbsoluteConstraints(760, 70, 150, -1));
458
459 omStrategyCB = new JComboBox();
460 getContentPane().add(omStrategyCB, new AbsoluteConstraints(760, 40, 230, -1));
461 omStrategyCB.addActionListener(new java.awt.event.ActionListener() {
462 public void actionPerformed(java.awt.event.ActionEvent evt) {
463 BOArepItem item = BOAagentRepository.getInstance()
464 .getOpponentModelStrategyRepItem(omStrategyCB.getSelectedItem().toString());
465 try {
466 Set<BOAparameter> params = item.getInstance().getParameterSpec();
467 omStrategyTF.setParams(params);
468 change.setEnabled(!(params.isEmpty()));
469 } catch (Exception e) {
470 e.printStackTrace();
471 }
472 }
473 });
474
475 getContentPane().add(change, new AbsoluteConstraints(915, 70, 75, -1));
476 change.addActionListener(new java.awt.event.ActionListener() {
477 public void actionPerformed(java.awt.event.ActionEvent evt) {
478 if (omStrategyTF.getBOAparameters().size() == 0) {
479 JOptionPane.showMessageDialog(null, "This item has no parameters.", "Item notification", 1);
480 } else {
481 Set<BOAparameter> result = new ParameterFrame(NegoGUIApp.negoGUIView.getFrame())
482 .getResult(omStrategyTF.getBOAparameters());
483 omStrategyTF.setParams(result);
484 }
485 }
486 });
487 }
488
489}
Note: See TracBrowser for help on using the repository browser.