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

Declaring a Foreign Function in a KB

Before using a foreign function within an expression, you must declare it to G2. Declare a function as foreign by choosing:

Selecting this declaration creates an instance of the FOREIGN-FUNCTION-DECLARATION class, and invokes the Text Editor immediately so that you can complete the declaration.

The following figure shows an example declaration of one of the sample functions used throughout this chapter, g2-demo-add.


The declaration text of this item is the declare foreign function statement (followed by the name of the function, its arguments and return type) that has been compiled and linked into the foreign image to which G2 is connected.

After you close the edit of the foreign function declaration, place the declaration on the workspace, and open its attribute table. The next figure shows part of such an attribute table with the declaration text.


After connecting to a foreign image, G2 maintains an internal list of the functions contained in the foreign image file. When you enter the foreign function name, G2 checks the arguments and return value against the information it has in the internal list. If a function of the name you enter does not exist, or if argument or return value discrepancies do exist, G2 displays an appropriate message in the Notes attribute of the foreign function declaration.

The class-specific attributes of G2 FOREIGN-FUNCTION-DECLARATION items are:

Attribute Description
Name-in-foreign-image
The name of the C function as it appears in the foreign image.
Allowable values:
Any foreign function name, text string.
Default value:
The name of the function in the declaration text, in lowercase.


Timeout-interval
The amount of time before G2 times out while waiting for a foreign function to return.
Allowable values:
any valid time interval
use default

Default value:
use default

Providing the Name of the C Function

The Name-in-foreign-image attribute indicates the exact name of the C function as it appears in the foreign image (the name you entered in the template file). By default, the value of this attribute is a lowercase version of the function name you enter in the declaration text.

If you edit the default name provided here, subsequently editing the declaration text will not update the Name-in-foreign-image attribute.

For instance, if you enter a function name as my-foreign-function in the declaration text, that name will appear in this attribute. If you then change the value to:

(using underscore characters instead of dashes as word separators), G2 will not change this value if you edit the declaration text later. Instead, you must edit this attribute directly.

Setting the Timeout Interval

The Timeout-interval attribute determines the amount of time allowed before G2 times out while waiting for a value from a foreign function. The value of this attribute (other than use default) overrides the Foreign-function-timeout-interval value set in the Timing Parameters system table, which is 30 seconds by default.

Typically, when G2 fails to receive a value from a foreign function, it means that the foreign image has exited due to a coding error.

Handling Possible Name Collisions

G2 maintains a name-to-image association between the Name-in-foreign-image attribute and the function name as it appears within the foreign image.

Because G2 can connect to more than one foreign image, the possibility of a name collision exists. A name collision occurs when a foreign image transmits a foreign function name for which G2 already has an association. G2 may have obtained that name from a different foreign image or from a name previously transmitted by the foreign image to which G2 is connecting. When checking for name collisions, G2 distinguishes between upper-and lower-case names.

If G2 detects a name collision, it issues a warning in the logbook and replaces the old name-to-image association with the new one. Although G2 can detect the collision and issue a warning, we recommend that you make sure that each foreign function in your KB has a distinct name and that you do not depend on this behavior.

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

Copyright © 1997 Gensym Corporation, Inc.