1 | package genius.gui.progress.session;
|
---|
2 |
|
---|
3 | import static org.mockito.Mockito.mock;
|
---|
4 | import static org.mockito.Mockito.when;
|
---|
5 |
|
---|
6 | import java.awt.BorderLayout;
|
---|
7 | import java.io.IOException;
|
---|
8 | import java.util.ArrayList;
|
---|
9 | import java.util.List;
|
---|
10 |
|
---|
11 | import javax.swing.JFrame;
|
---|
12 |
|
---|
13 | import org.junit.Test;
|
---|
14 |
|
---|
15 | import genius.core.AgentID;
|
---|
16 | import genius.core.Bid;
|
---|
17 | import genius.core.Deadline;
|
---|
18 | import genius.core.DeadlineType;
|
---|
19 | import genius.core.Domain;
|
---|
20 | import genius.core.actions.Offer;
|
---|
21 | import genius.core.events.MultipartyNegoActionEvent;
|
---|
22 | import genius.core.events.SessionEndedNormallyEvent;
|
---|
23 | import genius.core.parties.NegotiationPartyInternal;
|
---|
24 | import genius.core.parties.PartyWithUtility;
|
---|
25 | import genius.core.parties.SessionsInfo;
|
---|
26 | import genius.core.persistent.PersistentDataType;
|
---|
27 | import genius.core.protocol.StackedAlternatingOffersProtocol;
|
---|
28 | import genius.core.session.Session;
|
---|
29 | import genius.core.timeline.ContinuousTimeline;
|
---|
30 | import genius.core.utility.UtilitySpace;
|
---|
31 | import genius.core.xml.SimpleElement;
|
---|
32 |
|
---|
33 | /***
|
---|
34 | * Simple test that runs the progress UI with random data for quick testing
|
---|
35 | *
|
---|
36 | */
|
---|
37 | public class SessionProgressUiTest {
|
---|
38 |
|
---|
39 | private static final int ROUNDS = 100;
|
---|
40 | private final static int NPARTIES = 3;
|
---|
41 | private static ArrayList<PartyWithUtility> partiesList = new ArrayList<>();
|
---|
42 | private static Session session;
|
---|
43 | private static List<NegotiationPartyInternal> parties = new ArrayList<NegotiationPartyInternal>();
|
---|
44 | private static SessionsInfo info;
|
---|
45 |
|
---|
46 | @Test
|
---|
47 | public void runSessionInProgressPanel() throws IOException, InterruptedException {
|
---|
48 | NegotiationPartyInternal partyInternal = mock(NegotiationPartyInternal.class);
|
---|
49 | Domain domain = mock(Domain.class);
|
---|
50 | UtilitySpace utilspace = mock(UtilitySpace.class);
|
---|
51 | when(utilspace.getDomain()).thenReturn(domain);
|
---|
52 |
|
---|
53 | when(partyInternal.getTimeLine()).thenReturn(mock(ContinuousTimeline.class));
|
---|
54 | when(partyInternal.getUtilitySpace()).thenReturn(utilspace);
|
---|
55 | parties.add(partyInternal);
|
---|
56 |
|
---|
57 | info = new SessionsInfo(new StackedAlternatingOffersProtocol(), PersistentDataType.DISABLED, false);
|
---|
58 |
|
---|
59 | for (int n = 0; n < NPARTIES; n++) {
|
---|
60 | partiesList.add(new myParty(n));
|
---|
61 | }
|
---|
62 | session = new Session(new Deadline(ROUNDS, DeadlineType.ROUND), info);
|
---|
63 |
|
---|
64 | final OutcomesListModel model = new OutcomesListModel(partiesList);
|
---|
65 | final ActionDocumentModel actiondocument = new ActionDocumentModel();
|
---|
66 |
|
---|
67 | final JFrame gui = new JFrame();
|
---|
68 | gui.setLayout(new BorderLayout());
|
---|
69 | gui.getContentPane().add(new SessionProgressUI(model, actiondocument, true, false, false), BorderLayout.CENTER);
|
---|
70 | gui.pack();
|
---|
71 | gui.setVisible(true);
|
---|
72 |
|
---|
73 | Thread thread = new Thread(new Runnable() {
|
---|
74 | @Override
|
---|
75 | public void run() {
|
---|
76 | insertRandomData(model, actiondocument);
|
---|
77 | insertEnd(model, actiondocument);
|
---|
78 | }
|
---|
79 |
|
---|
80 | });
|
---|
81 | thread.start();
|
---|
82 | thread.join();
|
---|
83 | gui.setVisible(false);
|
---|
84 |
|
---|
85 | // TODO check actual output in various panels?
|
---|
86 | }
|
---|
87 |
|
---|
88 | public static void main(String[] args) throws IOException, InterruptedException {
|
---|
89 | new SessionProgressUiTest().runSessionInProgressPanel();
|
---|
90 | }
|
---|
91 |
|
---|
92 | /**
|
---|
93 | * Insert random negotiation events in the models
|
---|
94 | *
|
---|
95 | * @param model
|
---|
96 | * @param actiondocument
|
---|
97 | */
|
---|
98 | private static void insertRandomData(OutcomesListModel model, ActionDocumentModel actiondocument) {
|
---|
99 | for (int round = 0; round < ROUNDS; round++) {
|
---|
100 | for (int turn = 0; turn < NPARTIES; turn++) {
|
---|
101 | try {
|
---|
102 | Thread.sleep(10);
|
---|
103 | } catch (InterruptedException e) {
|
---|
104 | e.printStackTrace();
|
---|
105 | }
|
---|
106 | model.addElement(new Outcome(null, round, turn, model.getParties(), Math.random() > 0.9,
|
---|
107 | new AgentID("party1"), (double) round / ROUNDS));
|
---|
108 | actiondocument.notifyChange(new MultipartyNegoActionEvent(
|
---|
109 | new Offer(model.getParties().get(turn).getID(), new Bid((Domain) null)), round, turn,
|
---|
110 | (double) round / ROUNDS, null, null));
|
---|
111 | }
|
---|
112 | }
|
---|
113 |
|
---|
114 | }
|
---|
115 |
|
---|
116 | /**
|
---|
117 | * Insert nego-finished in the models
|
---|
118 | *
|
---|
119 | * @param model
|
---|
120 | * @param actiondocument
|
---|
121 | */
|
---|
122 |
|
---|
123 | private static void insertEnd(OutcomesListModel model, ActionDocumentModel actiondocument) {
|
---|
124 |
|
---|
125 | actiondocument.notifyChange(new SessionEndedNormallyEvent(session, new Bid((Domain) null), parties));
|
---|
126 | }
|
---|
127 |
|
---|
128 | }
|
---|
129 |
|
---|
130 | /**
|
---|
131 | * Support class for the main() example code.
|
---|
132 | */
|
---|
133 | class myParty implements PartyWithUtility {
|
---|
134 |
|
---|
135 | private AgentID id;
|
---|
136 | private UtilitySpace utilspace;
|
---|
137 |
|
---|
138 | public myParty(int n) {
|
---|
139 | this.id = new AgentID("PartyNr" + n);
|
---|
140 | utilspace = new myUtilSpace();
|
---|
141 | }
|
---|
142 |
|
---|
143 | @Override
|
---|
144 | public AgentID getID() {
|
---|
145 | return id;
|
---|
146 | }
|
---|
147 |
|
---|
148 | @Override
|
---|
149 | public UtilitySpace getUtilitySpace() {
|
---|
150 | return utilspace;
|
---|
151 | }
|
---|
152 | }
|
---|
153 |
|
---|
154 | class myUtilSpace implements UtilitySpace {
|
---|
155 |
|
---|
156 | @Override
|
---|
157 | public Domain getDomain() {
|
---|
158 | return null;
|
---|
159 | }
|
---|
160 |
|
---|
161 | @Override
|
---|
162 | public double getUtility(Bid bid) {
|
---|
163 | return Math.random();
|
---|
164 | }
|
---|
165 |
|
---|
166 | @Override
|
---|
167 | public Double discount(double util, double time) {
|
---|
168 | return 0.8 * util;
|
---|
169 | }
|
---|
170 |
|
---|
171 | @Override
|
---|
172 | public UtilitySpace copy() {
|
---|
173 | return null;
|
---|
174 | }
|
---|
175 |
|
---|
176 | @Override
|
---|
177 | public String isComplete() {
|
---|
178 | return null;
|
---|
179 | }
|
---|
180 |
|
---|
181 | @Override
|
---|
182 | public SimpleElement toXML() throws IOException {
|
---|
183 | return null;
|
---|
184 | }
|
---|
185 |
|
---|
186 | @Override
|
---|
187 | public Double getReservationValue() {
|
---|
188 | return null;
|
---|
189 | }
|
---|
190 |
|
---|
191 | @Override
|
---|
192 | public String getName() {
|
---|
193 | return null;
|
---|
194 | }
|
---|
195 | }
|
---|