Understanding Thought Processes
Saffron Technology™ Thought Processes (THOPs) are user-defined functions that allow you to tie together various SaffronMemoryBase™ and other capabilities using a scripting language. Thought Processes can help perform a wide variety of actions such as: simplifying the execution of complex sequential queries, calling outside applications to be used in conjunction with Saffron Technology reasoning functions, or converting query results into data suitable for UI widgets. THOPs are written in one of the supported script languages (currently, only JavaScript is available).
If you are familiar with other database products, think of Thought Processes as stored procedures. THOPs can be created via the REST API or by using the developer tools in Saffron Admin. After a Thought Process is defined, it becomes callable through the standard REST API.
Currently, SaffronMemoryBase includes two versions of Thought Processes: THOPs 1.0 and THOPs 2.0.
THOPs 1.0
These Thought Processes are run synchronously THOPs 1.0 runs via an HTTP GET call with the calling client and waits for a result.
THOPs 1.0 are called using a GET operation and uses the Tomcat application server. This version is based on the Rhino JavaSript engine with extensions.
Use THOPs 1.0 for the following types of operations:
- Simple single queries
- Fast operations
- Where asynchronization is not necessary
THOPs 2.0
The THOPs 2.0 layer enables Thought Processes to run asynchronously and to have bi-directional communication with the calling client. The processes can be long-running, which is why we refer to them as Deep Thoughts (DT). When initiated by a caller, DTs can run arbitrarily long and can send messages to the client before delivering a final result.
The following is an example of a Deep Thought that can deliver a partial result to the calling client as soon as data is available and deliver the remaining results later:
Deep Thoughts can also listen to messages being sent by the caller and reply to those messages individually (as well as return partial or final results).
Here, THOPs can register for messages from the caller. THOPs can also reply to messages or send messages proactively:
Clients start an instance of a Deep Thought (DT) by POSTing to the thopname/result
resource. The result of the POST doesn't contain a final result, but an Event Bus address to listen for messages from the DT as well as a start address. Clients then send a message to the start address to actually start the DT (it lies dormant to give the client a chance to register to the listen address).
A DT can send a (partial) result to the client using the send(data)
function. data
is any JS object; it will be turned into JSON during transmission to the client.
A DT can register for messages sent by the client after the DT was started using the on(address, handler)
function. Address
is any string that the client uses to send messages to the DT . DT's handler
will then be called with the message as well as a replyHandler
. The DT can use the replyHandler
to send a message back to the calling client. (Alternatively it can use the send
function)
As part of default parameters for a DT, a timeout value can be specified after which a DT instance shuts down (and the client will be unable to send messages to the DT).
Use THOPs 2.0 for the following types of operations:
- complex queries that can't be expressed in a single query
- business logic when writing apps based on SUIT and THOPS
- integrating SMB APIs and foreign APIs
- using a stored procedure in a relational DB
- deploying code at runtime