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.PartyId import PartyId
|
---|
6 | from geniusweb.actions.Votes import Votes
|
---|
7 | from geniusweb.inform.Inform import Inform
|
---|
8 | from geniusweb.inform.OptIn import OptIn
|
---|
9 | from geniusweb.protocol.ProtocolException import ProtocolException
|
---|
10 | from geniusweb.protocol.session.mopac.PartyStates import PartyStates
|
---|
11 | from geniusweb.protocol.session.mopac.phase.DefaultPhase import DefaultPhase
|
---|
12 | from geniusweb.protocol.session.mopac.phase.Phase import Phase
|
---|
13 | from geniusweb.voting.CollectedVotes import CollectedVotes
|
---|
14 | from geniusweb.voting.VotingEvaluator import VotingEvaluator
|
---|
15 |
|
---|
16 |
|
---|
17 | class 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 |
|
---|