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