206 lines
7.9 KiB
JavaScript
206 lines
7.9 KiB
JavaScript
|
|
/** Combine multiple KB slices into one. Model instances are the main
|
||
|
|
* interaction point with the Knowledge Base. Methods for following
|
||
|
|
* edges between nodes are here.
|
||
|
|
*
|
||
|
|
* Extend this class to add model specific methods to a Model.
|
||
|
|
*
|
||
|
|
* @class Model
|
||
|
|
* @memberof jibo.kb
|
||
|
|
* @param {string|string[]} kbNames KB slice names to use in this
|
||
|
|
* model.
|
||
|
|
* @param {string} httpUrl Base URL of KB service to use.
|
||
|
|
*/
|
||
|
|
|
||
|
|
/** Create a new Model object.
|
||
|
|
*
|
||
|
|
* This behaves the same as `kb.createModel()`, except the KB
|
||
|
|
* names are not required to start with /. KB names that don't
|
||
|
|
* start with / will be relative to this Model. Specifically, they
|
||
|
|
* will relative to the KB name of the first KB in this model.
|
||
|
|
*
|
||
|
|
* For example, if this Model has the KB names `['/snap',
|
||
|
|
* '/snap/albums']` and this method is called as
|
||
|
|
* `model.createModel(['history', '/jibo.loop'])`, then the new
|
||
|
|
* model returned by this method will have the KB names
|
||
|
|
* `['/snap/history', '/jibo.loop']`.
|
||
|
|
*
|
||
|
|
* @method jibo.kb.Model#createModel
|
||
|
|
* @param {string|string[]} kbNames KB slice names to use in new
|
||
|
|
* model.
|
||
|
|
* @param {string} [httpUrl] Base URL of KB service to use
|
||
|
|
* (defaults to same URL as `this`).
|
||
|
|
* @returns {jibo.kb.Model} New Model object
|
||
|
|
*/
|
||
|
|
|
||
|
|
/** Create a new empty node on this Model. Will be attached to the
|
||
|
|
* first KB in this Model.
|
||
|
|
*
|
||
|
|
* @method jibo.kb.Model#createNode
|
||
|
|
* @param {string} nodeType Node type.
|
||
|
|
* @param {Object} [data] Initial data.
|
||
|
|
* @returns {jibo.kb.Node} New node object
|
||
|
|
*/
|
||
|
|
|
||
|
|
/** Load a node or array of nodes, by their IDs. All KBs in this
|
||
|
|
* Model slices are searched in order until found. If node is not
|
||
|
|
* found result will be `null`. If an array is supplied, then
|
||
|
|
* multiple IDs are searched for, and an array of results is
|
||
|
|
* returned. If callback is omitted, a promise is returned
|
||
|
|
* instead.
|
||
|
|
*
|
||
|
|
* @method jibo.kb.Model#load
|
||
|
|
* @param {string|string[]} ids ID or array of IDs to load.
|
||
|
|
* @param {Function} [callback] `(err, nodes) => {}`. If callback is
|
||
|
|
* omitted, a promise that resolves to `nodes` is returned instead.
|
||
|
|
* @returns {Promise} A promise that resolves with the value of
|
||
|
|
* `node` if the callback is omitted.
|
||
|
|
*/
|
||
|
|
|
||
|
|
/** Load the root node of the first KB. If a KB slice name is
|
||
|
|
* given, then load the root node for that KB instead. If callback
|
||
|
|
* is omitted a promise is returned instead.
|
||
|
|
*
|
||
|
|
* @method jibo.kb.Model#loadRoot
|
||
|
|
* @param {string} [kbName] Name of KB slice to load root node
|
||
|
|
* from. Default is first slice.
|
||
|
|
* @param {Function} [callback] (err, rootNode) => {}. If callback
|
||
|
|
* is omitted a promise that resolves to `rootNode` is returned
|
||
|
|
* instead.
|
||
|
|
* @returns {Promise} A promise that resolves with the value of
|
||
|
|
* `rootNode` if the callback is omitted.
|
||
|
|
*/
|
||
|
|
|
||
|
|
/** Preload all nodes connected by the layer names into the
|
||
|
|
* cache. Does not return the nodes. If callback is omitted a
|
||
|
|
* promise is returned instead.
|
||
|
|
*
|
||
|
|
* @method jibo.kb.Model#loadLayers
|
||
|
|
* @param {jibo.kb.Node} node Initial node to start from.
|
||
|
|
* @param {string|string[]} layers Edge names to follow.
|
||
|
|
* @param {Function} [callback] Callback for the method. If callback
|
||
|
|
* is omitted a promise is returned instead.
|
||
|
|
* @returns {Promise} A promise if the callback is omitted.
|
||
|
|
*/
|
||
|
|
|
||
|
|
/** Save all nodes connected by the layer names. Only used after
|
||
|
|
* activating the cache and preloading the given layers with
|
||
|
|
* loadLayers(). If callback is omitted a promise is returned
|
||
|
|
* instead.
|
||
|
|
*
|
||
|
|
* @method jibo.kb.Model#saveLayers
|
||
|
|
* @param {jibo.kb.Node} node Initial node to start from.
|
||
|
|
* @param {string|string[]} layers Edge names to follow.
|
||
|
|
* @param {Function} [callback] Callback for the method. If callback
|
||
|
|
* is omitted a promise is returned instead.
|
||
|
|
* @returns {Promise} A promise if the callback is omitted.
|
||
|
|
*/
|
||
|
|
|
||
|
|
/** Load all nodes connected by the layer names. Execute the given
|
||
|
|
* action once on each node. If callback is omitted a promise is
|
||
|
|
* returned instead.
|
||
|
|
*
|
||
|
|
* @method jibo.kb.Model#visitLayers
|
||
|
|
* @param {jibo.kb.Node} node Initial node to start from.
|
||
|
|
* @param {string|string[]} layers Edge names to follow.
|
||
|
|
* @param {Function} action `(eachNode, callback) => {}`
|
||
|
|
* @param {Function} [callback] Callback for the method. If callback
|
||
|
|
* is omitted a promise is returned instead.
|
||
|
|
* @returns {Promise} A promise if the callback is omitted.
|
||
|
|
*/
|
||
|
|
|
||
|
|
/** Create a clone of this Model object and its slices, but with
|
||
|
|
* the cache enabled. Cache based operations are intended to be
|
||
|
|
* short term in nature. Release the clone for garbage collection
|
||
|
|
* when finished.
|
||
|
|
*
|
||
|
|
* @method jibo.kb.Model#begin
|
||
|
|
* @returns {jibo.kb.Model} A new Model object.
|
||
|
|
*/
|
||
|
|
|
||
|
|
/** Synchronously fetch a node, or array of nodes, from the
|
||
|
|
* cache. Use on models created with `begin()` and preloaded with
|
||
|
|
* nodes. Nodes searched for but not found during preloading will
|
||
|
|
* be null. Nodes not preloaded will be undefined.
|
||
|
|
*
|
||
|
|
* @method jibo.kb.Model#fetch
|
||
|
|
* @param {string|string[]} ids ID or array of IDs node to fetch.
|
||
|
|
* @param {boolean} [quietly] Suppress warning if node not found
|
||
|
|
* in cache.
|
||
|
|
* @throws Exception if cache is not enabled.
|
||
|
|
* @returns {jibo.kb.Node} Node or `null` if not found. If
|
||
|
|
* an array of IDs is supplied, then an array of nodes is
|
||
|
|
* returned.
|
||
|
|
*/
|
||
|
|
|
||
|
|
/** Synchronously fetch the root node of the first KB from cache.
|
||
|
|
* If a KB slice name is given, then load the root node for that
|
||
|
|
* KB instead.
|
||
|
|
*
|
||
|
|
* @method jibo.kb.Model#fetchRoot
|
||
|
|
* @param {string} [kbName] Name of KB slice to load root node
|
||
|
|
* from. Default is first slice.
|
||
|
|
* @param {boolean} [quietly] Suppress warning if root node not
|
||
|
|
* found in cache.
|
||
|
|
* @throws Exception if cache is not enabled.
|
||
|
|
* @returns {jibo.kb.Node} Root node or `null` if not in cache.
|
||
|
|
*/
|
||
|
|
|
||
|
|
/** Turn on the cache. Don't call this directly, use begin() instead.
|
||
|
|
* @method jibo.kb.Model#enableCache
|
||
|
|
* @private
|
||
|
|
*/
|
||
|
|
|
||
|
|
/** Actual load command. Wrapped by interceptLoad when cache is
|
||
|
|
* enabled.
|
||
|
|
*
|
||
|
|
* @method jibo.kb.Model#_load
|
||
|
|
* @param {string} id Id of node to load
|
||
|
|
* @param {Function} callback (err, node) => {}
|
||
|
|
* @private
|
||
|
|
*/
|
||
|
|
|
||
|
|
/** Load a single node by its id. All KBs in this
|
||
|
|
* Model will be searched in order until found. Null returned (via
|
||
|
|
* callback) if not found.
|
||
|
|
*
|
||
|
|
* @method jibo.kb.Model#_loadOne
|
||
|
|
* @param {string} id ID to load.
|
||
|
|
* @param {Function} [callback] (err, node) => {}.
|
||
|
|
* @private
|
||
|
|
*/
|
||
|
|
|
||
|
|
/** Load an array of nodes by id. All KBs in this Model will be
|
||
|
|
* searched, in order, for each id in the array. An array is always
|
||
|
|
* returned, with the same length as the ids parameter. Nodes are
|
||
|
|
* returned in the array in the same order requested. Any not-found
|
||
|
|
* IDs will return a null in place of its node at the proper array
|
||
|
|
* position.
|
||
|
|
*
|
||
|
|
* @method jibo.kb.Model#_loadArray
|
||
|
|
* @param {string[]} ids Array of IDs of nodes to load.
|
||
|
|
* @param {Function} [callback] (err, nodes) => {}.
|
||
|
|
* @private
|
||
|
|
*/
|
||
|
|
|
||
|
|
/** Look through the pool and return the kb slice
|
||
|
|
* that has the given name
|
||
|
|
*
|
||
|
|
* @method jibo.kb.Model#_getKnowledgeDatabase
|
||
|
|
* @param {string} kbName Name of kb slice to find in pool
|
||
|
|
* @returns {jibo.kb.KnowledgeDatabase} Matching kb slice object, or
|
||
|
|
* undefined if not found
|
||
|
|
* @private
|
||
|
|
*/
|
||
|
|
|
||
|
|
/** check if cache is enabled and throw exception if not
|
||
|
|
*
|
||
|
|
* @method jibo.kb.Model#_assertCache
|
||
|
|
* @private
|
||
|
|
*/
|
||
|
|
|
||
|
|
/** Convert a single item to an array for arguments that may be a
|
||
|
|
* single thing or an array of things
|
||
|
|
*
|
||
|
|
* @method jibo.kb.Model#<_toArray
|
||
|
|
* @private
|
||
|
|
*/
|