SleepLink

From OpenCog
Jump to: navigation, search

The SleepLink is a kind of FunctionLink that, when executed, will first execute its outgoing set, which should result in a NumberNode, and then use the value of the NumberNode to sleep the current thread for that many seconds. The result of execution is a NumberNode holding how many seconds remain for the sleep (currently zero, unless the thread is interrupted by some outside mechanism).

It only makes sense to use this link in it's own thread; you don't want to block whatever thread you are interacting in. New threads can be created with ParallelLink and ThreadJoinLink.

Example usage:

(use-modules (opencog) (opencog exec))
(cog-execute! (SleepLink (PlusLink (NumberNode 1) (NumberNode 2))))

The above will sleep the current thread for three seconds.

Attention: NumberNodes are problematic for the AtomSpace, since they tend to accumulate like cruft, taking up space and doing nothing useful. A superior solution is to use ValueOfLink together with FloatValue. So, for example:

(cog-set-value! (Concept "abc") (Predicate "sleepy key") (FloatValue 3))
(cog-execute! (SleepLink (ValueOf (Concept "abc") (Predicate "sleepy key"))))

This attaches a floating-point value of "3" to (Concept "abc") under the key "sleepy key" (recall that every atom has a key-value store attached to it!). This value can be changed at any time. Later, then the sleep is needed, one can simply look up the value to find out how long to sleep. One can also specify formulas to compute how long to sleep.

See also