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