source: src/main/java/genius/gui/session/SessionPanel.java@ 126

Last change on this file since 126 was 126, checked in by Aron Hammond, 6 years ago

Added function to calculate opposition to MultiLateralAnalysis.java

Moved code to add RLBOA listeners to RLBOAUtils is misc package

Added input for strategyParameters to SessionPanel (gui)

!! close SessionInfo after tournament; this caused /tmp/ to fill up with GeniusData files

Our own package:

  • Added opponents and strategies that are mentioned in the report
  • Change class hierarchy, agents can now extend from RLBOAagentBilateral to inherit RL functionality.
  • States extend from AbstractState
File size: 8.7 KB
Line 
1package genius.gui.session;
2
3import java.awt.BorderLayout;
4import java.io.FileOutputStream;
5import java.io.IOException;
6import java.text.DateFormat;
7import java.text.SimpleDateFormat;
8import java.util.ArrayList;
9import java.util.Date;
10import java.util.List;
11import java.util.concurrent.ExecutionException;
12import java.util.concurrent.TimeoutException;
13
14import javax.swing.JFrame;
15import javax.swing.JOptionPane;
16import javax.swing.JPanel;
17import javax.xml.stream.XMLStreamException;
18
19import genius.core.AgentID;
20import genius.core.exceptions.InstantiateException;
21import genius.core.exceptions.NegotiatorException;
22import genius.core.listener.Listener;
23import genius.core.logging.ConsoleLogger;
24import genius.core.logging.FileLogger;
25import genius.core.logging.XmlLogger;
26import genius.core.misc.RLBOAUtils;
27import genius.core.parties.NegotiationParty;
28import genius.core.parties.NegotiationPartyInternal;
29import genius.core.parties.SessionsInfo;
30import genius.core.protocol.MultilateralProtocol;
31import genius.core.repository.ParticipantRepItem;
32import genius.core.repository.ProfileRepItem;
33import genius.core.session.ExecutorWithTimeout;
34import genius.core.session.MultilateralSessionConfiguration;
35import genius.core.session.Participant;
36import genius.core.session.RepositoryException;
37import genius.core.session.Session;
38import genius.core.session.SessionConfiguration;
39import genius.core.session.SessionManager;
40import genius.core.session.TournamentManager;
41import genius.gui.progress.session.ActionDocumentModel;
42import genius.gui.progress.session.OutcomesListModel;
43import genius.gui.progress.session.SessionProgressUI;
44
45/**
46 * Session Panel. Asks user to configure a session. When user presses run, the
47 * panel changes into a progress panel and a session runner is started.
48 */
49@SuppressWarnings("serial")
50public class SessionPanel extends JPanel {
51 public SessionPanel() {
52 final SessionModel model = new SessionModel();
53
54 setLayout(new BorderLayout());
55 add(new SessionConfigPanel(model), BorderLayout.CENTER);
56 model.addListener(new Listener<MultilateralSessionConfiguration>() {
57 @Override
58 public void notifyChange(final MultilateralSessionConfiguration config) {
59 new Thread(new Runnable() {
60 @Override
61 public void run() {
62 boolean showChart = model.getShowChart().getValue();
63 boolean biChart = model.getParticipantsModel().getSize() == 2
64 && model.getBilateralUtilUtilPlot().getValue();
65 runSession(config, showChart, biChart, model.getBilateralShowAllBids().getValue(),
66 model.getPrintEnabled().getValue());
67 }
68 }).start();
69 }
70
71 });
72
73 }
74
75 /**
76 * Runs a session and waits for completion.
77 *
78 * @param config
79 * @param showChart
80 * true iff a progress chart should be shown.
81 * @param useBiChart
82 * true iff the bilateral progress chart is to be used.
83 * @param showAllBids
84 */
85 private void runSession(MultilateralSessionConfiguration config, boolean showChart, boolean useBiChart,
86 boolean showAllBids, boolean isPrintEnabled) {
87 System.out.println("run session, with " + config);
88 try {
89 start(config, showChart, useBiChart, showAllBids, isPrintEnabled);
90 } catch (InstantiateException | RepositoryException | NegotiatorException | XMLStreamException | IOException
91 | TimeoutException | ExecutionException e) {
92 e.printStackTrace();
93 JOptionPane.showMessageDialog(null, "Session failed to run: " + e.getMessage(), "Warning",
94 JOptionPane.WARNING_MESSAGE);
95 }
96
97 }
98
99 /**
100 *
101 * @param config
102 * @param showChart
103 * @param showBiChart
104 * true if progress chart has to be shown
105 * @param showAllBids
106 * if the bilateral progress chart should be used. Ignored if
107 * showBiChart is false.
108 * @param isPrintEnabled
109 * true iff system out print is enabled.
110 * @throws InstantiateException
111 * @throws RepositoryException
112 * @throws NegotiatorException
113 * @throws XMLStreamException
114 * @throws IOException
115 * @throws TimeoutException
116 * @throws ExecutionException
117 */
118 public void start(MultilateralSessionConfiguration config, boolean showChart, boolean showBiChart,
119 boolean showAllBids, boolean isPrintEnabled) throws InstantiateException, RepositoryException,
120 NegotiatorException, XMLStreamException, IOException, TimeoutException, ExecutionException {
121
122 if (config.getParties().size() < 2) {
123 throw new IllegalArgumentException("There should be at least two negotiating agents !");
124 }
125
126 MultilateralProtocol protocol = TournamentManager.getProtocol(config.getProtocol());
127 SessionsInfo info = new SessionsInfo(protocol, config.getPersistentDataType(), isPrintEnabled);
128 Session session = new Session(config.getDeadline(), info);
129
130 ExecutorWithTimeout executor = new ExecutorWithTimeout(1000 * config.getDeadline().getTimeOrDefaultTimeout());
131 List<NegotiationPartyInternal> negoparties = getNegotiationParties(config, session, info, executor);
132 SessionManager sessionManager = new SessionManager((SessionConfiguration) config, negoparties, session,
133 executor);
134
135 RLBOAUtils.addReinforcementAgentListeners(negoparties, sessionManager);
136
137 displayProgress(negoparties, sessionManager, showChart, showBiChart, showAllBids);
138
139 // connect the loggers.
140 DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd-HHmmss");
141 String fileName = String.format("log/Log-Session_%s", dateFormat.format(new Date()));
142 FileLogger filelogger = new FileLogger(fileName);
143 XmlLogger xmlLogger = new XmlLogger(new FileOutputStream(fileName + ".xml"), "Session");
144 sessionManager.addListener(filelogger);
145 sessionManager.addListener(xmlLogger);
146 sessionManager.addListener(new ConsoleLogger());
147
148 System.out.println("Negotiation session has started.");
149 Thread t = new Thread(sessionManager);
150 t.start();
151 try {
152 t.join();
153 } catch (InterruptedException e) {
154 e.printStackTrace();
155 }
156
157 // close the loggers
158 System.out.println("Negotiation session has stopped.");
159 try {
160 filelogger.close();
161 } catch (IOException e) {
162 e.printStackTrace();
163 }
164 try {
165 xmlLogger.close();
166 } catch (IOException e) {
167 e.printStackTrace();
168 }
169 info.close();
170
171 }
172
173 /**
174 *
175 * @param negoparties
176 * @param sessionManager
177 * @param showChart
178 * true iff any progress chart has to be shown
179 * @param biChart
180 * if the bilateral progress chart has to be shown. Ignored if
181 * showChart is false.
182 * @param showAllBids
183 */
184 private void displayProgress(List<NegotiationPartyInternal> negoparties, SessionManager sessionManager,
185 boolean showChart, boolean biChart, boolean showAllBids) {
186 OutcomesListModel model = new OutcomesListModel(negoparties);
187 ActionDocumentModel actiondocument = new ActionDocumentModel();
188 sessionManager.addListener(model);
189 sessionManager.addListener(actiondocument);
190 removeAll();
191 add(new SessionProgressUI(model, actiondocument, showChart, biChart, showAllBids), BorderLayout.CENTER);
192 revalidate();
193 }
194
195 /**
196 *
197 * @param config
198 * @param session
199 * @param info
200 * @param executor
201 * the executor in which this session runs.
202 * @return the parties for this negotiation. Converts the config into actual
203 * {@link NegotiationParty}s
204 * @throws RepositoryException
205 * @throws NegotiatorException
206 * @throws ExecutionException
207 * @throws TimeoutException
208 */
209 private List<NegotiationPartyInternal> getNegotiationParties(MultilateralSessionConfiguration config,
210 Session session, SessionsInfo info, ExecutorWithTimeout executor)
211 throws RepositoryException, NegotiatorException, TimeoutException, ExecutionException {
212 List<ParticipantRepItem> parties = new ArrayList<>();
213 List<ProfileRepItem> profiles = new ArrayList<>();
214 List<AgentID> names = new ArrayList<AgentID>();
215
216 for (Participant participant : config.getParties()) {
217 ParticipantRepItem strategy = participant.getStrategy();
218 parties.add(strategy);
219 if (!strategy.isMediator()) {
220 profiles.add(participant.getProfile());
221 names.add(participant.getId());
222 }
223 }
224
225 return TournamentManager.getPartyList(executor, config, info, session);
226 // List<NegotiationPartyInternal> negoparties =
227 // TournamentGenerator.generateSessionParties(parties, profiles,
228 // names, session, info);
229 // return negoparties;
230 }
231
232 private List<NegotiationPartyInternal> getNonMediators(List<NegotiationPartyInternal> negoparties) {
233 List<NegotiationPartyInternal> list = new ArrayList<>();
234 for (NegotiationPartyInternal party : negoparties) {
235 if (!party.isMediator()) {
236 list.add(party);
237 }
238 }
239 return list;
240 }
241
242 /**
243 * simple stub to run this stand-alone (for testing).
244 *
245 * @param args
246 */
247 public static void main(String[] args) {
248 final JFrame gui = new JFrame();
249 gui.setLayout(new BorderLayout());
250 gui.getContentPane().add(new SessionPanel(), BorderLayout.CENTER);
251 gui.pack();
252 gui.setVisible(true);
253 }
254
255}
Note: See TracBrowser for help on using the repository browser.