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

Creating Lists and Arrays

To create a list or array class:

Setting the Array Length

The Array-length attribute lets you specify how many elements the array contains. The default value is 0.

The array length should correspond to the number of initial values that you provide. If the number of initial values differs from the length of the array, G2 populates the array with the initial value for the array type.

Defining the Element Type

The Element-type attribute defines the class or type of the list or array. You cannot change the value of this attribute, unless you modify the Attribute-initializations attribute of user-defined class of ITEM-LIST or ITEM-ARRAY.

The valid types or classes for lists and arrays are listed in the section The List and Array Classes.

Allowing Duplicate List Elements

The Allow-duplicate-elements? list attribute determines whether a list permits duplicate elements.

Changing the default yes value to no disallows duplicate list elements in the list.

If you allow duplicate elements in a list, and then change the attribute, G2 does not indicate whether duplicate elements already exist, but prevents you from subsequently adding any duplicates.


Note: Inserting elements into a list that does not allow duplicate elements takes longer than into a list that allows duplicates.

For information on the backward compatibility feature for inserting duplicate elements into lists, see Ignoring Duplicate List Element Error.

Providing Initial Values for Array Elements

The Initial-values attribute specifies an initial value for each array element. The number of initial values should correspond with the array length or should contain one initial value.

You can use this attribute to populate an array as the section Populating an Array describes. For G2-ARRAYs and ITEM-ARRAYs, you can specify item names as the initial values.

Elements in arrays must always have a value, except for ITEM-ARRAYs and G2-ARRAYs, which can have an initial value of none. If you do not specify initial values for the array, G2 provides initial values as follows.

Array Type Default
G2-ARRAY
none
VALUE-ARRAY
0.0
ITEM-ARRAY
none
SYMBOL ARRAY
g2
TEXT-ARRAY
""
TRUTH-VALUE-ARRAY
false
QUANTITY-ARRAY
0.0
FLOAT-ARRAY
0.0
INTEGER-ARRAY
0

G2-ARRAY Initial Values Conflict

You can set the initial values of a G2-ARRAY subclass in the Attribute-initializations attribute of its class definition.

Specifying Symbolic Initial Values in Arrays

Three types of arrays can contain symbolic values:

When you enter symbolic values in the Attribute-initializations attribute of a class definition, the syntax differs slightly depending on the array class.

For G2-ARRAY and VALUE-ARRAY classes, precede symbolic names with the symbol. For SYMBOL-ARRAYs, enter only the symbolic name, as follows:

For this array class... Enter symbolic values like this...
G2-ARRAY AND
VALUE-ARRAY

the symbol cold, the symbol warm, the symbol hot
SYMBOL-ARRAY
cold, warm, hot

Using Permanent-Membership Lists and Arrays

If the List-is-permanent attribute of a list, or the Array-is-permanent attribute of an array is no (the default), membership in the list or array is transient:

You can make list and array element knowledge permanent by changing the value of the List-is-permanent or Array-is-permanent attribute to yes. A permanent-membership list or array is unaffected by resetting or restarting G2, and by saving and loading the KB, provided that all member items have the properties described in Complying to Permanent Membership.

Levels of Permanency in Lists and Arrays

The List-is-permanent and Array-is-permanent attributes do not determine whether the list or array item is itself permanent, and do not determine whether any items referenced as elements are permanent. The attributes control only whether membership is permanent.

Thus three levels of permanency apply to lists and arrays:

Be careful not to confuse these levels of permanency when you work with lists and arrays.

Initial Values of Arrays

Unlike lists, arrays can have initial values. If you do not provide initial values for an array, G2 populates each element with its initial value, which is type dependent. For example, for an INTEGER-ARRAY, the G2 supplies an initial value of 0; for a symbol array, G2 supplies the symbol g2.

When a permanent-membership array is first activated, G2 initializes it with either its default or user-provided initial values. The array then maintains initial or changed values throughout KB Restart and Reset operations, no further initialization takes place, and the values are saved in the KB.

Complying to Permanent Membership

For an item to remain in a permanent-membership list or array when G2 is reset, the item must be permanent. Otherwise resetting G2 will delete it, as with any transient item, precluding its continued membership in the list or array.

For an item to remain in a permanent-membership list or array when a KB is saved and reloaded, the item must be uniquely identifiable. Otherwise G2 will not be able to identify the item referenced in the list or array and restore the reference to it.

An item is uniquely identifiable if any of the following is true:

You can add a UUID to a class by creating a subclass that includes the unique-identification mixin class as one of the direct superior classes. G2 then includes a UUID attribute and automatically generates a UUID value when an instance is created. For more information about UUIDs, see Universal Unique Identifiers.

Failure of an item to comply to permanent membership does not preclude its inclusion in a permanent-membership list or array. G2 does not signal an error if you populate permanent-membership list and array elements with non-compliant items, and does not post any messages if their non-compliance causes G2 to remove them from the list or array.

Maintaining Permanent-Membership Lists and Arrays

It is the KB developer's responsibility to monitor and maintain the integrity of permanent-membership lists and arrays. After adding items to them, G2 does not monitor members for conformance to permanent membership, nor does it inform you if it is unable to save any list or array item as a permanent member during a KB save or reset operation.

For items participating in permanent-membership lists and arrays, G2 does not signal an error if you:

Restoring Permanent-Membership Lists and Arrays

After successfully saving permanent-membership lists and arrays that contain items, G2 attempts to restore each item to membership when you load the KB. The inability to restore a previously saved list or array item member is called a rendezvous failure. Such a failures occurs if G2 is unable to locate one or more of the item members at KB load time.

This is how G2 handles rendezvous failures:

If ... Then G2 ...
The name or UUID reference in a list or array does not correspond to any item because the item has been deleted, changed its name, or lost its UUID
Removes the list member. Replaces the item reference for an array with no value.


Note: Single-module KB saving is not compromised by inter-module item references in permanent lists and arrays.

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

Copyright © 1997 Gensym Corporation, Inc.