[1] | 1 | package genius.gui.session;
|
---|
| 2 |
|
---|
| 3 | import java.awt.BorderLayout;
|
---|
| 4 | import java.io.FileOutputStream;
|
---|
| 5 | import java.io.IOException;
|
---|
| 6 | import java.text.DateFormat;
|
---|
| 7 | import java.text.SimpleDateFormat;
|
---|
| 8 | import java.util.ArrayList;
|
---|
| 9 | import java.util.Date;
|
---|
| 10 | import java.util.List;
|
---|
| 11 | import java.util.concurrent.ExecutionException;
|
---|
| 12 | import java.util.concurrent.TimeoutException;
|
---|
| 13 |
|
---|
| 14 | import javax.swing.JFrame;
|
---|
| 15 | import javax.swing.JOptionPane;
|
---|
| 16 | import javax.swing.JPanel;
|
---|
| 17 | import javax.xml.stream.XMLStreamException;
|
---|
| 18 |
|
---|
| 19 | import genius.core.AgentID;
|
---|
| 20 | import genius.core.exceptions.InstantiateException;
|
---|
| 21 | import genius.core.exceptions.NegotiatorException;
|
---|
| 22 | import genius.core.listener.Listener;
|
---|
| 23 | import genius.core.logging.ConsoleLogger;
|
---|
| 24 | import genius.core.logging.FileLogger;
|
---|
| 25 | import genius.core.logging.XmlLogger;
|
---|
| 26 | import genius.core.parties.NegotiationParty;
|
---|
| 27 | import genius.core.parties.NegotiationPartyInternal;
|
---|
| 28 | import genius.core.parties.SessionsInfo;
|
---|
| 29 | import genius.core.protocol.MultilateralProtocol;
|
---|
| 30 | import genius.core.repository.ParticipantRepItem;
|
---|
| 31 | import genius.core.repository.ProfileRepItem;
|
---|
| 32 | import genius.core.session.ExecutorWithTimeout;
|
---|
| 33 | import genius.core.session.MultilateralSessionConfiguration;
|
---|
| 34 | import genius.core.session.Participant;
|
---|
| 35 | import genius.core.session.RepositoryException;
|
---|
| 36 | import genius.core.session.Session;
|
---|
| 37 | import genius.core.session.SessionConfiguration;
|
---|
| 38 | import genius.core.session.SessionManager;
|
---|
| 39 | import genius.core.session.TournamentManager;
|
---|
| 40 | import genius.gui.progress.session.ActionDocumentModel;
|
---|
| 41 | import genius.gui.progress.session.OutcomesListModel;
|
---|
| 42 | import 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")
|
---|
| 49 | public 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 | }
|
---|