1 | package geniusweb.protocol.session.learn;
|
---|
2 |
|
---|
3 | import java.util.Collections;
|
---|
4 | import java.util.LinkedList;
|
---|
5 | import java.util.List;
|
---|
6 | import java.util.UUID;
|
---|
7 | import java.util.stream.Collectors;
|
---|
8 |
|
---|
9 | import com.fasterxml.jackson.annotation.JsonCreator;
|
---|
10 | import com.fasterxml.jackson.annotation.JsonProperty;
|
---|
11 |
|
---|
12 | import geniusweb.actions.FileLocation;
|
---|
13 | import geniusweb.deadline.Deadline;
|
---|
14 | import geniusweb.protocol.session.SessionProtocol;
|
---|
15 | import geniusweb.protocol.session.SessionSettings;
|
---|
16 | import geniusweb.protocol.session.TeamInfo;
|
---|
17 | import geniusweb.protocol.session.saop.SAOPSettings;
|
---|
18 | import geniusweb.references.Parameters;
|
---|
19 | import geniusweb.references.PartyWithProfile;
|
---|
20 | import tudelft.utilities.logging.Reporter;
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * Settings for learn protocol.
|
---|
24 | */
|
---|
25 | public class LearnSettings implements SessionSettings {
|
---|
26 | private final List<TeamInfo> participants;
|
---|
27 | private final Deadline deadline;
|
---|
28 |
|
---|
29 | /**
|
---|
30 | *
|
---|
31 | * @param participants the list of {@link PartyWithProfile} in clockwise
|
---|
32 | * order. There must be at least 2 to run the SAOP
|
---|
33 | * protocol. But SAOP can be initialized with less, for
|
---|
34 | * use in TournamentSettings.
|
---|
35 | * <table>
|
---|
36 | * <caption>Required parameters</caption>
|
---|
37 | * <tr>
|
---|
38 | * <td>persistentstate</td>
|
---|
39 | * <td>a {@link FileLocation}, serialized as json eg
|
---|
40 | * <code>"b29f3bf5-1dc4-499e-a676-7b2dbb864a03"</code> ,
|
---|
41 | * where the party's persistant state is stored; this
|
---|
42 | * must match the persistantstate used in earlier calls
|
---|
43 | * for e.g. {@link SAOPSettings}.</td>
|
---|
44 | * </tr>
|
---|
45 | *
|
---|
46 | * <tr>
|
---|
47 | * <td>negotiationdata</td>
|
---|
48 | * <td>a List of {@link FileLocation}s, serialized as
|
---|
49 | * json, matching the negotiationdata used in earlier
|
---|
50 | * calls for eg {@link SAOPSettings}.</td>
|
---|
51 | * </tr>
|
---|
52 | * </table>
|
---|
53 | * @param deadline the deadline of the negotiation.
|
---|
54 | */
|
---|
55 | @SuppressWarnings("unused")
|
---|
56 | @JsonCreator
|
---|
57 | public LearnSettings(
|
---|
58 | @JsonProperty("participants") List<TeamInfo> participants,
|
---|
59 | @JsonProperty("deadline") Deadline deadline) {
|
---|
60 | this.participants = participants;
|
---|
61 | this.deadline = deadline;
|
---|
62 | if (participants == null) {
|
---|
63 | throw new IllegalArgumentException("participants must not be null");
|
---|
64 | }
|
---|
65 | if (deadline == null) {
|
---|
66 | throw new IllegalArgumentException("deadline must not be null");
|
---|
67 | }
|
---|
68 |
|
---|
69 | for (PartyWithProfile pwithp : getAllParties()) {
|
---|
70 | Parameters params = pwithp.getParty().getParameters();
|
---|
71 | Object statestr = params.get("persistentstate");
|
---|
72 | if (!(statestr instanceof String))
|
---|
73 | throw new IllegalArgumentException(
|
---|
74 | "persistentstate parameter containing UUID string is required, but found "
|
---|
75 | + statestr);
|
---|
76 | // check that it contains a UUID. Don't store it, we don't need
|
---|
77 | // it.
|
---|
78 | UUID.fromString((String) statestr);
|
---|
79 |
|
---|
80 | // and check the negotiationdata
|
---|
81 | Object negotiationsstr = params.get("negotiationdata");
|
---|
82 | if (!(negotiationsstr instanceof List))
|
---|
83 | throw new IllegalArgumentException(
|
---|
84 | "negotiationdata parameter containing a List is required");
|
---|
85 |
|
---|
86 | for (Object negostr : (List) negotiationsstr) {
|
---|
87 | if (!(negostr instanceof String))
|
---|
88 | throw new IllegalArgumentException(
|
---|
89 | "The list in negotiationdata must contain UUID strings but found "
|
---|
90 | + negostr);
|
---|
91 | UUID.fromString((String) negostr);
|
---|
92 | }
|
---|
93 | }
|
---|
94 | }
|
---|
95 |
|
---|
96 | @Override
|
---|
97 | public Double getMaxRunTime() {
|
---|
98 | return deadline.getDuration() / 1000d;
|
---|
99 | }
|
---|
100 |
|
---|
101 | @Override
|
---|
102 | public SessionProtocol getProtocol(Reporter logger) {
|
---|
103 | return new Learn(new LearnState(this), logger);
|
---|
104 | }
|
---|
105 |
|
---|
106 | @Override
|
---|
107 | public List<TeamInfo> getTeams() {
|
---|
108 | return Collections.unmodifiableList(participants);
|
---|
109 | }
|
---|
110 |
|
---|
111 | /**
|
---|
112 | * @return the deadline for this negotiation
|
---|
113 | */
|
---|
114 | public Deadline getDeadline() {
|
---|
115 | return deadline;
|
---|
116 | }
|
---|
117 |
|
---|
118 | @Override
|
---|
119 | public List<PartyWithProfile> getAllParties() {
|
---|
120 | return participants.stream()
|
---|
121 | .map(particip -> particip.getParties().get(0))
|
---|
122 | .collect(Collectors.toList());
|
---|
123 | }
|
---|
124 |
|
---|
125 | @Override
|
---|
126 | public Integer getTeamSize() {
|
---|
127 | return 1;
|
---|
128 | }
|
---|
129 |
|
---|
130 | @Override
|
---|
131 | public String toString() {
|
---|
132 | return "LearnSettings" + participants + "," + deadline + "]";
|
---|
133 | }
|
---|
134 |
|
---|
135 | @Override
|
---|
136 | public int hashCode() {
|
---|
137 | final int prime = 31;
|
---|
138 | int result = 1;
|
---|
139 | result = prime * result
|
---|
140 | + ((deadline == null) ? 0 : deadline.hashCode());
|
---|
141 | result = prime * result
|
---|
142 | + ((participants == null) ? 0 : participants.hashCode());
|
---|
143 | return result;
|
---|
144 | }
|
---|
145 |
|
---|
146 | @Override
|
---|
147 | public boolean equals(Object obj) {
|
---|
148 | if (this == obj)
|
---|
149 | return true;
|
---|
150 | if (obj == null)
|
---|
151 | return false;
|
---|
152 | if (getClass() != obj.getClass())
|
---|
153 | return false;
|
---|
154 | LearnSettings other = (LearnSettings) obj;
|
---|
155 | if (deadline == null) {
|
---|
156 | if (other.deadline != null)
|
---|
157 | return false;
|
---|
158 | } else if (!deadline.equals(other.deadline))
|
---|
159 | return false;
|
---|
160 | if (participants == null) {
|
---|
161 | if (other.participants != null)
|
---|
162 | return false;
|
---|
163 | } else if (!participants.equals(other.participants))
|
---|
164 | return false;
|
---|
165 | return true;
|
---|
166 | }
|
---|
167 |
|
---|
168 | @Override
|
---|
169 | public SessionSettings with(TeamInfo team) {
|
---|
170 | if (team.getSize() != 1)
|
---|
171 | throw new IllegalArgumentException(
|
---|
172 | "Team must be size 1 but found " + team);
|
---|
173 | List<TeamInfo> newparts = new LinkedList<>(participants);
|
---|
174 | newparts.add(team);
|
---|
175 | return new LearnSettings(newparts, deadline);
|
---|
176 | }
|
---|
177 |
|
---|
178 | }
|
---|