Class TLcdSimulator
TLcdSimulator
is the class that is responsible for the timing of the simulation:
it produces the correct simulation time at each simulation step. Associated with a
TLcdSimulator
are:
- A simulator model
- One or more layer factories
- One or more views
The simulator model
is responsible for providing the
TLcdSimulator
with the ILcdModel
objects holding the domain data to be simulated,
and for putting these data in the state corresponding to a given simulation time.
The layer factories are used by the TLcdSimulator to create layers for the views, for each of the different simulation models.
TLcdSimulator
can be used in two different modes:
- time factor mode (default): there is a fixed relation between real time and simulation time.
- time step mode: the simulator will perform a simulation
at successive
time-steps
as fast as it can.
You can choose between the two modes with the setUseTimeStep(boolean)
method.
-
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
Register a listener for the PropertyChange event.void
Adds the given view as one of the views of this simulator and properly initializes it with the necessary simulation layers.protected long
Returns the difference in milliseconds between some fixed but arbitrary time, and the current wall-clock time.Return the begin date of the simulation.getDate()
Return the current date of the simulation.int
Returns the minimal delay between updates in milliseconds.Return the end date of the simulation.Return theILcdGXYLayerFactory
to create the simulation layer(s) for the GXY views.Returns an array ofILcdGXYView
on which the simulation is shown.Returns a collection of all theILcdLayer
used in this simulator.Return theILspLayerFactory
to create the simulation layer(s) for a Lightspeed view.ILspView[]
Returns an array ofILspView
on which the simulation is shown.int
Returns the maximum percentage of CPU to be used by the simulation.ILcdGXYLayer[][]
Returns a two-dimensional array ofILcdGXYLayer
.ILspLayer[][]
Returns a two-dimensional array ofILspLayer
.Return theILcdSimulatorModel
that is simulated by this simulator.int
Returns the status of the simulator.Return the status of the simulator as a String.double
Returns the time factor, this is the factor by which simulation is accelerated with respect to real time.long
Returns the time step in milliseconds.int
Returns the current update rate.boolean
Determines whether the simulator manages the number of cached background layers of the views to which it is linked.boolean
Returns whether the simulation is suspended while the mouse is pressed.boolean
Returns whether this simulator plays the simulation in a loop.boolean
Returns true if time tracing is active, false otherwise.boolean
Deprecated.This method has been deprecated.boolean
Returns whether the same simulation layer can be used in different GXY views, or each GXY view has to have its own layer.boolean
Returns true if using time steps, false otherwise.void
pause()
Pause the simulatorvoid
Remove a listener for the PropertyChange event.void
removeView
(ILcdView aView) Removes the given view from this simulator's viewsvoid
run()
Start the simulatorvoid
Sets the date associated with this simulator to the given datevoid
setDelayBetweenUpdateMs
(int aDelayBetweenUpdateMs) Set the minimal delay in milliseconds between two simulation updates.void
setGXYLayerFactory
(ILcdGXYLayerFactory aGXYLayerFactory) Set theILcdGXYLayerFactory
to create the simulation layer(s) for the GXY views.void
setGXYView
(ILcdGXYView[] aGXYView) Set an array ofILcdGXYView
on which the simulation is shown.void
setLspLayerFactory
(ILspLayerFactory aLayerFactory) Set theILspLayerFactory
to create the simulation layer(s) for a Lightspeed view.void
setLspView
(ILspView[] aLspView) Set an array ofILspView
on which the simulation is shown.void
setManageNumberOfCachedBackgroundLayers
(boolean aManageNumberOfCachedBackgroundLayers) Enables or disables number of cached background layer management.void
setMaxCPUUsage
(int aMaxCPUUsage) Set the maximum percentage of CPU to be used by the simulation.void
setPauseOnMousePressed
(boolean aPauseOnMousePressed) Set whether to suspend the simulation when the mouse is pressed on one of the simulation views.void
setPlayInLoop
(boolean aPlayInLoop) Sets whether this simulator should play the simulation in a loop.void
setSimulatorModel
(ILcdSimulatorModel aSimulatorModel) Set theILcdSimulatorModel
to be simulated by this simulator.void
setTimeFactor
(double aTimeFactor) Sets the time factor, this is the factor by which simulation is accelerated with respect to real time.void
setTimerTraceOn
(boolean aTimerTraceOn) Set time tracing to aTimerTraceOn.void
setTimeStep
(long aTimeStep) Sets the time step to use expressed in milliseconds.void
setTraceOn
(boolean aTraceOn) Deprecated.This method has been deprecated.void
setUseSameGXYLayer
(boolean aUseSameGXYLayer) Set whether the same simulation layer can be used in different GXY views, or each GXY view has to have its own layer.void
setUseTimeStep
(boolean aUseTimeStep) Set whether to use a time-step simulation.void
stop()
Stop the simulator.
-
Field Details
-
STOPPED
public static final int STOPPEDThe Status when stopped.- See Also:
-
RUNNING
public static final int RUNNINGThe Status when running.- See Also:
-
PAUSING
public static final int PAUSINGThe Status when pausing.- See Also:
-
ENDED
public static final int ENDEDThe Status when ended.- See Also:
-
-
Constructor Details
-
TLcdSimulator
public TLcdSimulator()Creates a newTLcdSimulator
-
-
Method Details
-
addView
Adds the given view as one of the views of this simulator and properly initializes it with the necessary simulation layers.
Currently only instances of
ILcdGXYView
andILspView
are accepted. Other implementations are ignored.- Parameters:
aView
- the view to be added to the views of this simulator
-
removeView
Removes the given view from this simulator's views- Parameters:
aView
- the view that needs to be removed from this simulator's views
-
getSimulatorModel
Return theILcdSimulatorModel
that is simulated by this simulator.- Returns:
- The
ILcdSimulatorModel
that is simulated by this simulator. - See Also:
-
setSimulatorModel
Set theILcdSimulatorModel
to be simulated by this simulator. The date information associated with the simulator is replaced with the date information associated with the simulator model. The simulator's date and the simulation's begin date and end date are set to the date, begin date, and end date of the simulator model.- Parameters:
aSimulatorModel
- TheILcdSimulatorModel
to be simulated by this simulator.- See Also:
-
getStatus
public int getStatus()Returns the status of the simulator. This is one of:- TLcdSimulator.RUNNING
- TLcdSimulator.STOPPED
- TLcdSimulator.PAUSING
- TLcdSimulator.ENDED
- Returns:
- The status of the simulator.
-
getStatusAsString
Return the status of the simulator as a String.- Returns:
- The status of the simulator as a String.
- See Also:
-
getMaxCPUUsage
public int getMaxCPUUsage()Returns the maximum percentage of CPU to be used by the simulation.- Returns:
- The maximum percentage of CPU to be used by the simulation.
- See Also:
-
setMaxCPUUsage
public void setMaxCPUUsage(int aMaxCPUUsage) Set the maximum percentage of CPU to be used by the simulation. It shall only be considered as an indication given to this class, not a strict value to be respected. It take as assumption that the CPU is not shared with any other threads or application. This property has priority over the delayBetweenUpdateMs property, when the percentage of the CPU time for one update, compared to the current value of delayBetweenUpdateMs (i.e.: CPU_time_update * 100 / delayBetweenUpdateMs), is greater than this property value.- Parameters:
aMaxCPUUsage
- A value between 1 and 100.- See Also:
-
isPauseOnMousePressed
public boolean isPauseOnMousePressed()Returns whether the simulation is suspended while the mouse is pressed.- See Also:
-
setPauseOnMousePressed
public void setPauseOnMousePressed(boolean aPauseOnMousePressed) Set whether to suspend the simulation when the mouse is pressed on one of the simulation views.
When the mouse is released, the simulation will continue as if the simulation had not been paused: the simulation time will jump to catch up the time lost between the press and release of the mouse.
By default, this feature is enabled.
Note: This feature does not work with Lightspeed views
- Parameters:
aPauseOnMousePressed
- When true, the simulation will be suspended while the mouse is being pressed.- See Also:
-
getTimeFactor
public double getTimeFactor()Returns the time factor, this is the factor by which simulation is accelerated with respect to real time. The default value for the time factor is 1.- Returns:
- The time factor, this is the factor by which simulation is accelerated with respect to real time.
- See Also:
-
setTimeFactor
public void setTimeFactor(double aTimeFactor) Sets the time factor, this is the factor by which simulation is accelerated with respect to real time.- Parameters:
aTimeFactor
- The time factor to be set.- See Also:
-
isPlayInLoop
public boolean isPlayInLoop()Returns whether this simulator plays the simulation in a loop.- Returns:
- whether this simulator plays the simulation in a loop.
-
setPlayInLoop
public void setPlayInLoop(boolean aPlayInLoop) Sets whether this simulator should play the simulation in a loop.- Parameters:
aPlayInLoop
- whether the simulator should play the simulation in a loop.
-
isTimerTraceOn
public boolean isTimerTraceOn()Returns true if time tracing is active, false otherwise.- Returns:
- true if time tracing is active, false otherwise.
- See Also:
-
setTimerTraceOn
public void setTimerTraceOn(boolean aTimerTraceOn) Set time tracing to aTimerTraceOn. When timer tracing is turned on, all timer related log messages are recorded.
To see the log output, you also have to enable tracing for this class. You can do that for example by using a logging configuration file. If you're using the default Java logging this code snippet can be used as well:
simulator.setTimerTraceOn( true ); Logger logger = Logger.getLogger( TLcdSimulator.class.getName() ); logger.setLevel( Level.ALL ); ConsoleHandler handler = new ConsoleHandler(); handler.setLevel( Level.ALL ); logger.addHandler( handler );
This property is meant for debugging purposes. As such, no property change events are fired when it changes.
- Parameters:
aTimerTraceOn
- The value to be set for time tracing.- See Also:
-
getUpdatesPerSecond
public int getUpdatesPerSecond()Returns the current update rate.- Returns:
- The current update rate.
-
getDelayBetweenUpdateMs
public int getDelayBetweenUpdateMs()Returns the minimal delay between updates in milliseconds.- Returns:
- The minimal delay between updates in milliseconds.
- See Also:
-
setDelayBetweenUpdateMs
public void setDelayBetweenUpdateMs(int aDelayBetweenUpdateMs) Set the minimal delay in milliseconds between two simulation updates. The actual delay is also controlled by the propertyMaxCPUUsage
. This property can be used to limit the maximum frame rate. If this property is for example set to 50 milliseconds, the theoretical maximum frame rate is 1000/50 = 20. In practice it will be somewhat lower because refreshing the view takes some time as well.- Parameters:
aDelayBetweenUpdateMs
- The delay in milliseconds between two simulation updates.- See Also:
-
isUseTimeStep
public boolean isUseTimeStep()Returns true if using time steps, false otherwise.- Returns:
- true if using time steps, false otherwise.
- See Also:
-
setUseTimeStep
public void setUseTimeStep(boolean aUseTimeStep) Set whether to use a time-step simulation. If set to true, (and if the methodgetTimeStep()
) returns a value > 0), the simulator will not perform a simulation that has a relation with the real elapsed time. Instead it will give an animation at successive time step as fast as it can. By default, this feature is disabled- Parameters:
aUseTimeStep
- The value to be set for time-stepping.- See Also:
-
getTimeStep
public long getTimeStep()Returns the time step in milliseconds.- Returns:
- The time step in milliseconds.
- See Also:
-
setTimeStep
public void setTimeStep(long aTimeStep) Sets the time step to use expressed in milliseconds. The default time step is 0.
This property is only relevant when this simulator works in
time-step
mode.- Parameters:
aTimeStep
- The time step in milliseconds.- See Also:
-
addPropertyChangeListener
Register a listener for the PropertyChange event.- Parameters:
l
- The listener to register.- See Also:
-
removePropertyChangeListener
Remove a listener for the PropertyChange event.- Parameters:
l
- The listener to remove.- See Also:
-
currentTime
protected long currentTime()Returns the difference in milliseconds between some fixed but arbitrary time, and the current wall-clock time. For example
System.nanoTime()/1e6
can be used.Overriding this method can be useful to synchronize the simulator with an external clock, for example from an audio or a video stream.
- Returns:
- the difference in milliseconds between some fixed but arbitrary time, and the current wall-clock time.
-
getDate
Return the current date of the simulation. Returns a date corresponding to January 1, 1970, 00:00:00 GMT if no valid simulator model is set.- Returns:
- The current date of the simulation or a date corresponding to January 1, 1970, 00:00:00 GMT if no valid simulator model is set.
- See Also:
-
setDate
Sets the date associated with this simulator to the given date- Parameters:
newDate
- the new date for this simulator
-
getBeginDate
Return the begin date of the simulation. Returns a date corresponding to January 1, 1970, 00:00:00 GMT if no valid simulator model is set.- Returns:
- The begin date of the simulation.
-
getEndDate
Return the end date of the simulation.- Returns:
- The end date of the simulation. Returns a date corresponding to January 1, 1970, 00:00:00 GMT if no valid simulator model is set.
-
run
public void run()Start the simulator -
stop
public void stop()Stop the simulator. Simulation time is reset to the start of the simulation. -
pause
public void pause()Pause the simulator -
getGXYView
Returns an array ofILcdGXYView
on which the simulation is shown.- Returns:
- An array of
ILcdGXYView
on which the simulation is shown. - See Also:
-
setGXYView
Set an array ofILcdGXYView
on which the simulation is shown.- Parameters:
aGXYView
- An array ofILcdGXYView
on which the simulation is shown.- See Also:
-
getLspView
Returns an array ofILspView
on which the simulation is shown.- Returns:
- An array of
ILspView
on which the simulation is shown.
-
setLspView
Set an array ofILspView
on which the simulation is shown.- Parameters:
aLspView
- An array ofILspView
on which the simulation is shown.- See Also:
-
getGXYLayerFactory
Return theILcdGXYLayerFactory
to create the simulation layer(s) for the GXY views.- Returns:
- The
ILcdGXYLayerFactory
to create the simulation layer(s) for the GXY views. - See Also:
-
setGXYLayerFactory
Set theILcdGXYLayerFactory
to create the simulation layer(s) for the GXY views.- Parameters:
aGXYLayerFactory
- TheILcdGXYLayerFactory
to create the simulation layer(s) for the GXY views.- See Also:
-
getLspLayerFactory
Return theILspLayerFactory
to create the simulation layer(s) for a Lightspeed view.- Returns:
- The
ILspLayerFactory
to create the simulation layer(s) for a Lightspeed view. - See Also:
-
setLspLayerFactory
Set theILspLayerFactory
to create the simulation layer(s) for a Lightspeed view.- Parameters:
aLayerFactory
- TheILspLayerFactory
to create the simulation layer(s) for a Lightspeed view.- See Also:
-
getLayers
Returns a collection of all theILcdLayer
used in this simulator. The returned collection can not be modified.- Returns:
- a collection of all the
ILcdLayer
used in this simulator. The returned collection can not be modified.
-
getSimulationGXYLayers
Returns a two-dimensional array ofILcdGXYLayer
. The first dimension corresponds to the differentILcdGXYView
of this simulator. The second dimension corresponds to the different simulation layers per view. The simulation layers are automatically generated when anILcdSimulatorModel
along with one or multiple views is set to this simulator. Each view should have the same number of layers, one for each track model that is contained in theILcdSimulatorModel
and should have the same order as the track models array that can be obtained by callingILcdSimulatorModel#getTrackModels()
. Based on whether this simulator is configured to share layers between views (see#setUseSameGXYLayer
) a layer for a specific track model will be shared across multiple GXY views.- Returns:
- A two-dimensional array of
ILcdGXYLayer
. The first dimension corresponds to the differentILcdGXYView
of this simulator. The second dimension corresponds to the different simulation layers per view.
-
getSimulationLspLayers
Returns a two-dimensional array ofILspLayer
. The first dimension corresponds to the differentILspView
of this simulator. The second dimension corresponds to the different simulation layers per view. The simulation layers are automatically generated when anILcdSimulatorModel
along with one or multiple views is set to this simulator. Each view should have the same number of layers, one for each track model that is contained in theILcdSimulatorModel
and should have the same order as the track models array that can be obtained by callingILcdSimulatorModel#getTrackModels()
.- Returns:
- A two-dimensional array of
ILspLayer
. The first dimension corresponds to the differentILspView
of this simulator. The second dimension corresponds to the different simulation layers per view.
-
setUseSameGXYLayer
public void setUseSameGXYLayer(boolean aUseSameGXYLayer) Set whether the same simulation layer can be used in different GXY views, or each GXY view has to have its own layer.- Parameters:
aUseSameGXYLayer
- If true a single layer is shared among all GXY simulation views, if false a separate simulation layer is created for each GXY simulation view.- See Also:
-
isUseSameGXYLayer
public boolean isUseSameGXYLayer()Returns whether the same simulation layer can be used in different GXY views, or each GXY view has to have its own layer. By default, this feature is disabled.- Returns:
- true if a single layer is shared among all GXY simulation views, false false if a separate simulation layer is created for each GXY simulation view.
- See Also:
-
isManageNumberOfCachedBackgroundLayers
public boolean isManageNumberOfCachedBackgroundLayers()Determines whether the simulator manages the number of cached background layers of the views to which it is linked. If set to true, the number of cached background layers will be set equal to the number of layers beneath the layer created by this simulator for every view this simulator creates a layer for. If set to false, the number of cached background layers will not be modified. By default, this feature is enabled.- Returns:
- true if background layer management is enabled; false otherwise
- See Also:
-
setManageNumberOfCachedBackgroundLayers
public void setManageNumberOfCachedBackgroundLayers(boolean aManageNumberOfCachedBackgroundLayers) Enables or disables number of cached background layer management.- Parameters:
aManageNumberOfCachedBackgroundLayers
- true to enable background layer management; false to disable it- See Also:
-
isTraceOn
public boolean isTraceOn()Deprecated.This method has been deprecated. It is recommended to use the standard Java logging framework directly.Returnstrue
if tracing is enabled for this class.- Returns:
- true if tracing is enabled for this class, false otherwise.
-
setTraceOn
public void setTraceOn(boolean aTraceOn) Deprecated.This method has been deprecated. It is recommended to use the standard Java logging framework directly.Enables tracing for this class instance. Calling this method with eithertrue
orfalse
as argument automatically turns off tracing for all other class instances for whichsetTraceOn
has not been called. If the argument isfalse
then only the informative, warning and error log messages are recorded.- Parameters:
aTraceOn
- if true then all log messages are recorded for this instance. If false, then only the informative, warning and error log messages are recorded.
-