source: geniuswebcore/geniusweb/protocol/session/learn/LearnState.py@ 97

Last change on this file since 97 was 96, checked in by Bart Vastenhouw, 2 years ago

Fixed small issues in domaineditor.

File size: 3.7 KB
Line 
1from typing import List, Dict, Set, Optional
2
3from geniusweb.actions.Action import Action
4from geniusweb.actions.LearningDone import LearningDone
5from geniusweb.actions.PartyId import PartyId
6from geniusweb.inform.Agreements import Agreements
7from geniusweb.progress.Progress import Progress
8from geniusweb.protocol.ProtocolException import ProtocolException
9from geniusweb.protocol.session.DefaultSessionState import DefaultSessionState
10from geniusweb.protocol.session.SessionResult import SessionResult
11from geniusweb.protocol.session.learn.LearnSettings import LearnSettings
12from geniusweb.references.PartyWithProfile import PartyWithProfile
13
14
15class LearnState (DefaultSessionState["LearnState", "LearnSettings"]):
16 '''
17 Stores the state for Learn protocol. Immutable.
18 '''
19
20 def __init__(self, actions:List[Action] , conns:List[PartyId] ,
21 progress:Optional[Progress] , settings:LearnSettings ,
22 partyprofiles:Dict[PartyId, PartyWithProfile]={},
23 e:Optional[ProtocolException]=None):
24 '''
25 @param actions the actions done by the parties
26 @param conns the existing party connections. we assume ownership
27 of this so it should not be modified although
28 connections may of course break. If null, default
29 empty list is used. Can be less than 2 parties in
30 the first phases of the setup.
31 @param progress the {@link Progress} line. can be null
32 @param settings the {@link LearnSettings}
33 @param partyprofiles map with the {@link PartyWithProfile} for connected
34 parties.
35 @param e the {@link ProtocolException}, or null if none
36 occurred.
37 '''
38
39 super().__init__(actions, conns, progress, settings, partyprofiles, e)
40
41# def LearnState(LearnSettings settings) {
42# '''
43# Creates the initial state from the given settings and progress=null
44#
45# @param settings the {@link LearnSettings}
46# '''
47# this(Collections.emptyList(), Collections.emptyList(), null, settings,
48# null, null);
49#
50# }
51
52 def With(self, actions:List[Action] , conns:List[PartyId],
53 progr:Optional[Progress] , settings:LearnSettings,
54 partyprofiles:Dict[PartyId, PartyWithProfile] ,
55 e:Optional[ProtocolException]) -> "LearnState":
56 return LearnState(actions, conns, progr, settings, partyprofiles,
57 e)
58
59 def getAgreements(self) -> Agreements:
60 return Agreements()
61
62 def getResults(self) -> List[SessionResult]:
63 return [SessionResult(self.getPartyProfiles(), self.getAgreements(),
64 {}, None)]
65
66 def isFinal(self, currentTimeMs:int) -> bool:
67 done = self._getDoneLearning()
68 # if done is empty we're probably still starting up.
69 return super().isFinal(currentTimeMs)\
70 or (len(done) != 0 and set(self.getConnections()).issubset(done))
71
72 def WithException(self, e:ProtocolException) -> "LearnState":
73 '''
74 @param e the error that occured
75 @return a new state with the error set.
76 '''
77 return LearnState(self.getActions(), self.getConnections(), self.getProgress(),
78 self.getSettings(), self.getPartyProfiles(), e)
79
80 def checkAction(self, actor:PartyId , action:Action) -> Optional[str]:
81 '''
82 @param actor the known real actor that did this action
83 @param action an {@link Action}
84 @return null if action seems ok, or message explaining why not.
85 '''
86 msg = super().checkAction(actor, action)
87 if msg != None:
88 return msg
89 if actor in self._getDoneLearning():
90 return "actor already was done learning"
91 if not isinstance(action, LearningDone):
92 return "Action " + str(action) + " is not allowed"
93 return None
94
95 def _getDoneLearning(self) -> Set[PartyId]:
96 '''
97 @return set of parties that reported they are done with learning.
98 '''
99 return set([party.getActor() for party in self.getActions() ])
100
Note: See TracBrowser for help on using the repository browser.