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

Working With Composite Types

The G2 composite types are structure and sequence. Composite types are those that are composed of one or more values of any general, specific, or composite type. G2 represents system-defined attributes whose values consist of complex data structures with structures and sequences.

A sequence is a list-like value that can contain any item or value, including other sequences and structures.

A structure consists of one or more pairs of names and values. The values of a name/value pair can consist of other structures or sequences. Use structures to represent item attributes and their values. A structure requires an even number of arguments.

For more information about working with sequences and structures, see Chapter 12, Attribute Access Facility.

Using the Structure Type

A structure value type consists of a set of subattribute name and value pairs, separated by a colon (:). All name-value subattribute pairs are separated with a comma (,) in this construct:

As an example, the next diagram shows the structure returned for the History-keeping-spec attribute of a variable:


Structures, which can have a virtually unlimited number of name-value subattributes (up to 523,263), including zero, are functionally similar to items. As such, you can access their attributes by:

The function structure ( ) creates and returns new structures.

Because structures consist of name and value pairs, they require an even number of arguments. The values of attributes of type structure can be any item or value, including other structures and sequences.

When the subattribute of a structure consists of an item, and that item is deleted from the KB, the attribute name remains within the structure, but has a value of none.

Structure Functions

Use these functions for working with structures:

To create a new structure with given attribute values:

When concluding new values using the structure ( ) function, omitting one or more subattributes replaces their current value with none. For example, if the History-keeping-spec attribute of a float-variable float-var-1 is currently:

then the value of that attribute is expressed as:


Concluding a new value for the number of data points with an expression such as:

results in the value changing to this:


To change one or more subattributes without changing other subattributes to the value none, use the change-attribute function or a subattribute reference to conclude a new value. For example, this expression:

changes the value of one subattribute, without changing others.

To create a new structure with given evaluated attribute values:

To create a new structure with a changed attribute:

To create a new structure with a changed evaluated attribute:

To create a new structure with a removed attribute:

To create a new structure with a removed evaluated attribute:

Structure Expressions

Use these expressions to access structures. You can refer to a system-defined attribute directly.

To return an attribute value:

To return a named by attribute value:

To return attribute values:

To return symbols of attributes within a structure:

To return attributes within a structure:

Using the Sequence Type

A sequence value type is a list-like entity that can contain any item or value, including other sequences and structures. Sequences can have a virtually unlimited number of elements (up to 1,046,526), including zero. Each sequence element is separated with a comma (,) in this construct:

As an example, the next diagram shows the sequence representing a portion of the Item-configuration attribute value.


Sequences are functionally similar to lists. As such, you can access their elements by:

When referencing and using sequences, remember that, unlike lists and array items, sequences are values. While you can use sequences in some list and array expressions, you pass sequence values as a copy, rather than as a reference, and change their values using the sequence functions.


Note: Deleting an item contained in a sequence changes the element value to none, but does not decrease the number of elements.

Sequence Functions

Use these functions to create and manipulate sequences.

To return a new sequence containing the given elements:

For example:

To return a new sequence one element shorter than the given sequence:

To return a new sequence whose first element is the given item-or-value:

To return a new sequence with specific elements inserted:

To return a new sequence with inserted arguments:

To return a new sequence with inserted elements before a given index:

To return a new sequence with inserted elements after a given index:

To change a single element of a sequence:

To concatenate two or more sequences:

To get a portion of a sequence:

Sequence Expressions

Use these expressions to access sequences:

To obtain a particular element in a sequence:

Returns the nth item-or-value in the given sequence with the first element at index 0.


Note: G2 allows text to be indexed, returning the character code of the zero-based nth character.

To iterate over elements in a sequence:

To return an element of a particular type from a sequence:

To determine whether an item-or-value is a member of a sequence:

To determine the number of elements in a sequence:

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

Copyright © 1997 Gensym Corporation, Inc.