com.speckled.specksim.imp.specks
Class AbstractSpeck

java.lang.Object
  extended by com.speckled.specksim.imp.specks.AbstractSpeck
All Implemented Interfaces:
Configurable, Speck
Direct Known Subclasses:
CollisionSpeck, FloodSpeck, NeighbourlySpeck, VelocitySpeck

public abstract class AbstractSpeck
extends java.lang.Object
implements Speck

Eases implementation by handling allocation of ids, logging control and repeatable random number generation. All Speck implementations should probably subclass this.

Author:
ryanm

Field Summary
 java.lang.String name
          The name of this speck type
protected static java.lang.String RNG_OFFSET_CONF_DESC
          The description to use for configuring the RNG seed offset
protected static java.lang.String RNG_OFFSET_CONF_NAME
          The name to use for configuring the RNG seed offset
protected static SpeckCapability simulator
          A reference to the simulator that contains this speck
 
Fields inherited from interface com.speckled.specksim.Speck
ID_BITS, SPECK_LOG_SOURCE
 
Constructor Summary
AbstractSpeck(java.lang.String name)
          Standard constructor
 
Method Summary
 java.lang.String getFlavourName()
          Gets a name for this flavour of Speck
protected  java.util.Random getRandom()
          Gets the random number generator for this speck.
static int getRandomSeedOffset()
          Gets the current value of the random seed offset.
 int id()
          Gets a unique identifier for this speck
abstract  void init()
          Called when the speck is added to the simulator, and when the simulator is reset.
 void initialise(SpeckCapability simulator)
          Called when the speck is added to the simulator, and when the simulator is reset.
 boolean isLoggingEnabled()
          Checks whether this speck should be logging events.
 void setID(int ID)
          Sets this speck's ID.
 void setLoggingEnabled(boolean b)
          Sets whether this speck should log any messages
static void setRandomSeedOffset(int offset)
          Sets the random seed offset.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface com.speckled.specksim.Speck
deInit, getState, shellEncountered
 
Methods inherited from interface com.ryanm.config.Configurable
getConfigurator
 

Field Detail

name

public final java.lang.String name
The name of this speck type


simulator

protected static SpeckCapability simulator
A reference to the simulator that contains this speck


RNG_OFFSET_CONF_NAME

protected static final java.lang.String RNG_OFFSET_CONF_NAME
The name to use for configuring the RNG seed offset

See Also:
Constant Field Values

RNG_OFFSET_CONF_DESC

protected static final java.lang.String RNG_OFFSET_CONF_DESC
The description to use for configuring the RNG seed offset

See Also:
Constant Field Values
Constructor Detail

AbstractSpeck

public AbstractSpeck(java.lang.String name)
Standard constructor

Parameters:
name - The name for this type of speck
Method Detail

id

public final int id()
Description copied from interface: Speck
Gets a unique identifier for this speck

Specified by:
id in interface Speck
Returns:
an integer identifier

getFlavourName

public final java.lang.String getFlavourName()
Description copied from interface: Speck
Gets a name for this flavour of Speck

Specified by:
getFlavourName in interface Speck
Returns:
A name for this type of Speck

setID

public final void setID(int ID)
Description copied from interface: Speck
Sets this speck's ID. Great care must be taken to ensure that all speck IDs are unique

Specified by:
setID in interface Speck
Parameters:
ID - The ID to set

initialise

public final void initialise(SpeckCapability simulator)
Description copied from interface: Speck
Called when the speck is added to the simulator, and when the simulator is reset. Would be a good place to post your events, set up your state etc.

Specified by:
initialise in interface Speck
Parameters:
simulator - The simulator that the speck is added to

init

public abstract void init()
Called when the speck is added to the simulator, and when the simulator is reset. Would be a good place to reset the speck's fields and post your events.


isLoggingEnabled

public boolean isLoggingEnabled()
Checks whether this speck should be logging events. Speck implementations should use this method before logging any messages in order to avoid saturating the log.

Returns:
true if the speck should log, false otherwise

setLoggingEnabled

public void setLoggingEnabled(boolean b)
Sets whether this speck should log any messages

Parameters:
b - true to enable logging, false to disable

getRandom

protected java.util.Random getRandom()
Gets the random number generator for this speck. The seed of the generator is always id * randomSeedOffset

Returns:
A RNG

getRandomSeedOffset

@ConfigurableVariable(value="RNG seed offset")
public static int getRandomSeedOffset()
Gets the current value of the random seed offset. This value is multiplied by the id of this speck to seed the RNG

Returns:
The random seed offset

setRandomSeedOffset

@ConfigurableVariable(value="RNG seed offset")
@Description(value="Used for generating random numbers")
@Priority(value=100)
public static void setRandomSeedOffset(int offset)
Sets the random seed offset. This value is multiplied by the id to get the seed for the RNG.

Parameters:
offset - The new offset value

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object