You can also use variables to obtain data from external data sources.
For information about creating external variables, see Using Variables to Represent External or Simulated Data.
Variable and Parameter Class Hierarchy
G2 provides built-in variable and parameter classes that correspond to the basic data types. In addition, G2 supports a number of additional variable classes that correspond to the various types of external data servers.
![]() |
Creating a Subclass of Variable or Parameter
If your application requires a variable or parameter with the same specifications in numerous locations, it is often useful to subclass one of the built-in variable or parameter classes.integer-parameter, which updates once every 2 seconds and which keeps a history of 100 data points, or you might create a subclass of float-variable, which obtains its data from an external data source and expires once every 10 seconds. By creating subclasses of variables and parameters, you avoid having to initialize the attributes each time an instance of the variable or parameter is created.
To create a subclass of variable or parameter:
Attribute-initializations for the class to initialize the Initializable-system-attributes, as needed.
Options-for-variable and Options-for-parameter to control forward and backward chaining
History-keeping-spec-for-variable-or-parameter to control the amount and type of history that the variable or parameter keeps
Initial-value for both variables and parameters
Validity-interval to control the expiration time for variables
Data-server to control the source of data for variables
given by a variable or parameter subclass.
To declare an attribute to contain a subclass of variable or parameter:
In the Class-specific-attributes of the class whose attribute will contain a variable or parameter, use the initially is given by syntax to refer to a subclass of variable or parameter.
float-history initially is given by a float-var
float-var is a subclass of the built-in G2 class float-variable.
Guidelines for Using Typed Attributes, Parameters, and Variables
Follow these guidelines for choosing between typed attributes, parameters, and variables. Use Typed Attributes Whenever Possible
Use typed attributes whenever possible when you only need to keep track of the current value of the data.
Use Parameters for Simple Histories
Use parameters when you need to:
if rules when the value of the parameter changes.
if rules when the value of the variable changes, or
Choose the Most Efficient Data Structure
When choosing between typed attributes, always choose the most efficient type of attribute that provides the minimum required functionality. Thus:
For a detailed explanation, see Use Strong Typing.
Try to Avoid Time-Based History Specifications
Always limit the size of histories of variables and parameters. Whenever possible, limit histories by specifying the maximum number of data points as opposed to the maximum age of the history. History specifications based on time cause G2 to allocate memory at run-time, which can cause processing delays. Also, history references are slower than array references, so you might want to use arrays instead of histories in performance-critical applications.
To specify a history based on points:
Specify the History-keeping-spec attribute of a variable or parameter as follows:
keep history with a maximum number of data points = integer