source: geniuswebcore/geniusweb/protocol/session/mopac/phase/OptInPhase.py

Last change on this file was 111, checked in by ruud, 2 months ago

Modifications for automatic java to python conversion. Overloaded methods now have different names.

File size: 3.2 KB
Line 
1# Generated from java by J2P
2from __future__ import annotations
3from geniusweb.actions.Action import Action
4from geniusweb.actions.EndNegotiation import EndNegotiation
5from geniusweb.actions.PartyId import PartyId
6from geniusweb.actions.Votes import Votes
7from geniusweb.inform.Agreements import Agreements
8from geniusweb.inform.Inform import Inform
9from geniusweb.inform.OptIn import OptIn
10from geniusweb.protocol.ProtocolException import ProtocolException
11from geniusweb.protocol.session.mopac.PartyStates import PartyStates
12from geniusweb.protocol.session.mopac.phase.DefaultPhase import DefaultPhase
13from geniusweb.protocol.session.mopac.phase.OfferPhase import OfferPhase
14from geniusweb.protocol.session.mopac.phase.Phase import Phase
15from geniusweb.voting.CollectedVotes import CollectedVotes
16from geniusweb.voting.VotingEvaluator import VotingEvaluator
17from typing import List
18from typing import Optional
19from typing import Type
20from typing import cast
21
22
23class 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)
Note: See TracBrowser for help on using the repository browser.