package geniusweb.protocol.session.mopac2.phase; import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.stream.Collectors; import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; import geniusweb.actions.Action; import geniusweb.actions.EndNegotiation; import geniusweb.actions.PartyId; import geniusweb.actions.Votes; import geniusweb.actions.VotesWithValue; import geniusweb.inform.Agreements; import geniusweb.inform.Inform; import geniusweb.inform.OptInWithValue; import geniusweb.protocol.ProtocolException; import geniusweb.protocol.session.mopac2.PartyStates; import geniusweb.voting.CollectedVotesWithValue; import geniusweb.voting.VotingEvaluatorWithValue; public class OptInPhase extends DefaultPhase { /** * The votes received in the {@link VotingPhase} */ private final List votes; @JsonCreator protected OptInPhase(@JsonProperty("votes") List votes, @JsonProperty("partyStates") PartyStates partyStates, @JsonProperty("deadlinems") Long deadlinems, @JsonProperty("evaluator") VotingEvaluatorWithValue evaluator) { super(partyStates, deadlinems, evaluator); this.votes = votes; } @Override public Inform getInform() { return new OptInWithValue(votes); } @Override public Phase with(PartyId actor, Action action, long now) { try { checkAction(actor, action, now); if (action instanceof VotesWithValue) checkExtends((VotesWithValue) action); return new OptInPhase(votes, partyStates.with(action), deadline, evaluator); } catch (ProtocolException e) { return this.with(e); } } /** * Check that this action extends previous action. * * @param newvotes new {@link Votes} just received * @throws ProtocolException if this action does not correctly extend * previous vote. */ private void checkExtends(VotesWithValue newvotes) throws ProtocolException { PartyId actor = newvotes.getActor(); // this actor is active so he must have voted in previous round. VotesWithValue prevVotes = votes.stream() .filter(v -> v.getActor().equals(actor)).findFirst().get(); if (!(newvotes.isExtending(prevVotes))) throw new ProtocolException("New votes " + newvotes + " does not extend previous vote " + prevVotes, actor); } @Override public OptInPhase with(ProtocolException e) { return new OptInPhase(votes, partyStates.with(e), deadline, evaluator); } @Override public OptInPhase finish() { PartyStates states = partyStates.finish(); Map votesmap = states .getActions(VotesWithValue.class).stream() .collect(Collectors.toMap(vts -> vts.getActor(), v -> v)); CollectedVotesWithValue allvotes = new CollectedVotesWithValue(votesmap, states.getPowers()); Agreements newAgree = evaluator.create(allvotes).getAgreements(); if (!newAgree.getMap().isEmpty()) { System.out.println("detected new agreements"); } PartyStates finalStates = states.with(newAgree); return new OptInPhase(votes, finalStates, deadline, evaluator); } @Override public OfferPhase checkedNext(long newdeadline) { return new OfferPhase(partyStates.flush(), newdeadline, evaluator); } @Override public List> getAllowedActions() { return Arrays.asList(VotesWithValue.class, EndNegotiation.class); } }