Difference between revisions of "Analytica Script"
m |
|||
Line 1: | Line 1: | ||
+ | [[Category:Doc_Status_D]] | ||
<!-- For Lumina use, do not change --> | <!-- For Lumina use, do not change --> | ||
+ | |||
+ | __TOC__ | ||
Analytica Script is a variant of the standard Analytica modeling language used in Definitions. Most users may ignore it, but it has a few commands of value to advanced users, including commands to open or close Windows, not available in the standard modeling language. Scripts have some slight differences in syntax from the standard language, described below. | Analytica Script is a variant of the standard Analytica modeling language used in Definitions. Most users may ignore it, but it has a few commands of value to advanced users, including commands to open or close Windows, not available in the standard modeling language. Scripts have some slight differences in syntax from the standard language, described below. | ||
Line 6: | Line 9: | ||
*The Script Attribute of a Button, Picture, or input varibale. The Script is executed when the end user clicks the button, or changes the definition of an Input variable. The Script attribute is deprecated, having been replaced in Analytica 4.6 by [[OnClick]], and [[OnChange]] attributes, which use standard Analytica syntax, and also allow assignment to Global variables. | *The Script Attribute of a Button, Picture, or input varibale. The Script is executed when the end user clicks the button, or changes the definition of an Input variable. The Script attribute is deprecated, having been replaced in Analytica 4.6 by [[OnClick]], and [[OnChange]] attributes, which use standard Analytica syntax, and also allow assignment to Global variables. | ||
− | *The Typescript Window: Press | + | *The Typescript Window: Press ''Ctrl+''' (''control+apostrophe'') to open this window. It offers a command line interface to Analytica. |
− | *In the special function EvaluateScript(script), which may be used in normal Analytica language. It evaluates its text parameter, script, as a script, and so lets you execute a script anywhere -- very powerful and a bit dangerous. | + | *In the special function [[EvaluateScript]](script), which may be used in normal Analytica language. It evaluates its text parameter, script, as a script, and so lets you execute a script anywhere -- very powerful and a bit dangerous. |
=== Button Scripts === | === Button Scripts === | ||
Line 20: | Line 23: | ||
=== Choice and Checkbox variables === | === Choice and Checkbox variables === | ||
− | You can add a Script that gets executed when the user selects a new Choice or (un)checks a Checkbox for a user-input variable defined as Choice(i, n) or as Checkbox(bool): | + | You can add a Script that gets executed when the user selects a new Choice or (un)checks a Checkbox for a user-input variable defined as [[Choice]](i, n) or as [[Checkbox]](bool): |
If Script attribute does not appear in Object view of the variable, you set it to appear: | If Script attribute does not appear in Object view of the variable, you set it to appear: | ||
Line 37: | Line 40: | ||
Within a Script, you can assign a new Definition or other Attribute to a global Variable, e.g. | Within a Script, you can assign a new Definition or other Attribute to a global Variable, e.g. | ||
− | + | :<code>Y := X + 100</code> | |
− | + | :<code>Definition OF Y --> X + 100</code> | |
When you execute the script (e.g. click the button), it assigns X + 100 as the new Definition of Y. | When you execute the script (e.g. click the button), it assigns X + 100 as the new Definition of Y. | ||
Line 44: | Line 47: | ||
If you enclose the assignment in parentheses, it assigns the value, rather than the expression, as the new Definition: | If you enclose the assignment in parentheses, it assigns the value, rather than the expression, as the new Definition: | ||
− | + | :<code>(Y := X + 100)</code> | |
− | + | :<code>Definition OF Y --> 110</code> | |
Analytica does not usually let you assign a new value to the Definition (or other Attribute) of a Global variable (or other Object). This is to maintain the '''''no side-effects rule''''', which makes Analytica models so much easier to understand and debug than normal programs. Computer scientists would say it is a '''''functional'''''rather than '''''procedural'''''language.<br>But, sometimes you do want to write a procedure that makes assignments to Global (i.e. nonlocal) variables. You may also include assignments to Globals in a Function that is called from a Button script -- or from a Function that is called from a Function called from a Button script, etc. In this case, the end user is explicitly pressing the button and initiating the change to the model -- and so, side-effects are allowed. | Analytica does not usually let you assign a new value to the Definition (or other Attribute) of a Global variable (or other Object). This is to maintain the '''''no side-effects rule''''', which makes Analytica models so much easier to understand and debug than normal programs. Computer scientists would say it is a '''''functional'''''rather than '''''procedural'''''language.<br>But, sometimes you do want to write a procedure that makes assignments to Global (i.e. nonlocal) variables. You may also include assignments to Globals in a Function that is called from a Button script -- or from a Function that is called from a Function called from a Button script, etc. In this case, the end user is explicitly pressing the button and initiating the change to the model -- and so, side-effects are allowed. | ||
Line 53: | Line 56: | ||
A third key difference of Scripts is that the availability of a variety of Script commands -- e.g. | A third key difference of Scripts is that the availability of a variety of Script commands -- e.g. | ||
− | + | :<code>Open Object Y</code> | |
opens the Object window of Y. Script commands take a list of parameters, usually object names, ''without parentheses around the parameters and without separating commas''. | opens the Object window of Y. Script commands take a list of parameters, usually object names, ''without parentheses around the parameters and without separating commas''. | ||
Line 59: | Line 62: | ||
These Script commands are mostly a relic of Demos, an early predecessor of Analytica. Some no longer work or are otherwise deprecated (discouraged), but a few can be useful for special purposes. For more, see [[:Category:Typescript Commands|Commands]] and [[Scripting Guide]]. | These Script commands are mostly a relic of Demos, an early predecessor of Analytica. Some no longer work or are otherwise deprecated (discouraged), but a few can be useful for special purposes. For more, see [[:Category:Typescript Commands|Commands]] and [[Scripting Guide]]. | ||
− | === | + | === See Also === |
− | + | * [[Script]] | |
− | [[Category: | + | * [[EvaluateScript]] |
+ | * [[OnClick]] | ||
+ | * [[OnChange]] | ||
+ | * [[Scripting Guide]] | ||
+ | * [[:Category:Typescript Commands]] |
Revision as of 04:34, 21 January 2016
Analytica Script is a variant of the standard Analytica modeling language used in Definitions. Most users may ignore it, but it has a few commands of value to advanced users, including commands to open or close Windows, not available in the standard modeling language. Scripts have some slight differences in syntax from the standard language, described below.
You can define Scripts in:
- The Script Attribute of a Button, Picture, or input varibale. The Script is executed when the end user clicks the button, or changes the definition of an Input variable. The Script attribute is deprecated, having been replaced in Analytica 4.6 by OnClick, and OnChange attributes, which use standard Analytica syntax, and also allow assignment to Global variables.
- The Typescript Window: Press Ctrl+' (control+apostrophe) to open this window. It offers a command line interface to Analytica.
- In the special function EvaluateScript(script), which may be used in normal Analytica language. It evaluates its text parameter, script, as a script, and so lets you execute a script anywhere -- very powerful and a bit dangerous.
Button Scripts
You create a button in a Diagram like any other object type:
- In Edit mode, drag the button icon from the far right of node type toolbar to the position on the diagram you want (or press control-0).
- Double-click the button to open its Object Window.
Now you can review and edit its Script attribute.
Choice and Checkbox variables
You can add a Script that gets executed when the user selects a new Choice or (un)checks a Checkbox for a user-input variable defined as Choice(i, n) or as Checkbox(bool):
If Script attribute does not appear in Object view of the variable, you set it to appear:
- In Edit mode, select Attributes... from the Object menu.
- In the list of attributes, scroll down until you find Script. Check it, and click OK.
You can then type into the Script attribute of a selected variable.
Script syntax
Each line in a Script may be a standard expression from the Analytica language, such as a call to a function. There are some minor differences in syntax: You can put expressions or commands on separate lines, without needing a semicolon ";" between the lines
Assigning to globals
Within a Script, you can assign a new Definition or other Attribute to a global Variable, e.g.
Y := X + 100
Definition OF Y --> X + 100
When you execute the script (e.g. click the button), it assigns X + 100 as the new Definition of Y.
If you enclose the assignment in parentheses, it assigns the value, rather than the expression, as the new Definition:
(Y := X + 100)
Definition OF Y --> 110
Analytica does not usually let you assign a new value to the Definition (or other Attribute) of a Global variable (or other Object). This is to maintain the no side-effects rule, which makes Analytica models so much easier to understand and debug than normal programs. Computer scientists would say it is a functionalrather than procedurallanguage.
But, sometimes you do want to write a procedure that makes assignments to Global (i.e. nonlocal) variables. You may also include assignments to Globals in a Function that is called from a Button script -- or from a Function that is called from a Function called from a Button script, etc. In this case, the end user is explicitly pressing the button and initiating the change to the model -- and so, side-effects are allowed.
Script Commands
A third key difference of Scripts is that the availability of a variety of Script commands -- e.g.
Open Object Y
opens the Object window of Y. Script commands take a list of parameters, usually object names, without parentheses around the parameters and without separating commas.
These Script commands are mostly a relic of Demos, an early predecessor of Analytica. Some no longer work or are otherwise deprecated (discouraged), but a few can be useful for special purposes. For more, see Commands and Scripting Guide.
Enable comment auto-refresher