1 | package genius.core.protocol;
|
---|
2 |
|
---|
3 | import java.util.ArrayList;
|
---|
4 | import java.util.Collection;
|
---|
5 | import java.util.HashMap;
|
---|
6 | import java.util.List;
|
---|
7 | import java.util.Map;
|
---|
8 | import java.util.concurrent.ExecutionException;
|
---|
9 |
|
---|
10 | import genius.core.Bid;
|
---|
11 | import genius.core.actions.Accept;
|
---|
12 | import genius.core.actions.Action;
|
---|
13 | import genius.core.actions.Offer;
|
---|
14 | import genius.core.exceptions.NegotiationPartyTimeoutException;
|
---|
15 | import genius.core.parties.NegotiationParty;
|
---|
16 | import genius.core.session.ActionException;
|
---|
17 | import genius.core.session.InvalidActionContentsError;
|
---|
18 | import genius.core.session.Round;
|
---|
19 | import genius.core.session.Session;
|
---|
20 |
|
---|
21 | /**
|
---|
22 | * An adapter for the protocol class. This implements default functionality for
|
---|
23 | * the methods in the Protocol interface and return default values for them.
|
---|
24 | *
|
---|
25 | * @author David Festen
|
---|
26 | */
|
---|
27 | public class DefaultMultilateralProtocol implements MultilateralProtocol {
|
---|
28 |
|
---|
29 | protected boolean isAborted = false;
|
---|
30 | private Bid lastOffer = null;
|
---|
31 |
|
---|
32 | @Override
|
---|
33 | public Round getRoundStructure(List<NegotiationParty> parties, Session session) {
|
---|
34 | return new Round();
|
---|
35 | }
|
---|
36 |
|
---|
37 | @Override
|
---|
38 | public Map<NegotiationParty, List<Action>> beforeSession(Session session, List<NegotiationParty> parties)
|
---|
39 | throws NegotiationPartyTimeoutException, ExecutionException, InterruptedException {
|
---|
40 | return new HashMap<NegotiationParty, List<Action>>();
|
---|
41 |
|
---|
42 | }
|
---|
43 |
|
---|
44 | @Override
|
---|
45 | public void afterSession(Session session, List<NegotiationParty> parties) {
|
---|
46 |
|
---|
47 | }
|
---|
48 |
|
---|
49 | @Override
|
---|
50 | public void applyAction(Action action, Session session) throws ActionException {
|
---|
51 | if (action instanceof Offer) {
|
---|
52 | checkOffer((Offer) action);
|
---|
53 | }
|
---|
54 | if (action instanceof Accept) {
|
---|
55 | checkAccept((Accept) action);
|
---|
56 | }
|
---|
57 | }
|
---|
58 |
|
---|
59 | private void checkAccept(Accept action) throws ActionException {
|
---|
60 | if (lastOffer == null) {
|
---|
61 | throw new InvalidActionContentsError(action.getAgent(),
|
---|
62 | "In DefaultMultilateralProtocol, an accept can only be done after an offer has been placed");
|
---|
63 | }
|
---|
64 | if (!lastOffer.equals(action.getBid())) {
|
---|
65 | throw new InvalidActionContentsError(action.getAgent(),
|
---|
66 | "In DefaultMultilateralProtocol, only the last placed offer can be accepted.");
|
---|
67 | }
|
---|
68 | }
|
---|
69 |
|
---|
70 | /**
|
---|
71 | * Check incoming offer
|
---|
72 | *
|
---|
73 | * @param action
|
---|
74 | * the offer
|
---|
75 | */
|
---|
76 | protected void checkOffer(Offer offer) throws ActionException {
|
---|
77 | lastOffer = offer.getBid();
|
---|
78 | }
|
---|
79 |
|
---|
80 | @Override
|
---|
81 | public boolean isFinished(Session session, List<NegotiationParty> parties) {
|
---|
82 | return isAborted;
|
---|
83 | }
|
---|
84 |
|
---|
85 | /**
|
---|
86 | * Get a map of parties that are listening to each other's response
|
---|
87 | *
|
---|
88 | * @param parties
|
---|
89 | * The parties involved in the current negotiation
|
---|
90 | * @return A map where the key is a
|
---|
91 | * {@link genius.core.parties.NegotiationParty} that is responding to
|
---|
92 | * a {@link NegotiationParty#chooseAction(java.util.List)} event,
|
---|
93 | * and the value is a list of {@link NegotiationParty} that are
|
---|
94 | * listening to that key party's response.
|
---|
95 | */
|
---|
96 | @Override
|
---|
97 | public Map<NegotiationParty, List<NegotiationParty>> getActionListeners(final List<NegotiationParty> parties) {
|
---|
98 | return new HashMap<NegotiationParty, List<NegotiationParty>>(0);
|
---|
99 | }
|
---|
100 |
|
---|
101 | /**
|
---|
102 | * This method should return the current agreement.
|
---|
103 | * <p/>
|
---|
104 | * Some protocols only have an agreement at the negotiation session, make
|
---|
105 | * sure that this method returns null until the end of the session in that
|
---|
106 | * case, because this method might be queried at intermediary steps.
|
---|
107 | *
|
---|
108 | * @param session
|
---|
109 | * The complete session history up to this point
|
---|
110 | * @return The agreed upon bid or null if no agreement
|
---|
111 | */
|
---|
112 | @Override
|
---|
113 | public Bid getCurrentAgreement(Session session, List<NegotiationParty> parties) {
|
---|
114 | return null;
|
---|
115 | }
|
---|
116 |
|
---|
117 | /**
|
---|
118 | * Gets the number of parties that currently agree to the offer.
|
---|
119 | * <p/>
|
---|
120 | * Default implementation returns 0 if no agreement or number of parties if
|
---|
121 | * agreement exists.
|
---|
122 | *
|
---|
123 | * @param session
|
---|
124 | * the current state of this session
|
---|
125 | * @param parties
|
---|
126 | * The parties currently participating
|
---|
127 | * @return the number of parties agreeing to the current agreement
|
---|
128 | */
|
---|
129 | @Override
|
---|
130 | public int getNumberOfAgreeingParties(Session session, List<NegotiationParty> parties) {
|
---|
131 | return getCurrentAgreement(session, parties) == null ? 0 : parties.size();
|
---|
132 | }
|
---|
133 |
|
---|
134 | /**
|
---|
135 | * Filters the list by including only the type of negotiation parties.
|
---|
136 | * Optionally, this behavior can be reversed (i.e. excluding only the given
|
---|
137 | * type of negotiation parties).
|
---|
138 | *
|
---|
139 | * @param negotiationParties
|
---|
140 | * The original list of parties
|
---|
141 | * @param negotiationPartyClass
|
---|
142 | * The type of parties to include (or exclude if inclusionFilter
|
---|
143 | * is set to false)
|
---|
144 | * @param inclusionFilter
|
---|
145 | * If set to true, we include the given type. Otherwise, exclude
|
---|
146 | * the given type
|
---|
147 | * @return The filtered list of parties
|
---|
148 | */
|
---|
149 | private Collection<NegotiationParty> filter(Collection<NegotiationParty> negotiationParties,
|
---|
150 | Class negotiationPartyClass, boolean inclusionFilter) {
|
---|
151 | Collection<NegotiationParty> filtered = new ArrayList<NegotiationParty>(negotiationParties.size());
|
---|
152 |
|
---|
153 | for (NegotiationParty party : negotiationParties) {
|
---|
154 | // if including and class is of the type searching for,
|
---|
155 | // or excluding and class is not of the type searching for.
|
---|
156 | if ((inclusionFilter && party.getClass().equals(negotiationPartyClass))
|
---|
157 | || (!inclusionFilter && !party.getClass().equals(negotiationPartyClass))) {
|
---|
158 | filtered.add(party);
|
---|
159 | }
|
---|
160 | }
|
---|
161 |
|
---|
162 | return filtered;
|
---|
163 | }
|
---|
164 |
|
---|
165 | /**
|
---|
166 | * Filters the list by including only the type of negotiation parties.
|
---|
167 | * Optionally, this behavior can be reversed (i.e. excluding only the given
|
---|
168 | * type of negotiation parties).
|
---|
169 | *
|
---|
170 | * @param negotiationParties
|
---|
171 | * The original list of parties
|
---|
172 | * @param negotiationPartyClass
|
---|
173 | * The type of parties to include
|
---|
174 | *
|
---|
175 | * @return The filtered list of parties
|
---|
176 | */
|
---|
177 | public Collection<NegotiationParty> includeOnly(Collection<NegotiationParty> negotiationParties,
|
---|
178 | Class negotiationPartyClass) {
|
---|
179 | return filter(negotiationParties, negotiationPartyClass, true);
|
---|
180 | }
|
---|
181 |
|
---|
182 | /**
|
---|
183 | * Filters the list by including only the type of negotiation parties.
|
---|
184 | * Optionally, this behavior can be reversed (i.e. excluding only the given
|
---|
185 | * type of negotiation parties).
|
---|
186 | *
|
---|
187 | * @param negotiationParties
|
---|
188 | * The original list of parties
|
---|
189 | * @param negotiationPartyClass
|
---|
190 | * The type of parties to include
|
---|
191 | *
|
---|
192 | * @return The filtered list of parties
|
---|
193 | */
|
---|
194 | public Collection<NegotiationParty> exclude(Collection<NegotiationParty> negotiationParties,
|
---|
195 | Class negotiationPartyClass) {
|
---|
196 | return filter(negotiationParties, negotiationPartyClass, false);
|
---|
197 | }
|
---|
198 |
|
---|
199 | /**
|
---|
200 | * Overwrites the rest of the protocol and sets the protocol state to finish
|
---|
201 | */
|
---|
202 | @Override
|
---|
203 | public void endNegotiation() {
|
---|
204 | System.out.println("Negotiation aborted");
|
---|
205 | isAborted = true;
|
---|
206 | }
|
---|
207 |
|
---|
208 | /**
|
---|
209 | * Overwrites the rest of the protocol and sets the protocol state to finish
|
---|
210 | *
|
---|
211 | * @param reason
|
---|
212 | * Optionally give a reason why the protocol is finished.
|
---|
213 | */
|
---|
214 | @Override
|
---|
215 | public void endNegotiation(String reason) {
|
---|
216 | System.out.println("Negotiation aborted: " + reason);
|
---|
217 | isAborted = true;
|
---|
218 | }
|
---|
219 |
|
---|
220 | /**
|
---|
221 | * @param parties
|
---|
222 | * all the parties in the negotiation
|
---|
223 | * @return map with as keys all {@link NegotiationParty} and as values for
|
---|
224 | * each key all other {@link NegotiationParty}s.
|
---|
225 | */
|
---|
226 | protected static Map<NegotiationParty, List<NegotiationParty>> listenToAll(List<NegotiationParty> parties) {
|
---|
227 | // create a new map of parties
|
---|
228 | Map<NegotiationParty, List<NegotiationParty>> map = new HashMap<NegotiationParty, List<NegotiationParty>>();
|
---|
229 |
|
---|
230 | // for each party add each other party
|
---|
231 | for (NegotiationParty listener : parties) {
|
---|
232 | ArrayList<NegotiationParty> talkers = new ArrayList<NegotiationParty>();
|
---|
233 | for (NegotiationParty talker : parties) {
|
---|
234 | if (talker != listener) {
|
---|
235 | talkers.add(talker);
|
---|
236 | }
|
---|
237 | }
|
---|
238 | map.put(listener, talkers);
|
---|
239 | }
|
---|
240 |
|
---|
241 | return map;
|
---|
242 | }
|
---|
243 |
|
---|
244 | /**
|
---|
245 | * @param parties
|
---|
246 | * all the parties in the negotiation
|
---|
247 | * @return map with as keys all {@link NegotiationParty} and as values an
|
---|
248 | * empty list.
|
---|
249 | */
|
---|
250 | protected static Map<NegotiationParty, List<NegotiationParty>> listenToNone(List<NegotiationParty> parties) {
|
---|
251 | Map<NegotiationParty, List<NegotiationParty>> listenersMap = new HashMap<NegotiationParty, List<NegotiationParty>>();
|
---|
252 | for (NegotiationParty party : parties) {
|
---|
253 | listenersMap.put(party, parties);
|
---|
254 | }
|
---|
255 | return listenersMap;
|
---|
256 | }
|
---|
257 |
|
---|
258 | }
|
---|