1 | # Generated from java by J2P
|
---|
2 | from __future__ import annotations
|
---|
3 | from geniusweb.actions.Action import Action
|
---|
4 | from geniusweb.actions.EndNegotiation import EndNegotiation
|
---|
5 | from geniusweb.actions.PartyId import PartyId
|
---|
6 | from geniusweb.actions.Votes import Votes
|
---|
7 | from geniusweb.inform.Agreements import Agreements
|
---|
8 | from geniusweb.inform.Inform import Inform
|
---|
9 | from geniusweb.inform.OptIn import OptIn
|
---|
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.OfferPhase import OfferPhase
|
---|
14 | from geniusweb.protocol.session.mopac.phase.Phase import Phase
|
---|
15 | from geniusweb.voting.CollectedVotes import CollectedVotes
|
---|
16 | from geniusweb.voting.VotingEvaluator import VotingEvaluator
|
---|
17 | from typing import List
|
---|
18 | from typing import Optional
|
---|
19 | from typing import Type
|
---|
20 | from typing import cast
|
---|
21 |
|
---|
22 |
|
---|
23 | class OptInPhase(DefaultPhase):
|
---|
24 |
|
---|
25 | def __init__(self, votes:List[Votes], partyStates:PartyStates, deadline:int, evaluator:VotingEvaluator):
|
---|
26 | '''
|
---|
27 | The votes received in the {@link VotingPhase}
|
---|
28 |
|
---|
29 | '''
|
---|
30 |
|
---|
31 | self.__votes:List[Votes] = None
|
---|
32 | super().__init__(partyStates,deadline,evaluator)
|
---|
33 | if (votes is None):
|
---|
34 | raise ValueError("votes must be not null")
|
---|
35 | self.__votes=votes
|
---|
36 |
|
---|
37 | #Override
|
---|
38 | def getInform(self) -> Inform:
|
---|
39 | return OptIn(self.__votes)
|
---|
40 |
|
---|
41 | #Override
|
---|
42 | def withAction(self,actor:PartyId,action:Action,now:int) -> Phase:
|
---|
43 | try:
|
---|
44 | self._checkAction(actor,action,now)
|
---|
45 | if isinstance(action,Votes):
|
---|
46 | self.__checkExtends(cast(Votes,action))
|
---|
47 | return OptInPhase(self.__votes,self._partyStates.withAction(action),self._deadline,self._evaluator)
|
---|
48 | except ProtocolException as e:
|
---|
49 | return self.withException(e)
|
---|
50 |
|
---|
51 | def __checkExtends(self,newvotes:Votes) -> None:
|
---|
52 | '''
|
---|
53 | Check that this action extends previous action.
|
---|
54 |
|
---|
55 | @param newvotes new {@link Votes} just received
|
---|
56 | @throws ProtocolException if this action does not correctly extend
|
---|
57 | previous vote.
|
---|
58 |
|
---|
59 | '''
|
---|
60 | actor:PartyId = newvotes.getActor()
|
---|
61 | prevVotes:Votes = [v for v in self.__votes if v.getActor() == actor ][0]
|
---|
62 | if not((newvotes.isExtending(prevVotes))):
|
---|
63 | raise ProtocolException("New votes " + str(newvotes) + " does not extend previous vote " + str(prevVotes),actor)
|
---|
64 |
|
---|
65 | #Override
|
---|
66 | def withException(self,e:ProtocolException) -> OptInPhase:
|
---|
67 | return OptInPhase(self.__votes,self._partyStates.withException(e),self._deadline,self._evaluator)
|
---|
68 |
|
---|
69 | #Override
|
---|
70 | def finish(self) -> OptInPhase:
|
---|
71 | states:PartyStates = self._partyStates.finish()
|
---|
72 | votesmap: Dict[PartyId, Votes] = \
|
---|
73 | {votes.getActor() : votes for votes in states.getActionsOfType(Votes) }
|
---|
74 | allvotes:CollectedVotes = CollectedVotes(votesmap,states.getPowers())
|
---|
75 | newAgree:Agreements = self._evaluator.create(allvotes).getAgreements()
|
---|
76 | # }
|
---|
77 | finalStates:PartyStates = states.withAgreement(newAgree)
|
---|
78 | return OptInPhase(self.__votes,finalStates,self._deadline,self._evaluator)
|
---|
79 |
|
---|
80 | #Override
|
---|
81 | def _checkedNext(self,newdeadline:int) -> OfferPhase:
|
---|
82 | return OfferPhase(self._partyStates.flush(),newdeadline,self._evaluator)
|
---|
83 |
|
---|
84 | #Override
|
---|
85 | def getAllowedActions(self) -> List[Type[Optional[Action]]]:
|
---|
86 | return [Votes,EndNegotiation]
|
---|
87 |
|
---|
88 | def getVotes(self) -> List[Votes]:
|
---|
89 | return list(self.__votes)
|
---|