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

Last change on this file since 127 was 127, checked in by Wouter Pasman, 6 years ago

#41 ROLL BACK of rev.126 . So this version is equal to rev. 125

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