Difference between revisions of "Analytica Script"

(rewrite to add scripts for checkbox and Choice variables and clarify organization)
Line 1: Line 1:
[[Category:Doc Status D]] <!-- For Lumina use, do not change -->
+
<!-- For Lumina use, do not change -->
  
Analytica Script is a variant of the Analytica modeling language used in Definitions. It is of value to advanced users, and may be ignored by most users. It has some slight differences in syntax, and a set of Scripting Commands not available elsewhere. Most useful, lets you assign new definitions and other attributes to global variables.
+
Analytica Script is a variant of the Analytica modeling language used in Definitions. &nbsp;It is of value to advanced users, and may be ignored by most users. &nbsp;Most useful, you can assign new definitions and other attributes to global variables in a Script. (Assignments to global variables are not allowed in Analytica definitions, except in user-defined functions called from a Script, directly or indirectly.) &nbsp;You can use a range of special Scripting Commands in a Script, such as commands to open or close Windows, which are&nbsp;not available in the standard modeling language. &nbsp;Scripts have&nbsp;some slight differences in syntax from the standard language, described below.&nbsp;
  
You can use Scripts in:
+
You can define Scripts in:  
* The Script Attribute of Buttons. This script is executed when the end user clicks the button.
 
* The Typescript Window: Press control+' (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 ===
+
*The Script Attribute of a Button. The Script is executed when the end user clicks the button.
 +
*The Script Attribute of a Variable defined as a&nbsp;Choice or Checkbox user input. The Script is executed when the user changes the selection from the Choice, or the check in a Checkbox.
 +
*The Typescript Window: Press control+' (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.
  
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).
+
=== Button Scripts  ===
  
Double-click the button to open its Object Window. Now you can review and edit its Script attribute.
+
You create a button in a Diagram like any other object type:
  
=== Script syntax ===
+
#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).
Each line in a Script may be a standard expression from the Analytica language, such as a call to a function. One difference from Analytica language syntax, is that you can put multiple lines with no ";" or other separator between lines (other than ''newline'').
+
#Double-click the button to open its Object Window.
  
Another key difference is that you can put an assignment to a global variable, e.g.
+
Now you can review and edit its Script attribute.
   Y := 10 + 100
+
 
  Definition OF Y --> 10 + 100
+
=== Choice and Checkbox variables ===
When you execute the script (e.g. click the button), it will assign X + 100 as a new definition to Y. If you put the assignment in parens, it assigns the value, rather than the expression:
+
 
   (Y := X + 100)
+
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):
  Definition OF Y --> 110
+
 
  Defeinition Y
+
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.<br>
 +
#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:&nbsp;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&nbsp;:= X + 100
 +
  Definition OF Y --&gt; 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&nbsp;:= X + 100)
 +
Definition OF Y --&gt; 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 '''''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.
 +
 
 +
=== Script Commands ===
 +
 
 +
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 you can use a variety of Script commands, e.g.
 
 
   Open Object Y
 
   Open Object Y
will open the Object window of Y.  Script commands take a list of parameters, usually object names, without parens and without separating commas.
 
  
For more, see [[:Category:Typescript Commands|Commands]] and [[Scripting Guide]].
+
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''.  
  
=== Assignments ===
+
For more, see [[:Category:Typescript Commands|Commands]] and [[Scripting Guide]].
  
Analytica does not normally 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 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 permissable.
+
<br>
  
 +
<br>
  
 +
Unlike normal Analytica language, you may
  
Unlike normal Analytica language, you may
+
[[Category:Doc_Status_D]]

Revision as of 19:24, 19 October 2010


Analytica Script is a variant of the Analytica modeling language used in Definitions.  It is of value to advanced users, and may be ignored by most users.  Most useful, you can assign new definitions and other attributes to global variables in a Script. (Assignments to global variables are not allowed in Analytica definitions, except in user-defined functions called from a Script, directly or indirectly.)  You can use a range of special Scripting Commands in a Script, such as commands to open or close Windows, which are 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. The Script is executed when the end user clicks the button.
  • The Script Attribute of a Variable defined as a Choice or Checkbox user input. The Script is executed when the user changes the selection from the Choice, or the check in a Checkbox.
  • The Typescript Window: Press control+' (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:

  1. 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).
  2. 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:

  1. In Edit mode, select Attributes... from the Object menu.
  2. 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.

For more, see Commands and Scripting Guide.




Unlike normal Analytica language, you may

Comments


You are not allowed to post comments.