the AdvanceStrategy which runs advances the Simulation
Optional
opt_name: stringname of this SimRunner.
Private
advanceThe AdvanceStrategys to run.
Private
appName of the application that created this SimRunner, for debugging. Useful when multiple apps are running simultaneously on a page: this tells which app this SimRunner belongs to.
Private
displayAmount of time between displaying frames of the simulation, in seconds.
Private
nonWhether the Timer stops firing when the window is not active (when a blur event occurs).
Private
timeAmount of time to advance the simulation, in seconds.
Static
Readonly
RESETName of GenericEvent that is broadcast when reset method occurs.
Adds the LabCanvas to the list of LabCanvas's that need to be repainted and memorized after each advance of the Simulation.
the LabCanvas to add to the list of LabCanvas's to update
Adds an object to the list of ErrorObserver objects to be notified when an error occurs.
object to add to the list of ErrorObserver objects
Adds an object to the list of Memorizable objects. These object's memorize
methods will be called from this object's memorize
method.
object to add to the list of Memorizable objects
if called during the memorize
method.
Adds the given Observer to this Subject's list of Observers, so that the Observer
will be notified of changes in this Subject. An Observer may call Subject.addObserver
during its observe
method.
the Observer to add
Adds the Parameter to the list of this Subject's available Parameters.
the Parameter to add
if a Parameter with the same name already exists.
Adds an AdvanceStrategy to the set being advanced.
the AdvanceStrategy to add
Private
advanceAdvances the simulation(s) to the target time and calls Memorizable.memorize on the list of Memorizables after each time step.
the AdvanceStrategy which advances the simulation
the time to advance to
Notifies all Observers that this Subject has changed by calling observe on each Observer.
An Observer may call addObserver or removeObserver during its observe
method.
a SubjectEvent with information relating to the change
Notifies all Observers that the Parameter with the given name has changed by calling observe on each Observer.
the language-independent or English name of the Parameter that has changed
if there is no Parameter with the given name
Private
callbackAdvances the Simulation AdvanceStrategy(s) to match the current Clock time and repaints the LabCanvas's. Calls Memorizable.memorize on the list of Memorizables after each time step. This is the callback function that is being run by the Timer.
Protected
getReturns whether broadcasting is enabled for this Subject. See setBroadcast.
whether broadcasting is enabled for this Subject
Whether the Timer is executing callback.
Returns the list of Memorizable objects stored in this MemoList.
the list of Memorizable objects
Returns true
if the Timer keeps firing even when the window is not active (not
frontmost window). The default is for the Timer to be stoppable, which prevents the
simulation from wasting CPU cycles when the user does not have the simulation window
active.
true
means the Timer keeps firing even when the browser window is
inactive
Returns the ParameterBoolean with the given name.
the language-independent or English name of the ParameterBoolean
the ParameterBoolean with the given name
if there is no ParameterBoolean with the given name
Returns the ParameterNumber with the given name.
the language-independent or English name of the ParameterNumber
the ParameterNumber with the given name
if there is no ParameterNumber with the given name
Returns the ParameterString with the given name.
the language-independent or English name of the ParameterString
the ParameterString with the given name
if there is no ParameterString with the given name
Presents an alert to the user about the exception with instructions about how to get the Simulation running again; calls pause to stop the Simulation.
the error that caused the exception
Notifies this Observer that a change has occurred in the Subject.
contains information about what has changed in the Subject: typically either a one-time GenericEvent, or a change to the value of a Parameter
Remove the LabCanvas from the list of LabCanvas's that need to be repainted and memorized after each advance of the Simulation.
the LabCanvas to remove from the list of LabCanvas's to update
Removes an object from the list of ErrorObserver objects to be notified when an error occurs.
object to remove from the list of ErrorObserver objects
Removes an object from the list of Memorizable objects.
object to remove from the list of Memorizable objects
if called during the memorize
method.
Removes the Observer from this Subject's list of Observers. An Observer may
call removeObserver
during its observe
method.
the Observer to detach from list of Observers
Removes the Parameter from the list of this Subject's available Parameters.
the Parameter to remove
Sets the Simulation to its initial conditions by calling AdvanceStrategy.reset, sets the Clock to match the simulation time (usually zero), and pauses the Clock. Broadcasts a RESET event.
the current time on the Clock after resetting
Save the initial conditions, which can be returned to with reset.
the current time on the Clock
Protected
setSets whether this Subject will broadcast events, typically used to temporarily disable broadcasting. Intended to be used in situations where a subclass overrides a method that broadcasts an event. This allows the subclass to prevent the superclass broadcasting that event, so that the subclass can broadcast the event when the method is completed.
whether this Subject should broadcast events
the previous value
Sets amount of time between displaying frames of the Simulation, in seconds. Can be set to zero, in which case the fastest possible frame rate will happen, which is usually 60 frames per second.
amount of time between displaying frames of the Simulation, in seconds.
Sets whether the Timer is executing callback. However, if non-stop mode is on, then this will not stop the Timer, see setNonStop.
true
causes the Timer to start firing
Sets whether the Timer keeps firing when the window is not active (not the frontmost window). The default is for the Timer to be stoppable, which prevents the simulation from wasting CPU cycles when the user does not have the simulation window active.
true
means the Timer keeps firing even when the browser
window is not active
Sets the length of a time step, the small increment of time by which to advance the Simulation's state. Several steps of this size may be taken to advance the Simulation time to be equal to or beyond the Clock time.
the length of a time step, in seconds.
Stops the Timer from executing callback()
, but only if the non-stop flag is
false
, see setNonStop.
Returns a minimal string representation of this object, usually giving just identity information like the class name and name of the object.
For an object whose main purpose is to represent another Printable object, it is
recommended to include the result of calling toStringShort
on that other object.
For example, calling toStringShort()
on a DisplayShape might return something like
this:
DisplayShape{polygon:Polygon{'chain3'}}
a minimal string representation of this object.
Generated using TypeDoc
Uses an AdvanceStrategy to advance the Simulation state; the process is driven by a Timer and a Clock to synchronize the Simulation with real time; updates the LabCanvas to show the current Simulation state.
How Simulation Advances with Clock
SimRunner advances the Simulation state, keeping it in sync with the Clock time, and therefore we see the Simulation advancing in real time. Here are the details:
The Timer repeatedly executes the SimRunner's callback. The callback continues being fired regardless of whether the Clock is paused, stepping, or running.
When the Clock is running or stepping,
callback()
advances the Simulation up to (or just beyond) the current clock time by calling AdvanceStrategy.advance. This keeps the Simulation in sync with the Clock.Stepping forward by a single time step employs the special step mode of Clock. When
callback()
sees the Clock is in step mode, it advances the Simulation by a single time step and then clears the Clock's step mode so that the Clock will thereafter be in the regular "paused" state.Sometimes the Simulation cannot be computed in real time. In that case
callback()
will retard the clock time when it is too far ahead of simulation time, by calling Clock.setTime. Once that happens Clock.getRealTime will be greater than Clock.getTime. We can calculate how much time has been lost due to performance problems by comparing these.When the Clock is paused
callback()
still updates the LabCanvas, so any changes to objects will be seen. This allows the user to position objects while the simulation is paused.The Timer period (the callback frequency) determines the frame rate of the simulation display, because a new frame is drawn each time the
callback()
callback fires. See setDisplayPeriod.The Timer period has no effect on how often the Simulation's differential equation is calculated; that is determined separately by the time step used when calling
AdvanceStrategy.advance()
. See setTimeStep.Stop Simulation When Window is Not Active
SimRunner listens for blur and focus events to stop and start the Timer. Those events occur when the browser window changes from being the front most active window to some other window or browser tab being active. This means the simulation will only run when the browser window is the frontmost active window. This helps reduce CPU usage when the user is not viewing the simulation.
There is a "non-stop" Parameter which allows the simulation to run even when the window is not active. This is useful if you want to view two simulations running in separate browser windows. See setNonStop.
Parameters Created
ParameterNumber named
TIME_STEP
, see setTimeStepParameterNumber named
DISPLAY_PERIOD
, see setDisplayPeriodParameterBoolean named
RUNNING
, see setRunningParameterBoolean named
FIRING
, see setFiringParameterBoolean named
NON_STOP
, see setNonStopEvents Broadcast
All the Parameters are broadcast when their values change. In addition:
RESET
, see reset.