1 | package geniusweb.protocol.session.mopac.phase;
|
---|
2 |
|
---|
3 | import java.util.Arrays;
|
---|
4 | import java.util.List;
|
---|
5 | import java.util.Map;
|
---|
6 | import java.util.stream.Collectors;
|
---|
7 |
|
---|
8 | import com.fasterxml.jackson.annotation.JsonCreator;
|
---|
9 | import com.fasterxml.jackson.annotation.JsonProperty;
|
---|
10 |
|
---|
11 | import geniusweb.actions.Action;
|
---|
12 | import geniusweb.actions.EndNegotiation;
|
---|
13 | import geniusweb.actions.PartyId;
|
---|
14 | import geniusweb.actions.Votes;
|
---|
15 | import geniusweb.inform.Agreements;
|
---|
16 | import geniusweb.inform.Inform;
|
---|
17 | import geniusweb.inform.OptIn;
|
---|
18 | import geniusweb.protocol.ProtocolException;
|
---|
19 | import geniusweb.protocol.session.mopac.PartyStates;
|
---|
20 | import geniusweb.voting.CollectedVotes;
|
---|
21 | import geniusweb.voting.VotingEvaluator;
|
---|
22 |
|
---|
23 | public class OptInPhase extends DefaultPhase {
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * The votes received in the {@link VotingPhase}
|
---|
27 | */
|
---|
28 | private final List<Votes> votes;
|
---|
29 |
|
---|
30 | @JsonCreator
|
---|
31 | protected OptInPhase(@JsonProperty("votes") List<Votes> votes,
|
---|
32 | @JsonProperty("partyStates") PartyStates partyStates,
|
---|
33 | @JsonProperty("deadlinems") Long deadlinems,
|
---|
34 | @JsonProperty("evaluator") VotingEvaluator evaluator) {
|
---|
35 | super(partyStates, deadlinems, evaluator);
|
---|
36 | this.votes = votes;
|
---|
37 | }
|
---|
38 |
|
---|
39 | @Override
|
---|
40 | public Inform getInform() {
|
---|
41 | return new OptIn(votes);
|
---|
42 | }
|
---|
43 |
|
---|
44 | @Override
|
---|
45 | public Phase with(PartyId actor, Action action, long now) {
|
---|
46 | try {
|
---|
47 | checkAction(actor, action, now);
|
---|
48 | if (action instanceof Votes)
|
---|
49 | checkExtends((Votes) action);
|
---|
50 |
|
---|
51 | return new OptInPhase(votes, partyStates.with(action), deadline,
|
---|
52 | evaluator);
|
---|
53 |
|
---|
54 | } catch (ProtocolException e) {
|
---|
55 | return this.with(e);
|
---|
56 | }
|
---|
57 | }
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * Check that this action extends previous action.
|
---|
61 | *
|
---|
62 | * @param newvotes new {@link Votes} just received
|
---|
63 | * @throws ProtocolException if this action does not correctly extend
|
---|
64 | * previous vote.
|
---|
65 | */
|
---|
66 | private void checkExtends(Votes newvotes) throws ProtocolException {
|
---|
67 | PartyId actor = newvotes.getActor();
|
---|
68 | // this actor is active so he must have voted in previous round.
|
---|
69 | Votes prevVotes = votes.stream().filter(v -> v.getActor().equals(actor))
|
---|
70 | .findFirst().get();
|
---|
71 | if (!(newvotes.isExtending(prevVotes)))
|
---|
72 | throw new ProtocolException("New votes " + newvotes
|
---|
73 | + " does not extend previous vote " + prevVotes, actor);
|
---|
74 | }
|
---|
75 |
|
---|
76 | @Override
|
---|
77 | public OptInPhase with(ProtocolException e) {
|
---|
78 | return new OptInPhase(votes, partyStates.with(e), deadline, evaluator);
|
---|
79 | }
|
---|
80 |
|
---|
81 | @Override
|
---|
82 | public OptInPhase finish() {
|
---|
83 | PartyStates states = partyStates.finish();
|
---|
84 | Map<PartyId, Votes> votesmap = states.getActions(Votes.class).stream()
|
---|
85 | .collect(Collectors.toMap(votes -> votes.getActor(), v -> v));
|
---|
86 | CollectedVotes allvotes = new CollectedVotes(votesmap,
|
---|
87 | states.getPowers());
|
---|
88 | Agreements newAgree = evaluator.create(allvotes).getAgreements();
|
---|
89 | if (!newAgree.getMap().isEmpty()) {
|
---|
90 | System.out.println("detected new agreements");
|
---|
91 | }
|
---|
92 | PartyStates finalStates = states.with(newAgree);
|
---|
93 |
|
---|
94 | return new OptInPhase(votes, finalStates, deadline, evaluator);
|
---|
95 | }
|
---|
96 |
|
---|
97 | @Override
|
---|
98 | public OfferPhase checkedNext(long newdeadline) {
|
---|
99 | return new OfferPhase(partyStates.flush(), newdeadline, evaluator);
|
---|
100 | }
|
---|
101 |
|
---|
102 | @Override
|
---|
103 | public List<Class<? extends Action>> getAllowedActions() {
|
---|
104 | return Arrays.asList(Votes.class, EndNegotiation.class);
|
---|
105 | }
|
---|
106 |
|
---|
107 | }
|
---|