org.moyoman.framework
Class Scheduler

java.lang.Object
  |
  +--org.moyoman.framework.Scheduler
All Implemented Interfaces:
Cloneable, Serializable

public class Scheduler
extends Object
implements Cloneable, Serializable

This class controls operations on the modules for a single game. These operations include the client making a move, the server making a move, or other operations such as the server suggesting a move for the client.

See Also:
Serialized Form

Field Summary
private  DAG dag
          The DAG object associated with this Scheduler.
private  GameId id
          The id of the current game.
private  Mode mode
          The mode of this game, e.g, FAST.
private  HashMap modules
          The name is a ModuleType object, the value a Module object.
private  HashMap nameToType
          The name is a ModuleName object, the value is a ModuleType object.
private static HashMap schedulers
          Key is a GameId object, value is a Scheduler object.
 
Constructor Summary
protected Scheduler(GameId id, Mode mode, Handicap handicap, Rules rules, float komi)
          Create a Scheduler object.
protected Scheduler(GameId id, Mode mode, Rules rules, float komi)
          Create a Scheduler object.
protected Scheduler(GameId id, Module[] mods, Mode mode)
          Create a Scheduler object.
 
Method Summary
private static void addPrerequisiteModules(Scheduler sch, HashSet hs, Module m)
          Get all of the modules which are dependent on the given module.
 Object clone()
          Override the Object.clone method.
 MoveDescriptor generateMove()
          Have the server generate a move.
protected  DAG getDAG()
          Get the DAG associated with this Scheduler.
protected  GameId getGameId()
          This method returns the game id.
 Module getModule(ModuleName name)
          Get a copy of a module of the specified name.
 Module getModule(ModuleType type)
          Get a copy of a module of the specified type.
 Module[] getModules()
          Get a copy of all of the modules.
protected  short getMoveNumber()
          Get the number of the move to be made next.
private  Module[] getRequired(Module module)
          Get the required modules for a given module.
static Scheduler getScheduler(GameId id, ModuleName name)
          Get a copy of the Scheduler object.
 Module getUnclonedModule(ModuleName name)
          Get the module of the specified name.
protected  Module getUnclonedModule(ModuleType type)
          Get the module of the given module type.
protected  Module[] getUnclonedModules()
          Get an array of all of the modules.
 MoveDescriptor makeMove(Move move)
          This method adds a move to the game modules.
protected  MoveDescriptor makePass()
          The move is made which is a pass.
protected  void setInitialBoard(Stone[] stones)
          Set the initial board position.
protected  MoveDescriptor suggestMove()
          Have the server suggest a move for the client.
 MoveDescriptor validateMove(Move move)
          This method validates a move.
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

schedulers

private static HashMap schedulers
Key is a GameId object, value is a Scheduler object.


dag

private DAG dag
The DAG object associated with this Scheduler.


mode

private Mode mode
The mode of this game, e.g, FAST.


modules

private HashMap modules
The name is a ModuleType object, the value a Module object.


nameToType

private HashMap nameToType
The name is a ModuleName object, the value is a ModuleType object.


id

private GameId id
The id of the current game.

Constructor Detail

Scheduler

protected Scheduler(GameId id,
                    Mode mode,
                    Handicap handicap,
                    Rules rules,
                    float komi)
             throws InternalErrorException
Create a Scheduler object. A Scheduler object can only be created directly by another class within the framework package. Modules that need a Scheduler object should call the getScheduler() static method.

Parameters:
id - The id of the game.
mode - The mode of the game.
handicap - The handicap for the game.
rules - The rule set to play the game
komi - The komi for the game.
Throws:
InternalErrorException - Thrown if an error occurs for any reason.

Scheduler

protected Scheduler(GameId id,
                    Mode mode,
                    Rules rules,
                    float komi)
             throws InternalErrorException
Create a Scheduler object. A Scheduler object can only be created directly by another class within the framework package. Modules that need a Scheduler object should call the getScheduler() static method.

Parameters:
id - The id of the game.
mode - The mode of the game.
rules - The rule set to play the game
komi - The komi for the game.
Throws:
InternalErrorException - Thrown if an error occurs for any reason.

Scheduler

protected Scheduler(GameId id,
                    Module[] mods,
                    Mode mode)
             throws InternalErrorException
Create a Scheduler object. This constructor is used to make a copy of itself except that only the modules in the mods parameter are used.

So for example, a LifeAndDeath module might want to get a Scheduler object in which the terminating module was LifeAndDeath, not MoveGenerator. If all of the modules are in the mods parameter, then this method does the same thing as the clone() method.

Parameters:
id - The id of the game to restore.
mods - The Module objects that represent the game.
mode - The mode of the game.
Throws:
InternalErrorException - Thrown if the operation fails for any reason.
Method Detail

generateMove

public MoveDescriptor generateMove()
                            throws Exception
Have the server generate a move.

Returns:
The server generated move.
Throws:
Exception - Thrown if the operation fails for any reason.

