![]() |
Note: G2 supports the same hierarchy of data types when declaring arguments and local names of methods and procedures.
For information on sequences and structures, see Using Structures and Sequences to Represent Composite Data.
Using Indexed Attributes
Indexed attributes provide an efficient way of accessing objects based on the value of an attribute of the object. When you declared an attribute with an index, G2 creates a hash table, which maps the value of the indexed attribute to the object in which it occurs. Hash tables provide rapid search capabilities based on attribute value by using an algorithm known as "hashing." if there exists ... such that statements.
Note: You typically do not use indexed attributes with floating point values.
To define an indexed attribute:
Declare Class-specific-attributes by using this syntax:
my-indexed-attribute is an integer, initially is 0, with an index
Strong typing means that you declare the attributes of a class, using the most restrictive data type applicable to the data. By declaring an attribute to have a data type, you ensure that the application performs type checking whenever the attribute receives a value, either from the operator or from G2. In addition, by always declaring an attribute to have a default value, you ensure that G2 can always perform its initial computation without error.
Specifically:
value or quantity data type when the value is an integer.
value data type when the data are either numbers or symbols.
text data type when the value is a text string.
item-or-value data type. Always declare class-specific attribute by using this syntax to provide an explicit data type:
number-of-connections is an integer, initially is 0
number-of-connections initially is 0, because this implicitly declares the attribute to be an item-or-value data type. If you do not declare type information, G2 cannot optimize references to the attribute. Furthermore, you introduce the possibility of storing unexpected types of data in your objects, which means you will have to include existence checking and type checking in your code. You should strive to create code that minimizes the need for existence and type checking. For more information about existence and type checking, see Avoid Existence and Type Checking.
Enumerate Symbolic Values
Enumerate the possible values of symbolic attributes by using this syntax:
status is a symbol, has values open, closed, or deferred, initially is open
Also, never use
none as the value of a symbolic attribute, because it is easily confused with the empty attribute value. Use unspecified, no-value, or a similar term instead.
truth-value data type rather than a symbolic value. This data type is more reliable because it does not require that you remember the unique symbol name. Instead, you will always use true or false.
For more information, see Using Indexed Attributes.
Always Declare an Attribute for Setpoints, Limits, and Constants
When declaring setpoints, limits, and constants, always declare an attribute or parameter for this type of data; never hard-code these values into a procedural statement or a rule. By providing attributes for all such constants, you create more generic code that you can easily configure later.