A set of Variables which represent the current state of a simulation. Variables are numbered from 0 to n-1 where n is the number of Variables.

VarsList is a Subject and each Variable is a Parameter of the VarsList. This makes the set of Variables available for scripting with EasyScriptParser.

Unlike other Subject classes, VarsList does not broadcast each Variable whenever the Variable changes. And VarsList prohibits adding general Parameters in its addParameter method, because it can only contain Variables.

As a Subject, the VarsList will broadcast the VARS_MODIFIED event to its Observers whenever Variables are added or removed.

Continuous vs. Discontinuous Changes

A change to a Variable is either continuous or discontinuous. This affects how a line graph of the Variable is drawn: DisplayGraph doesn't draw a line at a point of discontinuity. A discontinuity is indicated by incrementing the sequence number of the Variable.

It is important to note that setValue and setValues have an optional parameter continuous which determines whether the change of variable is continuous or discontinuous.

Here are some guidelines about when a change in a variable should be marked as being discontinuous by incrementing the sequence number:

  1. When a change increments only a few variables, be sure to increment any variables that are dependent on those variables. For example, if velocity of an object is discontinuously changed, then the kinetic, potential and total energy should all be marked as discontinuous.

  2. When dragging an object, don't increment variables of other objects.

  3. When some parameter such as gravity or mass changes, increment any derived variables (like energy) that depend on that parameter.

Deleted Variables

When a variable is no longer used it has the reserved name 'DELETED'. Any such variable should be ignored. This allows variables to be added or removed without affecting the index of other existing variables.

Events Broadcast

Hierarchy (view full)

Implements

Constructors

  • Parameters

    • varNames: string[]

      array of language-independent variable names; these will be underscorized so the English names can be passed in here. See Util.toName.

    • localNames: string[]

      array of localized variable names

    • Optional opt_name: string

      name of this VarsList

    Returns VarsList

    Throws

    if varNames and localNames are different lengths, or contain anything other than strings, or have duplicate values

Properties

histArray_: number[][] = []

Recent history of the simulation state for debugging; an array of copies of the vars array.

history_: boolean = Util.DEBUG

Whether to save simulation state history.

timeIdx_: number = -1

Index of time variable, or -1 if there is no time variable.

DELETED: "DELETED" = 'DELETED'

If a variable name is 'DELETED' then that variable is not in use and should be ignored.

TIME: "TIME" = 'TIME'

Language-independent name of time variable.

VARS_MODIFIED: "VARS_MODIFIED" = 'VARS_MODIFIED'

Name of event signifying that the set of variables has been modified: variables may have been added or removed, or the name of variables changed.

