source: src/main/java/genius/gui/tournament/MultiTournamentModel.java@ 346

Last change on this file since 346 was 139, checked in by Wouter Pasman, 6 years ago

#54 added GUI stuff for uncertainty seed. Not yet connected with actual code.

File size: 7.9 KB
Line 
1package genius.gui.tournament;
2
3import java.util.ArrayList;
4import java.util.Arrays;
5import java.util.List;
6
7import javax.swing.event.ListDataEvent;
8import javax.swing.event.ListDataListener;
9
10import genius.core.config.MultilateralTournamentConfiguration;
11import genius.core.listener.DefaultListenable;
12import genius.core.listener.Listener;
13import genius.core.persistent.PersistentDataType;
14import genius.core.repository.MultiPartyProtocolRepItem;
15import genius.core.repository.ParticipantRepItem;
16import genius.core.repository.PartyRepItem;
17import genius.core.repository.ProfileRepItem;
18import genius.gui.deadline.DeadlineModel;
19import genius.gui.negosession.ContentProxy;
20import genius.gui.panels.BooleanModel;
21import genius.gui.panels.NumberModel;
22import genius.gui.panels.SingleSelectionModel;
23import genius.gui.panels.SubsetSelectionModel;
24
25/**
26 * Contains the basic elements of MultilateralTournamentConfiguration, but
27 * mutable and the subcomponents are listenable so that we can use it for the
28 * MVC pattern. It listens to changes in the protocol and updates the model when
29 * necessary.
30 *
31 * <p>
32 * You can get notified when the multitournamentmodel is complete (as indicated
33 * by the user, he can press 'start' in the GUI). The data passed with the
34 * notification is the {@link MultilateralTournamentConfiguration}.
35 *
36 * @author W.Pasman
37 *
38 */
39public class MultiTournamentModel
40 extends DefaultListenable<MultilateralTournamentConfiguration> {
41
42 // models are all final, as they will be used to hook up the GUI.
43
44 private final SubsetSelectionModel<ProfileRepItem> profileModel;
45 private final SubsetSelectionModel<ParticipantRepItem> partyModel;
46 private final SingleSelectionModel<MultiPartyProtocolRepItem> protocolModel;
47 private final DeadlineModel deadlineModel = new DeadlineModel();
48 private final SingleSelectionModel<PartyRepItem> mediatorModel;
49 private final NumberModel numTournamentsModel = new NumberModel(1, 1,
50 2000000000, 1);
51 private final NumberModel numAgentsPerSessionModel = new NumberModel(1, 1,
52 2000000000, 1);
53 private final BooleanModel agentRepetitionModel = new BooleanModel(false);
54 private final BooleanModel randomSessionOrderModel = new BooleanModel(
55 false);
56 private final BooleanModel enablePrintModel = new BooleanModel(false);
57 private final BilateralOptionsModel bilateralOptionsModel;
58 private final SingleSelectionModel<PersistentDataType> persistentDatatypeModel = new SingleSelectionModel<PersistentDataType>(
59 Arrays.asList(PersistentDataType.values()));
60
61 public MultiTournamentModel() {
62 // load initial models.
63 protocolModel = new SingleSelectionModel<>(
64 ContentProxy.fetchProtocols());
65 profileModel = new SubsetSelectionModel<ProfileRepItem>(
66 ContentProxy.fetchProfiles());
67
68 // stubs for the partyModel and mediatorModel, will be set properly in
69 // updateSubmodels.
70 partyModel = new SubsetSelectionModel<ParticipantRepItem>(
71 new ArrayList<ParticipantRepItem>());
72 mediatorModel = new SingleSelectionModel<PartyRepItem>(
73 new ArrayList<PartyRepItem>());
74
75 bilateralOptionsModel = new BilateralOptionsModel(protocolModel);
76
77 updateSubmodels();
78 updateAgentRepetition();
79
80 addConstraints();
81
82 }
83
84 public SubsetSelectionModel<ProfileRepItem> getProfileModel() {
85 return profileModel;
86 }
87
88 /**
89 * @return model containing the deadline information
90 */
91 public DeadlineModel getDeadlineModel() {
92 return deadlineModel;
93 }
94
95 /**
96 * @return model containing the parties to use in the tournament
97 */
98 public SubsetSelectionModel<ParticipantRepItem> getPartyModel() {
99 return partyModel;
100 }
101
102 /**
103 * @return the model containing the number of tournaments to be run. This is
104 * also called "number of sessions" in some places. May be somethign
105 * historic.
106 */
107 public NumberModel getNumTournamentsModel() {
108 return numTournamentsModel;
109 }
110
111 /**
112 * @return the model containing the number of agents per session.
113 */
114 public NumberModel getNumAgentsPerSessionModel() {
115 return numAgentsPerSessionModel;
116 }
117
118 /**
119 * @return mediator model, or null if no mediator for this protocol
120 */
121 public SingleSelectionModel<PartyRepItem> getMediatorModel() {
122 return mediatorModel;
123 }
124
125 /**
126 * @return this model, converted in a
127 * {@link MultilateralTournamentConfiguration}.
128 */
129 public MultilateralTournamentConfiguration getConfiguration() {
130 List<ProfileRepItem> profilesB = new ArrayList<>();
131 List<ParticipantRepItem> partiesB = new ArrayList<>();
132
133 if (!bilateralOptionsModel.getPlayBothSides().getValue()) {
134 profilesB = bilateralOptionsModel.getProfileModelB()
135 .getSelectedItems();
136 partiesB = bilateralOptionsModel.getPartyModelB()
137 .getSelectedItems();
138 }
139 return new MultilateralTournamentConfiguration(
140 protocolModel.getSelection(), deadlineModel.getDeadline(),
141 mediatorModel.getSelection(), partyModel.getSelectedItems(),
142 profileModel.getSelectedItems(), partiesB, profilesB,
143 numTournamentsModel.getValue().intValue(),
144 numAgentsPerSessionModel.getValue().intValue(),
145 agentRepetitionModel.getValue(),
146 randomSessionOrderModel.getValue(),
147 persistentDatatypeModel.getSelection(),
148 enablePrintModel.getValue());
149
150 }
151
152 /**
153 * @return model containing the protocol
154 */
155 public SingleSelectionModel<MultiPartyProtocolRepItem> getProtocolModel() {
156 return protocolModel;
157 }
158
159 public BooleanModel getAgentRepetitionModel() {
160 return agentRepetitionModel;
161 }
162
163 public BooleanModel getRandomSessionOrderModel() {
164 return randomSessionOrderModel;
165 }
166
167 /**
168 * Call this when model is completed (user clicked 'start'). TODO check that
169 * the model is indeed complete.
170 */
171 public void modelIsComplete() {
172 notifyChange(getConfiguration());
173 }
174
175 public BilateralOptionsModel getBilateralOptionsModel() {
176 return bilateralOptionsModel;
177 }
178
179 public SingleSelectionModel<PersistentDataType> getPersistentDatatypeModel() {
180 return persistentDatatypeModel;
181 }
182
183 /******************* support funcs ***********************/
184 /**
185 * Update the repetition setting. Must go to "true, locked" mode if
186 * agentsPerSession is bigger than the number of available agents.
187 */
188 private void updateAgentRepetition() {
189 agentRepetitionModel.setLock(false);
190 if (numAgentsPerSessionModel.getValue().intValue() > partyModel
191 .getSelectedItems().size()) {
192 agentRepetitionModel.setValue(true);
193 agentRepetitionModel.setLock(true);
194 }
195 }
196
197 /**
198 * connecting listeners that check the constraints between the fields in the
199 * model
200 */
201 private void addConstraints() {
202 // protocol has major impact on the submodels
203 protocolModel.addListDataListener(new ListDataListener() {
204
205 @Override
206 public void intervalRemoved(ListDataEvent e) {
207 }
208
209 @Override
210 public void intervalAdded(ListDataEvent e) {
211 }
212
213 @Override
214 public void contentsChanged(ListDataEvent e) {
215 updateSubmodels();
216 }
217 });
218
219 // The "Agents per session" field by default equals number of profiles;
220 // the agent repetition is depending on this too.
221 profileModel.addListener(new Listener<ProfileRepItem>() {
222 @Override
223 public void notifyChange(ProfileRepItem data) {
224 numAgentsPerSessionModel
225 .setValue(profileModel.getSelectedItems().size());
226 updateAgentRepetition();
227 }
228 });
229
230 // #parties and numAgentsPerSession -> repetition
231 partyModel.addListener(new Listener<ParticipantRepItem>() {
232 @Override
233 public void notifyChange(ParticipantRepItem data) {
234 updateAgentRepetition();
235 }
236 });
237 numAgentsPerSessionModel.addListener(new Listener<Number>() {
238 @Override
239 public void notifyChange(Number data) {
240 updateAgentRepetition();
241 }
242 });
243 }
244
245 /**
246 * Update the models after a protocol change.
247 *
248 * @param protocol
249 * the new protocol.
250 */
251 private void updateSubmodels() {
252 MultiPartyProtocolRepItem protocol = protocolModel.getSelection();
253 partyModel.setAllItems(ContentProxy.fetchPartiesForProtocol(protocol));
254 mediatorModel
255 .setAllItems(ContentProxy.fetchMediatorsForProtocol(protocol));
256 }
257
258 public BooleanModel getEnablePrint() {
259 return enablePrintModel;
260 }
261
262}
Note: See TracBrowser for help on using the repository browser.