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

Last change on this file since 100 was 100, checked in by ruud, 14 months ago

python installs also wheel to avoid error messages

File size: 3.0 KB
Line 
1from typing import List, Any
2
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.Inform import Inform
8from geniusweb.inform.OptIn import OptIn
9from geniusweb.protocol.ProtocolException import ProtocolException
10from geniusweb.protocol.session.mopac.PartyStates import PartyStates
11from geniusweb.protocol.session.mopac.phase.DefaultPhase import DefaultPhase
12from geniusweb.protocol.session.mopac.phase.Phase import Phase
13from geniusweb.voting.CollectedVotes import CollectedVotes
14from geniusweb.voting.VotingEvaluator import VotingEvaluator
15
16
17class OptInPhase ( DefaultPhase ):
18
19 def __init__(self, votes:List[Votes] ,
20 partyStates:PartyStates ,
21 deadline:int,
22 evaluator:VotingEvaluator ):
23 super().__init__(partyStates, deadline, evaluator)
24 self._votes = votes
25 if votes==None:
26 raise ValueError("votes must be not null")
27
28 def getInform(self)->Inform :
29 return OptIn(self._votes)
30
31 def With(self, actor:PartyId , action:Action , now:int)->Phase :
32 try:
33 self._checkAction(actor, action, now)
34 if isinstance(action,Votes):
35 self._checkExtends(action)
36
37 return OptInPhase(self._votes, self._partyStates.WithAction(action),
38 self._deadline, self._evaluator)
39
40 except ProtocolException as e:
41 return self.WithException(e)
42
43 def _checkExtends(self, newvotes:Votes ) :
44 '''
45 Check that this action extends previous action.
46
47 @param newvotes new {@link Votes} just received
48 @throws ProtocolException if this action does not correctly extend
49 previous vote.
50 '''
51 actor = newvotes.getActor();
52 # this actor is active so he must have voted in previous round.
53 prevVotes = [v for v in self._votes if v.getActor() == actor ][0]
54 if not newvotes.isExtending(prevVotes):
55 raise ProtocolException("New votes " + str(newvotes)
56 + " does not extend previous vote " + str(prevVotes), actor)
57
58 def WithException(self, e:ProtocolException) ->"OptInPhase" :
59 return OptInPhase(self._votes, self._partyStates.WithException(e), self._deadline, self._evaluator)
60
61 def finish(self)-> "OptInPhase":
62 states = self._partyStates.finish()
63 votesmap = { }
64 for v in states.getActionsOfType(Votes):
65 votesmap[v.getActor()]=v
66 allvotes = CollectedVotes(votesmap, states.getPowers())
67 newAgree = self._evaluator.create(allvotes).getAgreements()
68 if len(newAgree.getMap())>0:
69 print("detected new agreements")
70 finalStates = states.WithAgreements(newAgree)
71
72 return OptInPhase(self._votes, finalStates, self._deadline, self._evaluator);
73
74 def _checkedNext(self, newdeadline:int) ->"OfferPhase" : #type:ignore
75 from geniusweb.protocol.session.mopac.phase.OfferPhase import OfferPhase
76 return OfferPhase(self._partyStates.flush(), newdeadline, self._evaluator)
77
78 def getAllowedActions(self) -> List[Any]: #<Class<? extends Action>> :
79 return [Votes, EndNegotiation]
80
81 def getVotes(self)->List[Votes]:
82 return list(self._votes)
83
Note: See TracBrowser for help on using the repository browser.