Methods

  • Add a Variable to this VarsList.

    Parameters

    Returns number

    the index number of the variable

    Throws

    if name if the Variable is 'DELETED'

  • Add a continguous block of ConcreteVariables.

    Parameters

    • names: string[]

      language-independent names of variables; these will be underscorized so the English name can be passed in here. See Util.toName.

    • localNames: string[]

      localized names of variables

    Returns number

    index index of first ConcreteVariable that was added

    Throws

    if any of the variable names is 'DELETED', or array of names is empty

  • Delete several variables, but leaves those places in the array as empty spots that can be allocated in future with addVariables. Until an empty spot is reallocated, the name of the variable at that spot has the reserved name 'DELETED' and should not be used.

    Parameters

    • index: number

      index of first variable to delete

    • howMany: number

      number of variables to delete

    Returns void

  • Returns index to put a contiguous group of variables. Expands the set of variables if necessary.

    Parameters

    • quantity: number

      number of contiguous variables to allocate

    Returns number

    index of first variable

  • Returns the value of the time variable, or throws an exception if there is no time variable.

    There are no explicit units for the time, so you can regard a time unit as any length of time, as long as it is consistent with other units. See About Units Of Measurement.

    Returns number

    the current simulation time

    Throws

    if there is no time variable

  • Returns the current value of the variable with the given index.

    Parameters

    • index: number

      the index of the variable of interest

    Returns number

    the current value of the variable of interest

  • Returns an array with the current value of each variable.

    Parameters

    • Optional computed: boolean

      whether to include computed variables, see Variable.isComputed; default is false.

    Returns number[]

    an array with the current value of each variable. Computed variables have value of NaN unless requested.

  • Returns the Variable object at the given index or with the given name

    Parameters

    • id: string | number

      the index or name of the variable; the name can be the English or language independent version of the name

    Returns Variable

    the Variable object at the given index or with the given name

  • Increments the sequence number for the specified variable(s), which indicates a discontinuity has occurred in the value of this variable. This information is used in a graph to prevent drawing a line between points that have a discontinuity. See Variable.getSequence.

    Parameters

    • Rest ...indexes: number[]

      the indexes of the variables to increment; if no index given then all variable's sequence numbers are incremented

    Returns void

  • Returns index of the Variable, or -1 if not found.

    Parameters

    • id: string | Variable

      the Variable or name of Variable; the name can be the English or language independent version of the name

    Returns number

    integer index number of the variable, or -1 if not found

  • Returns the number of variables available. This includes any deleted variables (which are not being used and should be ignored).

    Returns number

    the number of variables in this VarsList

  • Prints recent 'history' set of variables to console for debugging. Prints the n-th oldest snapshot, where n=1 is the most recent snapshot, n=2 is the snapshot previous to the most recent, etc. See saveHistory.

    Parameters

    • Optional index: number

      the index of the snapshot to print, where 1 is most recent; if no index is specified, then prints a selected set of recent histories.

    Returns string

    the history variables formatted as code to recreate the situation

  • Prints one set of history variables.

    Parameters

    • idx: number

      the index of the snapshot to print, where 1 is most recent;

    Returns string

  • Saves the current variables in a 'history' set, for debugging, to be able to reproduce an error condition. See printHistory.

    Returns void

  • Sets 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.

    Parameters

    • value: boolean

      whether this Subject should broadcast events

    Returns boolean

    the previous value

  • Indicates the specified Variables are being automatically computed. See Variable.isComputed.

    Parameters

    • Rest ...indexes: number[]

      the indexes of the variables

    Returns void

  • Sets whether to store recent history, see saveHistory.

    Parameters

    • value: boolean

      true means recent history should be stored

    Returns void

  • Sets the current simulation time. There are no explicit units for the time, so you can regard a time unit as seconds or years as desired. See About Units Of Measurement.

    Parameters

    • time: number

      the current simulation time.

    Returns void

    Throws

    if there is no time variable

  • Sets the specified variable to the given value. Variables are numbered starting at zero. Assumes this is a discontinous change, so the sequence number is incremented unless you specify that this is a continuous change in the variable. See incrSequence.

    Parameters

    • index: number

      the index of the variable within the array of variables

    • value: number

      the value to set the variable to

    • Optional continuous: boolean

      true means this new value is continuous with previous values; false (the default) means the new value is discontinuous with previous values, so the sequence number for the variable is incremented

    Returns void

    Throws

    if value is NaN for a non-computed variable

  • Sets the value of each variable from the given list of values. When the length of vars is less than length of VarsList then the remaining variables are not modified. Assumes this is a discontinous change, so the sequence number is incremented unless you specify that this is a continuous change in the variable. See incrSequence.

    Parameters

    • vars: number[]

      array of state variables

    • Optional continuous: boolean

      true means this new value is continuous with previous values; false (the default) means the new value is discontinuous with previous values, so the sequence number for the variable is incremented

    Returns void

    Throws

    if length of vars exceeds length of VarsList

  • Returns the index of the time variable, or -1 if there is no time variable.

    Returns number

    the index of the time variable, or -1 if there is no time variable

  • Returns the set of Variable objects in this VarsList, in their correct ordering.

    Returns Variable[]

    the set of Variable objects in this VarsList, in their correct ordering.

  • 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'}}
    

    Returns string

    a minimal string representation of this object.

Generated using TypeDoc