1 | package negotiator.parties;
|
---|
2 |
|
---|
3 | import java.awt.Component;
|
---|
4 | import java.awt.event.ActionEvent;
|
---|
5 | import java.awt.event.ActionListener;
|
---|
6 | import java.util.ArrayList;
|
---|
7 | import java.util.HashMap;
|
---|
8 | import java.util.List;
|
---|
9 |
|
---|
10 | import javax.swing.JComboBox;
|
---|
11 | import javax.swing.JLabel;
|
---|
12 | import javax.swing.JProgressBar;
|
---|
13 | import javax.swing.JTextArea;
|
---|
14 | import javax.swing.table.AbstractTableModel;
|
---|
15 |
|
---|
16 | import genius.core.Bid;
|
---|
17 | import genius.core.exceptions.Warning;
|
---|
18 | import genius.core.issue.Issue;
|
---|
19 | import genius.core.issue.IssueDiscrete;
|
---|
20 | import genius.core.issue.Value;
|
---|
21 | import genius.core.issue.ValueDiscrete;
|
---|
22 | import genius.core.utility.AdditiveUtilitySpace;
|
---|
23 | import genius.core.utility.EvaluatorDiscrete;
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * NegoInfo is the class that contains all the negotiation data, and handles the
|
---|
27 | * GUI, updating the JTable. This is the main interface to the actual JTable.
|
---|
28 | * This is usually called XXXModel but I dont like the 'model' in the name. We
|
---|
29 | * implement actionlistener to hear the combo box events that require
|
---|
30 | * re-rendering of the total cost and utility field. We are pretty hard-wired
|
---|
31 | * for a 3-column table, with column 0 the labels, column 1 the opponent bid and
|
---|
32 | * col2 our own bid.
|
---|
33 | *
|
---|
34 | * <p>
|
---|
35 | * Many dialogs in negotiator.parties package use this.
|
---|
36 | */
|
---|
37 | @SuppressWarnings("serial")
|
---|
38 | class NegoInfo extends AbstractTableModel implements ActionListener {
|
---|
39 | public Bid ourOldBid; // Bid is hashmap <issueID,Value>. Our current bid is
|
---|
40 | // only in the comboboxes,
|
---|
41 | // use getBid().
|
---|
42 | public Bid lastAccepted;
|
---|
43 | public AdditiveUtilitySpace utilitySpace; // WARNING: this may be null
|
---|
44 | public List<Issue> issues = new ArrayList<Issue>();
|
---|
45 | // the issues, in row order as in the GUI. Init to empty, to enable
|
---|
46 | // freshly initialized NegoInfo to give useful results to the GUI.
|
---|
47 | public ArrayList<Integer> IDs; // the IDs/numbers of the issues, ordered to
|
---|
48 | // row number
|
---|
49 | public ArrayList<JComboBox> comboBoxes; // the combo boxes for the second
|
---|
50 | // column, ordered to row number
|
---|
51 |
|
---|
52 | NegoInfo(Bid our, Bid lastAccepted, AdditiveUtilitySpace us)
|
---|
53 | throws Exception {
|
---|
54 | // Wouter: just discovered that assert does nothing...........
|
---|
55 | // David@Wouter: Assert only works when assert compile flag is set to
|
---|
56 | // true
|
---|
57 | ourOldBid = our;
|
---|
58 | this.lastAccepted = lastAccepted;
|
---|
59 | utilitySpace = us;
|
---|
60 | issues = utilitySpace.getDomain().getIssues();
|
---|
61 | IDs = new ArrayList<Integer>();
|
---|
62 | for (int i = 0; i < issues.size(); i++)
|
---|
63 | IDs.add(issues.get(i).getNumber());
|
---|
64 | makeComboBoxes();
|
---|
65 | }
|
---|
66 |
|
---|
67 | @Override
|
---|
68 | public int getColumnCount() {
|
---|
69 | return 3;
|
---|
70 | }
|
---|
71 |
|
---|
72 | @Override
|
---|
73 | public int getRowCount() {
|
---|
74 | // the extra row shows the utility of the bids.
|
---|
75 | return issues.size() + 1;
|
---|
76 | }
|
---|
77 |
|
---|
78 | @Override
|
---|
79 | public boolean isCellEditable(int row, int col) {
|
---|
80 | return (col == 2 && row < issues.size());
|
---|
81 | }
|
---|
82 |
|
---|
83 | private String[] colNames = { "Issue", "Last Accepted Bid", "Your bid" };
|
---|
84 |
|
---|
85 | @Override
|
---|
86 | public String getColumnName(int col) {
|
---|
87 | return colNames[col];
|
---|
88 | }
|
---|
89 |
|
---|
90 | public void setOurBid(Bid bid) throws Exception {
|
---|
91 | ourOldBid = bid;
|
---|
92 | if (bid == null)
|
---|
93 | try {
|
---|
94 | ourOldBid = utilitySpace.getMaxUtilityBid();
|
---|
95 | } catch (Exception e) {
|
---|
96 | System.out.println("error getting max utility first bid:"
|
---|
97 | + e.getMessage());
|
---|
98 | e.printStackTrace();
|
---|
99 | }
|
---|
100 | makeComboBoxes(); // reset all
|
---|
101 | setComboBoxes(ourOldBid);
|
---|
102 | }
|
---|
103 |
|
---|
104 | void makeComboBoxes() throws Exception {
|
---|
105 | comboBoxes = new ArrayList<JComboBox>();
|
---|
106 | for (Issue issue : issues) {
|
---|
107 | if (!(issue instanceof IssueDiscrete))
|
---|
108 | throw new IllegalArgumentException("Not supported issue "
|
---|
109 | + issue + ": not IssueDiscrete. ");
|
---|
110 | List<ValueDiscrete> values = ((IssueDiscrete) issue).getValues();
|
---|
111 | JComboBox cbox = new JComboBox();
|
---|
112 | EvaluatorDiscrete eval = null;
|
---|
113 | if (utilitySpace != null)
|
---|
114 | eval = (EvaluatorDiscrete) utilitySpace.getEvaluator(issue
|
---|
115 | .getNumber());
|
---|
116 | for (ValueDiscrete val : values) {
|
---|
117 | String utilinfo = "";
|
---|
118 | if (eval != null)
|
---|
119 | try {
|
---|
120 | // utilinfo="("+eval.getEvaluation(val)+")";
|
---|
121 | utilinfo = "(" + eval.getValue(val) + ")";
|
---|
122 |
|
---|
123 | } catch (Exception e) {
|
---|
124 | System.out.println("no evaluator for " + val + "???");
|
---|
125 | }
|
---|
126 |
|
---|
127 | cbox.addItem(val + utilinfo);
|
---|
128 | }
|
---|
129 | comboBoxes.add(cbox);
|
---|
130 | for (JComboBox b : comboBoxes)
|
---|
131 | b.addActionListener(this);
|
---|
132 | }
|
---|
133 | }
|
---|
134 |
|
---|
135 | /**
|
---|
136 | * set the initial combo box selections according to ourOldBid Note, we can
|
---|
137 | * only handle Discrete evaluators right now.
|
---|
138 | *
|
---|
139 | * @throws if
|
---|
140 | * there is a problem with the issues and evaluators.
|
---|
141 | */
|
---|
142 | void setComboBoxes(Bid bid) throws Exception {
|
---|
143 | for (int i = 0; i < issues.size(); i++) {
|
---|
144 | IssueDiscrete iss = (IssueDiscrete) issues.get(i);
|
---|
145 | ValueDiscrete val = (ValueDiscrete) bid.getValue(iss.getNumber());
|
---|
146 | comboBoxes.get(i).setSelectedIndex(
|
---|
147 | ((IssueDiscrete) iss).getValueIndex(val));
|
---|
148 | }
|
---|
149 | }
|
---|
150 |
|
---|
151 | /**
|
---|
152 | * get the currently chosen evaluation value of given row in the table.
|
---|
153 | *
|
---|
154 | * @param bid
|
---|
155 | * : which bid (the column in the table are for ourBid and
|
---|
156 | * opponentBid)
|
---|
157 | * @return the evaluation of the given row in the bid. returns null if the
|
---|
158 | * bid has no value in that row.
|
---|
159 | * @throws probablly
|
---|
160 | * if rownr is out of range 0...issues.size()-1
|
---|
161 | */
|
---|
162 | Value getCurrentEval(Bid bid, int rownr) throws Exception {
|
---|
163 | if (bid == null)
|
---|
164 | return null;
|
---|
165 | Integer ID = IDs.get(rownr); // get ID of the issue in question.
|
---|
166 | return bid.getValue(ID); // get the current value for that issue in the
|
---|
167 | // bid
|
---|
168 | }
|
---|
169 |
|
---|
170 | /**
|
---|
171 | * get a render component
|
---|
172 | *
|
---|
173 | * @return the Component that can be rendered to show this cell.
|
---|
174 | */
|
---|
175 | @Override
|
---|
176 | public Component getValueAt(int row, int col) {
|
---|
177 | if (row == issues.size()) {
|
---|
178 | if (col == 0)
|
---|
179 | return new JLabel("Utility:");
|
---|
180 | if (utilitySpace == null)
|
---|
181 | return new JLabel("No UtilSpace");
|
---|
182 | Bid bid;
|
---|
183 | if (col == 1)
|
---|
184 | bid = lastAccepted;
|
---|
185 | else
|
---|
186 | try {
|
---|
187 | bid = getBid();
|
---|
188 | } catch (Exception e) {
|
---|
189 | bid = null;
|
---|
190 | System.out.println("Internal err with getBid:"
|
---|
191 | + e.getMessage());
|
---|
192 | }
|
---|
193 | ;
|
---|
194 | JProgressBar bar = new JProgressBar(0, 100);
|
---|
195 | bar.setStringPainted(true);
|
---|
196 | try {
|
---|
197 | bar.setValue((int) (0.5 + 100.0 * utilitySpace.getUtility(bid)));
|
---|
198 | bar.setIndeterminate(false);
|
---|
199 | } catch (Exception e) {
|
---|
200 | new Warning("Exception during cost calculation:"
|
---|
201 | + e.getMessage(), false, 1);
|
---|
202 | bar.setIndeterminate(true);
|
---|
203 | }
|
---|
204 |
|
---|
205 | return bar;
|
---|
206 | }
|
---|
207 |
|
---|
208 | switch (col) {
|
---|
209 | case 0:
|
---|
210 | return new JTextArea(issues.get(row).getName());
|
---|
211 | case 1:
|
---|
212 | Value value = null;
|
---|
213 | try {
|
---|
214 | value = getCurrentEval(lastAccepted, row);
|
---|
215 | } catch (Exception e) {
|
---|
216 | System.out.println("Err EnterBidDialog2.getValueAt: "
|
---|
217 | + e.getMessage());
|
---|
218 | }
|
---|
219 | if (value == null)
|
---|
220 | return new JTextArea("-");
|
---|
221 | return new JTextArea(value.toString());
|
---|
222 | case 2:
|
---|
223 | return comboBoxes.get(row);
|
---|
224 | }
|
---|
225 | return null;
|
---|
226 | }
|
---|
227 |
|
---|
228 | Bid getBid() throws Exception {
|
---|
229 | HashMap<Integer, Value> values = new HashMap<Integer, Value>();
|
---|
230 |
|
---|
231 | for (int i = 0; i < issues.size(); i++)
|
---|
232 | values.put(IDs.get(i), ((IssueDiscrete) issues.get(i))
|
---|
233 | .getValue(comboBoxes.get(i).getSelectedIndex()));
|
---|
234 | // values.put(IDs.get(i), (Value)comboBoxes.get(i).getSelectedItem());
|
---|
235 | return new Bid(utilitySpace.getDomain(), values);
|
---|
236 | }
|
---|
237 |
|
---|
238 | public void actionPerformed(ActionEvent e) {
|
---|
239 | // System.out.println("event d!"+e);
|
---|
240 | // receiveMessage the cost and utility of our own bid.
|
---|
241 | fireTableCellUpdated(issues.size(), 2);
|
---|
242 | fireTableCellUpdated(issues.size() + 1, 2);
|
---|
243 | }
|
---|
244 |
|
---|
245 | }
|
---|