[virtualcommons-svn] commit/foraging: alllee: fixes + cleanup for imposed strategy condition.
Status: Beta
Brought to you by:
alllee
From: Bitbucket <com...@bi...> - 2012-03-05 22:41:33
|
1 new commit in foraging: https://bitbucket.org/virtualcommons/foraging/changeset/d572fcd98734/ changeset: d572fcd98734 user: alllee date: 2012-03-05 23:41:32 summary: fixes + cleanup for imposed strategy condition. Currently, the imposed strategy for each group is calculated and sent when the show vote screen request is received by the server. affected #: 8 files diff -r b4e233eb7aff92bf8ed80d699427363a2a7dbf97 -r d572fcd98734afe5bf746264acab8c548bd1f71d src/main/java/edu/asu/commons/foraging/client/ClientDataModel.java --- a/src/main/java/edu/asu/commons/foraging/client/ClientDataModel.java +++ b/src/main/java/edu/asu/commons/foraging/client/ClientDataModel.java @@ -273,6 +273,7 @@ public void setSelectedStrategies(List<Strategy> selectedStrategies) { this.selectedStrategies = selectedStrategies; + getRoundConfiguration().setSelectedRules(selectedStrategies); } public List<Strategy> getSelectedStrategies() { diff -r b4e233eb7aff92bf8ed80d699427363a2a7dbf97 -r d572fcd98734afe5bf746264acab8c548bd1f71d src/main/java/edu/asu/commons/foraging/conf/RoundConfiguration.java --- a/src/main/java/edu/asu/commons/foraging/conf/RoundConfiguration.java +++ b/src/main/java/edu/asu/commons/foraging/conf/RoundConfiguration.java @@ -851,10 +851,4 @@ return getProperty("survey-confirmation-message", "Please make sure you have completed the survey before continuing. Have you completed the survey?"); } - - public String getImposedStrategyInstructions(Strategy strategy) { - ST st = createStringTemplate(getProperty("imposed-strategy-instructions")); - st.add("strategy", strategy); - return st.render(); - } } diff -r b4e233eb7aff92bf8ed80d699427363a2a7dbf97 -r d572fcd98734afe5bf746264acab8c548bd1f71d src/main/java/edu/asu/commons/foraging/model/GroupDataModel.java --- a/src/main/java/edu/asu/commons/foraging/model/GroupDataModel.java +++ b/src/main/java/edu/asu/commons/foraging/model/GroupDataModel.java @@ -831,7 +831,10 @@ } public void setImposedStrategy(Strategy imposedStrategy) { - this.imposedStrategy = imposedStrategy; + // XXX: defensive set, don't override an already imposed strategy. + if (this.imposedStrategy == null) { + this.imposedStrategy = imposedStrategy; + } } } diff -r b4e233eb7aff92bf8ed80d699427363a2a7dbf97 -r d572fcd98734afe5bf746264acab8c548bd1f71d src/main/java/edu/asu/commons/foraging/model/ServerDataModel.java --- a/src/main/java/edu/asu/commons/foraging/model/ServerDataModel.java +++ b/src/main/java/edu/asu/commons/foraging/model/ServerDataModel.java @@ -63,6 +63,8 @@ // Maps client Identifiers to the GroupDataModel that the client belongs to private final Map<Identifier, GroupDataModel> clientsToGroups = new HashMap<Identifier, GroupDataModel>(); + private Map<Strategy, Integer> imposedStrategyDistribution; + public ServerDataModel() { super(EventTypeChannel.getInstance()); } @@ -463,24 +465,41 @@ } + public List<GroupDataModel> allocateImposedStrategyDistribution(Map<Strategy, Integer> imposedStrategyDistribution) { - List<GroupDataModel> groups = getOrderedGroups(); - int numberOfGroups = groups.size(); - Collections.shuffle(groups); - Iterator<GroupDataModel> groupIterator = groups.iterator(); - int numberOfStrategies = 0; - for (Map.Entry<Strategy, Integer> entry : imposedStrategyDistribution.entrySet()) { - Strategy strategy = entry.getKey(); - int occurrences = entry.getValue(); - if (numberOfStrategies > numberOfGroups) { - throw new IllegalArgumentException("Invalid number of strategies : " + numberOfStrategies + " for " + numberOfGroups + " groups."); - } - for (int i = 0; i < occurrences; i++) { - GroupDataModel group = groupIterator.next(); - group.setImposedStrategy(strategy); - numberOfStrategies++; - } - } - return groups; + if (imposedStrategyDistribution == null || imposedStrategyDistribution.isEmpty()) { + throw new IllegalArgumentException("No strategy distribution defined. Please create a strategy distribution and try again."); + } + List<GroupDataModel> groups = getOrderedGroups(); + int numberOfGroups = groups.size(); + Collections.shuffle(groups); + Iterator<GroupDataModel> groupIterator = groups.iterator(); + int numberOfStrategies = 0; + for (Map.Entry<Strategy, Integer> entry : imposedStrategyDistribution.entrySet()) { + Strategy strategy = entry.getKey(); + int occurrences = entry.getValue(); + if (numberOfStrategies > numberOfGroups) { + throw new IllegalArgumentException("Invalid number of strategies : " + numberOfStrategies + " for " + numberOfGroups + " groups."); + } + for (int i = 0; i < occurrences; i++) { + GroupDataModel group = groupIterator.next(); + group.setImposedStrategy(strategy); + numberOfStrategies++; + } + } + return groups; } + + public List<GroupDataModel> allocateImposedStrategyDistribution() { + return allocateImposedStrategyDistribution(imposedStrategyDistribution); + } + + public void setImposedStrategyDistribution(Map<Strategy, Integer> strategyDistribution) { + this.imposedStrategyDistribution = strategyDistribution; + } + + public Map<Strategy, Integer> getImposedStrategyDistribution() { + return imposedStrategyDistribution; + } + } diff -r b4e233eb7aff92bf8ed80d699427363a2a7dbf97 -r d572fcd98734afe5bf746264acab8c548bd1f71d src/main/java/edu/asu/commons/foraging/server/ForagingServer.java --- a/src/main/java/edu/asu/commons/foraging/server/ForagingServer.java +++ b/src/main/java/edu/asu/commons/foraging/server/ForagingServer.java @@ -63,6 +63,7 @@ import edu.asu.commons.foraging.event.RuleVoteRequest; import edu.asu.commons.foraging.event.SanctionAppliedEvent; import edu.asu.commons.foraging.event.SetImposedStrategyEvent; +import edu.asu.commons.foraging.event.ShowVoteScreenRequest; import edu.asu.commons.foraging.event.SurveyIdSubmissionRequest; import edu.asu.commons.foraging.event.SynchronizeClientEvent; import edu.asu.commons.foraging.event.TrustGameResultsFacilitatorEvent; @@ -599,21 +600,9 @@ sendFacilitatorMessage("Ignoring request to impose strategy " + event); return; } + serverDataModel.setImposedStrategyDistribution(event.getStrategyDistribution()); + sendFacilitatorMessage("Setting imposed strategy distribution to " + event.getStrategyDistribution()); persister.store(event); - try { - List<GroupDataModel> groups = serverDataModel.allocateImposedStrategyDistribution(event.getStrategyDistribution()); - StringBuilder builder = new StringBuilder(); - for (GroupDataModel group: groups) { - builder.append('[').append(group).append(':').append(group.getImposedStrategy()).append(']'); - for (Identifier id: group.getClientIdentifiers()) { - transmit(new SetImposedStrategyEvent(id, group.getImposedStrategy())); - } - } - sendFacilitatorMessage("Server has imposed strategies for all groups: " + builder); - } - catch (IllegalArgumentException exception) { - sendFacilitatorMessage("Couldn't allocate strategy distribution: " + event, exception); - } } }); addEventProcessor(new EventTypeProcessor<ShowRequest>(ShowRequest.class, true) { @@ -624,6 +613,11 @@ sendFacilitatorMessage("Ignoring show request from non facilitator id: " + request.getId()); return; } + // FIXME: bah, more special casing. figure out a better way + // to determine when to send the imposed strategy.. + if (request instanceof ShowVoteScreenRequest && getCurrentRoundConfiguration().isImposedStrategyEnabled()) { + sendImposedStrategy(); + } // if this is a ShowExitInstructionsRequest, is this the last round at least? if (request instanceof ShowExitInstructionsRequest && ! getCurrentRoundConfiguration().isLastRound()) { sendFacilitatorMessage("Ignoring request to show exit instructions, we are not at the last round yet."); @@ -869,6 +863,23 @@ break; } } + + private void sendImposedStrategy() { + try { + List<GroupDataModel> groups = serverDataModel.allocateImposedStrategyDistribution(); + StringBuilder builder = new StringBuilder(); + for (GroupDataModel group: groups) { + builder.append('[').append(group).append(':').append(group.getImposedStrategy()).append(']'); + for (Identifier id: group.getClientIdentifiers()) { + transmit(new SetImposedStrategyEvent(id, group.getImposedStrategy())); + } + } + sendFacilitatorMessage("Server has imposed strategies for all groups: " + builder); + } + catch (IllegalArgumentException exception) { + sendFacilitatorMessage("Couldn't allocate strategy distribution: " + serverDataModel.getImposedStrategyDistribution(), exception); + } + } private void setupRound() { persister.initialize(getCurrentRoundConfiguration()); diff -r b4e233eb7aff92bf8ed80d699427363a2a7dbf97 -r d572fcd98734afe5bf746264acab8c548bd1f71d src/main/java/edu/asu/commons/foraging/ui/GameWindow2D.java --- a/src/main/java/edu/asu/commons/foraging/ui/GameWindow2D.java +++ b/src/main/java/edu/asu/commons/foraging/ui/GameWindow2D.java @@ -740,19 +740,24 @@ } public void showVotingScreen() { - if (votingPanel == null) { - votingPanel = new JPanel(); - votingPanel.setLayout(new BoxLayout(votingPanel, BoxLayout.Y_AXIS)); - votingInstructionsEditorPane = UserInterfaceUtils.createInstructionsEditorPane(); - votingInstructionsScrollPane = new JScrollPane(votingInstructionsEditorPane); - votingInstructionsEditorPane.setText(client.getCurrentRoundConfiguration().getVotingInstructions()); - votingPanel.add(votingInstructionsScrollPane); - votingForm = new VotingForm(client); - votingPanel.add(votingForm); - votingPanel.setName(VotingForm.NAME); - add(votingPanel); - } - showPanel(VotingForm.NAME); + SwingUtilities.invokeLater(new Runnable() { + public void run() { + if (votingPanel == null) { + votingPanel = new JPanel(); + votingPanel.setLayout(new BoxLayout(votingPanel, BoxLayout.Y_AXIS)); + votingInstructionsEditorPane = UserInterfaceUtils.createInstructionsEditorPane(); + votingInstructionsScrollPane = new JScrollPane(votingInstructionsEditorPane); + RoundConfiguration configuration = client.getCurrentRoundConfiguration(); + votingInstructionsEditorPane.setText(configuration.getVotingInstructions()); + votingPanel.add(votingInstructionsScrollPane); + votingForm = new VotingForm(client); + votingPanel.add(votingForm); + votingPanel.setName(VotingForm.NAME); + add(votingPanel); + } + showPanel(VotingForm.NAME); + } + }); } public void showVotingResults(final List<Strategy> selectedRules, final Map<Strategy, Integer> votingResults) { @@ -882,17 +887,9 @@ } public void strategyNominationSubmitted() { - setInstructions(dataModel.getRoundConfiguration().getSubmittedVoteInstructions()); + RoundConfiguration roundConfiguration = dataModel.getRoundConfiguration(); + setInstructions(roundConfiguration.getSubmittedVoteInstructions()); showInstructionsPanel(); } - public void showImposedStrategy(final Strategy strategy) { - SwingUtilities.invokeLater(new Runnable() { - @Override public void run() { - setInstructions(dataModel.getRoundConfiguration().getImposedStrategyInstructions(strategy)); - } - }); - } - - } diff -r b4e233eb7aff92bf8ed80d699427363a2a7dbf97 -r d572fcd98734afe5bf746264acab8c548bd1f71d src/main/resources/configuration/iu/2011/imposed-punish/server.xml --- a/src/main/resources/configuration/iu/2011/imposed-punish/server.xml +++ b/src/main/resources/configuration/iu/2011/imposed-punish/server.xml @@ -334,9 +334,6 @@ Below is the list of different strategies. Please read the list carefully. Press "Submit" once you have finished reviewing the strategies. </p> - <ol> - {foragingStrategies: { strategy | <li>{strategy}</li> }} - </ol> {else} <h1>Strategy Nomination Instructions</h1><hr> @@ -361,7 +358,7 @@ <h1>The Assigned Strategy</h1><hr><p>Your group has been assigned the following strategy. Every member in your group is being notified of the same strategy.</p> - <p><b> {first(selectedRules)} </b></p> + <p><b> {first(self.selectedRules)} </b></p> {else} <h1>Submitted</h1> diff -r b4e233eb7aff92bf8ed80d699427363a2a7dbf97 -r d572fcd98734afe5bf746264acab8c548bd1f71d src/main/resources/configuration/iu/2011/imposed/server.xml --- a/src/main/resources/configuration/iu/2011/imposed/server.xml +++ b/src/main/resources/configuration/iu/2011/imposed/server.xml @@ -274,7 +274,7 @@ {if (tiebreaker)} <p><b>NOTE:</b> There was a tie and the selected strategy listed here was randomly selected as the winner.</p> {endif} - <p><b> {first(selectedRules)} </b></p> + <p><b> {first(selectedStrategies)} </b></p><h2>Implementation</h2><hr><p> @@ -343,9 +343,6 @@ Below is the list of different strategies. Please read the list carefully. Press "Submit" once you have finished reviewing the strategies. </p> - <ol> - {foragingStrategies: { strategy | <li>{strategy}</li> }} - </ol> {else} <h1>Strategy Nomination Instructions</h1><hr> @@ -371,9 +368,8 @@ <h1>The Assigned Strategy</h1><hr><p>Your group has been assigned the following strategy. Every member in your group is being notified of the same strategy.</p> - <ul> - <li><b>{first(selectedRules)}</b></li> - </ul> + <p><b> {first(self.selectedRules)} </b></p> + {else} <h1>Submitted</h1><hr> Repository URL: https://bitbucket.org/virtualcommons/foraging/ -- This is a commit notification from bitbucket.org. You are receiving this because you have the service enabled, addressing the recipient of this email. |