Getting Started | Documentation | Glish | Learn More | Programming | Contact Us |
Version 1.9 Build 1556 |
|
Along with clients and asynchronous shell commands, a final way to create an agent is using a subsequence. A subsequence is just like a function except when called it returns an agent value, which you can then use to send and receive events to and from the subsequence. In the body of a subsequence the predefined variable self refers to its agent value. For example, the following script creates two subsequences, and when executed it prints 36 followed by [8 125 1030.3]:
subsequence power(exponent) { whenever self->compute do self->ready( $value ^ exponent ) } square := power(2) cube := power(3) square->compute( 6 ) cube->compute( [2, 5, 10.1] ) whenever square->ready, cube->ready do print $value
The first set of statements defines power as a subsequence that is invoked with an argument exponent and responds to compute events by generating a ready event whose value is the value of the compute event raised to the given exponent. (The keyword subsequence, by the way, can be abbreviated subseq.) The two assignments bind square and cube to agents corresponding to different instances of power. The next two statements send those agents compute events with a single integer value and a three-element double-precision vector value, respectively. The final whenever statement prints the value of any ready events generated by square or cube.
In a sense, a subsequence provides a separate ``thread" running inside the Glish interpreter. Each instance of a subsequence has its own set of local variables which it ``remembers" between receiving events. Presently there is little mechanism for controlling a subsequence (as discussed in the previous section). In particular, there is no way right now to terminate an instance of a subsequence once it has begun. We do not yet have much experience with subsequences so it is possible that they will change somewhat in the future.
Subsequences are a somewhat more disciplined instance of the more general create_agent function (§ 7.2.1, page , § 10.9, page ). In particular, the following subsequence:
subseq example(x, y) { do_something(x) whenever self->do_y do do_something(y) }is identical to:
func example(x, y) { self := create_agent() do_something(x) whenever self->do_y do do_something(y) return self }