1 | package genius.gui.tournament;
|
---|
2 |
|
---|
3 | import java.util.ArrayList;
|
---|
4 | import java.util.Arrays;
|
---|
5 | import java.util.List;
|
---|
6 |
|
---|
7 | import javax.swing.event.ListDataEvent;
|
---|
8 | import javax.swing.event.ListDataListener;
|
---|
9 |
|
---|
10 | import genius.core.config.MultilateralTournamentConfiguration;
|
---|
11 | import genius.core.listener.DefaultListenable;
|
---|
12 | import genius.core.listener.Listener;
|
---|
13 | import genius.core.persistent.PersistentDataType;
|
---|
14 | import genius.core.repository.MultiPartyProtocolRepItem;
|
---|
15 | import genius.core.repository.ParticipantRepItem;
|
---|
16 | import genius.core.repository.PartyRepItem;
|
---|
17 | import genius.core.repository.ProfileRepItem;
|
---|
18 | import genius.gui.deadline.DeadlineModel;
|
---|
19 | import genius.gui.negosession.ContentProxy;
|
---|
20 | import genius.gui.panels.BooleanModel;
|
---|
21 | import genius.gui.panels.NumberModel;
|
---|
22 | import genius.gui.panels.SingleSelectionModel;
|
---|
23 | import 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 | */
|
---|
39 | public 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 | }
|
---|