1 | package geniusweb.protocol.session.mopac2.phase;
|
---|
2 |
|
---|
3 | import java.util.Arrays;
|
---|
4 | import java.util.List;
|
---|
5 | import java.util.stream.Collectors;
|
---|
6 |
|
---|
7 | import com.fasterxml.jackson.annotation.JsonCreator;
|
---|
8 | import com.fasterxml.jackson.annotation.JsonProperty;
|
---|
9 |
|
---|
10 | import geniusweb.actions.Action;
|
---|
11 | import geniusweb.actions.EndNegotiation;
|
---|
12 | import geniusweb.actions.Offer;
|
---|
13 | import geniusweb.actions.PartyId;
|
---|
14 | import geniusweb.actions.VotesWithValue;
|
---|
15 | import geniusweb.inform.Inform;
|
---|
16 | import geniusweb.inform.Voting;
|
---|
17 | import geniusweb.protocol.ProtocolException;
|
---|
18 | import geniusweb.protocol.session.mopac2.PartyStates;
|
---|
19 | import geniusweb.voting.VotingEvaluatorWithValue;
|
---|
20 |
|
---|
21 | public class VotingPhase extends DefaultPhase {
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * the offers received in the {@link OfferPhase}
|
---|
25 | */
|
---|
26 | private final List<Offer> offers;
|
---|
27 |
|
---|
28 | @JsonCreator
|
---|
29 | protected VotingPhase(@JsonProperty("offers") List<Offer> offers,
|
---|
30 | @JsonProperty("partyStates") PartyStates partyStates,
|
---|
31 | @JsonProperty("deadlinems") Long deadlinems,
|
---|
32 | @JsonProperty("evaluator") VotingEvaluatorWithValue evaluator) {
|
---|
33 | super(partyStates, deadlinems, evaluator);
|
---|
34 | this.offers = offers;
|
---|
35 | }
|
---|
36 |
|
---|
37 | @Override
|
---|
38 | public VotingPhase with(PartyId actor, Action action, long now) {
|
---|
39 | try {
|
---|
40 | checkAction(actor, action, now);
|
---|
41 | } catch (ProtocolException ex) {
|
---|
42 | return this.with(ex);
|
---|
43 | }
|
---|
44 | return new VotingPhase(offers, partyStates.with(action), deadline,
|
---|
45 | evaluator);
|
---|
46 | }
|
---|
47 |
|
---|
48 | @Override
|
---|
49 | public Inform getInform() {
|
---|
50 | return new Voting(offers, partyStates.getPowers());
|
---|
51 | }
|
---|
52 |
|
---|
53 | @Override
|
---|
54 | public VotingPhase with(ProtocolException e) {
|
---|
55 | return new VotingPhase(offers, partyStates.with(e), deadline,
|
---|
56 | evaluator);
|
---|
57 | }
|
---|
58 |
|
---|
59 | @Override
|
---|
60 | public VotingPhase finish() {
|
---|
61 | return new VotingPhase(offers, partyStates.finish(), deadline,
|
---|
62 | evaluator);
|
---|
63 | }
|
---|
64 |
|
---|
65 | @Override
|
---|
66 | protected Phase checkedNext(long deadln) {
|
---|
67 | return new OptInPhase(getVotes(), partyStates.flush(), deadln,
|
---|
68 | evaluator);
|
---|
69 | }
|
---|
70 |
|
---|
71 | @Override
|
---|
72 | public List<Class<? extends Action>> getAllowedActions() {
|
---|
73 | return Arrays.asList(VotesWithValue.class, EndNegotiation.class);
|
---|
74 | }
|
---|
75 |
|
---|
76 | /**
|
---|
77 | * @return all votes done in this phase.
|
---|
78 | */
|
---|
79 | public List<VotesWithValue> getVotes() {
|
---|
80 | return partyStates.getActions().stream()
|
---|
81 | .filter(act -> act instanceof VotesWithValue)
|
---|
82 | .map(act -> (VotesWithValue) act).collect(Collectors.toList());
|
---|
83 | }
|
---|
84 |
|
---|
85 | }
|
---|