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

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

Fixed small issues in domaineditor.

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