1 | package genius.core.protocol;
|
---|
2 |
|
---|
3 | import java.util.ArrayList;
|
---|
4 | import java.util.Arrays;
|
---|
5 | import java.util.Collections;
|
---|
6 | import java.util.HashMap;
|
---|
7 | import java.util.List;
|
---|
8 | import java.util.Map;
|
---|
9 |
|
---|
10 | import genius.core.Bid;
|
---|
11 | import genius.core.Vote;
|
---|
12 | import genius.core.actions.Action;
|
---|
13 | import genius.core.actions.InformVotingResult;
|
---|
14 | import genius.core.actions.OfferForVoting;
|
---|
15 | import genius.core.actions.VoteForOfferAcceptance;
|
---|
16 | import genius.core.parties.NegotiationParty;
|
---|
17 | import genius.core.session.Round;
|
---|
18 | import genius.core.session.Session;
|
---|
19 | import genius.core.session.Turn;
|
---|
20 |
|
---|
21 | /**
|
---|
22 | * Basic implementation of a mediator based protocol.
|
---|
23 | * <p>
|
---|
24 | * Protocol:
|
---|
25 | *
|
---|
26 | * <ol>
|
---|
27 | * <li>Mediator proposes an {@link OfferForVoting}
|
---|
28 | * <li>Agents {@link VoteForOfferAcceptance} to accept/reject
|
---|
29 | * <li>Mediator sends parties a {@link InformVotingResult}
|
---|
30 | * </ol>
|
---|
31 | *
|
---|
32 | * This protocol takes the last {@link InformVotingResult} that contains a
|
---|
33 | * {@link Vote#ACCEPT} as the current agreement. If no such vote exists, it is
|
---|
34 | * assumed no agreement has been reached yet.
|
---|
35 | *
|
---|
36 | * @author David Festen
|
---|
37 | * @author Reyhan
|
---|
38 | */
|
---|
39 | public class SimpleMediatorBasedProtocol extends MediatorProtocol {
|
---|
40 |
|
---|
41 | @Override
|
---|
42 | public Round getRoundStructure(List<NegotiationParty> parties, Session session) {
|
---|
43 |
|
---|
44 | // initialize and split parties
|
---|
45 | Round round = new Round();
|
---|
46 | NegotiationParty mediator = getMediator(parties);
|
---|
47 | List<NegotiationParty> otherParties = getNonMediators(parties);
|
---|
48 |
|
---|
49 | // mediator opening turn
|
---|
50 | round.addTurn(new Turn(mediator, OfferForVoting.class));
|
---|
51 |
|
---|
52 | // other parties' turn
|
---|
53 | for (NegotiationParty otherParty : otherParties) {
|
---|
54 | round.addTurn(new Turn(otherParty, VoteForOfferAcceptance.class));
|
---|
55 | }
|
---|
56 |
|
---|
57 | // mediator finishing turn
|
---|
58 | round.addTurn(new Turn(mediator, InformVotingResult.class));
|
---|
59 |
|
---|
60 | // return new round structure
|
---|
61 | return round;
|
---|
62 | }
|
---|
63 |
|
---|
64 | @Override
|
---|
65 | public Map<NegotiationParty, List<NegotiationParty>> getActionListeners(List<NegotiationParty> parties) {
|
---|
66 |
|
---|
67 | Map<NegotiationParty, List<NegotiationParty>> map = new HashMap<NegotiationParty, List<NegotiationParty>>();
|
---|
68 |
|
---|
69 | NegotiationParty mediator = getMediator(parties);
|
---|
70 |
|
---|
71 | // all other negotiating parties listen to the mediator
|
---|
72 | for (NegotiationParty party : getNonMediators(parties)) {
|
---|
73 | map.put(party, Arrays.asList(mediator));
|
---|
74 | }
|
---|
75 |
|
---|
76 | // the mediator listens to all other negotiating parties.
|
---|
77 | map.put(mediator, getNonMediators(parties));
|
---|
78 |
|
---|
79 | return map;
|
---|
80 | }
|
---|
81 |
|
---|
82 | /**
|
---|
83 | * Returns the last offer for voting as the current agreement.
|
---|
84 | *
|
---|
85 | * @param session
|
---|
86 | * The complete session history up to this point
|
---|
87 | * @return The current agreement (the bid from the last action from the
|
---|
88 | * mediator that was {@link Vote#ACCEPT}), or null if no agreement
|
---|
89 | * yet.
|
---|
90 | */
|
---|
91 | @Override
|
---|
92 | public Bid getCurrentAgreement(Session session, List<NegotiationParty> parties) {
|
---|
93 |
|
---|
94 | // search from last to first bid for an accepted bid.
|
---|
95 | List<Round> rounds = new ArrayList<Round>(session.getRounds());
|
---|
96 | Collections.reverse(rounds);
|
---|
97 | for (Round round : rounds) {
|
---|
98 | InformVotingResult voteres = getInformVotingResult(round);
|
---|
99 | if (voteres != null && voteres.getVotingResult() == Vote.ACCEPT) {
|
---|
100 | return voteres.getBid();
|
---|
101 | }
|
---|
102 | }
|
---|
103 |
|
---|
104 | return null;
|
---|
105 | }
|
---|
106 |
|
---|
107 | /********************* support functions ************************/
|
---|
108 | /**
|
---|
109 | * Find the first {@link InformVotingResult} action in this round.
|
---|
110 | *
|
---|
111 | * @param round
|
---|
112 | * the round to check
|
---|
113 | * @return first {@link InformVotingResult} of round, or null if no such
|
---|
114 | * action.
|
---|
115 | */
|
---|
116 | private InformVotingResult getInformVotingResult(Round round) {
|
---|
117 | for (Action action : round.getActions()) {
|
---|
118 | if (action instanceof InformVotingResult) {
|
---|
119 | return (InformVotingResult) action;
|
---|
120 | }
|
---|
121 | }
|
---|
122 | return null;
|
---|
123 | }
|
---|
124 |
|
---|
125 | }
|
---|