1 | package genius.gui.progress.session;
|
---|
2 |
|
---|
3 | import javax.swing.JTextArea;
|
---|
4 | import javax.swing.text.BadLocationException;
|
---|
5 | import javax.swing.text.Document;
|
---|
6 | import javax.swing.text.PlainDocument;
|
---|
7 |
|
---|
8 | import genius.core.events.MultipartyNegoActionEvent;
|
---|
9 | import genius.core.events.NegotiationEvent;
|
---|
10 | import genius.core.events.SessionEndedNormallyEvent;
|
---|
11 | import genius.core.listener.Listener;
|
---|
12 | import genius.core.logging.CsvLogger;
|
---|
13 | import genius.core.session.Session;
|
---|
14 |
|
---|
15 | /**
|
---|
16 | * A document containing all actions that occured on the NegotiationEvent
|
---|
17 | * stream. Since this is a {@link Document} you can directly use this in a
|
---|
18 | * {@link JTextArea}.
|
---|
19 | *
|
---|
20 | */
|
---|
21 | @SuppressWarnings("serial")
|
---|
22 | public class ActionDocumentModel extends PlainDocument implements Listener<NegotiationEvent> {
|
---|
23 |
|
---|
24 | private int currentround = -1;
|
---|
25 |
|
---|
26 | public ActionDocumentModel() {
|
---|
27 | writeln("Starting negotiation session.\n");
|
---|
28 | }
|
---|
29 |
|
---|
30 | @Override
|
---|
31 | public void notifyChange(NegotiationEvent e) {
|
---|
32 | if (e instanceof MultipartyNegoActionEvent) {
|
---|
33 | MultipartyNegoActionEvent e1 = (MultipartyNegoActionEvent) e;
|
---|
34 | int round = e1.getRound();
|
---|
35 | if (round != currentround) {
|
---|
36 | currentround = round;
|
---|
37 | writeln("Round " + currentround);
|
---|
38 | }
|
---|
39 | writeln(" Turn " + e1.getTurn() + ":" + e1.getAction().getAgent() + " " + e1.getAction());
|
---|
40 | } else if (e instanceof SessionEndedNormallyEvent) {
|
---|
41 | SessionEndedNormallyEvent e1 = (SessionEndedNormallyEvent) e;
|
---|
42 | if (e1.getAgreement() == null)
|
---|
43 | writeln("No agreement found.");
|
---|
44 | else {
|
---|
45 | writeln("Found an agreement: " + e1.getAgreement());
|
---|
46 | }
|
---|
47 | Session session = ((SessionEndedNormallyEvent) e).getSession();
|
---|
48 | double runTime = session.getRuntimeInSeconds();
|
---|
49 | writeln("Finished negotiation session in " + runTime + " s.");
|
---|
50 | try {
|
---|
51 | writeln(CsvLogger.logSingleSession(((SessionEndedNormallyEvent) e).getSession(),
|
---|
52 | session.getInfo().getProtocol(), e1.getParties(), runTime));
|
---|
53 | } catch (Exception e2) {
|
---|
54 | e2.printStackTrace();
|
---|
55 | }
|
---|
56 |
|
---|
57 | }
|
---|
58 | }
|
---|
59 |
|
---|
60 | private void writeln(String text) {
|
---|
61 | try {
|
---|
62 | insertString(getLength(), text + "\n", null);
|
---|
63 | } catch (BadLocationException e) {
|
---|
64 | e.printStackTrace();
|
---|
65 | }
|
---|
66 | }
|
---|
67 |
|
---|
68 | }
|
---|