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