| Prev | Next | Start of Chapter | End of Chapter | Contents | Glossary | Index | Comments | (5 out of 8)

Using Variables and Parameters to Represent Historical Data

Often, your application needs to keep a history of typed data for time-based analysis and reasoning. G2 supports these data structures for keeping a history of typed data:

You specify the data type by using one of the built-in variable classes directly or by creating a subclass of the appropriate variable or parameter class.

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.

This figure shows the hierarchy of built-in parameter and variable classes:


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.

For example, you might create a subclass of 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.

Once you have created a subclass of variable or parameter, you can declare an attribute of a class to be given by the variable or parameter subclass, or you can create an instance of the variable or parameter subclass and use the instance directly.

When choosing the superior class for a variable or parameter subclass, you should always choose the most restrictive class for the type of data.

To create a subclass of variable or parameter:

  1. Create a class definition whose superior class is one of the built-in G2 variable or parameter classes.

  2. Specify Attribute-initializations for the class to initialize the Initializable-system-attributes, as needed.

You can initialize these system-defined attributes:

Declaring an Attribute that Contains a Variable or Parameter

In some cases, it makes sense to create a subobject that contains a variable or parameter. For example, you do this when you want to keep a history of typed values that the application computes over time. To do this, you declare the attribute of a class to be given by a variable or parameter subclass.


Note: In general, we do not recommend embedding in a subobject variables that obtain their values from external sources.

For a discussion of embedding external variables in subobjects, see Do Not Embed External Variables in Subobjects.

To declare an attribute to contain a subclass of variable or parameter:

For example:

where float-var is a subclass of the built-in G2 class float-variable.

Comparison Between Typed Attributes, Parameters, and Variables

When you represent data in G2, you must think carefully about whether to use a simple typed attribute, a parameter, or a variable. This table summarizes the important differences between these types of attributes in G2:

Typed Attributes
Parameters

Variables
Efficiency for value assignments
Low
Low
Higher
Efficiency for value references
High
High
Lower
Type checking
Yes
Yes
Yes
Initial value
No
Yes
Yes
History
No
Yes
Yes
Specify value through G2 action buttons, radio buttons, check boxes, sliders, and type-in boxes
No
Yes
Yes
Forward chaining
Always
Only when value changes
Only when value or expiration changes
Backward chaining
No
No
Yes
Data expiration
No
No
Yes
Default update interval
No
No
Yes
Data seeking
No
No
Yes
Formulas
No
No
Yes
External data source
No
No
Yes

As this table shows, a significant trade-off exists between efficiency and functionality among typed attributes, parameters, and variables:


Note: While parameters are as inefficient as attributes for value assignments, they offer somewhat of a performance advantage over attributes when the value of the parameter does not change. The difference is that attributes always forward chain, whereas parameters only forward chain when the value changes.

For more information, see the references in this table:

For information on... See...
Using parameters and variables for forward chaining
Using "If" Rules for Data-Driven Processing
Backward chaining, data expiration, default update interval, data seeking, formulas, external data sources
Using Variables to Represent External or Simulated Data

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.

For more information, see Guidelines for Declaring Typed Attributes.

Use Parameters for Simple Histories

Use parameters when you need to:

Use Variables for More Complex Histories

Use variables when you need to:

For information about external data sources, variable expiration, and data seeking, see Using Variables to Represent External or Simulated Data.

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:

Choose the Most Restrictive Data Type

When choosing the superior class for a variable or parameter subclass, you should always choose the most restrictive class for the type of data.

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:

| Prev | Next | Start of Chapter | End of Chapter | Contents | Glossary | Index | Comments | (5 out of 8)

Copyright © 1997 Gensym Corporation, Inc.