source: protocol/src/main/java/geniusweb/protocol/session/learn/LearnState.java@ 31

Last change on this file since 31 was 31, checked in by bart, 3 years ago

New protocols Learn and APPLearn. Fixed memory leak.

File size: 3.8 KB
Line 
1package geniusweb.protocol.session.learn;
2
3import java.util.Collections;
4import java.util.List;
5import java.util.Map;
6import java.util.Set;
7import java.util.stream.Collectors;
8
9import geniusweb.actions.Action;
10import geniusweb.actions.LearningDone;
11import geniusweb.actions.PartyId;
12import geniusweb.inform.Agreements;
13import geniusweb.progress.Progress;
14import geniusweb.protocol.ProtocolException;
15import geniusweb.protocol.session.DefaultSessionState;
16import geniusweb.protocol.session.SessionResult;
17import geniusweb.references.PartyWithProfile;
18
19/**
20 * Stores the state for Learn protocol. Immutable.
21 */
22public class LearnState extends DefaultSessionState<LearnState, LearnSettings> {
23
24 /**
25 *
26 * @param actions the actions done by the parties
27 * @param conns the existing party connections. we assume ownership
28 * of this so it should not be modified although
29 * connections may of course break. If null, default
30 * empty list is used. Can be less than 2 parties in
31 * the first phases of the setup.
32 * @param progress the {@link Progress} line. can be null
33 * @param settings the {@link LearnSettings}
34 * @param partyprofiles map with the {@link PartyWithProfile} for connected
35 * parties. null is equivalent to an empty map.
36 * @param e the {@link ProtocolException}, or null if none
37 * occurred.
38 */
39 public LearnState(List<Action> actions, List<PartyId> conns,
40 Progress progress, LearnSettings settings,
41 Map<PartyId, PartyWithProfile> partyprofiles, ProtocolException e) {
42 super(actions, conns, progress, settings, partyprofiles, e);
43 }
44
45 /**
46 * Creates the initial state from the given settings and progress=null
47 *
48 * @param settings the {@link LearnSettings}
49 */
50 public LearnState(LearnSettings settings) {
51 this(Collections.emptyList(), Collections.emptyList(), null, settings,
52 null, null);
53
54 }
55
56 @Override
57 public LearnState with(List<Action> actions, List<PartyId> conns,
58 Progress progr, LearnSettings settings,
59 Map<PartyId, PartyWithProfile> partyprofiles, ProtocolException e) {
60 return new LearnState(actions, conns, progr, settings, partyprofiles,
61 e);
62 }
63
64 @Override
65 public Agreements getAgreements() {
66 return new Agreements();
67 }
68
69 @Override
70 public SessionResult getResult() {
71 return new SessionResult(getPartyProfiles(), getAgreements(),
72 Collections.emptyMap(), null);
73 }
74
75 @Override
76 public boolean isFinal(long currentTimeMs) {
77 Set<PartyId> done = getDoneLearning();
78 // if done is empty we're probably still starting up.
79 return super.isFinal(currentTimeMs)
80 || (!done.isEmpty() && done.containsAll(getConnections()));
81 }
82
83 /**
84 *
85 * @param e the error that occured
86 * @return a new state with the error set.
87 */
88 @Override
89 public LearnState with(ProtocolException e) {
90 return new LearnState(getActions(), getConnections(), getProgress(),
91 getSettings(), getPartyProfiles(), e);
92 }
93
94 @Override
95 public LearnSettings getSettings() {
96 return super.getSettings();
97 }
98
99 /**
100 *
101 * @param actor the known real actor that did this action
102 * @param action an {@link Action}
103 * @return null if action seems ok, or message explaining why not.
104 */
105 @Override
106 public String checkAction(PartyId actor, Action action) {
107 String msg = super.checkAction(actor, action);
108 if (msg != null)
109 return msg;
110 if (getDoneLearning().contains(actor))
111 return "actor already was done learning";
112 if (!(action instanceof LearningDone))
113 return "Action " + action + " is not allowed";
114 return null;
115 }
116
117 /**
118 *
119 * @return set of parties that reported they are done with learning.
120 */
121 private Set<PartyId> getDoneLearning() {
122 return getActions().stream().map(party -> party.getActor())
123 .collect(Collectors.toSet());
124 }
125
126}
Note: See TracBrowser for help on using the repository browser.