getMoveNumber

protected short getMoveNumber()
                       throws InternalErrorException
Get the number of the move to be made next. This would be 1 at the very beginning of the game.

Returns:
The move number.
InternalErrorException

suggestMove

protected MoveDescriptor suggestMove()
                              throws Exception
Have the server suggest a move for the client.

Returns:
The server generated move.
Exception

getModule

public Module getModule(ModuleType type)
                 throws NoSuchDataException
Get a copy of a module of the specified type.

Parameters:
type - The type of the module to get.
Returns:
A clone of the specified module.
Throws:
NoSuchDataException - Thrown if the module type is not contained by the scheduler.

getUnclonedModule

protected Module getUnclonedModule(ModuleType type)
                            throws NoSuchDataException
Get the module of the given module type. This method should be used instead of getModule() if some read only type operation is being performed, and the overhead of cloning is not required.

Parameters:
type - The type of the module to get.
Returns:
A Module object of the given type.
Throws:
NoSuchDataException - Thrown if the module type is not contained by the scheduler.

getModule

public Module getModule(ModuleName name)
                 throws NoSuchDataException
Get a copy of a module of the specified name.

Parameters:
name - The name of the module to get.
Returns:
A clone of the specified module.
Throws:
NoSuchDataException - Thrown if the module name is not contained by the scheduler.

getUnclonedModule

public Module getUnclonedModule(ModuleName name)
                         throws NoSuchDataException
Get the module of the specified name. This method should be used instead of getModule() if some read only type operation is being performed, and the overhead of cloning is not required.

Parameters:
name - The name of the module to get.
Returns:
A Module object of the given name.
Throws:
NoSuchDataException - Thrown if the module name is not contained by the scheduler.

getModules

public Module[] getModules()
Get a copy of all of the modules.

Returns:
An array containing all of the modules.

getUnclonedModules

protected Module[] getUnclonedModules()
Get an array of all of the modules. This method should be used instead of getModules() if some read only type operation is being performed, and the overhead of cloning is not required.

Returns:
An array containing all of the modules.

getGameId

protected GameId getGameId()
This method returns the game id.

Returns:
The id of the current game.

getDAG

protected DAG getDAG()
Get the DAG associated with this Scheduler.

Returns:
The DAG.

getScheduler

public static Scheduler getScheduler(GameId id,
                                     ModuleName name)
                              throws NoSuchDataException,
                                     InternalErrorException
Get a copy of the Scheduler object. It is not an exact copy, because it only contains modules which terminate in the module specified by the name parameter.

This method would be used by a module which was doing search based processing.

Parameters:
id - The id of the game.
name - The name of the terminating module.
Returns:
The new Scheduler object.
Throws:
NoSuchDataException - Thrown if the module name is not present in the Scheduler.
InternalErrorException - Thrown if the operation fails for any reason.

addPrerequisiteModules

private static void addPrerequisiteModules(Scheduler sch,
                                           HashSet hs,
                                           Module m)
Get all of the modules which are dependent on the given module. The end result of this method is that all the prerequisite modules are added to the HashSet parameter. These are the actual Module objects from the Scheduler, and not clones.

Parameters:
sch - The Scheduler object.
hs - The HashSet to add the Module objects to.
m - The Module from which prerequisite modules are determined.

makeMove

public MoveDescriptor makeMove(Move move)
                        throws Exception
This method adds a move to the game modules. This method can be called either when the client has made a move, or when the server has made a move and the module objects need to be updated.

Parameters:
move - The move to be made.
Returns:
A MoveDescriptor object for the move.
Throws:
Exception - Thrown if any of the modules throw an exception.

validateMove

public MoveDescriptor validateMove(Move move)
                            throws Exception
This method validates a move. This method is called by a player whose moves may not be legal. This method will throw an exception if the move is not a legal one.

Parameters:
move - The move to be made.
Returns:
A MoveDescriptor object for the move.
Throws:
Exception - Thrown if the move is illegal, or an error occurs for any reason.

getRequired

private Module[] getRequired(Module module)
Get the required modules for a given module. The modules are cloned so that the module is allowed to modify them.

Parameters:
module - The module for which the prerequisite modules are gotten.
Returns:
An array of Module objects.

makePass

protected MoveDescriptor makePass()
                           throws Exception
The move is made which is a pass. This method can be called either when the client passes or when the server passes.

Returns:
A MoveDescriptor which describes the move.
Exception

setInitialBoard

protected void setInitialBoard(Stone[] stones)
                        throws InternalErrorException
Set the initial board position. Generally speaking, this method will be called to place handicap stones, but if for some reason one module needs to replace another, then this method can be called to initialize it.

Parameters:
stones - The initial board position.
Throws:
InternalErrorException - Thrown if the operation fails for any reason.

clone

public Object clone()
Override the Object.clone method.

Overrides:
clone in class Object
Returns:
A Scheduler object which is a clone of this one.