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

Using Attributes to Represent Value Types

Your application contains numerous typed values such as:

G2 supports these data structures for representing simple values and types:

This section discusses the various data types that you can use to declare attributes and provides general guidelines for representing typed values.

Hierarchy of Data Types

G2 supports this hierarchy of data types when declaring attributes specific to a class:



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."

Indexed attribute lookups are much faster than explicit searches, such as looping over a class, or implicit searches, such as if there exists ... such that statements.


Note: You typically do not use indexed attributes with floating point values.

To define an indexed attribute:

Guidelines for Declaring Typed Attributes

This section discusses various guidelines for declaring data types for class-specific attributes.

Use Strong Typing

Always declare attributes by using strong typing to avoid writing procedural statements that require explicit type checking, whereby G2 must check to see if a particular value is of the correct type.

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:

Avoid Declaring Attributes with the Item-or-value Data Type

If possible, avoid declaring attributes with the item-or-value data type.

Always declare class-specific attribute by using this syntax to provide an explicit data type:

Never use the declaration 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:

You need enumerations when you auto-generate a GUIDE dialog. They also provide a degree of self-documentation.

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.

Use Truth Values for Two Alternative Symbolic Values

If there are only two symbolic values for an attribute, such as on/off or yes/no, you should use the 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.

Use Indexed Attributes to Access Objects Based on Attribute Value

Define your classes with indexed attributes when you need to find objects based on attribute value.

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.

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

Copyright © 1997 Gensym Corporation, Inc.