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

Using Structures and Sequences to Represent Composite Data

Your application might need to represent more complex data, such as nested data structures of a composite type. In particular, G2 represents all system-defined attributes, using these two nested data structures:

Another term for a structure is a property list.

Structures and sequences offer similar functionality to items and lists, respectively. However, for performance reasons, you should not use them interchangeably.

Accessing System-Defined Attributes

The primary reason for using structures and sequences is to access the system-defined attributes of all the built-in G2 classes, using the attribute access facility. Attribute access enables you to:

Comparison Between Structures and Items

The properties of structures and items are:

Property Structures Items
Has iconic representation


Has methods


Has inheritance


Can save as permanent knowledge


Consists of attributes and values


Can have nested structures and sequences


Can conclude values into attributes


Has fixed set of attributes defined by a class definition


Has arbitrary set of attributes that you can add to and removed from, per instance


Must be created and deleted explicitly


Requires minimum memory


Memory and existence are managed automatically


Comparison Between Sequences and Lists

The properties of sequences and lists are:

Property Sequences Lists
Has iconic representation


Requires minimum memory


Can save elements as permanent KB knowledge


Can have elements of structures and sequences


Can programmatically manipulate elements


Supports fast insertion and removal


Guidelines for Creating Composite Data Structures

In addition to using sequences and structures to access system-defined attributes, you can also use sequences and structures to create your own composite data structures. Follow these guidelines for deciding when to use sequences and structures.

Use Structures to Create Items with Dynamic, Nested Attribute Values

Use structures when you need to:

Use Sequences to Create Nested Lists

Use sequences when you need to:

In general, if you are manipulating large collections by inserting and removing elements, you should use lists rather than sequences for maximum efficiency. The reason is that inserting and removing at the beginning or end of a list is a constant time operation. However, the time it takes to insert a sequence at the beginning or end of another sequence depends on the length of the sequence. For sequences with more than ten elements, the performance penalty can be significant.

The exception to this recommendation is when you are inserting a sequence at the end of the same sequence, which is a constant time operation. Thus, if this is the only type of manipulation you are doing, you can use a sequence; otherwise, use a list.

Using Structures to Represent Attribute-Value Pairs

A structure consists of any number of attribute name and value pairs, separated by a colon (:). All name-value attribute pairs are separated with a comma (,). For example, here is the structure returned for the History-keeping-spec attribute of a variable:


You can declare data types, using structure in the same contexts that you declare any data type:

Creating and Accessing a Structure

You use the same function for creating user-defined structures that you use for accessing system-defined structures.

To create or access a value of type structure:

Accessing Values of a Structure

Structures are analogous to items in G2. Therefore, you can access their attributes by:

Using Sequences to Represent Nested Data Structures

A sequence is a list-like data structure that consists of any number of items or values, separated with a comma (,), including other sequences and structures. For example, here is the sequence representing a portion of the Item-configuration attribute value:


You can declare data types, using sequence in the same contexts that you declare any data type:

Creating and Accessing a Sequence

You use the same function for creating user-defined sequences that you use for accessing system-defined sequences.

To create or access a value of type sequence:

Accessing Values of a Sequence

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


Note: The only constant-time operation for manipulating sequences is when you use S = insert-at-end(S, element). The performance of the other operations depends on the number of elements in the sequence.

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


Note: Deleting an item contained in a sequence removes the item. For example, if a sequence consists of six item elements and one item is deleted from the KB, the sequence then consists of five elements.

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

Copyright © 1997 Gensym Corporation, Inc.