public abstract class BasicAssembly
extends java.lang.Object
implements java.lang.Runnable
Abstract Base class for creating Simkit scenarios.
A simkit Assembly represents a complete simulation initialized with the parameters from a single design point of some notional design of experiments. The simulation can be run at that design point any number of times, with statistical data gathered and reported semi-automatically.
BasicAssembly
is intended to be subclassed, and provides many
template methods and hooks that allow your sublcass to provide customized
behavior, while also providing the basic functionality of managing multiple
simulation runs.
After instantiating your subclass, the design point it represents is run
by calling init()
and then run
.
init
instantiates all the objects in the simulation and connects
the listener pattern connections needed by the model. You
specify what the simulation entities will be by implementing the required template
method createSimEntities()
. You also have the opportunity to
create instruments for the model in the form of
simkit.stat.SampleStatistics
instances that you create in the
optional methods createReplicationStats()
and (less frequently)
createDesignPointStats()
. By default, anything you put into
the replicationStats
array will be summarized without doing
anything in createDesignPointStats
.
After all your creation methods are called, the hookup methods are called. In the hookup methods, you operate on all the objects previously created to set up the listening pattern. You do this for two distinct reasons: First, to make the connections that implement your event graph, and second, for data collection (all the stats objects you created need to listen for property changes in order to gather data from the model when it runs.)
Note. You can add a stats object (or any other PropertyChangeListener as
a listener to this object via the protected instance variable
propertyChangeSupport
. Instead of saying
this.addPropertyChangeListener(somePropertychangeListener);
you say
propertyChangeSupport.addPropertyChangeListener(somePropertychangeListener);
Later, if you want to fire a PropertyChange, you invoke the firing method
on propertyChangeSupport
propertyChangeSupport.firePropertyChange(<args>);
Modifier and Type | Field and Description |
---|---|
private int |
designPointID |
protected SampleStatistics[] |
designPointStats |
private java.text.DecimalFormat |
form |
protected boolean |
hookupsCalled |
static java.util.logging.Logger |
LOGGER |
private int |
numberReplications |
private boolean |
printReplicationReports |
private boolean |
printSummaryReport |
protected java.beans.PropertyChangeListener[] |
propertyChangeListener |
protected java.beans.PropertyChangeSupport |
propertyChangeSupport |
private boolean |
reallyVerbose |
protected java.util.LinkedHashMap<java.lang.Integer,java.util.List<SampleStatistics>> |
replicationData |
protected SampleStatistics[] |
replicationStats |
private boolean |
saveReplicationData |
protected SimEntity[] |
simEntity |
private boolean |
singleStep |
protected boolean |
stopped |
private double |
stopTime |
private boolean |
verbose |
Constructor and Description |
---|
BasicAssembly()
Default constructor sets parameters of BasicAssembly to their
default values.
|
Modifier and Type | Method and Description |
---|---|
protected void |
createDesignPointStats()
The default behavior is to create a
SimpleStatsTally
instance for each element in replicationStats with the
corresponding name + "mean". |
protected void |
createObjects()
Create all the objects used.
|
protected void |
createPropertyChangeListeners()
Optional method.
|
protected void |
createReplicationStats()
This hook method allows sub-classes to add instrumentation that
is accumulated during a replication.
|
protected abstract void |
createSimEntities()
Template method for creating the SimEntities in the model.
|
int |
getDesignPointID() |
SampleStatistics[] |
getDesignPointStats() |
int |
getIDforReplicationStateName(java.lang.String state) |
int |
getNumberReplications() |
java.util.Map<java.lang.Integer,java.util.List<SampleStatistics>> |
getReplicationData() |
protected java.lang.String |
getReplicationReport(int rep)
This can be done generically.
|
SampleStatistics |
getReplicationStat(java.lang.String name,
int replication) |
SampleStatistics[] |
getReplicationStats(int id) |
SimEntity[] |
getSimEntities()
These are the actual SimEnties in the array, but the array itself is
a copy.
|
double |
getStopTime() |
protected java.lang.String |
getSummaryReport()
This can be done generically.
|
void |
handleSimEvent(SimEvent simEvent)
Empty, implemented here so subclasses don't have to implement
(abstract method of BasicSimEntity)
|
protected void |
hookupDesignPointListeners()
Set up all outer stats propertyChangeListeners.
|
protected void |
hookupPropertyChangeListeners()
Optional method.
|
protected abstract void |
hookupReplicationListeners()
Template method for hooking up listening in replications.
|
protected abstract void |
hookupSimEventListeners()
Template method for hooking up the listening pattern in
already-instantiated SimEntities.
|
void |
init() |
boolean |
isPrintReplicationReports() |
boolean |
isPrintSummaryReport() |
boolean |
isReallyVerbose()
The value of Schedule.reallyVerbose.
|
boolean |
isSaveReplicationData() |
boolean |
isSingleStep() |
boolean |
isStopped() |
boolean |
isVerbose() |
protected void |
performHookups()
Call all the hookup methods.
|
void |
processSimEvent(SimEvent simEvent)
Empty, implemented here so subclasses don't have to implement
(abstract method of BasicSimEntity)
|
protected void |
replicationDidFinish()
Hook method called after the event list runs to completion for a replication.
|
void |
reset()
Resets all inner stats.
|
void |
run()
Execute the simulation for the desired number of replications.
|
protected void |
saveReplicationStats()
Save all replicationStats for a given iteration.
|
protected void |
scheduleDidReset()
Hook method that will be called just after Schedule.reset() and just
prior to calling
Schedule.startSimlulation()
This method should rarely be needed because simkit idiom is to handle all
preparation for a simulation run in the various reset methods. |
protected void |
scheduleWillReset()
Hook method that will be called prior to calling Schedule.reset().
|
void |
setDesignPointID(int id) |
void |
setNumberReplications(int num) |
void |
setPrintReplicationReports(boolean b) |
void |
setPrintSummaryReport(boolean b) |
void |
setReallyVerbose(boolean value)
Determines the value of Schedule.reallyVerbose.
|
void |
setSaveReplicationData(boolean b) |
void |
setSingleStep(boolean b) |
void |
setStopTime(double time) |
void |
setVerbose(boolean yn) |
protected void |
simulationDidFinish()
Hook method called after all replications have completed.
|
protected void |
simulationWillBeStopped()
Hook method that is called when
stop() is called on an
Assembly. |
void |
stop()
Stops simulation wherever it is in the loop, providing a means to stop
the simulation in mid-run.
|
public static final java.util.logging.Logger LOGGER
protected java.util.LinkedHashMap<java.lang.Integer,java.util.List<SampleStatistics>> replicationData
protected SampleStatistics[] replicationStats
protected SampleStatistics[] designPointStats
protected SimEntity[] simEntity
protected java.beans.PropertyChangeListener[] propertyChangeListener
protected boolean hookupsCalled
protected boolean stopped
private double stopTime
private boolean reallyVerbose
private boolean singleStep
private int numberReplications
private boolean printReplicationReports
private boolean printSummaryReport
private boolean saveReplicationData
private int designPointID
protected java.beans.PropertyChangeSupport propertyChangeSupport
private final java.text.DecimalFormat form
private boolean verbose
public BasicAssembly()
printReplicationReports = false printSummaryReport = true saveReplicationData = false numberReplications = 1
public void init()
public void reset()
Resets all inner stats. State resetting for SimEntities is their responsibility. Outer stats are not reset.
protected void createObjects()
createSimEntities()
method is abstract and will
be implemented in the concrete subclass. The others are empty by
default. The createReplicationStats()
method must be
overridden if any replications stats are needed.protected void performHookups()
protected abstract void createSimEntities()
protected abstract void hookupSimEventListeners()
protected abstract void hookupReplicationListeners()
protected void hookupPropertyChangeListeners()
protected void createDesignPointStats()
SimpleStatsTally
instance for each element in replicationStats
with the
corresponding name + "mean".
When overriding this method, if you call super to retain this
behavior, be sure to also call super in hookupDesignPointListeners()
or the default hookups won't be made.protected void createReplicationStats()
replicationsStats
. By doing this, your stats object
here will be summarized in the output data for each design point (by default).
Any object you create here should be hooked up (i.e. registered to listen
for the approprate state changes) in the corresponding hook method
hookupReplicationListeners()
Thid method is called from createObjects()
after
createSimEntities()
and before createDesignPointStats()
protected void createPropertyChangeListeners()
createObjects()
protected void hookupDesignPointListeners()
createDesignPointListeners()
. Therefore, if you override
createDesignPointListeners()
, you will likely have to override
hookupDesignPointListeners()
. If your implementation of
createDesignPointListeners()
calls super, then be sure to
call super here as well.public void setStopTime(double time)
public double getStopTime()
public void setSingleStep(boolean b)
public boolean isSingleStep()
public void setNumberReplications(int num)
public int getNumberReplications()
public void setPrintReplicationReports(boolean b)
public boolean isPrintReplicationReports()
public void setPrintSummaryReport(boolean b)
public boolean isPrintSummaryReport()
public void setDesignPointID(int id)
public int getDesignPointID()
public void setSaveReplicationData(boolean b)
public boolean isSaveReplicationData()
public void setReallyVerbose(boolean value)
value
- Given valuepublic boolean isReallyVerbose()
public void handleSimEvent(SimEvent simEvent)
simEvent
- Given SimEventpublic void processSimEvent(SimEvent simEvent)
simEvent
- Given SimEventpublic SampleStatistics[] getDesignPointStats()
public SampleStatistics[] getReplicationStats(int id)
public SampleStatistics getReplicationStat(java.lang.String name, int replication)
public int getIDforReplicationStateName(java.lang.String state)
public java.util.Map<java.lang.Integer,java.util.List<SampleStatistics>> getReplicationData()
protected void saveReplicationStats()
protected java.lang.String getReplicationReport(int rep)
rep
- The replication number for this reportprotected java.lang.String getSummaryReport()
public SimEntity[] getSimEntities()
public boolean isStopped()
public void stop()
public void run()
run
in interface java.lang.Runnable
protected void scheduleWillReset()
protected void scheduleDidReset()
Schedule.startSimlulation()
This method should rarely be needed because simkit idiom is to handle all
preparation for a simulation run in the various reset methods. The
notable exception is if there are objects that hold state information
in static variables, or use static variables as the way to keep a
strong reference to a re-usable SimEntity. Such objects may need to
have a static resetter called on them between replications.
Subclasses may use this place for any custom code that should run just
before time begins to advance.protected void replicationDidFinish()
protected void simulationDidFinish()
protected void simulationWillBeStopped()
stop()
is called on an
Assembly. After this method has had a chance to run, the model will be
stoped where ever it is in the loop/replication.public boolean isVerbose()
public void setVerbose(boolean yn)