1 | from typing import List, Any
|
---|
2 |
|
---|
3 | from geniusweb.actions.Action import Action
|
---|
4 | from geniusweb.actions.EndNegotiation import EndNegotiation
|
---|
5 | from geniusweb.actions.Offer import Offer
|
---|
6 | from geniusweb.actions.PartyId import PartyId
|
---|
7 | from geniusweb.actions.Votes import Votes
|
---|
8 | from geniusweb.inform.Inform import Inform
|
---|
9 | from geniusweb.inform.Voting import Voting
|
---|
10 | from geniusweb.protocol.ProtocolException import ProtocolException
|
---|
11 | from geniusweb.protocol.session.mopac.PartyStates import PartyStates
|
---|
12 | from geniusweb.protocol.session.mopac.phase.DefaultPhase import DefaultPhase
|
---|
13 | from geniusweb.protocol.session.mopac.phase.OptInPhase import OptInPhase
|
---|
14 | from geniusweb.protocol.session.mopac.phase.Phase import Phase
|
---|
15 | from geniusweb.voting.VotingEvaluator import VotingEvaluator
|
---|
16 |
|
---|
17 |
|
---|
18 | class VotingPhase (DefaultPhase):
|
---|
19 |
|
---|
20 | def __init__(self, offers: List[Offer],
|
---|
21 | partyStates:PartyStates,
|
---|
22 | deadline: int,
|
---|
23 | evaluator:VotingEvaluator ) :
|
---|
24 | super().__init__(partyStates, deadline, evaluator)
|
---|
25 | self._offers = offers
|
---|
26 | if offers==None:
|
---|
27 | raise ValueError("offers must be not null")
|
---|
28 |
|
---|
29 | def With(self, actor:PartyId ,action: Action , now:int)-> "VotingPhase" :
|
---|
30 | try:
|
---|
31 | self._checkAction(actor, action, now)
|
---|
32 | except ProtocolException as ex:
|
---|
33 | return self.WithException(ex)
|
---|
34 | return VotingPhase(self._offers, self._partyStates.WithAction(action), self._deadline,
|
---|
35 | self._evaluator);
|
---|
36 |
|
---|
37 | def getInform(self)->Inform :
|
---|
38 | return Voting(self._offers, self._partyStates.getPowers())
|
---|
39 |
|
---|
40 | def WithException(self, e:ProtocolException ) ->"VotingPhase" :
|
---|
41 | return VotingPhase(self._offers, self._partyStates.WithException(e), self._deadline,
|
---|
42 | self._evaluator)
|
---|
43 |
|
---|
44 | def finish(self)->"VotingPhase" :
|
---|
45 | return VotingPhase(self._offers, self._partyStates.finish(), self._deadline,
|
---|
46 | self._evaluator)
|
---|
47 |
|
---|
48 | def _checkedNext(self,deadln:int) ->Phase :
|
---|
49 | return OptInPhase(self.getVotes(), self._partyStates.flush(), deadln,
|
---|
50 | self._evaluator);
|
---|
51 |
|
---|
52 | def getAllowedActions(self) ->List[Any]: #Class<? extends Action>>:
|
---|
53 | return [Votes, EndNegotiation]
|
---|
54 |
|
---|
55 | def getVotes(self)->List[Votes]:
|
---|
56 | '''
|
---|
57 | @return all votes done in this phase.
|
---|
58 | '''
|
---|
59 | return [ act for act in self._partyStates.getActions() if isinstance(act, Votes)]
|
---|
60 |
|
---|
61 | def getOffers(self)->List[Offer]:
|
---|
62 | '''
|
---|
63 | @return the offers received in the {@link OfferPhase}
|
---|
64 | '''
|
---|
65 | return list(self._offers) |
---|