1 | package geniusweb.exampleparties.simpleshaop;
|
---|
2 |
|
---|
3 | import static org.junit.Assert.assertEquals;
|
---|
4 | import static org.junit.Assert.assertFalse;
|
---|
5 | import static org.junit.Assert.assertNotNull;
|
---|
6 | import static org.junit.Assert.assertTrue;
|
---|
7 | import static org.mockito.Matchers.eq;
|
---|
8 | import static org.mockito.Mockito.mock;
|
---|
9 | import static org.mockito.Mockito.verify;
|
---|
10 |
|
---|
11 | import java.io.IOException;
|
---|
12 | import java.net.URI;
|
---|
13 | import java.net.URISyntaxException;
|
---|
14 | import java.nio.charset.StandardCharsets;
|
---|
15 | import java.nio.file.Files;
|
---|
16 | import java.nio.file.Paths;
|
---|
17 | import java.util.ArrayList;
|
---|
18 | import java.util.HashMap;
|
---|
19 | import java.util.LinkedList;
|
---|
20 | import java.util.List;
|
---|
21 | import java.util.Map;
|
---|
22 | import java.util.logging.Level;
|
---|
23 |
|
---|
24 | import javax.websocket.DeploymentException;
|
---|
25 |
|
---|
26 | import org.junit.Before;
|
---|
27 | import org.junit.Ignore;
|
---|
28 | import org.junit.Test;
|
---|
29 |
|
---|
30 | import com.fasterxml.jackson.core.JsonParseException;
|
---|
31 | import com.fasterxml.jackson.databind.JsonMappingException;
|
---|
32 | import com.fasterxml.jackson.databind.ObjectMapper;
|
---|
33 |
|
---|
34 | import geniusweb.actions.Accept;
|
---|
35 | import geniusweb.actions.Action;
|
---|
36 | import geniusweb.actions.ElicitComparison;
|
---|
37 | import geniusweb.actions.EndNegotiation;
|
---|
38 | import geniusweb.actions.Offer;
|
---|
39 | import geniusweb.actions.PartyId;
|
---|
40 | import geniusweb.bidspace.AllBidsList;
|
---|
41 | import geniusweb.connection.ConnectionEnd;
|
---|
42 | import geniusweb.issuevalue.Bid;
|
---|
43 | import geniusweb.issuevalue.DiscreteValue;
|
---|
44 | import geniusweb.issuevalue.Value;
|
---|
45 | import geniusweb.party.Capabilities;
|
---|
46 | import geniusweb.party.inform.ActionDone;
|
---|
47 | import geniusweb.party.inform.Finished;
|
---|
48 | import geniusweb.party.inform.Inform;
|
---|
49 | import geniusweb.party.inform.Settings;
|
---|
50 | import geniusweb.party.inform.YourTurn;
|
---|
51 | import geniusweb.profile.Profile;
|
---|
52 | import geniusweb.profileconnection.ProfileConnectionFactory;
|
---|
53 | import geniusweb.profileconnection.ProfileInterface;
|
---|
54 | import geniusweb.progress.ProgressRounds;
|
---|
55 | import geniusweb.references.Parameters;
|
---|
56 | import geniusweb.references.ProfileRef;
|
---|
57 | import geniusweb.references.ProtocolRef;
|
---|
58 | import geniusweb.references.Reference;
|
---|
59 | import tudelft.utilities.listener.DefaultListenable;
|
---|
60 | import tudelft.utilities.logging.ReportToLogger;
|
---|
61 | import tudelft.utilities.logging.Reporter;
|
---|
62 |
|
---|
63 | public class ShaopPartyTest {
|
---|
64 |
|
---|
65 | private static final String SHAOP = "SHAOP";
|
---|
66 | private static final PartyId otherparty = new PartyId("other");
|
---|
67 | //private static final String PROFILE = "src/test/resources/testprofile.json";
|
---|
68 |
|
---|
69 | private static final String PROFILE = "src/test/resources/jobs1.json";
|
---|
70 |
|
---|
71 | private final static ObjectMapper jackson = new ObjectMapper();
|
---|
72 |
|
---|
73 | private ShaopParty party;
|
---|
74 | private final TestConnection connection = new TestConnection();
|
---|
75 | private final ProtocolRef protocol = mock(ProtocolRef.class);
|
---|
76 | private final ProgressRounds progress = mock(ProgressRounds.class);
|
---|
77 | private Settings settings;
|
---|
78 | private Profile profile;
|
---|
79 | private final Parameters parameters = new Parameters();
|
---|
80 |
|
---|
81 | @Before
|
---|
82 | public void before() throws JsonParseException, JsonMappingException,
|
---|
83 | IOException, URISyntaxException {
|
---|
84 | party = new ShaopParty();
|
---|
85 | settings = new Settings(new PartyId("party1"),
|
---|
86 | new ProfileRef(new URI("file:" + PROFILE)), protocol, progress,
|
---|
87 | parameters);
|
---|
88 |
|
---|
89 | String serialized = new String(Files.readAllBytes(Paths.get(PROFILE)),
|
---|
90 | StandardCharsets.UTF_8);
|
---|
91 | profile = jackson.readValue(serialized, Profile.class);
|
---|
92 |
|
---|
93 | }
|
---|
94 |
|
---|
95 | @Test
|
---|
96 | public void smokeTest() {
|
---|
97 | }
|
---|
98 |
|
---|
99 | @Test
|
---|
100 | public void getDescriptionTest() {
|
---|
101 | assertNotNull(party.getDescription());
|
---|
102 | }
|
---|
103 |
|
---|
104 |
|
---|
105 |
|
---|
106 |
|
---|
107 |
|
---|
108 |
|
---|
109 |
|
---|
110 |
|
---|
111 |
|
---|
112 |
|
---|
113 |
|
---|
114 | /*
|
---|
115 |
|
---|
116 | @Test
|
---|
117 | public void getCapabilitiesTest() {
|
---|
118 | Capabilities capabilities = party.getCapabilities();
|
---|
119 | assertFalse("party does not define protocols",
|
---|
120 | capabilities.getBehaviours().isEmpty());
|
---|
121 | }
|
---|
122 |
|
---|
123 | @Test
|
---|
124 | public void testInformConnection() {
|
---|
125 | party.connect(connection);
|
---|
126 | // agent should not start acting just after an inform
|
---|
127 | assertEquals(0, connection.getActions().size());
|
---|
128 | }
|
---|
129 |
|
---|
130 | @Test
|
---|
131 | public void testInformSettings() {
|
---|
132 | party.connect(connection);
|
---|
133 | connection.notifyListeners(settings);
|
---|
134 | assertEquals(0, connection.getActions().size());
|
---|
135 | }
|
---|
136 |
|
---|
137 | @Test
|
---|
138 | public void testInformAndConnection() {
|
---|
139 | party.connect(connection);
|
---|
140 | party.notifyChange(settings);
|
---|
141 | assertEquals(0, connection.getActions().size());
|
---|
142 | }
|
---|
143 |
|
---|
144 | @Test
|
---|
145 | public void testOtherWalksAway() {
|
---|
146 | party.connect(connection);
|
---|
147 | party.notifyChange(settings);
|
---|
148 |
|
---|
149 | party.notifyChange(new ActionDone(new EndNegotiation(otherparty)));
|
---|
150 |
|
---|
151 | // party should not act at this point
|
---|
152 | assertEquals(0, connection.getActions().size());
|
---|
153 | }
|
---|
154 |
|
---|
155 | @Test
|
---|
156 | public void testAgentHasFirstTurn() {
|
---|
157 | party.connect(connection);
|
---|
158 | party.notifyChange(settings);
|
---|
159 |
|
---|
160 | party.notifyChange(new YourTurn());
|
---|
161 | assertEquals(1, connection.getActions().size());
|
---|
162 | assertTrue(connection.getActions().get(0) instanceof Offer);
|
---|
163 | }
|
---|
164 |
|
---|
165 | @Test
|
---|
166 | public void testAgentElicitsComparison() {
|
---|
167 | party.connect(connection);
|
---|
168 | party.notifyChange(settings);
|
---|
169 |
|
---|
170 | Bid bid = makeBid("3", "3"); // not yet in profile
|
---|
171 | party.notifyChange(new ActionDone(new Offer(otherparty, bid)));
|
---|
172 | party.notifyChange(new YourTurn());
|
---|
173 | assertEquals(1, connection.getActions().size());
|
---|
174 | //assertTrue(connection.getActions().get(0) instanceof ElicitComparison);
|
---|
175 |
|
---|
176 | }
|
---|
177 |
|
---|
178 | @Ignore
|
---|
179 | @Test
|
---|
180 | public void testAgentAccepts() {
|
---|
181 | // to make agent accept, the offered bid must score >0.9
|
---|
182 | // we would need to mock a lot t make that happen
|
---|
183 | // as we need to place >10 comparable bids for that.
|
---|
184 | party.connect(connection);
|
---|
185 | party.notifyChange(settings);
|
---|
186 |
|
---|
187 | Bid bid = makeBid("1", "1");// best pssible
|
---|
188 | party.notifyChange(new ActionDone(new Offer(otherparty, bid)));
|
---|
189 | party.notifyChange(new YourTurn());
|
---|
190 | assertEquals(1, connection.getActions().size());
|
---|
191 | assertTrue(connection.getActions().get(0) instanceof Accept);
|
---|
192 |
|
---|
193 | }
|
---|
194 |
|
---|
195 | @Test
|
---|
196 | public void testAgentLogsFinal() {
|
---|
197 | // this log output is optional, this is to show how to check log
|
---|
198 | Reporter reporter = mock(Reporter.class);
|
---|
199 | party = new ShaopParty(reporter);
|
---|
200 | party.connect(connection);
|
---|
201 | party.notifyChange(settings);
|
---|
202 | party.notifyChange(new Finished(null));
|
---|
203 |
|
---|
204 | verify(reporter).log(eq(Level.INFO),
|
---|
205 | eq("Final ourcome:Finished[null]"));
|
---|
206 | }
|
---|
207 |
|
---|
208 | @Test
|
---|
209 | public void testAgentsUpdatesProgress() {
|
---|
210 | party.connect(connection);
|
---|
211 | party.notifyChange(settings);
|
---|
212 |
|
---|
213 | party.notifyChange(new ActionDone(new Offer(null, mock(Bid.class))));
|
---|
214 |
|
---|
215 | //party.notifyChange(new YourTurn());
|
---|
216 | //verify(progress).advance();
|
---|
217 | }
|
---|
218 |
|
---|
219 | @Test
|
---|
220 | public void testGetCapabilities() {
|
---|
221 | assertTrue(party.getCapabilities().getBehaviours().contains(SHAOP));
|
---|
222 | }
|
---|
223 |
|
---|
224 |
|
---|
225 | */
|
---|
226 |
|
---|
227 |
|
---|
228 |
|
---|
229 |
|
---|
230 |
|
---|
231 |
|
---|
232 |
|
---|
233 |
|
---|
234 |
|
---|
235 |
|
---|
236 | private Bid myMakeBid(String val1, String val2,String val3, String val4,String val5, String val6) {
|
---|
237 | Map<String, Value> map = new HashMap<>();
|
---|
238 | map.put("lease car", new DiscreteValue(val1));
|
---|
239 | map.put("permanent contract", new DiscreteValue(val2));
|
---|
240 | map.put("career development opportunities", new DiscreteValue(val3));
|
---|
241 | map.put("fte", new DiscreteValue(val4));
|
---|
242 | map.put("salary", new DiscreteValue(val5));
|
---|
243 | map.put("work from home", new DiscreteValue(val6));
|
---|
244 | return new Bid(map);
|
---|
245 |
|
---|
246 | }
|
---|
247 |
|
---|
248 |
|
---|
249 |
|
---|
250 |
|
---|
251 |
|
---|
252 |
|
---|
253 | private Bid makeBid(String val1, String val2) {
|
---|
254 | Map<String, Value> map = new HashMap<>();
|
---|
255 | map.put("iss1", new DiscreteValue(val1));
|
---|
256 | map.put("iss2", new DiscreteValue(val2));
|
---|
257 | return new Bid(map);
|
---|
258 |
|
---|
259 | }
|
---|
260 |
|
---|
261 |
|
---|
262 | @Test
|
---|
263 | public void WinkyA2UpdateTest() {
|
---|
264 |
|
---|
265 | List<Bid> bidOrder = new ArrayList<Bid>();
|
---|
266 |
|
---|
267 | String[] iss1 = {"yes", "no"};
|
---|
268 | String[] iss2 = {"yes", "no"};
|
---|
269 | String[] iss3 = {"low", "medium", "high"};
|
---|
270 | String[] iss4 = {"0.6", "0.8", "1.0"};
|
---|
271 | String[] iss5 = {"2000", "2500", "3000", "3500", "4000"};
|
---|
272 | String[] iss6 = {"0", "1", "2"};
|
---|
273 |
|
---|
274 |
|
---|
275 |
|
---|
276 |
|
---|
277 |
|
---|
278 |
|
---|
279 | for (int i = 0; i < 2; i++) {
|
---|
280 | for (int j = 0; j < 1; j++) {
|
---|
281 | for (int j2 = 0; j2 < 1; j2++) {
|
---|
282 | for (int k = 0; k < 1; k++) {
|
---|
283 | for (int k2 = 0; k2 < 3; k2++) {
|
---|
284 | for (int l = 0; l < 1; l++) {
|
---|
285 | bidOrder.add( myMakeBid(iss1[i], iss2[j], iss3[j2], iss4[k], iss5[k2], iss6[l]) );
|
---|
286 | }
|
---|
287 | }
|
---|
288 | }
|
---|
289 | }
|
---|
290 |
|
---|
291 | }
|
---|
292 | }
|
---|
293 |
|
---|
294 |
|
---|
295 |
|
---|
296 |
|
---|
297 |
|
---|
298 | AllBidsList allBids = new AllBidsList(profile.getDomain());
|
---|
299 |
|
---|
300 |
|
---|
301 |
|
---|
302 | Reporter reporter = new ReportToLogger("");
|
---|
303 |
|
---|
304 | ProfileInterface profileint = null;
|
---|
305 | try {
|
---|
306 | profileint = ProfileConnectionFactory
|
---|
307 | .create(settings.getProfile().getURI(), reporter);
|
---|
308 | } catch (IOException | DeploymentException e) {
|
---|
309 | // TODO Auto-generated catch block
|
---|
310 | e.printStackTrace();
|
---|
311 | }
|
---|
312 |
|
---|
313 |
|
---|
314 | GravityEs g = new GravityEs(profile.getDomain(), bidOrder);
|
---|
315 |
|
---|
316 | Bid b = myMakeBid(iss1[1], iss2[1], iss3[0], iss4[0], iss5[0], iss6[0]);
|
---|
317 |
|
---|
318 | log2( g.getUtilityForBid(bidOrder.get(bidOrder.size()-1)) );
|
---|
319 |
|
---|
320 | /*
|
---|
321 | WinkyA2 winky = new WinkyA2(profile.getDomain(), bidOrder, profileint);
|
---|
322 |
|
---|
323 |
|
---|
324 | Bid b = myMakeBid(iss1[1], iss2[1], iss3[0], iss4[0], iss5[0], iss6[0]);
|
---|
325 | bidOrder.add(3, b);
|
---|
326 |
|
---|
327 |
|
---|
328 | log2("New bid => "+b+"\n\n");
|
---|
329 |
|
---|
330 | WinkyA2 winky1 = new WinkyA2(profile.getDomain(), bidOrder, profileint);
|
---|
331 |
|
---|
332 |
|
---|
333 | bidOrder.remove(3);
|
---|
334 | bidOrder.add(4, b);
|
---|
335 |
|
---|
336 | WinkyA2 winky2 = new WinkyA2(profile.getDomain(), bidOrder, profileint);
|
---|
337 | */
|
---|
338 |
|
---|
339 |
|
---|
340 | }
|
---|
341 |
|
---|
342 |
|
---|
343 |
|
---|
344 | private void log2(Object o) {
|
---|
345 |
|
---|
346 | System.out.println(o);
|
---|
347 |
|
---|
348 | }
|
---|
349 |
|
---|
350 |
|
---|
351 | @Test
|
---|
352 | public void getBestBidInKnownSetTest() {
|
---|
353 | /*
|
---|
354 | party.connect(connection);
|
---|
355 | party.notifyChange(settings);
|
---|
356 |
|
---|
357 | //Bid myBid = makeBid("1", "1");// best pssible
|
---|
358 | //party.myBids.add(myBid);
|
---|
359 |
|
---|
360 | Bid oppBid = makeBid("4", "4");// worst pssible
|
---|
361 | party.oppBids.add(oppBid);
|
---|
362 |
|
---|
363 |
|
---|
364 | //assertEquals(0.0, party.getAgreementProbability(myBid, true),0.0000001 );
|
---|
365 | assertEquals(1.0, party.getAgreementProbability(oppBid, true),0.0000001);
|
---|
366 |
|
---|
367 |
|
---|
368 | MyBidDetails b1 = party.getBestBidInKnownSet(0.1);
|
---|
369 | System.out.println(b1.getBid()+" => "+b1.getEu());
|
---|
370 | party.myBids.add(b1.getBid());
|
---|
371 |
|
---|
372 | MyBidDetails b2 = party.getBestBidInKnownSet(0.1);
|
---|
373 | System.out.println(b2.getBid()+" => "+b2.getEu());
|
---|
374 | party.myBids.add(b2.getBid());
|
---|
375 |
|
---|
376 | MyBidDetails b3 = party.getBestBidInKnownSet(0.1);
|
---|
377 | System.out.println(b3.getBid()+" => "+b3.getEu());
|
---|
378 | party.myBids.add(b3.getBid());
|
---|
379 |
|
---|
380 | MyBidDetails b4 = party.getBestBidInKnownSet(0.1);
|
---|
381 | System.out.println(b4.getBid()+" => "+b4.getEu());
|
---|
382 | */
|
---|
383 |
|
---|
384 |
|
---|
385 | }
|
---|
386 |
|
---|
387 |
|
---|
388 | @Test
|
---|
389 | public void testSendOffer() {
|
---|
390 | /*
|
---|
391 | party.connect(connection);
|
---|
392 | party.notifyChange(settings);
|
---|
393 |
|
---|
394 | party.notifyChange(new ActionDone(new Offer(null, makeBid("4", "4"))));
|
---|
395 | party.notifyChange( new YourTurn());
|
---|
396 |
|
---|
397 | System.out.println("oppBids => " + party.oppBids );
|
---|
398 | System.out.println("myBids => " + party.myBids );
|
---|
399 | */
|
---|
400 |
|
---|
401 | //party.notifyChange(new ActionDone(new Offer(null, makeBid("3", "4"))));
|
---|
402 | //party.notifyChange( new YourTurn() );
|
---|
403 |
|
---|
404 | //System.out.println("oppBids => " + party.oppBids );
|
---|
405 | //System.out.println("myBids => " + party.myBids );
|
---|
406 |
|
---|
407 | }
|
---|
408 |
|
---|
409 |
|
---|
410 | }
|
---|
411 |
|
---|
412 | /**
|
---|
413 | * A "real" connection object, because the party is going to subscribe etc, and
|
---|
414 | * without a real connection we would have to do a lot of mocks that would make
|
---|
415 | * the test very hard to read.
|
---|
416 | *
|
---|
417 | */
|
---|
418 | class TestConnection extends DefaultListenable<Inform>
|
---|
419 | implements ConnectionEnd<Inform, Action> {
|
---|
420 | private List<Action> actions = new LinkedList<>();
|
---|
421 |
|
---|
422 | @Override
|
---|
423 | public void send(Action action) throws IOException {
|
---|
424 | actions.add(action);
|
---|
425 | }
|
---|
426 |
|
---|
427 | @Override
|
---|
428 | public Reference getReference() {
|
---|
429 | return null;
|
---|
430 | }
|
---|
431 |
|
---|
432 | @Override
|
---|
433 | public URI getRemoteURI() {
|
---|
434 | return null;
|
---|
435 | }
|
---|
436 |
|
---|
437 | @Override
|
---|
438 | public void close() {
|
---|
439 |
|
---|
440 | }
|
---|
441 |
|
---|
442 | @Override
|
---|
443 | public Error getError() {
|
---|
444 | return null;
|
---|
445 | }
|
---|
446 |
|
---|
447 | public List<Action> getActions() {
|
---|
448 | return actions;
|
---|
449 | }
|
---|
450 |
|
---|
451 |
|
---|
452 |
|
---|
453 |
|
---|
454 |
|
---|
455 |
|
---|
456 | }
|
---|