source: src/main/java/genius/core/protocol/SimpleMediatorBasedProtocol.java

Last change on this file was 127, checked in by Wouter Pasman, 6 years ago

#41 ROLL BACK of rev.126 . So this version is equal to rev. 125

File size: 3.7 KB
Line 
1package genius.core.protocol;
2
3import java.util.ArrayList;
4import java.util.Arrays;
5import java.util.Collections;
6import java.util.HashMap;
7import java.util.List;
8import java.util.Map;
9
10import genius.core.Bid;
11import genius.core.Vote;
12import genius.core.actions.Action;
13import genius.core.actions.InformVotingResult;
14import genius.core.actions.OfferForVoting;
15import genius.core.actions.VoteForOfferAcceptance;
16import genius.core.parties.NegotiationParty;
17import genius.core.session.Round;
18import genius.core.session.Session;
19import 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 */
39public 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}
Note: See TracBrowser for help on using the repository browser.