1 | package geniusweb.protocol.session.shaop;
|
---|
2 |
|
---|
3 | import java.util.Collections;
|
---|
4 | import java.util.HashMap;
|
---|
5 | import java.util.LinkedList;
|
---|
6 | import java.util.List;
|
---|
7 | import java.util.Map;
|
---|
8 | import java.util.stream.Collectors;
|
---|
9 |
|
---|
10 | import com.fasterxml.jackson.annotation.JsonCreator;
|
---|
11 |
|
---|
12 | import geniusweb.actions.Action;
|
---|
13 | import geniusweb.actions.PartyId;
|
---|
14 | import geniusweb.progress.Progress;
|
---|
15 | import geniusweb.protocol.ProtocolException;
|
---|
16 | import geniusweb.protocol.partyconnection.ProtocolToPartyConnections;
|
---|
17 | import geniusweb.protocol.session.SessionResult;
|
---|
18 | import geniusweb.protocol.session.SessionSettings;
|
---|
19 | import geniusweb.protocol.session.SessionState;
|
---|
20 | import geniusweb.references.PartyWithProfile;
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * The state without all the "with" functioality to make this more readable.
|
---|
24 | * immutable. json-serializable.
|
---|
25 | */
|
---|
26 | public abstract class BareSHAOPState implements SessionState {
|
---|
27 |
|
---|
28 | protected final List<Action> actions;
|
---|
29 |
|
---|
30 | protected final transient ProtocolToPartyConnections connections;
|
---|
31 |
|
---|
32 | protected final Progress progress;
|
---|
33 |
|
---|
34 | protected final SHAOPSettings settings;
|
---|
35 |
|
---|
36 | protected final ProtocolException error;
|
---|
37 |
|
---|
38 | protected final int teamNr;
|
---|
39 |
|
---|
40 | protected final Map<PartyId, Integer> partyNumbers;
|
---|
41 |
|
---|
42 | protected final Map<PartyId, Double> totalSpent;
|
---|
43 |
|
---|
44 | /**
|
---|
45 | *
|
---|
46 | * @param actions value for actions done so far. null equals to empty list
|
---|
47 | * @param conns the currently existing connections. Can be empty/null.
|
---|
48 | * Each connection represents another party. Normally the
|
---|
49 | * connections are in the order SHAOP1,COB1,SHAOP2,COB2,..
|
---|
50 | * so 2 parties for each team. The protocol should check
|
---|
51 | * this.
|
---|
52 | * @param progr the {@link Progress} that governs this session. Can be
|
---|
53 | * null if session did not yet start.
|
---|
54 | * @param settings the settings used for the session
|
---|
55 | * @param e the exception that occured, usually null. All errors
|
---|
56 | * occuring due to faulty {@link Action}s translate to
|
---|
57 | * {@link ProtocolException}s. All errors in our own code
|
---|
58 | * are bugs (not ProtocolExceptions) and should result in a
|
---|
59 | * throw.
|
---|
60 | * @param teamNr the teamnr (0,1,2..) that has the turn. 2* gives index
|
---|
61 | * {@link #connections} and settings
|
---|
62 | * @param partyNrs a map for each known PartyId to a number. The number is
|
---|
63 | * the index in both {@link SHAOPSettings#getTeams()} and in
|
---|
64 | * {@link #connections}. null is empyt map.
|
---|
65 | * @param spent total accumulated elicitation costs so far for each
|
---|
66 | * party. only SHAOP parties accumulate costs. null = empty
|
---|
67 | * map
|
---|
68 | */
|
---|
69 | @JsonCreator
|
---|
70 | public BareSHAOPState(List<Action> actions,
|
---|
71 | ProtocolToPartyConnections conns, Progress progr,
|
---|
72 | SHAOPSettings settings, ProtocolException e, int teamNr,
|
---|
73 | Map<PartyId, Integer> partyNrs, Map<PartyId, Double> spent) {
|
---|
74 | if (conns == null) {
|
---|
75 | this.connections = new ProtocolToPartyConnections(
|
---|
76 | Collections.emptyList());
|
---|
77 | } else {
|
---|
78 | this.connections = conns;
|
---|
79 | }
|
---|
80 | if (actions == null) {
|
---|
81 | this.actions = new LinkedList<>();
|
---|
82 | } else {
|
---|
83 | this.actions = actions;
|
---|
84 | }
|
---|
85 | if (partyNrs == null) {
|
---|
86 | this.partyNumbers = Collections.emptyMap();
|
---|
87 | } else {
|
---|
88 | this.partyNumbers = partyNrs;
|
---|
89 | }
|
---|
90 | if (spent == null) {
|
---|
91 | this.totalSpent = Collections.emptyMap();
|
---|
92 | } else {
|
---|
93 | this.totalSpent = spent;
|
---|
94 | }
|
---|
95 |
|
---|
96 | if (!connections.allunique()) {
|
---|
97 | throw new IllegalArgumentException(
|
---|
98 | "There can not be multiple connections for a party:"
|
---|
99 | + conns);
|
---|
100 | }
|
---|
101 |
|
---|
102 | this.progress = progr;
|
---|
103 | this.settings = settings;
|
---|
104 | this.error = e;
|
---|
105 | this.teamNr = teamNr;
|
---|
106 | }
|
---|
107 |
|
---|
108 | public ProtocolToPartyConnections getConnections() {
|
---|
109 | return connections;
|
---|
110 | }
|
---|
111 |
|
---|
112 | /**
|
---|
113 | *
|
---|
114 | * @return party ID of current team leader (SHAOP party)
|
---|
115 | */
|
---|
116 | public PartyId getCurrentTeam() {
|
---|
117 | return connections.get(2 * teamNr).getParty();
|
---|
118 | }
|
---|
119 |
|
---|
120 | /**
|
---|
121 | * @param id the {@link PartyId}
|
---|
122 | * @return the PartyWithProfile associated with that id.
|
---|
123 | */
|
---|
124 | public PartyWithProfile getPartyProfile(PartyId id) {
|
---|
125 | return settings.getAllParties().get(partyNumbers.get(id));
|
---|
126 | }
|
---|
127 |
|
---|
128 | /**
|
---|
129 | *
|
---|
130 | * @return the index of each party
|
---|
131 | */
|
---|
132 | public Map<PartyId, Integer> getPartyNumbers() {
|
---|
133 | return Collections.unmodifiableMap(partyNumbers);
|
---|
134 | }
|
---|
135 |
|
---|
136 | /**
|
---|
137 | *
|
---|
138 | * @return unmodifyable list of actions done so far.
|
---|
139 | */
|
---|
140 | @Override
|
---|
141 | public List<Action> getActions() {
|
---|
142 | return Collections.unmodifiableList(actions);
|
---|
143 | }
|
---|
144 |
|
---|
145 | @Override
|
---|
146 | public Progress getProgress() {
|
---|
147 | return progress;
|
---|
148 | }
|
---|
149 |
|
---|
150 | @Override
|
---|
151 | public SessionSettings getSettings() {
|
---|
152 | return settings;
|
---|
153 | }
|
---|
154 |
|
---|
155 | @Override
|
---|
156 | public boolean isFinal(long currentTimeMs) {
|
---|
157 | return error != null
|
---|
158 | || (progress != null && progress.isPastDeadline(currentTimeMs));
|
---|
159 | }
|
---|
160 |
|
---|
161 | public ProtocolException getError() {
|
---|
162 | return error;
|
---|
163 | }
|
---|
164 |
|
---|
165 | @Override
|
---|
166 | public String toString() {
|
---|
167 | return this.getClass().getSimpleName() + "[" + actions + ","
|
---|
168 | + connections + "," + progress + "," + settings + "," + error
|
---|
169 | + "]";
|
---|
170 | }
|
---|
171 |
|
---|
172 | @Override
|
---|
173 | public SessionResult getResult() {
|
---|
174 | Map<PartyId, Double> penalties = new HashMap<>();
|
---|
175 | for (PartyId party : partyNumbers.keySet()) {
|
---|
176 | Double spent = totalSpent.get(party);
|
---|
177 | if (spent == null) {
|
---|
178 | spent = 0d;
|
---|
179 | } else {
|
---|
180 | spent = Math.max(0, Math.min(1.0, spent));
|
---|
181 | }
|
---|
182 | penalties.put(party, spent);
|
---|
183 | }
|
---|
184 | Map<PartyId, PartyWithProfile> allparties = partyNumbers.keySet()
|
---|
185 | .stream().collect(Collectors.toMap(pid -> pid, pid -> settings
|
---|
186 | .getAllParties().get(partyNumbers.get(pid))));
|
---|
187 | return new SessionResult(allparties, getAgreements(), penalties,
|
---|
188 | getError());
|
---|
189 |
|
---|
190 | }
|
---|
191 |
|
---|
192 | @Override
|
---|
193 | public int hashCode() {
|
---|
194 | final int prime = 31;
|
---|
195 | int result = 1;
|
---|
196 | result = prime * result + ((actions == null) ? 0 : actions.hashCode());
|
---|
197 | result = prime * result + ((error == null) ? 0 : error.hashCode());
|
---|
198 | result = prime * result
|
---|
199 | + ((partyNumbers == null) ? 0 : partyNumbers.hashCode());
|
---|
200 | result = prime * result
|
---|
201 | + ((progress == null) ? 0 : progress.hashCode());
|
---|
202 | result = prime * result
|
---|
203 | + ((settings == null) ? 0 : settings.hashCode());
|
---|
204 | result = prime * result + teamNr;
|
---|
205 | result = prime * result
|
---|
206 | + ((totalSpent == null) ? 0 : totalSpent.hashCode());
|
---|
207 | return result;
|
---|
208 | }
|
---|
209 |
|
---|
210 | @Override
|
---|
211 | public boolean equals(Object obj) {
|
---|
212 | if (this == obj)
|
---|
213 | return true;
|
---|
214 | if (obj == null)
|
---|
215 | return false;
|
---|
216 | if (getClass() != obj.getClass())
|
---|
217 | return false;
|
---|
218 | BareSHAOPState other = (BareSHAOPState) obj;
|
---|
219 | if (actions == null) {
|
---|
220 | if (other.actions != null)
|
---|
221 | return false;
|
---|
222 | } else if (!actions.equals(other.actions))
|
---|
223 | return false;
|
---|
224 | if (error == null) {
|
---|
225 | if (other.error != null)
|
---|
226 | return false;
|
---|
227 | } else if (!error.equals(other.error))
|
---|
228 | return false;
|
---|
229 | if (partyNumbers == null) {
|
---|
230 | if (other.partyNumbers != null)
|
---|
231 | return false;
|
---|
232 | } else if (!partyNumbers.equals(other.partyNumbers))
|
---|
233 | return false;
|
---|
234 | if (progress == null) {
|
---|
235 | if (other.progress != null)
|
---|
236 | return false;
|
---|
237 | } else if (!progress.equals(other.progress))
|
---|
238 | return false;
|
---|
239 | if (settings == null) {
|
---|
240 | if (other.settings != null)
|
---|
241 | return false;
|
---|
242 | } else if (!settings.equals(other.settings))
|
---|
243 | return false;
|
---|
244 | if (teamNr != other.teamNr)
|
---|
245 | return false;
|
---|
246 | if (totalSpent == null) {
|
---|
247 | if (other.totalSpent != null)
|
---|
248 | return false;
|
---|
249 | } else if (!totalSpent.equals(other.totalSpent))
|
---|
250 | return false;
|
---|
251 | return true;
|
---|
252 | }
|
---|
253 |
|
---|
254 | }
|
---|