[52] | 1 | package geniusweb.protocol.session.mopac2.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.actions.VotesWithValue;
|
---|
| 16 | import geniusweb.inform.Agreements;
|
---|
| 17 | import geniusweb.inform.Inform;
|
---|
| 18 | import geniusweb.inform.OptInWithValue;
|
---|
| 19 | import geniusweb.protocol.ProtocolException;
|
---|
| 20 | import geniusweb.protocol.session.mopac2.PartyStates;
|
---|
| 21 | import geniusweb.voting.CollectedVotesWithValue;
|
---|
| 22 | import geniusweb.voting.VotingEvaluatorWithValue;
|
---|
| 23 |
|
---|
| 24 | public 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 | }
|
---|