Buttons
Buttons are User-Interface controls that you can place on your diagrams. They usually appear with a beveled edge, with a Title that gives an indication of what they do when clicked. When a user of your model clicks on the button, an action that you define is launched. A wide variety of things can be done when a button is clicked, including changing other variables, modifying the model itself, launching evaluations, opening or closing windows, evaluating expressions, and more. In some cases you can "implement" functionality that extends the functionality that comes with Analytica.
The Ideal Gas Law example model illustrates a simplistic use of buttons.
This interface contains four buttons: Find_P
, Find_V
, Find_n
and Find_T
. When you press Find_P
, in calculates the pressure in an ideal gas given the volume, number of molecules and temperature, and then sets the value of P
to this value. We say the button has a side-effect of changing the value of variable P
.
Creating a Button
From Analytica Enterprise, a button is created by dragging a button node from the toolbar while in edit mode:
OnClick attribute
new to Analytica 4.6
When a button is pressed, the OnClick attribute of the button is evaluated. The OnClick attribute an expression using precisely the same syntax as the Definition of a variable. From the OnClick expression, you can make use of any Analytica function. Unlike a variable, side-effects such as assignments to other variables or attributes is allowed, and the final result of evaluation is not stored or returned.
For example, in the ideal gas law model (ignoring for the moment the flexibility to select units of measurement), the Find_P
script would be an Assignment to P
as follows.
P := n * R * T / V
Like a definition, expressions can span multiple lines, and sequential expressions are delineated by a semi-colon (;).
The OnClick attribute was introduced in Analytica 4.6. In Analytica 4.5 and earlier, you need to use the Script attribute.
Script Attribute
The Script attribute of the button contains typescript code that you write, and is executed after the OnClick expression evaluates. If an error occurs during the evaluation of OnClick, the Script is not executed. Typescript uses a different language from the Expression Syntax used in a variable Definition.
The Script attribute is deprecated as of Analytica 4.6 as a result of the introduction of the OnClick attribute. The problem with the Script attribute is that because it uses what is essentially a different language, it means that you need to learn about Typescript in addition to Analytica expressions. The Script attribute continues to function so that legacy models work fine, and in some rare circumstances, you may simply want to run a particular typescript command, in which case Script would be more convenient.
Because there are several gotchas with writing button scripts -- especially the confusion that often arises between the assignment operator in expression syntax and typescript assignment, both of which use := -- a recommended practice is to place all your code in a User-Defined Function, and limit your button script to a call to the function.
Picture nodes
A picture node, created when you copy/paste and image to a diagram, will also act like a button if you define an OnClick or Script attribute.
Enable comment auto-refresher