source: protocol/src/main/java/geniusweb/protocol/session/mopac2/phase/OptInPhase.java@ 52

Last change on this file since 52 was 52, checked in by ruud, 14 months ago

Fixed small issues in domaineditor.

File size: 3.3 KB
Line 
1package geniusweb.protocol.session.mopac2.phase;
2
3import java.util.Arrays;
4import java.util.List;
5import java.util.Map;
6import java.util.stream.Collectors;
7
8import com.fasterxml.jackson.annotation.JsonCreator;
9import com.fasterxml.jackson.annotation.JsonProperty;
10
11import geniusweb.actions.Action;
12import geniusweb.actions.EndNegotiation;
13import geniusweb.actions.PartyId;
14import geniusweb.actions.Votes;
15import geniusweb.actions.VotesWithValue;
16import geniusweb.inform.Agreements;
17import geniusweb.inform.Inform;
18import geniusweb.inform.OptInWithValue;
19import geniusweb.protocol.ProtocolException;
20import geniusweb.protocol.session.mopac2.PartyStates;
21import geniusweb.voting.CollectedVotesWithValue;
22import geniusweb.voting.VotingEvaluatorWithValue;
23
24public class OptInPhase extends DefaultPhase {
25
26 /**
27 * The votes received in the {@link VotingPhase}
28 */
29 private final List<VotesWithValue> votes;
30
31 @JsonCreator
32 protected OptInPhase(@JsonProperty("votes") List<VotesWithValue> votes,
33 @JsonProperty("partyStates") PartyStates partyStates,
34 @JsonProperty("deadlinems") Long deadlinems,
35 @JsonProperty("evaluator") VotingEvaluatorWithValue evaluator) {
36 super(partyStates, deadlinems, evaluator);
37 this.votes = votes;
38 }
39
40 @Override
41 public Inform getInform() {
42 return new OptInWithValue(votes);
43 }
44
45 @Override
46 public Phase with(PartyId actor, Action action, long now) {
47 try {
48 checkAction(actor, action, now);
49 if (action instanceof VotesWithValue)
50 checkExtends((VotesWithValue) action);
51
52 return new OptInPhase(votes, partyStates.with(action), deadline,
53 evaluator);
54
55 } catch (ProtocolException e) {
56 return this.with(e);
57 }
58 }
59
60 /**
61 * Check that this action extends previous action.
62 *
63 * @param newvotes new {@link Votes} just received
64 * @throws ProtocolException if this action does not correctly extend
65 * previous vote.
66 */
67 private void checkExtends(VotesWithValue newvotes)
68 throws ProtocolException {
69 PartyId actor = newvotes.getActor();
70 // this actor is active so he must have voted in previous round.
71 VotesWithValue prevVotes = votes.stream()
72 .filter(v -> v.getActor().equals(actor)).findFirst().get();
73 if (!(newvotes.isExtending(prevVotes)))
74 throw new ProtocolException("New votes " + newvotes
75 + " does not extend previous vote " + prevVotes, actor);
76 }
77
78 @Override
79 public OptInPhase with(ProtocolException e) {
80 return new OptInPhase(votes, partyStates.with(e), deadline, evaluator);
81 }
82
83 @Override
84 public OptInPhase finish() {
85 PartyStates states = partyStates.finish();
86 Map<PartyId, VotesWithValue> votesmap = states
87 .getActions(VotesWithValue.class).stream()
88 .collect(Collectors.toMap(vts -> vts.getActor(), v -> v));
89 CollectedVotesWithValue allvotes = new CollectedVotesWithValue(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(VotesWithValue.class, EndNegotiation.class);
108 }
109
110}
Note: See TracBrowser for help on using the repository browser.