cz.cuni.pogamut.experiments
Class Experiment

java.lang.Object
  extended by cz.cuni.pogamut.experiments.AbstractExperimentHandle
      extended by cz.cuni.pogamut.experiments.Experiment
All Implemented Interfaces:
ExperimentHandle, LocalExperimentHandle
Direct Known Subclasses:
BasicExperiment

public abstract class Experiment
extends AbstractExperimentHandle
implements LocalExperimentHandle

Provides common infrastructure for creating new types of experiments. You have to override three stage* methods to write your own type of experiment. These methods will be called by the framework, don't call them by yourself as it may result in unpredictable behaviour. Call setResult() to finish your experiment when the result was computed otherwise call handleException() exit without returning the result. Don't call stageThreeFinis() to quit your experiment. Framework will call this method from the two mentioned above.

Possible orders of execution states:

Experiment can also be PUASED at any time.


Field Summary
private  ExperimentDescriptor descriptor
          Descriptor used to initialize this experiment.
protected  Flag<ExperimentExecutionState> experimentState
          Flag with actual state of experiment.
private  java.lang.Thread experimentThread
          Note that this thread usually terminates after the experiment is started, this serves only for the startup phase to complete in separate thread.
private  java.util.logging.Logger log
          Log of the experiment.
private  UTServer server
          Interface to the world of UT2004.
(package private)  FlagListener<UTServerState> serverStateListener
          UTServer state listener, if server state is switched to PAUSED then pause the experiment as well and vice versa.
protected  TimeProvider timeProvider
          Object for retrieving actual time of sim.
protected  Timer timer
          Timer used to schedule events in experiment.
 
Fields inherited from class cz.cuni.pogamut.experiments.AbstractExperimentHandle
exception, experimentRunningLatch, result, stateFlag
 
Constructor Summary
protected Experiment(ExperimentDescriptor descriptor, UTServer server)
          Creates a new instance of Experiment
 
Method Summary
 ExperimentDescriptor getDescriptor()
           
 Experiment getExperiment()
          Returns this.
 Flag<ExperimentExecutionState> getExperimentExecutionFlag()
          Execution state of the experiment.
 java.util.logging.Logger getLogger()
           
 java.lang.String getName()
          Return name of the experiment.
 UTServer getServer()
           
 TimeProvider getTimeProvider()
           
protected  void handleException(java.lang.Exception e)
          Writes exception to the log and stops experiment (sets state to FAILED).
protected  void handleReplay()
          Starts replay recording if it is set in the descriptor.
 void runExperiment()
          Starts the experiment.
private  void setExperimentState(ExperimentExecutionState state)
           
 void setResultAndTerminate(ExperimentResult result)
          Sets result of this experiment and TERMINATE the experiment.
 void schedule(TimerTask task, long delay)
          Schedules task for execution.
protected abstract  void stageOneInit()
          Initializes all resources that the experiment may need (eg. rule engine).
protected abstract  void stageThreeFinish()
          Release resources used by the experiment, should stop running threads etc.
protected abstract  void stageTwoStart()
          Runs the experiment, always called after stageOneInit().
 void stop()
          Aborts execution of the experiment, disconnect all bot and calls stageThreeFinish().
 
Methods inherited from class cz.cuni.pogamut.experiments.AbstractExperimentHandle
getException, getResult
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface cz.cuni.pogamut.experiments.ExperimentHandle
getException, getResult
 

Field Detail

timeProvider

protected TimeProvider timeProvider
Object for retrieving actual time of sim.


timer

protected Timer timer
Timer used to schedule events in experiment.


server

private UTServer server
Interface to the world of UT2004.


descriptor

private ExperimentDescriptor descriptor
Descriptor used to initialize this experiment.


serverStateListener

FlagListener<UTServerState> serverStateListener
UTServer state listener, if server state is switched to PAUSED then pause the experiment as well and vice versa.


experimentThread

private java.lang.Thread experimentThread
Note that this thread usually terminates after the experiment is started, this serves only for the startup phase to complete in separate thread.


log

private java.util.logging.Logger log
Log of the experiment.


experimentState

protected Flag<ExperimentExecutionState> experimentState
Flag with actual state of experiment.

Constructor Detail

Experiment

protected Experiment(ExperimentDescriptor descriptor,
                     UTServer server)
Creates a new instance of Experiment

Method Detail

stageOneInit

protected abstract void stageOneInit()
                              throws java.lang.Exception
Initializes all resources that the experiment may need (eg. rule engine). This method is called from runExperiment()

Throws:
java.lang.Exception

stageTwoStart

protected abstract void stageTwoStart()
                               throws java.lang.Exception
Runs the experiment, always called after stageOneInit(). This method is called from runExperiment()

Throws:
java.lang.Exception

stageThreeFinish

protected abstract void stageThreeFinish()
Release resources used by the experiment, should stop running threads etc. Called either when the experiment finishes or when it fails (even if exception was raised).


runExperiment

public final void runExperiment()
Starts the experiment. This method is nonblocking, results can be fetched by getResult() method. Experiment is executed in it's own thread.


handleReplay

protected void handleReplay()
                     throws PogamutException
Starts replay recording if it is set in the descriptor.

Throws:
PogamutException

setResultAndTerminate

public void setResultAndTerminate(ExperimentResult result)
Sets result of this experiment and TERMINATE the experiment. Should be called before the end of the experiment.


schedule

public void schedule(TimerTask task,
                     long delay)
Schedules task for execution.


getExperiment

public Experiment getExperiment()
Returns this. This method is present because Experiment implements LocalExperimentHandle interface.

Specified by:
getExperiment in interface LocalExperimentHandle

getExperimentExecutionFlag

public Flag<ExperimentExecutionState> getExperimentExecutionFlag()
Execution state of the experiment.

Specified by:
getExperimentExecutionFlag in interface ExperimentHandle
Overrides:
getExperimentExecutionFlag in class AbstractExperimentHandle
Returns:
Flag with execution state of experiment.

getLogger

public java.util.logging.Logger getLogger()
Specified by:
getLogger in interface LocalExperimentHandle
Returns:
Logger of this experiment.

setExperimentState

private void setExperimentState(ExperimentExecutionState state)

stop

public void stop()
Aborts execution of the experiment, disconnect all bot and calls stageThreeFinish().

Specified by:
stop in interface ExperimentHandle

handleException

protected void handleException(java.lang.Exception e)
Writes exception to the log and stops experiment (sets state to FAILED).


getTimeProvider

public TimeProvider getTimeProvider()

getName

public java.lang.String getName()
Return name of the experiment.

Specified by:
getName in interface ExperimentHandle

getServer

public UTServer getServer()

getDescriptor

public ExperimentDescriptor getDescriptor()