1 | package agents.nastyagent;
|
---|
2 |
|
---|
3 | import java.util.ArrayList;
|
---|
4 | import java.util.HashMap;
|
---|
5 | import java.util.List;
|
---|
6 |
|
---|
7 | import genius.core.AgentID;
|
---|
8 | import genius.core.Bid;
|
---|
9 | import genius.core.actions.Action;
|
---|
10 | import genius.core.actions.ActionWithBid;
|
---|
11 | import genius.core.list.Tuple;
|
---|
12 | import genius.core.parties.NegotiationInfo;
|
---|
13 | import genius.core.persistent.StandardInfo;
|
---|
14 | import genius.core.persistent.StandardInfoList;
|
---|
15 |
|
---|
16 | /**
|
---|
17 | * Checks if data is retained in persistent storage properly. This uses a hack.
|
---|
18 | *
|
---|
19 | *
|
---|
20 | */
|
---|
21 | public class CheckStoredData extends NastyAgent {
|
---|
22 | private static final String DATA = "data";
|
---|
23 | static boolean runBefore = false;
|
---|
24 | static List<ActionWithBid> previousActions = new ArrayList<ActionWithBid>();
|
---|
25 |
|
---|
26 | public void init(NegotiationInfo info) {
|
---|
27 | super.init(info);
|
---|
28 | if (runBefore) {
|
---|
29 | checkStore((StandardInfoList) data.get());
|
---|
30 | }
|
---|
31 | }
|
---|
32 |
|
---|
33 | private void checkStore(StandardInfoList standardInfoList) {
|
---|
34 | if (standardInfoList.size() != 1) {
|
---|
35 | throw new IllegalStateException("Expected 1 info but found " + standardInfoList.size());
|
---|
36 | }
|
---|
37 | StandardInfo info = standardInfoList.get(0);
|
---|
38 | if (info.getStartingAgent() == previousActions.get(0).getAgent().toString()) {
|
---|
39 | throw new IllegalStateException("the starting agent is not reported properly");
|
---|
40 | }
|
---|
41 |
|
---|
42 | List<Tuple<String, Double>> utilities = info.getUtilities();
|
---|
43 | if (utilities.size() != previousActions.size()) {
|
---|
44 | throw new IllegalStateException("The number of actions is not the expected " + previousActions.size());
|
---|
45 | }
|
---|
46 |
|
---|
47 | for (int n = 0; n < previousActions.size(); n++) {
|
---|
48 | if (!utilities.get(n).get1().startsWith(previousActions.get(n).getAgent().toString())) {
|
---|
49 | throw new IllegalStateException("agent at utility " + n + " differs");
|
---|
50 | }
|
---|
51 | if (utilities.get(n).get2() != utilitySpace.getUtility(previousActions.get(n).getBid())) {
|
---|
52 | throw new IllegalStateException("utility " + n + " differs");
|
---|
53 | }
|
---|
54 | }
|
---|
55 |
|
---|
56 | }
|
---|
57 |
|
---|
58 | @Override
|
---|
59 | public Action chooseAction(List<Class<? extends Action>> possibleActions) {
|
---|
60 | Action action = super.chooseAction(possibleActions);
|
---|
61 | if (!runBefore && action instanceof ActionWithBid) {
|
---|
62 | previousActions.add((ActionWithBid) action);
|
---|
63 | }
|
---|
64 | return action;
|
---|
65 | }
|
---|
66 |
|
---|
67 | @Override
|
---|
68 | public void receiveMessage(AgentID sender, Action action) {
|
---|
69 | if (!runBefore && action instanceof ActionWithBid) {
|
---|
70 | previousActions.add((ActionWithBid) action);
|
---|
71 | }
|
---|
72 | }
|
---|
73 |
|
---|
74 | @Override
|
---|
75 | public HashMap<String, String> negotiationEnded(Bid acceptedBid) {
|
---|
76 | // we actually got here. Report it.
|
---|
77 | super.negotiationEnded(acceptedBid);
|
---|
78 | runBefore = true;
|
---|
79 |
|
---|
80 | return null;
|
---|
81 | }
|
---|
82 |
|
---|
83 | }
|
---|