[gee-svn] SF.net SVN: gabel: [147] trunk/gee/src/java/edu/indiana/psych/gee
Status: Alpha
Brought to you by:
alllee
|
From: <al...@us...> - 2006-03-05 08:00:00
|
Revision: 147 Author: alllee Date: 2006-03-04 23:59:51 -0800 (Sat, 04 Mar 2006) ViewCVS: http://svn.sourceforge.net/gabel/?rev=147&view=rev Log Message: ----------- mindless hygiene. Modified Paths: -------------- trunk/gee/src/java/edu/indiana/psych/gee/AbstractExperiment.java trunk/gee/src/java/edu/indiana/psych/gee/Experiment.java trunk/gee/src/java/edu/indiana/psych/gee/ExperimentConfiguration.java trunk/gee/src/java/edu/indiana/psych/gee/forager/ForagerConfiguration.java trunk/gee/src/java/edu/indiana/psych/gee/forager/ForagerExperiment.java trunk/gee/src/java/edu/indiana/psych/gee/forager/ForagerGameState.java trunk/gee/src/java/edu/indiana/psych/gee/forager/client/ForagerClientGameState.java trunk/gee/src/java/edu/indiana/psych/gee/forager/event/ClientPositionUpdateEvent.java trunk/gee/src/java/edu/indiana/psych/gee/service/ExperimentService.java Modified: trunk/gee/src/java/edu/indiana/psych/gee/AbstractExperiment.java =================================================================== --- trunk/gee/src/java/edu/indiana/psych/gee/AbstractExperiment.java 2006-03-05 07:40:40 UTC (rev 146) +++ trunk/gee/src/java/edu/indiana/psych/gee/AbstractExperiment.java 2006-03-05 07:59:51 UTC (rev 147) @@ -1,6 +1,5 @@ package edu.indiana.psych.gee; -import java.util.Iterator; import java.util.List; import org.apache.commons.logging.Log; @@ -16,12 +15,14 @@ /** * $Id$ * - * Abstract base class for Experiments, providing convenience methods. + * Abstract base class for Experiments, providing convenience methods for + * obtaining an ExperimentConfiguration of the appropriate type, registering + * with an ExperimentService specified via Spring, * * @author <a href='al...@cs...'>Allen Lee</a> * @version $Revision$ */ -public abstract class AbstractExperiment<T extends ExperimentConfiguration> implements Experiment<T> { +public abstract class AbstractExperiment<C extends ExperimentConfiguration> implements Experiment<C> { private final Log logger = LogFactory.getLog(getClass()); private final EventChannel channel; @@ -33,7 +34,7 @@ private ExperimentService service; private String name; - private T configuration; + private C configuration; private ExperimentRoundParameters currentParameters; public AbstractExperiment() { @@ -41,11 +42,11 @@ dispatcher = DispatcherFactory.getInstance().createServerDispatcher(channel); } - public T getConfiguration() { + public C getConfiguration() { return configuration; } - public void setConfiguration(T configuration) { + public void setConfiguration(C configuration) { if (configuration == null) { throw new IllegalArgumentException("Invalid configuration"); } @@ -88,16 +89,7 @@ return getConfiguration().getConsentForm(); } - public boolean isFull() { - // TODO Auto-generated method stub - return false; - } - public String toString() { - return String.format("[ %s ] experiment [ %s ] running on port %d", - super.toString(), getName(), getServerPort()); - } - public String getDescription() { return getConfiguration().getDescription(); } @@ -122,6 +114,17 @@ return running; } + public boolean isFull() { + // TODO Auto-generated method stub + return false; + } + + public String toString() { + return String.format("[ %s ] experiment [ %s ] running on port %d", + super.toString(), getName(), getServerPort()); + } + + /** * Returns the special instructions to be used for the next available * round. If this is the first time this method has been called when the @@ -167,7 +170,7 @@ } protected Duration getDelayBetweenRounds() { - return configuration.getDelayBetweenRounds(); + return getConfiguration().getDelayBetweenRounds(); } protected EventChannel getEventChannel() { Modified: trunk/gee/src/java/edu/indiana/psych/gee/Experiment.java =================================================================== --- trunk/gee/src/java/edu/indiana/psych/gee/Experiment.java 2006-03-05 07:40:40 UTC (rev 146) +++ trunk/gee/src/java/edu/indiana/psych/gee/Experiment.java 2006-03-05 07:59:51 UTC (rev 147) @@ -12,7 +12,7 @@ * @version $Revision$ */ -public interface Experiment<T extends ExperimentConfiguration> { +public interface Experiment<C extends ExperimentConfiguration> { public static final Experiment NULL = new Experiment() { private final Log logger = LogFactory.getLog(getClass()); @@ -67,8 +67,8 @@ public boolean isFull(); public boolean isRunning(); - public T getConfiguration(); - public void setConfiguration(T configuration); + public C getConfiguration(); + public void setConfiguration(C configuration); // interface State { // public final static State WAITING = new State() { Modified: trunk/gee/src/java/edu/indiana/psych/gee/ExperimentConfiguration.java =================================================================== --- trunk/gee/src/java/edu/indiana/psych/gee/ExperimentConfiguration.java 2006-03-05 07:40:40 UTC (rev 146) +++ trunk/gee/src/java/edu/indiana/psych/gee/ExperimentConfiguration.java 2006-03-05 07:59:51 UTC (rev 147) @@ -14,7 +14,7 @@ * @version $Revision$ */ -public interface ExperimentConfiguration<T extends ExperimentRoundParameters> { +public interface ExperimentConfiguration<P extends ExperimentRoundParameters> { /** @@ -34,9 +34,9 @@ public boolean isTriggeredExperiment(); - public List<T> getAllParameters(); + public List<P> getAllParameters(); - public T getCurrentParameters(); + public P getCurrentParameters(); public String getDescription(); Modified: trunk/gee/src/java/edu/indiana/psych/gee/forager/ForagerConfiguration.java =================================================================== --- trunk/gee/src/java/edu/indiana/psych/gee/forager/ForagerConfiguration.java 2006-03-05 07:40:40 UTC (rev 146) +++ trunk/gee/src/java/edu/indiana/psych/gee/forager/ForagerConfiguration.java 2006-03-05 07:59:51 UTC (rev 147) @@ -2,17 +2,12 @@ import java.util.List; -import javax.persistence.DiscriminatorValue; import javax.persistence.Entity; -import javax.persistence.Inheritance; -import javax.persistence.InheritanceType; import javax.persistence.JoinColumn; import javax.persistence.OneToMany; -import javax.persistence.OrderBy; import javax.persistence.PrimaryKeyJoinColumn; import javax.persistence.Table; import javax.persistence.Transient; -import javax.persistence.*; import org.hibernate.annotations.Proxy; @@ -41,6 +36,10 @@ super(serverName, port); } + public static ForagerConfiguration getDefaultConfiguration() { + return new ForagerConfiguration("groups.psych.indiana.edu", 25000); + } + @Transient public Duration getDelayBetweenRounds() { return Duration.create(30); Modified: trunk/gee/src/java/edu/indiana/psych/gee/forager/ForagerExperiment.java =================================================================== --- trunk/gee/src/java/edu/indiana/psych/gee/forager/ForagerExperiment.java 2006-03-05 07:40:40 UTC (rev 146) +++ trunk/gee/src/java/edu/indiana/psych/gee/forager/ForagerExperiment.java 2006-03-05 07:59:51 UTC (rev 147) @@ -26,7 +26,6 @@ import edu.indiana.psych.gee.net.ClientReadyEvent; import edu.indiana.psych.gee.net.ConnectionEvent; import edu.indiana.psych.gee.net.Dispatcher; -import edu.indiana.psych.gee.service.ExperimentService; import edu.indiana.psych.gee.time.Duration; import edu.indiana.psych.gee.util.GeeUtils; Modified: trunk/gee/src/java/edu/indiana/psych/gee/forager/ForagerGameState.java =================================================================== --- trunk/gee/src/java/edu/indiana/psych/gee/forager/ForagerGameState.java 2006-03-05 07:40:40 UTC (rev 146) +++ trunk/gee/src/java/edu/indiana/psych/gee/forager/ForagerGameState.java 2006-03-05 07:59:51 UTC (rev 147) @@ -2,7 +2,6 @@ import java.awt.Point; import java.util.HashMap; -import java.util.Iterator; import java.util.Map; import edu.indiana.psych.gee.Identifier; @@ -30,7 +29,8 @@ // Map<Identifier, ClientState> used to maintain the set of // currently connected clients, their positions and other useful // information. - protected final Map clients = new HashMap(); + protected final Map<Identifier, ClientState> clients = + new HashMap<Identifier, ClientState>(); protected ForagerGameState(EventChannel channel) { this.channel = channel; @@ -62,30 +62,27 @@ } /** - * Returns a Map<Identifier, Point> representing the latest client - * positions. + * Returns a Map associating client Identifiers and Point locations + * representing the latest client positions. */ - public synchronized Map getClientPositions() { - Map positions = new HashMap(); - for (Iterator iter = clients.values().iterator(); iter.hasNext(); ) { - ClientState clientState = (ClientState) iter.next(); + public synchronized Map<Identifier, Point> getClientPositions() { + Map<Identifier, Point> positions = new HashMap<Identifier, Point>(); + for (ClientState clientState : clients.values()) { positions.put(clientState.id(), clientState.getPosition()); } return positions; } public String toString() { - StringBuffer buffer = new StringBuffer(); + StringBuilder buffer = new StringBuilder(); int clientId = 0; - synchronized (clients) { - for (Iterator iterator = clients.values().iterator(); iterator.hasNext(); ) { - ClientState state = (ClientState) iterator.next(); + for (ClientState state : clients.values()) { Point position = state.getPosition(); buffer.append(state.getFoodEaten()).append(' '); buffer.append(position.x).append(' ').append(position.y).append(' '); buffer.append(state.getFoodEaten()).append(' '); - clientId++; + buffer.append(clientId++); } } @@ -95,16 +92,16 @@ // now we just need to associate a brightness-over-time with a particular // client. public int getFoodEatenFor(Identifier id) { - ClientState state = (ClientState) clients.get(id); + ClientState state = clients.get(id); if (state == null) { - // FIXME: perhaps we should just return 0 instead. + // FIXME: perhaps we should just return 0 instead? throw new IllegalArgumentException("no client state available for: " + id); } return state.getFoodEaten(); } public boolean hasClientEaten(Identifier id) { - ClientState clientState = (ClientState) clients.get(id); + ClientState clientState = clients.get(id); if (clientState == null) { error("asked if client [" + id + "] ate, but this client doesn't exist."); Modified: trunk/gee/src/java/edu/indiana/psych/gee/forager/client/ForagerClientGameState.java =================================================================== --- trunk/gee/src/java/edu/indiana/psych/gee/forager/client/ForagerClientGameState.java 2006-03-05 07:40:40 UTC (rev 146) +++ trunk/gee/src/java/edu/indiana/psych/gee/forager/client/ForagerClientGameState.java 2006-03-05 07:59:51 UTC (rev 147) @@ -3,7 +3,6 @@ import java.awt.Point; import java.util.Collections; import java.util.HashSet; -import java.util.Iterator; import java.util.Map; import java.util.Set; @@ -24,7 +23,7 @@ public class ForagerClientGameState extends ForagerGameState { - private Set foodPositions = new HashSet(); + private Set<Point> foodPositions = new HashSet<Point>(); public ForagerClientGameState(EventChannel channel) { super(channel); @@ -38,18 +37,17 @@ /** * Returns a Set<Point> containing the positions of food pellets. */ - public synchronized Set getFoodPositions() { + public synchronized Set<Point> getFoodPositions() { return Collections.unmodifiableSet(foodPositions); } public synchronized void initialize(RoundStartedEvent event) { clear(); // Map<Identifier, Point> - Map positions = event.getInitialData(); - for (Iterator iter = positions.entrySet().iterator(); iter.hasNext(); ) { - Map.Entry entry = (Map.Entry) iter.next(); - Identifier id = (Identifier) entry.getKey(); - Point point = (Point) entry.getValue(); + Map<Identifier, Point> positions = event.getInitialData(); + for (Map.Entry<Identifier, Point> entry : positions.entrySet()) { + Identifier id = entry.getKey(); + Point point = entry.getValue(); addClient(id, point); } } @@ -61,25 +59,22 @@ */ public synchronized void update(ClientPositionUpdateEvent event, ForagerGameWindow window) { // incoming client positions are Map<Identifier, Point> - Map clientPositions = event.getClientPositions(); + Map<Identifier, Point> clientPositions = event.getClientPositions(); // incoming foodPositions is just a Set of Points (which is all the // client needs to know) foodPositions = event.getFoodPositions(); // update all client positions. - for (Iterator iterator = clientPositions.entrySet().iterator(); - iterator.hasNext(); ) - { - Map.Entry entry = (Map.Entry) iterator.next(); - Identifier id = (Identifier) entry.getKey(); - Point point = (Point) entry.getValue(); - ClientState clientState = (ClientState) clients.get(id); + for (Map.Entry<Identifier, Point> entry : clientPositions.entrySet()) { + Identifier id = entry.getKey(); + Point point = entry.getValue(); + ClientState clientState = clients.get(id); if (clientState == null) { throw new IllegalStateException("clients didn't contain id: " + id); } clientState.setPosition(point); } if (event.wasFoodConsumed()) { - ClientState clientState = (ClientState) clients.get(event.id()); + ClientState clientState = clients.get(event.id()); clientState.incrementFoodEaten(); // FIXME: drop an event into the event channel to signal the ForagerGameWindow // instead of doing this. @@ -87,6 +82,7 @@ } // double bytesPerSecond = getBytesPerSecond(event.getNumberOfBytes(), event.getCreationTime()); window.update(); + clientPositions.clear(); } // private double getBytesPerSecond(int numberOfBytes, long creationTime) { Modified: trunk/gee/src/java/edu/indiana/psych/gee/forager/event/ClientPositionUpdateEvent.java =================================================================== --- trunk/gee/src/java/edu/indiana/psych/gee/forager/event/ClientPositionUpdateEvent.java 2006-03-05 07:40:40 UTC (rev 146) +++ trunk/gee/src/java/edu/indiana/psych/gee/forager/event/ClientPositionUpdateEvent.java 2006-03-05 07:59:51 UTC (rev 147) @@ -20,7 +20,6 @@ * * Can also maintain a list of food positions, if the food is visible * - * @author Andy Jones * @author Allen Lee * @version $Revision$ */ @@ -28,22 +27,21 @@ implements ExperimentUpdateEvent { private static final long serialVersionUID = -128693557750400520L; - // Map<Identifier, Point> - private final Map clientPositions; - private final Set foodPositions; + private final Map<Identifier, Point> clientPositions; + private final Set<Point> foodPositions; private final boolean foodEaten; // private int numberOfBytes; - public ClientPositionUpdateEvent(Identifier clientId, Map positions) { + public ClientPositionUpdateEvent(Identifier clientId, Map<Identifier, Point> positions) { this(clientId, false, positions); } - public ClientPositionUpdateEvent(Identifier clientId, boolean foodEaten, Map positions) { + public ClientPositionUpdateEvent(Identifier clientId, boolean foodEaten, Map<Identifier, Point> positions) { this(clientId, foodEaten, positions, Collections.EMPTY_SET); } - public ClientPositionUpdateEvent(Identifier clientId, boolean foodEaten, Map positions, Set foodPositions) { + public ClientPositionUpdateEvent(Identifier clientId, boolean foodEaten, Map<Identifier, Point> positions, Set<Point> foodPositions) { super(clientId); this.foodEaten = foodEaten; this.clientPositions = positions; @@ -55,11 +53,11 @@ return foodEaten; } - public Map getClientPositions() { + public Map<Identifier, Point> getClientPositions() { return clientPositions; } - public Set getFoodPositions() { + public Set<Point> getFoodPositions() { return foodPositions; } @@ -72,7 +70,7 @@ } public Point getClientPosition(Identifier id) { - return (Point) clientPositions.get(id); + return clientPositions.get(id); } Modified: trunk/gee/src/java/edu/indiana/psych/gee/service/ExperimentService.java =================================================================== --- trunk/gee/src/java/edu/indiana/psych/gee/service/ExperimentService.java 2006-03-05 07:40:40 UTC (rev 146) +++ trunk/gee/src/java/edu/indiana/psych/gee/service/ExperimentService.java 2006-03-05 07:59:51 UTC (rev 147) @@ -9,8 +9,6 @@ import org.apache.commons.logging.LogFactory; import edu.indiana.psych.gee.Experiment; -import edu.indiana.psych.gee.forager.ForagerConfiguration; -import edu.indiana.psych.gee.forager.ForagerExperiment; /** * $Id$ This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |