Class Experiment
- java.lang.Object
-
- jasima.core.experiment.Experiment
-
- All Implemented Interfaces:
Notifier<Experiment,Experiment.ExperimentEvent>,ValueStore,Serializable,Cloneable
- Direct Known Subclasses:
AbstractMultiExperiment,SimulationExperiment
public abstract class Experiment extends Object implements Notifier<Experiment,Experiment.ExperimentEvent>, ValueStore, Cloneable, Serializable
An Experiment is something that produces results depending on various parameters. The usual lifecycle is to create an experiment, set parameters to their proper values, execute the experiment by calling it'srunExperiment()method. After execution a set of results are available using thegetResults()method.Experiments are not supposed to be executed more than once, e.g., to run multiple replications of an experiment (see
MultipleReplicationExperiment) you have to create multiple instances. Therefore experiments should be cloneable.This class is intended as the base class for Experiments doing something useful. Besides a name this class only has a single parameter "initialSeed" (see
getInitialSeed()/setInitialSeed(long)). This parameter is supposed to be used as the starting value for all (pseudo) random number generation activities at experiment runtime. This means two experiments having the sameinitialSeedand all other experiment parameters being the same should behave deterministically and produce exactly the same results.The only results produced by this class are "runTime" (type Double; measuring the real time required to execute an experiment) and "expAborted" (type Integer, a value >0 indicates some problems causing early termination).
Experiments can have listeners registered (derived from
ExperimentListener), which are informed of various events such as an experiment's start and completion and can be used by subclasses to provide additional events.- Author:
- Torsten Hildebrandt
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceExperiment.ExperimentEventstatic classExperiment.ExperimentStatestatic classExperiment.UniqueNamesCheckingHashMapDeprecated.
-
Field Summary
Fields Modifier and Type Field Description protected intabortedstatic longDEFAULT_SEEDJust an arbitrary default seed.protected Throwableerrorstatic StringEXCEPTIONstatic StringEXCEPTION_MESSAGEstatic StringEXP_ABORTEDprotected Map<String,Object>resultMapstatic StringRUNTIME
-
Constructor Summary
Constructors Constructor Description Experiment()
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description voidabort()Marks experiment execution to be aborted by some error condition.protected voidaddErrorResults()protected voidaddStandardResults()protected voidafterRun()This method is called immediately afterperformRun(), but beforedone().protected voidbeforeRun()This method is called immediately beforeperformRun(), but afterinit().voidcancel()Requests the experiment to cancel its execution prematurely.protected voidcheckCancelledOrInterrupted()Checks whether the experiment was requested to cancel or the executing Thread was interrupted.Experimentclone()protected voiddone()This method can be overridden to perform any required clean-up.protected ExperimentCompletableFutureexecuteSubExperiment(Experiment sub)This is a convenience method to run a sub experiment without having to worry aboutExperimentExecutorandnestingLevel.protected voidfinalActions()This method can be used for actions like clean-up that should be done irrespectively of whether an exception occurred during experiment execution or not.protected voidfinish()This method gives experiments and listeners a chance to view/modify results.ThrowablegetError()longgetInitialSeed()MsgCategorygetLogLevel()StringgetName()Map<String,Object>getPropsWithValues()Retrieves a map containing the name and current value for each of this experiment's properties.Map<String,Object>getResults()Returns the result map produced when executing this experiment.Experiment.ExperimentStategetState()protected voidhandleExecutionError(Throwable t)Lifecycle method that is executed if there was an Exception during experiment execution.protected voidinit()This method is called to perform any initializations required before the experiment is run.booleanisCancelled()Checks whether the experiment was requested tocancel()its execution.static voidmain(String... args)intnestingLevel()The level in the call hierarchy this experiment is executed in.voidnestingLevel(int nestingLevel)Sets the nesting level.NotifierImpl<Experiment,Experiment.ExperimentEvent>notifierImpl()protected abstract voidperformRun()Contains the code to actually do something useful.voidprint(MsgCategory category, String message)Triggers a print event of the given category.voidprint(MsgCategory category, String messageFormat, Object... params)Triggers a print event of the given category.voidprint(String message)Triggers a print event of category "info".voidprint(String messageFormat, Object... params)Same asprint(MsgCategory, String, Object...), just defaulting to the categoryINFO.voidprintResults()Prints the results of this experiment toSystem.out.protected voidproduceResults()Populates the result mapresultMapwith values produced during experiment execution.protected ObjectreadResolve()protected voidrequireState(Experiment.ExperimentState expected)Checks, if an experiment is in a certain state.Map<String,Object>runExperiment()Runs the experiment in a synchronous way.ExperimentCompletableFuturerunExperimentAsync()Trigger asynchronous execution of the experiment in the default thread pool.ExperimentCompletableFuturerunExperimentAsync(ExecutorService pool)Call therunExperiment()method in an asynchronous way.protected Map<String,Object>runExperimentInternal()Runs the experiment.protected doublerunTimeReal()Returns the run time (in seconds) of an Experiment.voidsetInitialSeed(long initialSeed)Sets the initial seed for this experiment.voidsetLogLevel(MsgCategory logLevel)Set the maximum level of logging messages that are supposed to be printed (e.g.voidsetName(String name)Set some descriptive name for this experiment.protected voidstarting()This method is called to perform any work that needs to be performed before the experiment can be initialized.ObservableValue<Experiment.ExperimentState>state()StringtoString()ValueStorevalueStoreImpl()Returns the implementation to use for adding ValueStore functionality.-
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface jasima.core.util.observer.Notifier
addListener, addListener, addListener, fire, fire, getListener, numListener, removeCurrentListener, removeListener
-
Methods inherited from interface jasima.core.util.ValueStore
valueStoreContains, valueStoreGet, valueStoreGet, valueStoreGetAllKeys, valueStoreGetNumKeys, valueStorePut, valueStoreRemove, valueStoreUpdate
-
-
-
-
Field Detail
-
DEFAULT_SEED
public static final long DEFAULT_SEED
Just an arbitrary default seed.- See Also:
- Constant Field Values
-
RUNTIME
public static final String RUNTIME
- See Also:
- Constant Field Values
-
EXP_ABORTED
public static final String EXP_ABORTED
- See Also:
- Constant Field Values
-
EXCEPTION
public static final String EXCEPTION
- See Also:
- Constant Field Values
-
EXCEPTION_MESSAGE
public static final String EXCEPTION_MESSAGE
- See Also:
- Constant Field Values
-
aborted
protected transient volatile int aborted
-
error
protected transient Throwable error
-
-
Method Detail
-
starting
protected void starting()
This method is called to perform any work that needs to be performed before the experiment can be initialized. It is called before theinit()method and can be used to perform additional parameter checks, for instance.
-
init
protected void init()
This method is called to perform any initializations required before the experiment is run.
-
beforeRun
protected void beforeRun()
This method is called immediately beforeperformRun(), but afterinit().
-
performRun
protected abstract void performRun()
Contains the code to actually do something useful. This is the only abstract method that sub-classes are required to implement.
-
afterRun
protected void afterRun()
This method is called immediately afterperformRun(), but beforedone().
-
done
protected void done()
This method can be overridden to perform any required clean-up. It is executed immediately afterafterRun(), but beforeproduceResults()andfinish().
-
produceResults
protected void produceResults()
Populates the result mapresultMapwith values produced during experiment execution. The Experiment always adds the two results "runTime" and "expAborted".
-
finish
protected void finish()
This method gives experiments and listeners a chance to view/modify results. It is called afterproduceResults().
-
runExperiment
public Map<String,Object> runExperiment()
Runs the experiment in a synchronous way. This is the main method to call in order to execute an experiment. Sub-classes normally don't have to overwrite this method but create customized behavior by overriding one of the life-cycle methods likeinit(),beforeRun(),performRun()(this one is required),done(),produceResults()orfinish().- Returns:
- The results of experiment execution.
- See Also:
runExperimentAsync()
-
runExperimentAsync
public ExperimentCompletableFuture runExperimentAsync(ExecutorService pool)
Call therunExperiment()method in an asynchronous way.- Parameters:
pool- TheExecutorServiceto use.- Returns:
- A
CompletableFutureto obtain experiment results. - See Also:
runExperiment(),runExperimentInternal(),runExperimentAsync()
-
runExperimentAsync
public ExperimentCompletableFuture runExperimentAsync()
Trigger asynchronous execution of the experiment in the default thread pool.- Returns:
- A
Futureto obtain experiment results. - See Also:
runExperiment(),runExperimentAsync(ExecutorService)
-
runExperimentInternal
protected Map<String,Object> runExperimentInternal()
Runs the experiment. This is the main method to call in order to execute an experiment. Sub-classes normally don't have to overwrite this method but create customized behavior by overriding one of the life-cycle methods likeinit(),beforeRun(),performRun()(this one is required),done(),produceResults()orfinish().- Returns:
- The results of experiment execution.
-
requireState
protected void requireState(Experiment.ExperimentState expected)
Checks, if an experiment is in a certain state.- Parameters:
expected- The expected state of an experiment.- Throws:
IllegalStateException- If not in the expected state.
-
handleExecutionError
protected void handleExecutionError(Throwable t)
Lifecycle method that is executed if there was an Exception during experiment execution. In addition an EXPERIMENT_ERROR event is fired.- Parameters:
t-
-
finalActions
protected void finalActions()
This method can be used for actions like clean-up that should be done irrespectively of whether an exception occurred during experiment execution or not. Any code executed in this method should take care that it is not producing any uncatched exceptions itself.
-
checkCancelledOrInterrupted
protected void checkCancelledOrInterrupted() throws CancellationExceptionChecks whether the experiment was requested to cancel or the executing Thread was interrupted. If so, aCancellationException(unchecked exception) is thrown.Experiments that wan't to be responsive to cancellation requests should call this method frequently from within the main execution Thread.
- Throws:
CancellationException- If the experiment was cancelled or interrupted.
-
addStandardResults
protected void addStandardResults()
-
addErrorResults
protected void addErrorResults()
-
cancel
public void cancel()
Requests the experiment to cancel its execution prematurely. This also implies aborting it.- See Also:
abort(),checkCancelledOrInterrupted()
-
isCancelled
public boolean isCancelled()
Checks whether the experiment was requested tocancel()its execution.- Returns:
true, ifcancel()was called before;falseotherwise.
-
abort
public void abort()
Marks experiment execution to be aborted by some error condition. This does not necessarily mean it's execution wascancel()led prematurely.
-
getResults
public final Map<String,Object> getResults()
Returns the result map produced when executing this experiment.- Returns:
- This experiment's results as an unmodifiable map.
-
getError
public final Throwable getError()
- Returns:
- The exception that terminated this experiment. Might be a
CancellationExceptionif this experiment's execution was cancelled.
-
state
public final ObservableValue<Experiment.ExperimentState> state()
- Returns:
- The current execution state of this experiment as an
ObservableValue.
-
getState
public final Experiment.ExperimentState getState()
- Returns:
- The current execution state of this experiment.
-
runTimeReal
protected double runTimeReal()
Returns the run time (in seconds) of an Experiment. The returned value is only valid after callingrunExperiment()and measures the time between callinginit()and the completion ofdone().- Returns:
- The real time (wall time in seconds) it took to run the experiment.
-
executeSubExperiment
protected ExperimentCompletableFuture executeSubExperiment(Experiment sub)
This is a convenience method to run a sub experiment without having to worry aboutExperimentExecutorandnestingLevel.- Parameters:
sub- The sub-experiment to run.- Returns:
- An
ExperimentCompletableFutureto access results.
-
getPropsWithValues
public Map<String,Object> getPropsWithValues()
Retrieves a map containing the name and current value for each of this experiment's properties.- Returns:
- A map of all Java Bean properties and their values.
-
print
public void print(String message)
Triggers a print event of category "info".- Parameters:
message- The message to print.- See Also:
print(MsgCategory, String)
-
print
public void print(MsgCategory category, String message)
Triggers a print event of the given category. If an appropriate listener is installed, this should produce an output ofmessage.- Parameters:
category- Category of the message.message- The message to print.- See Also:
ConsolePrinter
-
print
public void print(MsgCategory category, String messageFormat, Object... params)
Triggers a print event of the given category. If an appropriate listener is installed, this should produce a message created by the given format string and parameters.- Parameters:
category- Category of the message.messageFormat- Format string for the message to produce.params- Parameters to use in the format string.
-
print
public void print(String messageFormat, Object... params)
Same asprint(MsgCategory, String, Object...), just defaulting to the categoryINFO.- Parameters:
messageFormat- The format String to use.params- Parameters to use when formatting the message.
-
printResults
public final void printResults()
Prints the results of this experiment toSystem.out.
-
nestingLevel
public void nestingLevel(int nestingLevel)
Sets the nesting level. This method is only for internal purposes.- Parameters:
nestingLevel- The nesting level for this experiment.
-
nestingLevel
public int nestingLevel()
The level in the call hierarchy this experiment is executed in. Experiments that spawn new sub-experiments (likeMultipleReplicationExperiment) are required to increase their children's nestingLevel by 1. IfexecuteSubExperiment(Experiment)is used, then this is set automatically to the correct value.- Returns:
- This experiment's nesting level.
-
setName
public void setName(String name)
Set some descriptive name for this experiment.- Parameters:
name- The name of the experiment.
-
getName
public String getName()
-
getInitialSeed
public long getInitialSeed()
-
setInitialSeed
public void setInitialSeed(long initialSeed)
Sets the initial seed for this experiment. If an experiment makes use of random influences, they should all and solely depend on this value.- Parameters:
initialSeed- The initial seed to use.- See Also:
RandomFactory
-
getLogLevel
public MsgCategory getLogLevel()
-
setLogLevel
public void setLogLevel(MsgCategory logLevel)
Set the maximum level of logging messages that are supposed to be printed (e.g. TRACE to produce a detailed log file). Default is INFO.- Parameters:
logLevel- The maximum log level to display/forward to listeners.
-
valueStoreImpl
public ValueStore valueStoreImpl()
Description copied from interface:ValueStoreReturns the implementation to use for adding ValueStore functionality.- Specified by:
valueStoreImplin interfaceValueStore
-
notifierImpl
public NotifierImpl<Experiment,Experiment.ExperimentEvent> notifierImpl()
- Specified by:
notifierImplin interfaceNotifier<Experiment,Experiment.ExperimentEvent>
-
clone
public Experiment clone()
-
readResolve
protected Object readResolve() throws ObjectStreamException
- Throws:
ObjectStreamException
-
-