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-stepsas fast as it can.
You can choose between the two modes with the setUseTimeStep(boolean) method.
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidRegister a listener for the PropertyChange event.voidAdds the given view as one of the views of this simulator and properly initializes it with the necessary simulation layers.protected longReturns 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.intReturns the minimal delay between updates in milliseconds.Return the end date of the simulation.Return theILcdGXYLayerFactoryto create the simulation layer(s) for the GXY views.Returns an array ofILcdGXYViewon which the simulation is shown.Returns a collection of all theILcdLayerused in this simulator.Return theILspLayerFactoryto create the simulation layer(s) for a Lightspeed view.ILspView[]Returns an array ofILspViewon which the simulation is shown.intReturns 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 theILcdSimulatorModelthat is simulated by this simulator.intReturns the status of the simulator.Return the status of the simulator as a String.doubleReturns the time factor, this is the factor by which simulation is accelerated with respect to real time.longReturns the time step in milliseconds.intReturns the current update rate.booleanDetermines whether the simulator manages the number of cached background layers of the views to which it is linked.booleanReturns whether the simulation is suspended while the mouse is pressed.booleanReturns whether this simulator plays the simulation in a loop.booleanReturns true if time tracing is active, false otherwise.booleanDeprecated.This method has been deprecated.booleanReturns whether the same simulation layer can be used in different GXY views, or each GXY view has to have its own layer.booleanReturns true if using time steps, false otherwise.voidpause()Pause the simulatorvoidRemove a listener for the PropertyChange event.voidremoveView(ILcdView aView) Removes the given view from this simulator's viewsvoidrun()Start the simulatorvoidSets the date associated with this simulator to the given datevoidsetDelayBetweenUpdateMs(int aDelayBetweenUpdateMs) Set the minimal delay in milliseconds between two simulation updates.voidsetGXYLayerFactory(ILcdGXYLayerFactory aGXYLayerFactory) Set theILcdGXYLayerFactoryto create the simulation layer(s) for the GXY views.voidsetGXYView(ILcdGXYView[] aGXYView) Set an array ofILcdGXYViewon which the simulation is shown.voidsetLspLayerFactory(ILspLayerFactory aLayerFactory) Set theILspLayerFactoryto create the simulation layer(s) for a Lightspeed view.voidsetLspView(ILspView[] aLspView) Set an array ofILspViewon which the simulation is shown.voidsetManageNumberOfCachedBackgroundLayers(boolean aManageNumberOfCachedBackgroundLayers) Enables or disables number of cached background layer management.voidsetMaxCPUUsage(int aMaxCPUUsage) Set the maximum percentage of CPU to be used by the simulation.voidsetPauseOnMousePressed(boolean aPauseOnMousePressed) Set whether to suspend the simulation when the mouse is pressed on one of the simulation views.voidsetPlayInLoop(boolean aPlayInLoop) Sets whether this simulator should play the simulation in a loop.voidsetSimulatorModel(ILcdSimulatorModel aSimulatorModel) Set theILcdSimulatorModelto be simulated by this simulator.voidsetTimeFactor(double aTimeFactor) Sets the time factor, this is the factor by which simulation is accelerated with respect to real time.voidsetTimerTraceOn(boolean aTimerTraceOn) Set time tracing to aTimerTraceOn.voidsetTimeStep(long aTimeStep) Sets the time step to use expressed in milliseconds.voidsetTraceOn(boolean aTraceOn) Deprecated.This method has been deprecated.voidsetUseSameGXYLayer(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.voidsetUseTimeStep(boolean aUseTimeStep) Set whether to use a time-step simulation.voidstop()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
ILcdGXYViewandILspVieware 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 theILcdSimulatorModelthat is simulated by this simulator.- Returns:
- The
ILcdSimulatorModelthat is simulated by this simulator. - See Also:
-
setSimulatorModel
Set theILcdSimulatorModelto 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- TheILcdSimulatorModelto 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-stepmode.- 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()/1e6can 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 ofILcdGXYViewon which the simulation is shown.- Returns:
- An array of
ILcdGXYViewon which the simulation is shown. - See Also:
-
setGXYView
Set an array ofILcdGXYViewon which the simulation is shown.- Parameters:
aGXYView- An array ofILcdGXYViewon which the simulation is shown.- See Also:
-
getLspView
Returns an array ofILspViewon which the simulation is shown.- Returns:
- An array of
ILspViewon which the simulation is shown.
-
setLspView
Set an array ofILspViewon which the simulation is shown.- Parameters:
aLspView- An array ofILspViewon which the simulation is shown.- See Also:
-
getGXYLayerFactory
Return theILcdGXYLayerFactoryto create the simulation layer(s) for the GXY views.- Returns:
- The
ILcdGXYLayerFactoryto create the simulation layer(s) for the GXY views. - See Also:
-
setGXYLayerFactory
Set theILcdGXYLayerFactoryto create the simulation layer(s) for the GXY views.- Parameters:
aGXYLayerFactory- TheILcdGXYLayerFactoryto create the simulation layer(s) for the GXY views.- See Also:
-
getLspLayerFactory
Return theILspLayerFactoryto create the simulation layer(s) for a Lightspeed view.- Returns:
- The
ILspLayerFactoryto create the simulation layer(s) for a Lightspeed view. - See Also:
-
setLspLayerFactory
Set theILspLayerFactoryto create the simulation layer(s) for a Lightspeed view.- Parameters:
aLayerFactory- TheILspLayerFactoryto create the simulation layer(s) for a Lightspeed view.- See Also:
-
getLayers
Returns a collection of all theILcdLayerused in this simulator. The returned collection can not be modified.- Returns:
- a collection of all the
ILcdLayerused in this simulator. The returned collection can not be modified.
-
getSimulationGXYLayers
Returns a two-dimensional array ofILcdGXYLayer. The first dimension corresponds to the differentILcdGXYViewof this simulator. The second dimension corresponds to the different simulation layers per view. The simulation layers are automatically generated when anILcdSimulatorModelalong 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 theILcdSimulatorModeland 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 differentILcdGXYViewof 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 differentILspViewof this simulator. The second dimension corresponds to the different simulation layers per view. The simulation layers are automatically generated when anILcdSimulatorModelalong 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 theILcdSimulatorModeland 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 differentILspViewof 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.Returnstrueif 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 eithertrueorfalseas argument automatically turns off tracing for all other class instances for whichsetTraceOnhas not been called. If the argument isfalsethen 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.
-