search

Class myphysicslab.lab.util.EasyScriptParser

Provided By
All Implemented Interfaces

Parses simple "EasyScript" scripts which get or set values of Parameters for a specified set of Subjects. Also executes some single word commands such as help.

The #script method creates a script which replicates all the current Parameter settings of all the specified Subjects.

The scripts can be executed by entering them in the myphysicslab.lab.util.Terminal command line interface, which passes them to EasyScriptParser. They can also be executed with Terminal functions such as eval and parseURL.

An application will set up EasyScriptParser to know about its dozen or so important Subjects. EasyScriptParser interrogates all the Subjects to find what settable Parameters they contain and their current values.

See Subject-Observer Pesign Pattern for background information about Parameters and Subjects.

EasyScript Syntax

The 'getter' syntax is

[SubjectName.]ParameterName[;]

The 'setter' syntax is

[SubjectName.]ParameterName=value[;]

where square brackets indicate optional elements. Multiple scripts can be on one line separated by a semicolon.

With both setter and getter syntax the value of the Parameter is available via #getResult after the script is parsed or in the result variable of Terminal.

  • SubjectName is the language-independent name returned by myphysicslab.lab.util.Subject#getName. If SubjectName is not provided, then the the list of Subjects is examined in order and the first Parameter with the given name is selected. The order is based on how the Subjects are passed in to the EasyScriptParser constructor.

  • ParameterName is the language-independent name returned by myphysicslab.lab.util.SubjectEvent#getName. (Note that Parameter extends SubjectEvent).

  • value is a string that is given as input to myphysicslab.lab.util.Parameter#setFromString. The string can be optionally surrounded with quotes like a JavaScript string in which case the quotes are removed and backslash escaped characters (quote, newline, tab, etc.) are replaced. If not surrounded with quotes then the string ends at the semicolon or end of line.

The English language version of Parameter or Subject names can also be given, they are converted to the language-independent form using myphysicslab.lab.util.UtilityCore#toName. Leading and trailing spaces are trimmed from names and (unquoted) values.

EasyScript Embedded in URL

To share a customized simulation with someone else, the #scriptURL method gives the URL of the current page along with a script that sets Parameters to their current values. The script follows a question mark in the URL, so it is called a 'query script' or 'query URL'. Here is an example:

http://www.myphysicslab.com/PendulumApp_en.html?DRIVE_AMPLITUDE=0;
DAMPING=0.1;GRAVITY=9.8;ANGLE=2.5;ANGLE_VELOCITY=0;DRAW_MODE=lines

A user can then send this custom URL to someone else, and when that other user enters the URL into a browser, the scripts embedded in the URL will be executed if myphysicslab.lab.util.Terminal#parseURL is called at startup that app, assuming that EasyScriptParser has been installed via myphysicslab.lab.util.Terminal#setParser.

The method #script returns just the script without the URL. This can be useful when creating a script to save with the myphysicslab.lab.util.Terminal#remember command, or to put in an HTML file, or paste into Terminal

Volatile Subjects

Summary: The initial settings for Parameters of volatile Subjects are recalculated whenever #update is called (which indicates that the configuration has changed). This results in a shorter script when specifying a different configuration at time zero.

Many applications have multiple configurations that the user can choose between. For example, in the Newtons Cradle simulation you can choose the number of pendulums. Each configuration has an associated set of initial conditions stored in the variables that define the state of the simulation.

To make the URL script as short as possible, we recalculate the set of initial conditions after selecting a new configuration. The configuration should itself be controlled by a Parameter, for example NewtonsCradleApp.en.NUM_BODIES. If the URL script specifies the configuration Parameter then we can assume the initial conditions for that configuration are in place.

This is accomplished by specifying a set of 'volatile' Subjects as an argument to the constructor. The myphysicslab.lab.model.VarsList is the most common volatile Subject, but there can be others as well. These are treated differently in that we recalculate the initial settings for Parameters of volatile Subjects during the #update method, which should be called whenever the configuration changes. (Note that Variables are also Parameters).

Other than recalculating the initial settings more frequently, the volatile Subjects are treated the same as other Subjects.

Technical Notes

The set of Parameters can change over time; this is especially true for Variables which are a type of Parameter. It is common for the engine2D physics engine apps (which use myphysicslab.lab.engine2D.ContactSim) to have a config() method which changes the set of rigid bodies and therefore the set of Variables.

To cope with a changing set of Parameters, we store only Parameter names, not references to actual Parameters. We store a parallel list of Subjects, so we can get the actual Parameter by asking the Subject for a Parameter with that name. This allows for the Parameter to be a different object, but as long as it has the same name and belongs to the same Subject we can find the Parameter and set or get it's value.

Single Word Commands

A small set of single word commands are recognized by EasyScriptParser, for example help. The help command lists the available single word commands. Use #addCommand to add a single word command.

new EasyScriptParser( subjects, volatile )

Parameters
subjectsArray<myphysicslab.lab.util.Subject>

list of Subject's to gather Parameters from; note that the order here is significant; the Parameters are processed according to the order of the Subjects in this list.

volatile(Array<myphysicslab.lab.util.Subject>|undefined)

those Subject's whose initial conditions change depending on another configuration parameter. Generally this is the VarsList of a simulation. These must also be included in subjects.

Instance Methods

Static Functions

Static Properties

Type Definitions